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.
20 package org.sonar.server.platform.db.migration.version.v83.util;
22 import java.sql.Connection;
23 import java.sql.PreparedStatement;
24 import java.sql.ResultSet;
25 import java.sql.SQLException;
26 import java.util.Locale;
27 import org.sonar.db.Database;
29 import static java.lang.String.format;
31 public class GetConstraintHelper {
33 private final Database db;
35 public GetConstraintHelper(Database db) {
39 String getH2Constraint(String tableName) throws SQLException {
40 try (Connection connection = db.getDataSource().getConnection();
41 PreparedStatement pstmt = connection
42 .prepareStatement(format("SELECT constraint_name "
43 + "FROM information_schema.constraints "
44 + "WHERE table_name = '%s' and constraint_type = 'PRIMARY KEY'", tableName.toUpperCase(Locale.ENGLISH)));
45 ResultSet rs = pstmt.executeQuery()) {
47 return rs.getString(1);
49 throw contraintNotFoundException(tableName);
53 String getPostgresSqlConstraint(String tableName) throws SQLException {
54 try (Connection connection = db.getDataSource().getConnection();
55 PreparedStatement pstmt = connection
56 .prepareStatement(format("SELECT conname " +
57 "FROM pg_constraint " +
61 " WHERE relname LIKE '%s')", tableName));
62 ResultSet rs = pstmt.executeQuery()) {
64 return rs.getString(1);
66 throw contraintNotFoundException(tableName);
70 String getOracleConstraint(String tableName) throws SQLException {
71 try (Connection connection = db.getDataSource().getConnection();
72 PreparedStatement pstmt = connection
73 .prepareStatement(format("SELECT constraint_name " +
74 "FROM user_constraints " +
75 "WHERE table_name = UPPER('%s') " +
76 "AND constraint_type='P'", tableName));
77 ResultSet rs = pstmt.executeQuery()) {
79 return rs.getString(1);
81 throw contraintNotFoundException(tableName);
85 String getMssqlConstraint(String tableName) throws SQLException {
86 try (Connection connection = db.getDataSource().getConnection();
87 PreparedStatement pstmt = connection
88 .prepareStatement(format("SELECT name " +
89 "FROM sys.key_constraints " +
90 "WHERE type = 'PK' " +
91 "AND OBJECT_NAME(parent_object_id) = '%s'", tableName));
92 ResultSet rs = pstmt.executeQuery()) {
94 return rs.getString(1);
96 throw contraintNotFoundException(tableName);
100 private static IllegalStateException contraintNotFoundException(String tableName) {
101 return new IllegalStateException(format("Cannot find constraint for table '%s'", tableName));