diff options
author | Lukasz Jarocki <lukasz.jarocki@sonarsource.com> | 2023-06-12 11:05:54 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-06-19 20:03:14 +0000 |
commit | 662aa306ee814d748d87b403d5ac03cf04727a2a (patch) | |
tree | fd86512cd5156a3e6a7059adad7e397093875b92 /server/sonar-db-core/src | |
parent | 15d898d6139d8c661aeb27ae5079a737230b7216 (diff) | |
download | sonarqube-662aa306ee814d748d87b403d5ac03cf04727a2a.tar.gz sonarqube-662aa306ee814d748d87b403d5ac03cf04727a2a.zip |
SONAR-19445 renamed permissions' component_uuid column to entity_uuid
Diffstat (limited to 'server/sonar-db-core/src')
3 files changed, 75 insertions, 5 deletions
diff --git a/server/sonar-db-core/src/it/java/org/sonar/db/DatabaseUtilsIT.java b/server/sonar-db-core/src/it/java/org/sonar/db/DatabaseUtilsIT.java index 7d532a01d7a..eb3a3273a00 100644 --- a/server/sonar-db-core/src/it/java/org/sonar/db/DatabaseUtilsIT.java +++ b/server/sonar-db-core/src/it/java/org/sonar/db/DatabaseUtilsIT.java @@ -62,6 +62,7 @@ import static org.sonar.db.DatabaseUtils.closeQuietly; import static org.sonar.db.DatabaseUtils.getDriver; import static org.sonar.db.DatabaseUtils.log; import static org.sonar.db.DatabaseUtils.tableColumnExists; +import static org.sonar.db.DatabaseUtils.getColumnMetadata; import static org.sonar.db.DatabaseUtils.tableExists; import static org.sonar.db.DatabaseUtils.toUniqueAndSortedList; @@ -144,6 +145,24 @@ public class DatabaseUtilsIT { } @Test + public void getColumnMetadata_whenTableNameLowerCaseColumnUpperCase_shouldFindColumn() throws SQLException { + String tableName = "tablea"; + String columnName = "COLUMNA"; + try (Connection connection = dbTester.openConnection()) { + assertThat(getColumnMetadata(connection, tableName, columnName)).isNotNull(); + } + } + + @Test + public void getColumnMetadata_whenArgumentInUpperCase_shouldFindColumn() throws SQLException { + String tableName = "TABLEA"; + String columnName = "COLUMNA"; + try (Connection connection = dbTester.openConnection()) { + assertThat(getColumnMetadata(connection, tableName, columnName)).isNotNull(); + } + } + + @Test public void closeQuietly_shouldCloseConnection() throws SQLException { try (Connection connection = dbTester.openConnection()) { assertThat(isClosed(connection)).isFalse(); diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/ColumnMetadata.java b/server/sonar-db-core/src/main/java/org/sonar/db/ColumnMetadata.java new file mode 100644 index 00000000000..cca97b942cc --- /dev/null +++ b/server/sonar-db-core/src/main/java/org/sonar/db/ColumnMetadata.java @@ -0,0 +1,24 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.db; + +public record ColumnMetadata(String name, boolean nullable, int sqlType, int limit) { + +} 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 64936983ade..a20a470e39a 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 @@ -106,7 +106,7 @@ public class DatabaseUtils { /** * Partition by 1000 elements a list of input and execute a function on each part. - * + * <p> * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query */ @@ -116,7 +116,7 @@ public class DatabaseUtils { /** * Partition by 1000 elements a list of input and execute a function on each part. - * + * <p> * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query */ @@ -147,7 +147,7 @@ public class DatabaseUtils { /** * Partition by 1000 elements a list of input and execute a consumer on each part. - * + * <p> * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query */ @@ -157,7 +157,7 @@ public class DatabaseUtils { /** * Partition by 1000 elements a list of input and execute a consumer on each part. - * + * <p> * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query * @@ -206,7 +206,7 @@ public class DatabaseUtils { /** * Partition by 1000 elements a list of input and execute a consumer on each part. - * + * <p> * The goal is to prevent issue with ORACLE when there's more than 1000 elements in a 'in ('X', 'Y', ...)' * and with MsSQL when there's more than 2000 parameters in a query */ @@ -403,6 +403,8 @@ public class DatabaseUtils { String schema = getSchema(connection); try (ResultSet rs = connection.getMetaData().getColumns(connection.getCatalog(), schema, tableName, null)) { while (rs.next()) { + // this is wrong and could lead to bugs, there is no point of going through each column - only one column contains column name + // see the contract (javadoc) of java.sql.DatabaseMetaData.getColumns for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) { String name = rs.getString(i); if (columnName.equalsIgnoreCase(name)) { @@ -413,6 +415,31 @@ public class DatabaseUtils { return false; } } + @CheckForNull + public static ColumnMetadata getColumnMetadata(Connection connection, String tableName, String columnName) throws SQLException { + ColumnMetadata columnMetadataLowerCase = getColumnMetadataWithCaseSensitiveTableName(connection, tableName.toLowerCase(Locale.US), columnName); + if (columnMetadataLowerCase != null) { + return columnMetadataLowerCase; + } + return getColumnMetadataWithCaseSensitiveTableName(connection, tableName.toUpperCase(Locale.US), columnName); + } + + @CheckForNull + public static ColumnMetadata getColumnMetadataWithCaseSensitiveTableName(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()) { + String name = rs.getString(4); + int type = rs.getInt(5); + int limit = rs.getInt(7); + boolean nullable = rs.getBoolean(11); + if (columnName.equalsIgnoreCase(name)) { + return new ColumnMetadata(name, nullable, type, limit); + } + } + return null; + } + } @CheckForNull static String getDriver(Connection connection) { |