Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

DropMsSQLDefaultConstraintsBuilder.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2023 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  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.
  10. *
  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.
  15. *
  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.
  19. */
  20. package org.sonar.server.platform.db.migration.sql;
  21. import com.google.common.base.Preconditions;
  22. import java.sql.Connection;
  23. import java.sql.PreparedStatement;
  24. import java.sql.ResultSet;
  25. import java.sql.SQLException;
  26. import java.util.Arrays;
  27. import java.util.LinkedList;
  28. import java.util.List;
  29. import java.util.stream.Collectors;
  30. import org.sonar.db.Database;
  31. import org.sonar.db.dialect.MsSql;
  32. import static java.lang.String.format;
  33. /**
  34. * See SONAR-13948. Some columns created in SQ < 5.6 were created with the default command which generated constraints on MS SQL server.
  35. */
  36. public class DropMsSQLDefaultConstraintsBuilder {
  37. private final Database db;
  38. private String tableName;
  39. private String[] columns;
  40. public DropMsSQLDefaultConstraintsBuilder(Database db) {
  41. this.db = db;
  42. }
  43. public DropMsSQLDefaultConstraintsBuilder setTable(String s) {
  44. this.tableName = s;
  45. return this;
  46. }
  47. public DropMsSQLDefaultConstraintsBuilder setColumns(String... columns) {
  48. this.columns = columns;
  49. return this;
  50. }
  51. public List<String> build() throws SQLException {
  52. Preconditions.checkArgument(columns.length > 0, "At least one column expected.");
  53. Preconditions.checkArgument(MsSql.ID.equals(db.getDialect().getId()), "Expected MsSql dialect was: " + db.getDialect().getId());
  54. return getMsSqlDropDefaultConstraintQueries();
  55. }
  56. private List<String> getMsSqlDropDefaultConstraintQueries() throws SQLException {
  57. List<String> dropQueries = new LinkedList<>();
  58. if (MsSql.ID.equals(db.getDialect().getId())) {
  59. List<String> defaultConstraints = getMssqlDefaultConstraints();
  60. for (String defaultConstraintName : defaultConstraints) {
  61. dropQueries.add("ALTER TABLE " + tableName + " DROP CONSTRAINT " + defaultConstraintName);
  62. }
  63. }
  64. return dropQueries;
  65. }
  66. private List<String> getMssqlDefaultConstraints() throws SQLException {
  67. List<String> defaultConstrainNames = new LinkedList<>();
  68. String commaSeparatedListOfColumns = Arrays.stream(columns).map(s -> "'" + s + "'")
  69. .collect(Collectors.joining(","));
  70. try (Connection connection = db.getDataSource().getConnection();
  71. PreparedStatement pstmt = connection
  72. .prepareStatement(format("SELECT d.name FROM sys.tables t "
  73. + "JOIN sys.default_constraints d ON d.parent_object_id = t.object_id "
  74. + "JOIN sys.columns c ON c.object_id = t.object_id AND c.column_id = d.parent_column_id "
  75. + "WHERE t.name = '%s' AND c.name in (%s)", tableName, commaSeparatedListOfColumns));
  76. ResultSet rs = pstmt.executeQuery()) {
  77. while (rs.next()) {
  78. defaultConstrainNames.add(rs.getString(1));
  79. }
  80. }
  81. return defaultConstrainNames;
  82. }
  83. }