aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-core
diff options
context:
space:
mode:
Diffstat (limited to 'server/sonar-db-core')
-rw-r--r--server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java24
-rw-r--r--server/sonar-db-core/src/test/java/org/sonar/db/DatabaseUtilsTest.java40
-rw-r--r--server/sonar-db-core/src/test/resources/org/sonar/db/DatabaseUtilsTest/sql.sql4
3 files changed, 68 insertions, 0 deletions
diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java b/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java
index 4f579f486f2..2d566ee3ede 100644
--- a/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java
+++ b/server/sonar-db-core/src/main/java/org/sonar/db/DatabaseUtils.java
@@ -351,6 +351,30 @@ public class DatabaseUtils {
}
}
+ public static boolean tableColumnExists(Connection connection, String tableName, String columnName) {
+ try {
+ return columnExists(connection, tableName.toLowerCase(Locale.US), columnName)
+ || columnExists(connection, tableName.toUpperCase(Locale.US), columnName);
+ } catch (SQLException e) {
+ throw wrapSqlException(e, "Can not check that column %s exists", columnName);
+ }
+ }
+
+ private static boolean columnExists(Connection connection, String tableName, String columnName) throws SQLException {
+ String schema = getSchema(connection);
+ try (ResultSet rs = connection.getMetaData().getColumns(connection.getCatalog(), schema, tableName, null)) {
+ while (rs.next()) {
+ for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
+ String name = rs.getString(i);
+ if (columnName.equalsIgnoreCase(name)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+ }
+
@CheckForNull
private static String getSchema(Connection connection) {
String schema = null;
diff --git a/server/sonar-db-core/src/test/java/org/sonar/db/DatabaseUtilsTest.java b/server/sonar-db-core/src/test/java/org/sonar/db/DatabaseUtilsTest.java
index 92b20692e32..21ae0cbba43 100644
--- a/server/sonar-db-core/src/test/java/org/sonar/db/DatabaseUtilsTest.java
+++ b/server/sonar-db-core/src/test/java/org/sonar/db/DatabaseUtilsTest.java
@@ -86,6 +86,46 @@ public class DatabaseUtilsTest {
}
@Test
+ public void find_column_with_lower_case_table_name_and_upper_case_column_name() throws SQLException {
+ String tableName = "tablea";
+ String columnName = "COLUMNA";
+ try (Connection connection = dbTester.openConnection()) {
+ assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName)).isTrue();
+ assertThat(DatabaseUtils.tableColumnExists(connection, tableName.toLowerCase(Locale.US), columnName)).isTrue();
+ }
+ }
+ @Test
+ public void find_column_with_upper_case_table_name_and_upper_case_column_name() throws SQLException {
+ String tableName = "TABLEA";
+ String columnName = "COLUMNA";
+ try (Connection connection = dbTester.openConnection()) {
+ assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName)).isTrue();
+ assertThat(DatabaseUtils.tableColumnExists(connection, tableName.toLowerCase(Locale.US), columnName)).isTrue();
+ }
+ }
+
+ @Test
+ public void find_column_with_lower_case_table_name_and_lower_case_column_name() throws SQLException {
+ String tableName = "tablea";
+ String columnName = "columna";
+ try (Connection connection = dbTester.openConnection()) {
+ assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName)).isTrue();
+ assertThat(DatabaseUtils.tableColumnExists(connection, tableName.toLowerCase(Locale.US), columnName)).isTrue();
+ }
+ }
+
+ @Test
+ public void find_column_with_upper_case_table_name_and_lower_case_column_name() throws SQLException {
+ String tableName = "TABLEA";
+ String columnName = "columna";
+ try (Connection connection = dbTester.openConnection()) {
+ assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName)).isTrue();
+ assertThat(DatabaseUtils.tableColumnExists(connection, tableName, columnName.toLowerCase(Locale.US))).isTrue();
+ assertThat(DatabaseUtils.tableColumnExists(connection, tableName.toLowerCase(Locale.US), columnName.toLowerCase(Locale.US))).isTrue();
+ }
+ }
+
+ @Test
public void should_close_connection() throws Exception {
try (Connection connection = dbTester.openConnection()) {
assertThat(isClosed(connection)).isFalse();
diff --git a/server/sonar-db-core/src/test/resources/org/sonar/db/DatabaseUtilsTest/sql.sql b/server/sonar-db-core/src/test/resources/org/sonar/db/DatabaseUtilsTest/sql.sql
index ec4b8782e31..e0fbf26800c 100644
--- a/server/sonar-db-core/src/test/resources/org/sonar/db/DatabaseUtilsTest/sql.sql
+++ b/server/sonar-db-core/src/test/resources/org/sonar/db/DatabaseUtilsTest/sql.sql
@@ -2,6 +2,10 @@ CREATE TABLE "schema_migrations" (
"version" VARCHAR(256) NOT NULL
);
+CREATE TABLE "TABLEA" (
+ "COLUMNA" VARCHAR(256) NOT NULL
+);
+
CREATE INDEX UPPER_CASE_NAME ON schema_migrations (version);
CREATE INDEX lower_case_name ON schema_migrations (version);