3 * Copyright (C) 2009-2021 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.sql;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import org.sonar.db.dialect.Dialect;
27 import org.sonar.db.dialect.Oracle;
28 import org.sonar.db.dialect.PostgreSql;
29 import org.sonar.server.platform.db.migration.def.ColumnDef;
31 import static com.google.common.base.Preconditions.checkArgument;
32 import static com.google.common.collect.Lists.newArrayList;
33 import static org.sonar.server.platform.db.migration.def.Validations.validateTableName;
36 * Generate SQL queries to update multiple columns of a single table.
38 * Note that this operation will not be re-entrant on:
40 * <li>Oracle 11G (may raise {@code ORA-01442: column to be modified to NOT NULL is already NOT NULL} or
41 * {@code ORA-01451: column to be modified to NULL cannot be modified to NULL})</li>
44 public class AlterColumnsBuilder {
46 private static final String ALTER_TABLE = "ALTER TABLE ";
47 private static final String ALTER_COLUMN = "ALTER COLUMN ";
49 private final Dialect dialect;
50 private final String tableName;
51 private final List<ColumnDef> columnDefs = newArrayList();
53 public AlterColumnsBuilder(Dialect dialect, String tableName) {
54 this.dialect = dialect;
55 this.tableName = validateTableName(tableName);
58 public AlterColumnsBuilder updateColumn(ColumnDef columnDef) {
59 // limitation of Oracle, only attribute changes must be defined in ALTER.
60 checkArgument(columnDef.getDefaultValue()==null, "Default value is not supported on alter of column '%s'", columnDef.getName());
61 columnDefs.add(columnDef);
65 public List<String> build() {
66 if (columnDefs.isEmpty()) {
67 throw new IllegalStateException("No column has been defined");
69 switch (dialect.getId()) {
71 return createPostgresQuery();
73 return createOracleQuery();
75 return createMsSqlAndH2Queries();
79 private List<String> createPostgresQuery() {
80 StringBuilder sql = new StringBuilder(ALTER_TABLE + tableName + " ");
81 for (Iterator<ColumnDef> columnDefIterator = columnDefs.iterator(); columnDefIterator.hasNext();) {
82 ColumnDef columnDef = columnDefIterator.next();
83 sql.append(ALTER_COLUMN);
84 addColumn(sql, columnDef, "TYPE ", false);
86 sql.append(ALTER_COLUMN);
87 sql.append(columnDef.getName());
88 sql.append(' ').append(columnDef.isNullable() ? "DROP" : "SET").append(" NOT NULL");
89 if (columnDefIterator.hasNext()) {
93 return Collections.singletonList(sql.toString());
96 private List<String> createOracleQuery() {
97 List<String> sqls = new ArrayList<>();
98 for (ColumnDef columnDef : columnDefs) {
99 StringBuilder sql = new StringBuilder(ALTER_TABLE + tableName + " ").append("MODIFY (");
100 addColumn(sql, columnDef, "", true);
102 sqls.add(sql.toString());
107 private List<String> createMsSqlAndH2Queries() {
108 List<String> sqls = new ArrayList<>();
109 for (ColumnDef columnDef : columnDefs) {
110 StringBuilder defaultQuery = new StringBuilder(ALTER_TABLE + tableName + " ");
111 defaultQuery.append(ALTER_COLUMN);
112 addColumn(defaultQuery, columnDef, "", true);
113 sqls.add(defaultQuery.toString());
118 private void addColumns(StringBuilder sql, String updateKeyword, String typePrefix, boolean addNotNullableProperty) {
119 for (Iterator<ColumnDef> columnDefIterator = columnDefs.iterator(); columnDefIterator.hasNext();) {
120 sql.append(updateKeyword);
121 addColumn(sql, columnDefIterator.next(), typePrefix, addNotNullableProperty);
122 if (columnDefIterator.hasNext()) {
128 private void addColumn(StringBuilder sql, ColumnDef columnDef, String typePrefix, boolean addNotNullableProperty) {
129 sql.append(columnDef.getName())
132 .append(columnDef.generateSqlType(dialect));
133 if (addNotNullableProperty) {
134 sql.append(columnDef.isNullable() ? " NULL" : " NOT NULL");