3 * Copyright (C) 2009-2023 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 com.google.common.base.Preconditions;
23 import java.sql.Connection;
24 import java.sql.PreparedStatement;
25 import java.sql.ResultSet;
26 import java.sql.SQLException;
27 import java.util.Arrays;
28 import java.util.LinkedList;
29 import java.util.List;
30 import java.util.stream.Collectors;
31 import org.sonar.db.Database;
32 import org.sonar.db.dialect.MsSql;
34 import static java.lang.String.format;
36 public class DropMsSQLDefaultConstraintsBuilder {
37 private final Database db;
38 private String tableName;
39 private String[] columns;
41 public DropMsSQLDefaultConstraintsBuilder(Database db) {
45 public DropMsSQLDefaultConstraintsBuilder setTable(String s) {
50 public DropMsSQLDefaultConstraintsBuilder setColumns(String... columns) {
51 this.columns = columns;
55 public List<String> build() throws SQLException {
56 Preconditions.checkArgument(columns.length > 0, "At least one column expected.");
57 Preconditions.checkArgument(MsSql.ID.equals(db.getDialect().getId()), "Expected MsSql dialect was: " + db.getDialect().getId());
58 return getMsSqlDropDefaultConstraintQueries();
61 private List<String> getMsSqlDropDefaultConstraintQueries() throws SQLException {
62 List<String> dropQueries = new LinkedList<>();
63 if (MsSql.ID.equals(db.getDialect().getId())) {
64 List<String> defaultConstraints = getMssqlDefaultConstraints();
65 for (String defaultConstraintName : defaultConstraints) {
66 dropQueries.add("ALTER TABLE " + tableName + " DROP CONSTRAINT " + defaultConstraintName);
72 private List<String> getMssqlDefaultConstraints() throws SQLException {
73 List<String> defaultConstrainNames = new LinkedList<>();
74 String commaSeparatedListOfColumns = Arrays.stream(columns).map(s -> "'" + s + "'")
75 .collect(Collectors.joining(","));
76 try (Connection connection = db.getDataSource().getConnection();
77 PreparedStatement pstmt = connection
78 .prepareStatement(format("SELECT d.name FROM sys.tables t "
79 + "JOIN sys.default_constraints d ON d.parent_object_id = t.object_id "
80 + "JOIN sys.columns c ON c.object_id = t.object_id AND c.column_id = d.parent_column_id "
81 + "WHERE t.name = '%s' AND c.name in (%s)", tableName, commaSeparatedListOfColumns));
82 ResultSet rs = pstmt.executeQuery()) {
84 defaultConstrainNames.add(rs.getString(1));
87 return defaultConstrainNames;