3 * Copyright (C) 2009-2020 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.
21 package org.sonar.server.platform.db.migration.version.v84.util;
23 import java.sql.SQLException;
24 import java.util.ArrayList;
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.Arrays.asList;
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 SqlHelper sqlHelper;
43 public DropPrimaryKeySqlGenerator(Database db, SqlHelper sqlHelper) {
45 this.sqlHelper = sqlHelper;
48 public List<String> generate(String tableName, String columnName, boolean isAutoGenerated) throws SQLException {
49 Dialect dialect = db.getDialect();
50 switch (dialect.getId()) {
52 return generateForPostgresSql(tableName, columnName, sqlHelper.getPostgresSqlConstraint(tableName));
54 return generateForMsSql(tableName, sqlHelper.getMssqlConstraint(tableName));
56 return generateForOracle(tableName, sqlHelper.getOracleConstraint(tableName), isAutoGenerated);
58 return generateForH2(tableName, columnName, sqlHelper.getH2Constraint(tableName));
60 throw new IllegalStateException(format("Unsupported database '%s'", dialect.getId()));
64 private List<String> generateForPostgresSql(String tableName, String column, String constraintName) throws SQLException {
65 List<String> statements = new ArrayList<>();
66 statements.add(format("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT", tableName, column));
68 String sequence = sqlHelper.getPostgresSqlSequence(tableName, column);
69 if (sequence != null) {
70 statements.add(format("DROP SEQUENCE %s", sequence));
73 statements.add(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));
78 private static List<String> generateForOracle(String tableName, String constraintName, boolean isAutoGenerated) {
79 List<String> statements = new ArrayList<>();
80 if (isAutoGenerated) {
81 statements.add(format("DROP TRIGGER %s_IDT", tableName));
82 statements.add(format("DROP SEQUENCE %s_SEQ", tableName));
85 // 'drop index' at the end ensures that associated index with primary key will be deleted
86 statements.add(format(GENERIC_DROP_CONSTRAINT_STATEMENT + " DROP INDEX", tableName, constraintName));
90 private static List<String> generateForMsSql(String tableName, String constraintName) {
91 return singletonList(format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName));
94 private static List<String> generateForH2(String tableName, String column, String constraintName) {
96 format(GENERIC_DROP_CONSTRAINT_STATEMENT, tableName, constraintName),
97 format("ALTER TABLE %s ALTER COLUMN %s INTEGER NOT NULL", tableName, column));