Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape schemaPattern for getSchemas #2195

Merged
merged 5 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1518,7 +1518,7 @@ public java.sql.ResultSet getSchemas(String catalog, String schemaPattern) throw
if (loggerExternal.isLoggable(Level.FINER) && Util.isActivityTraceOn()) {
loggerExternal.finer(toString() + ACTIVITY_ID + ActivityCorrelator.getCurrent().toString());
}
return getSchemasInternal(catalog, schemaPattern);
return getSchemasInternal(catalog, escapeIDName(schemaPattern));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,57 @@ public void testDBSchemasForDashedCatalogNameWithPattern() throws SQLException {
}
}

/**
* Tests that the schemaPattern parameter containing _ and % are escaped by
* {@link SQLServerDatabaseMetaData#getSchemas(String catalog, String schemaPattern)}.
*
* @throws SQLException
*/
@Test
@Tag(Constants.xAzureSQLDW)
@Tag(Constants.xAzureSQLDB)
public void testDBSchemasForSchemaPatternWithWildcards() throws SQLException {
UUID id = UUID.randomUUID();
String testCatalog = "catalog" + id;
String[] schemas = {"some_schema", "some%schema", "some[schema"};
String[] schemaPatterns = {"some\\_schema", "some\\%schema", "some\\[schema"};

try (Connection conn = getConnection(); Statement stmt = conn.createStatement()) {
TestUtils.dropDatabaseIfExists(testCatalog, connectionString);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));
stmt.execute(String.format("USE [%s]", testCatalog));

for (int i = 0; i < schemas.length; ++i) {
stmt.execute(String.format("CREATE SCHEMA [%s]", schemas[i]));

try (ResultSet rs = conn.getMetaData().getSchemas(testCatalog, schemaPatterns[i])) {

MessageFormat schemaEmptyFormat = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[] schemaMsgArgs = {schemas[i]};
Object[] catalogMsgArgs = {testCatalog};

boolean hasResults = false;
while (rs.next()) {
hasResults = true;
String schemaName = rs.getString(1);
String catalogName = rs.getString(2);
assertTrue(!StringUtils.isEmpty(schemaName), schemaEmptyFormat.format(schemaMsgArgs));
assertTrue(!StringUtils.isEmpty(catalogName), schemaEmptyFormat.format(catalogMsgArgs));
assertEquals(schemaName, schemaMsgArgs[0]);
assertEquals(catalogName, catalogMsgArgs[0]);
}

MessageFormat atLeastOneFoundFormat = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
assertTrue(hasResults, atLeastOneFoundFormat.format(schemaMsgArgs));
}
}
} catch (Exception e) {
fail(TestResource.getResource("R_unexpectedErrorMessage") + e.getMessage());
} finally {
TestUtils.dropDatabaseIfExists(testCatalog, connectionString);
}
}

/**
* Get All Tables.
*
Expand Down
Loading