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

fix for getSchema when using "-" in name #718

Merged
merged 6 commits into from
Jun 20, 2018
Merged
Show file tree
Hide file tree
Changes from 5 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 @@ -1177,8 +1177,9 @@ private java.sql.ResultSet getSchemasInternal(String catalog,
String schema = "sys.schemas";
String schemaName = "sys.schemas.name";
if (null != catalog && catalog.length() != 0) {
schema = catalog + "." + schema;
schemaName = catalog + "." + schemaName;
final String catalogId = Util.escapeSQLId(catalog);
schema = catalogId + "." + schema;
schemaName = catalogId + "." + schemaName;
}

// The common schemas need to be under null catalog name however the schemas specific to the particular catalog has to have the current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
*/
package com.microsoft.sqlserver.jdbc.databasemetadata;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
Expand All @@ -23,6 +25,8 @@
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.UUID;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.text.MessageFormat;
Expand Down Expand Up @@ -175,6 +179,107 @@ public void testDBSchema() throws SQLException {
assertTrue(!StringUtils.isEmpty(rs.getString(1)), form.format(msgArgs));
}
}

/**
* Tests that the catalog parameter containing - is escaped by {@link SQLServerDatabaseMetaData#getSchemas(String catalog, String schemaPattern)}.
*
* @throws SQLException
*/
@Test
public void testDBSchemasForDashedCatalogName() throws SQLException {
UUID id = UUID.randomUUID();
String testCatalog = "dash-catalog" + id;
String testSchema = "some-schema" + id;
Statement stmt = connection.createStatement();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move this into try with resources.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot resolve variables declared in try-with-resources in a finally block, and a finally block is used to drop the created db.


try (Connection dashConn = DriverManager.getConnection(connectionString); Statement dashStatement = dashConn.createStatement()) {

Utils.dropDatabaseIfExists(testCatalog, stmt);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));

dashStatement.execute(String.format("USE [%s]", testCatalog));
dashStatement.execute(String.format("CREATE SCHEMA [%s]", testSchema));

DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet rs = databaseMetaData.getSchemas(testCatalog, null);

MessageFormat schemaEmptyFormat = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[] schemaMsgArgs = {"Schema"};

boolean hasResults = false;
boolean hasDashCatalogSchema = false;
while (rs.next()) {
hasResults = true;
String schemaName = rs.getString(1);
assertTrue(!StringUtils.isEmpty(schemaName), schemaEmptyFormat.format(schemaMsgArgs));
String catalogName = rs.getString(2);
if (schemaName.equals(testSchema)) {
hasDashCatalogSchema = true;
assertEquals(catalogName, testCatalog);
}
else {
assertNull(catalogName);
}
}

MessageFormat atLeastOneFoundFormat = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
assertTrue(hasResults, atLeastOneFoundFormat.format(schemaMsgArgs));

MessageFormat dashCatalogFormat = new MessageFormat(TestResource.getResource("R_atLeastOneFound"));
assertTrue(hasDashCatalogSchema, dashCatalogFormat.format(new Object[] {testSchema}));
}
finally {
Utils.dropDatabaseIfExists(testCatalog, stmt);
}
stmt.close();
}

/**
* Tests that the catalog parameter containing - is escaped by {@link SQLServerDatabaseMetaData#getSchemas(String catalog, String schemaPattern)}.
*
* @throws SQLException
*/
@Test
public void testDBSchemasForDashedCatalogNameWithPattern() throws SQLException {
UUID id = UUID.randomUUID();
String testCatalog = "dash-catalog" + id;
String testSchema = "some-schema" + id;
Statement stmt = connection.createStatement();

try (Connection dashConn = DriverManager.getConnection(connectionString); Statement dashStatement = dashConn.createStatement()) {

Utils.dropDatabaseIfExists(testCatalog, stmt);
stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog));

dashStatement.execute(String.format("USE [%s]", testCatalog));
dashStatement.execute(String.format("CREATE SCHEMA [%s]", testSchema));

DatabaseMetaData databaseMetaData = connection.getMetaData();
ResultSet rs = databaseMetaData.getSchemas(testCatalog, "some-%");

MessageFormat schemaEmptyFormat = new MessageFormat(TestResource.getResource("R_nameEmpty"));
Object[] schemaMsgArgs = {testSchema};
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));
}
finally {
Utils.dropDatabaseIfExists(testCatalog, stmt);
}
stmt.close();
}

/**
* Get All Tables.
Expand Down