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.List;
25 import org.sonar.db.Database;
26 import org.sonar.db.dialect.Dialect;
27 import org.sonar.db.dialect.H2;
28 import org.sonar.db.dialect.MsSql;
29 import org.sonar.db.dialect.Oracle;
30 import org.sonar.db.dialect.PostgreSql;
32 import static java.lang.String.format;
33 import static java.util.Collections.singletonList;
35 public class DropPrimaryKeySqlGenerator {
36 private static final String GENERIC_DROP_CONSTRAINT_STATEMENT = "ALTER TABLE %s DROP CONSTRAINT %s";
38 private final Database db;
39 private final DbPrimaryKeyConstraintFinder dbConstraintFinder;
41 public DropPrimaryKeySqlGenerator(Database db, DbPrimaryKeyConstraintFinder dbConstraintFinder) {
43 this.dbConstraintFinder = dbConstraintFinder;
46 public List<String> generate(String tableName, String columnName, boolean isAutoGenerated) throws SQLException {
47 Dialect dialect = db.getDialect();
48 String constraintName = dbConstraintFinder.findConstraintName(tableName);
49 switch (dialect.getId()) {
51 return generateForPostgresSql(tableName, columnName, constraintName);
53 return generateForMsSql(tableName, constraintName);
55 return generateForOracle(tableName, constraintName, isAutoGenerated);
57 return generateForH2(tableName, constraintName);
59 throw new IllegalStateException(format("Unsupported database '%s'", dialect.getId()));
63 private List<String> generateForPostgresSql(String tableName, String column, String constraintName) throws SQLException {
64 List<String> statements = new ArrayList<>();
65 statements.add(format("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT", tableName, column));
67 String sequence = dbConstraintFinder.getPostgresSqlSequence(tableName, column);
68 if (sequence != null) {
69 statements.add(format("DROP SEQUENCE %s", sequence));
72 statements.add(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));
77 private static List<String> generateForOracle(String tableName, String constraintName, boolean isAutoGenerated) {
78 List<String> statements = new ArrayList<>();
79 if (isAutoGenerated) {
80 statements.add(format("DROP TRIGGER %s_IDT", tableName));
81 statements.add(format("DROP SEQUENCE %s_SEQ", tableName));
84 // 'drop index' at the end ensures that associated index with primary key will be deleted
85 statements.add(format(GENERIC_DROP_CONSTRAINT_STATEMENT + " DROP INDEX", tableName, constraintName));
89 private static List<String> generateForMsSql(String tableName, String constraintName) {
90 return singletonList(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));
93 private static List<String> generateForH2(String tableName, String constraintName) {
94 return singletonList(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));