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.sql.SQLException;
23 import java.util.ArrayList;
24 import java.util.Collection;
25 import java.util.List;
26 import org.sonar.db.Database;
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.Oracle;
31 import org.sonar.db.dialect.PostgreSql;
33 import static java.lang.String.format;
34 import static java.util.Collections.singleton;
35 import static java.util.Collections.singletonList;
37 public class DropPrimaryKeySqlGenerator {
38 private static final String GENERIC_DROP_CONSTRAINT_STATEMENT = "ALTER TABLE %s DROP CONSTRAINT %s";
40 private final Database db;
41 private final DbPrimaryKeyConstraintFinder dbConstraintFinder;
43 public DropPrimaryKeySqlGenerator(Database db, DbPrimaryKeyConstraintFinder dbConstraintFinder) {
45 this.dbConstraintFinder = dbConstraintFinder;
48 public List<String> generate(String tableName, String columnName, boolean isAutoGenerated) throws SQLException {
49 return generate(tableName, singleton(columnName), isAutoGenerated);
52 public List<String> generate(String tableName, Collection<String> columnNames, boolean isAutoGenerated) throws SQLException {
53 Dialect dialect = db.getDialect();
54 String constraintName = dbConstraintFinder.findConstraintName(tableName);
55 switch (dialect.getId()) {
57 return generateForPostgresSql(tableName, columnNames, constraintName);
59 return generateForMsSql(tableName, constraintName);
61 return generateForOracle(tableName, constraintName, isAutoGenerated);
63 return generateForH2(tableName, constraintName);
65 throw new IllegalStateException(format("Unsupported database '%s'", dialect.getId()));
69 private List<String> generateForPostgresSql(String tableName, Collection<String> columns, String constraintName) throws SQLException {
70 List<String> statements = new ArrayList<>();
71 for (String column : columns) {
72 statements.add(format("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT", tableName, column));
73 String sequence = dbConstraintFinder.getPostgresSqlSequence(tableName, column);
74 if (sequence != null) {
75 statements.add(format("DROP SEQUENCE %s", sequence));
79 statements.add(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));
83 private static List<String> generateForOracle(String tableName, String constraintName, boolean isAutoGenerated) {
84 List<String> statements = new ArrayList<>();
85 if (isAutoGenerated) {
86 statements.add(format("DROP TRIGGER %s_IDT", tableName));
87 statements.add(format("DROP SEQUENCE %s_SEQ", tableName));
90 // 'drop index' at the end ensures that associated index with primary key will be deleted
91 statements.add(format(GENERIC_DROP_CONSTRAINT_STATEMENT + " DROP INDEX", tableName, constraintName));
95 private static List<String> generateForMsSql(String tableName, String constraintName) {
96 return singletonList(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));
99 private static List<String> generateForH2(String tableName, String constraintName) {
100 return singletonList(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));