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.Collections;
26 import java.util.List;
27 import java.util.Optional;
28 import org.sonar.db.Database;
29 import org.sonar.db.dialect.Dialect;
30 import org.sonar.db.dialect.H2;
31 import org.sonar.db.dialect.MsSql;
32 import org.sonar.db.dialect.Oracle;
33 import org.sonar.db.dialect.PostgreSql;
35 import static java.lang.String.format;
36 import static java.util.Collections.singleton;
37 import static java.util.Collections.singletonList;
39 public class DropPrimaryKeySqlGenerator {
40 private static final String GENERIC_DROP_CONSTRAINT_STATEMENT = "ALTER TABLE %s DROP CONSTRAINT %s";
42 private final Database db;
43 private final DbPrimaryKeyConstraintFinder dbConstraintFinder;
45 public DropPrimaryKeySqlGenerator(Database db, DbPrimaryKeyConstraintFinder dbConstraintFinder) {
47 this.dbConstraintFinder = dbConstraintFinder;
50 public List<String> generate(String tableName, String columnName, boolean isAutoGenerated) throws SQLException {
51 return generate(tableName, singleton(columnName), isAutoGenerated);
54 public List<String> generate(String tableName, Collection<String> columnNames, boolean isAutoGenerated) throws SQLException {
55 Dialect dialect = db.getDialect();
56 Optional<String> constraintName = dbConstraintFinder.findConstraintName(tableName);
57 if (!constraintName.isPresent()) {
58 return Collections.emptyList();
60 switch (dialect.getId()) {
62 return generateForPostgresSql(tableName, columnNames, constraintName.get());
64 return generateForMsSql(tableName, constraintName.get());
66 return generateForOracle(tableName, constraintName.get(), isAutoGenerated);
68 return generateForH2(tableName, constraintName.get());
70 throw new IllegalStateException(format("Unsupported database '%s'", dialect.getId()));
74 private List<String> generateForPostgresSql(String tableName, Collection<String> columns, String constraintName) throws SQLException {
75 List<String> statements = new ArrayList<>();
76 for (String column : columns) {
77 statements.add(format("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT", tableName, column));
78 String sequence = dbConstraintFinder.getPostgresSqlSequence(tableName, column);
79 if (sequence != null) {
80 statements.add(format("DROP SEQUENCE %s", sequence));
84 statements.add(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));
88 private static List<String> generateForOracle(String tableName, String constraintName, boolean isAutoGenerated) {
89 List<String> statements = new ArrayList<>();
90 if (isAutoGenerated) {
91 statements.add(format("DROP TRIGGER %s_IDT", tableName));
92 statements.add(format("DROP SEQUENCE %s_SEQ", tableName));
95 // 'drop index' at the end ensures that associated index with primary key will be deleted
96 statements.add(format(GENERIC_DROP_CONSTRAINT_STATEMENT + " DROP INDEX", tableName, constraintName));
100 private static List<String> generateForMsSql(String tableName, String constraintName) {
101 return singletonList(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));
104 private static List<String> generateForH2(String tableName, String constraintName) {
105 return singletonList(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));