You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SqlHelper.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.version.v84.util;
  21. import java.sql.Connection;
  22. import java.sql.PreparedStatement;
  23. import java.sql.ResultSet;
  24. import java.sql.SQLException;
  25. import java.util.Locale;
  26. import org.sonar.db.Database;
  27. import static java.lang.String.format;
  28. public class SqlHelper {
  29. private final Database db;
  30. public SqlHelper(Database db) {
  31. this.db = db;
  32. }
  33. String getH2Constraint(String tableName) throws SQLException {
  34. try (Connection connection = db.getDataSource().getConnection();
  35. PreparedStatement pstmt = connection
  36. .prepareStatement(format("SELECT constraint_name "
  37. + "FROM information_schema.constraints "
  38. + "WHERE table_name = '%s' and constraint_type = 'PRIMARY KEY'", tableName.toUpperCase(Locale.ENGLISH)));
  39. ResultSet rs = pstmt.executeQuery()) {
  40. if (rs.next()) {
  41. return rs.getString(1);
  42. }
  43. throw contraintNotFoundException(tableName);
  44. }
  45. }
  46. String getPostgresSqlConstraint(String tableName) throws SQLException {
  47. try (Connection connection = db.getDataSource().getConnection();
  48. PreparedStatement pstmt = connection
  49. .prepareStatement(format("SELECT conname " +
  50. "FROM pg_constraint " +
  51. "WHERE conrelid = " +
  52. " (SELECT oid " +
  53. " FROM pg_class " +
  54. " WHERE relname LIKE '%s')", tableName));
  55. ResultSet rs = pstmt.executeQuery()) {
  56. if (rs.next()) {
  57. return rs.getString(1);
  58. }
  59. throw contraintNotFoundException(tableName);
  60. }
  61. }
  62. String getPostgresSqlSequence(String tableName, String columnName) throws SQLException {
  63. try (Connection connection = db.getDataSource().getConnection();
  64. PreparedStatement pstmt = connection
  65. .prepareStatement(format("SELECT pg_get_serial_sequence('%s', '%s')", tableName, columnName));
  66. ResultSet rs = pstmt.executeQuery()) {
  67. if (rs.next()) {
  68. return rs.getString(1);
  69. }
  70. throw new IllegalStateException(format("Cannot find sequence for table '%s' on column '%s'", tableName, columnName));
  71. }
  72. }
  73. String getOracleConstraint(String tableName) throws SQLException {
  74. try (Connection connection = db.getDataSource().getConnection();
  75. PreparedStatement pstmt = connection
  76. .prepareStatement(format("SELECT constraint_name " +
  77. "FROM user_constraints " +
  78. "WHERE table_name = UPPER('%s') " +
  79. "AND constraint_type='P'", tableName));
  80. ResultSet rs = pstmt.executeQuery()) {
  81. if (rs.next()) {
  82. return rs.getString(1);
  83. }
  84. throw contraintNotFoundException(tableName);
  85. }
  86. }
  87. String getMssqlConstraint(String tableName) throws SQLException {
  88. try (Connection connection = db.getDataSource().getConnection();
  89. PreparedStatement pstmt = connection
  90. .prepareStatement(format("SELECT name " +
  91. "FROM sys.key_constraints " +
  92. "WHERE type = 'PK' " +
  93. "AND OBJECT_NAME(parent_object_id) = '%s'", tableName));
  94. ResultSet rs = pstmt.executeQuery()) {
  95. if (rs.next()) {
  96. return rs.getString(1);
  97. }
  98. throw contraintNotFoundException(tableName);
  99. }
  100. }
  101. private static IllegalStateException contraintNotFoundException(String tableName) {
  102. return new IllegalStateException(format("Cannot find constraint for table '%s'", tableName));
  103. }
  104. }