Skip to content

Commit

Permalink
FIR-32802 use catalog parameter in getTables() & getColumns()
Browse files Browse the repository at this point in the history
  • Loading branch information
alexradzin committed May 2, 2024
1 parent 1053928 commit d860191
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.IntStream;

import static java.lang.String.format;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -54,21 +56,28 @@ void getSchemas() throws SQLException {

@ParameterizedTest
@CsvSource(value = {
",",
",information_schema",
",information%",
",%schema",
",%form%",
"{database},",
"{database},information_schema",
"{database},information%",
"{database},%schema",
"{database},%form%",
",,information_schema",
",information_schema,information_schema",
",information%,information_schema",
",%schema,information_schema",
",%form%,information_schema",
"{database},,information_schema",
"{database},information_schema,information_schema",
"{database},information%,information_schema",
"{database},%schema,information_schema",
"{database},%form%,information_schema",

"wrong_catalog,,",
"wrong_catalog,%form%,",
})
void getSchemasInformationSchema(String catalog, String schemaPattern) throws SQLException {
void getSchemasInformationSchema(String catalog, String schemaPattern, String expectedSchemasStr) throws SQLException {
String database = integration.ConnectionInfo.getInstance().getDatabase();
String cat = catalog == null ? null : catalog.replace("{database}", database);
assertEquals(List.of(List.of("information_schema", database)), getSchemas(dbmd -> dbmd.getSchemas(cat, schemaPattern)));
List<List<String>> expectedSchemas = expectedSchemasStr == null ?
List.of()
:
Arrays.stream(expectedSchemasStr.split(";")).map(schema -> List.of(schema, database)).collect(toList());
assertEquals(expectedSchemas, getSchemas(dbmd -> dbmd.getSchemas(cat, schemaPattern)));
}

@ParameterizedTest
Expand Down Expand Up @@ -109,6 +118,7 @@ void getSchemasInformationSchema(String catalog, String schemaPattern) throws SQ
",,,VIEW;TABLE,engines,",
",,%in%,VIEW;TABLE,engines,tables",
",,,TABLE,,",
"wrong_catalog,%form%,%in%,VIEW,,engines",
})
void getTables(String catalog, String schemaPattern, String tableNamePattern, String types, String requiredTableName, String forbiddenTableName) throws SQLException {
String database = integration.ConnectionInfo.getInstance().getDatabase();
Expand All @@ -125,7 +135,7 @@ void getTables(String catalog, String schemaPattern, String tableNamePattern, St
System.out.println(row.get(2));
}
if (requiredTableName == null) {
assertTrue(tables.isEmpty());
assertTrue(tables.isEmpty(), "List of tables must be empty but it was not");
} else {
assertTrue(tables.contains(requiredTableName), format("Required table %s is not found", requiredTableName));
}
Expand Down Expand Up @@ -172,6 +182,7 @@ void getTables(String catalog, String schemaPattern, String tableNamePattern, St
",,,nobody,,information_schema.columns.column_name",
",,%in%,no-one,,information_schema.columns.column_name",
",,,does-not-exist,,information_schema.columns.column_name",
"wrong_catalog,%form%,%in%,type,,information_schema.engines.type",
})
void getColumns(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern, String requiredColumn, String forbiddenColumn) throws SQLException {
String database = integration.ConnectionInfo.getInstance().getDatabase();
Expand All @@ -186,7 +197,7 @@ void getColumns(String catalog, String schemaPattern, String tableNamePattern, S
columns.add(IntStream.of(1, 2, 3).boxed().map(i -> (String)row.get(i)).collect(joining(".")));
}
if (requiredColumn == null) {
assertTrue(columns.isEmpty());
assertTrue(columns.isEmpty(), "List of columns must be empty but it was not");
} else {
assertTrue(columns.contains(requiredColumn), format("Required column %s is not found", requiredColumn));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public ResultSet getColumns(String catalog, String schemaPattern, String tableNa
QueryResult.Column.builder().name(IS_GENERATEDCOLUMN).type(TEXT).build());

List<List<?>> rows = new ArrayList<>();
String query = MetadataUtil.getColumnsQuery(schemaPattern, tableNamePattern, columnNamePattern);
String query = MetadataUtil.getColumnsQuery(catalog, schemaPattern, tableNamePattern, columnNamePattern);
try (Statement statement = connection.createStatement();
ResultSet columnDescription = statement.executeQuery(query)) {
while (columnDescription.next()) {
Expand Down Expand Up @@ -1316,7 +1316,7 @@ public ResultSet getColumnPrivileges(String catalog, String schemaPattern, Strin
).map(e -> QueryResult.Column.builder().name(e.getKey()).type(e.getValue()).build()).collect(toList());

List<List<?>> rows = new ArrayList<>();
String query = MetadataUtil.getColumnsQuery(schemaPattern, tableNamePattern, columnNamePattern);
String query = MetadataUtil.getColumnsQuery(catalog, schemaPattern, tableNamePattern, columnNamePattern);
try (Statement statement = connection.createStatement();
ResultSet columnDescription = statement.executeQuery(query)) {
while (columnDescription.next()) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/firebolt/jdbc/metadata/MetadataUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class MetadataUtil {
// return queryBuilder.conditions(conditions).build().toSql();
// }

public String getColumnsQuery(String schemaPattern, String tableNamePattern, String columnNamePattern) {
public String getColumnsQuery(String catalog, String schemaPattern, String tableNamePattern, String columnNamePattern) {
Query.QueryBuilder queryBuilder = Query.builder().select(
"table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position")
.from("information_schema.columns");
Expand All @@ -44,15 +44,15 @@ public String getColumnsQuery(String schemaPattern, String tableNamePattern, Str
ofNullable(tableNamePattern).ifPresent(pattern -> conditions.add(format("table_name LIKE '%s'", pattern)));
ofNullable(columnNamePattern).ifPresent(pattern -> conditions.add(format("column_name LIKE '%s'", pattern)));
ofNullable(schemaPattern).ifPresent(pattern -> conditions.add(format("table_schema LIKE '%s'", pattern)));
ofNullable(catalog).ifPresent(pattern -> conditions.add(String.format("table_catalog LIKE '%s'", pattern)));
return queryBuilder.conditions(conditions).build().toSql();
}

public String getTablesQuery(@SuppressWarnings("java:S1172") String catalog, String schema, String tableName, String[] types) {
public String getTablesQuery(String catalog, String schema, String tableName, String[] types) {
Query.QueryBuilder queryBuilder = Query.builder().select("table_schema, table_name, table_type").from("information_schema.tables");
List<String> conditions = new ArrayList<>();
conditions.add(format("table_type IN (%s)", Arrays.stream(types).map(t -> format("'%s'", t)).collect(joining(", "))));
// Uncomment once table catalogs are supported. Remove suppress warning ava:S1172 from the first parameter also.
//ofNullable(catalog).ifPresent(pattern -> conditions.add(String.format("table_catalog LIKE '%s'",pattern)));
ofNullable(catalog).ifPresent(pattern -> conditions.add(String.format("table_catalog LIKE '%s'", pattern)));
ofNullable(schema).ifPresent(pattern -> conditions.add(format("table_schema LIKE '%s'", pattern)));
ofNullable(tableName).ifPresent(pattern -> conditions.add(format("table_name LIKE '%s'", pattern)));
return queryBuilder.conditions(conditions).orderBy("table_schema, table_name").build().toSql();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ void shouldGetTablesQueryWhenGettingQueryWithoutArguments() {
@Test
void shouldGetColumnsQueryWhenGettingQueryWithArguments() {
assertEquals(
"SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position FROM information_schema.columns WHERE table_name LIKE 'tableName' AND column_name LIKE 'col%' AND table_schema LIKE 'schema'",
MetadataUtil.getColumnsQuery("schema", "tableName", "col%"));
"SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position FROM information_schema.columns WHERE table_name LIKE 'tableName' AND column_name LIKE 'col%' AND table_schema LIKE 'schema' AND table_catalog LIKE 'db'",
MetadataUtil.getColumnsQuery("db", "schema", "tableName", "col%"));
}

@Test
void shouldGetColumnsQueryWhenGettingQueryWithoutArguments() {
assertEquals(
"SELECT table_schema, table_name, column_name, data_type, column_default, is_nullable, ordinal_position FROM information_schema.columns",
MetadataUtil.getColumnsQuery(null, null, null));
MetadataUtil.getColumnsQuery(null, null, null, null));
}
}

0 comments on commit d860191

Please sign in to comment.