3 * Copyright (C) 2009-2017 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.Arrays;
23 import java.util.Collections;
24 import java.util.Iterator;
25 import java.util.List;
26 import org.sonar.core.util.stream.MoreCollectors;
27 import org.sonar.db.dialect.Dialect;
28 import org.sonar.db.dialect.H2;
29 import org.sonar.db.dialect.MsSql;
30 import org.sonar.db.dialect.MySql;
31 import org.sonar.db.dialect.Oracle;
32 import org.sonar.db.dialect.PostgreSql;
35 * Generate a SQL query to drop multiple columns from a table
37 public class DropColumnsBuilder {
39 private final Dialect dialect;
40 private final String tableName;
41 private final String[] columns;
43 public DropColumnsBuilder(Dialect dialect, String tableName, String... columns) {
44 this.tableName = tableName;
45 this.dialect = dialect;
46 this.columns = columns;
49 public List<String> build() {
50 switch (dialect.getId()) {
53 StringBuilder sql = new StringBuilder().append("ALTER TABLE ").append(tableName).append(" ");
54 dropColumns(sql, "DROP COLUMN ", columns);
55 return Collections.singletonList(sql.toString());
57 return Collections.singletonList(getMsSQLStatement(columns));
59 return Collections.singletonList(getOracleStatement());
61 return Arrays.stream(columns).map(this::getMsSQLStatement).collect(MoreCollectors.toList(columns.length));
63 throw new IllegalStateException(String.format("Unsupported database '%s'", dialect.getId()));
67 private String getOracleStatement() {
68 StringBuilder sql2 = new StringBuilder().append("ALTER TABLE ").append(tableName).append(" ");
69 sql2.append("DROP (");
70 dropColumns(sql2, "", columns);
72 return sql2.toString();
75 private String getMsSQLStatement(String... columnNames) {
76 StringBuilder sql1 = new StringBuilder().append("ALTER TABLE ").append(tableName).append(" ");
77 sql1.append("DROP COLUMN ");
78 dropColumns(sql1, "", columnNames);
79 return sql1.toString();
82 private static void dropColumns(StringBuilder sql, String columnPrefix, String... columnNames) {
83 Iterator<String> columnNamesIterator = Arrays.stream(columnNames).iterator();
84 while (columnNamesIterator.hasNext()) {
85 sql.append(columnPrefix);
86 sql.append(columnNamesIterator.next());
87 if (columnNamesIterator.hasNext()) {