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.v83.util;
23 import java.sql.SQLException;
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.Arrays.asList;
34 import static java.util.Collections.singletonList;
35 import static java.util.Locale.ENGLISH;
36 import static org.sonar.server.platform.db.migration.sql.CreateTableBuilder.PRIMARY_KEY_PREFIX;
38 public class DropPrimaryKeySqlGenerator {
40 private final Database db;
41 private GetConstraintHelper getConstraintHelper;
43 public DropPrimaryKeySqlGenerator(Database db, GetConstraintHelper getConstraintHelper) {
45 this.getConstraintHelper = getConstraintHelper;
48 public List<String> generate(String tableName, String originalTableName, String columnName) throws SQLException {
49 Dialect dialect = db.getDialect();
50 switch (dialect.getId()) {
52 return generateForPostgresSql(tableName, originalTableName, columnName, getConstraintHelper.getPostgresSqlConstraint(tableName));
54 return generateForMsSql(tableName, getConstraintHelper.getMssqlConstraint(tableName));
56 return generateForOracle(tableName, getConstraintHelper.getOracleConstraint(tableName));
58 return generateForH2(tableName, originalTableName, columnName);
60 throw new IllegalStateException(format("Unsupported database '%s'", dialect.getId()));
64 private static List<String> generateForPostgresSql(String tableName, String originalTableName, String column, String constraintName) {
66 format("ALTER TABLE %s ALTER COLUMN %s DROP DEFAULT", tableName, column),
67 format("DROP SEQUENCE %s_%s_seq", originalTableName, column),
68 format("ALTER TABLE %s DROP CONSTRAINT %s", tableName, constraintName));
71 private static List<String> generateForOracle(String tableName, String constraintName) {
73 format("DROP TRIGGER %s_IDT", tableName),
74 format("DROP SEQUENCE %s_SEQ", tableName),
75 format("ALTER TABLE %s DROP CONSTRAINT %s", tableName, constraintName));
78 private static List<String> generateForMsSql(String tableName, String constraintName) {
79 return singletonList(format("ALTER TABLE %s DROP CONSTRAINT %s", tableName, constraintName));
82 private static List<String> generateForH2(String tableName, String originalTableName, String column) {
84 format("ALTER TABLE %s DROP CONSTRAINT %s%s", tableName, PRIMARY_KEY_PREFIX.toUpperCase(ENGLISH), originalTableName),
85 format("ALTER TABLE %s ALTER COLUMN %s INTEGER NOT NULL", tableName, column));