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 schema for getProcedures and getProcedureColumns in SQLServerDatabaseMetaData #2369

Merged
merged 4 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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 @@ -1425,7 +1425,7 @@ public java.sql.ResultSet getProcedureColumns(String catalog, String schema, Str
// proc name supports escaping
proc = escapeIDName(proc);
arguments[0] = proc;
arguments[1] = schema;
arguments[1] = escapeIDName(schema);
tkyc marked this conversation as resolved.
Show resolved Hide resolved
arguments[2] = catalog;
// col name supports escaping
col = escapeIDName(col);
Expand Down Expand Up @@ -1466,7 +1466,7 @@ public java.sql.ResultSet getProcedures(String catalog, String schema,
*/
String[] arguments = new String[3];
arguments[0] = escapeIDName(proc);
arguments[1] = schema;
arguments[1] = escapeIDName(schema);
arguments[2] = catalog;
return getResultSetWithProvidedColumnNames(catalog, CallableHandles.SP_STORED_PROCEDURES, arguments,
getProceduresColumnNames);
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/microsoft/sqlserver/jdbc/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ public static void dropTableIfExists(String tableName, java.sql.Statement stmt)
dropObjectIfExists(tableName, "U", stmt);
}

public static void dropTableWithSchemaIfExists(String tableNameWithSchema, java.sql.Statement stmt) throws SQLException {
stmt.execute("IF OBJECT_ID('" + tableNameWithSchema + "', 'U') IS NOT NULL DROP TABLE " + tableNameWithSchema + ";");
}


public static void dropProcedureWithSchemaIfExists(String procedureWithSchema, java.sql.Statement stmt) throws SQLException {
stmt.execute("IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'" + procedureWithSchema + "') AND type in (N'P', N'PC')) DROP PROCEDURE " + procedureWithSchema + ";");
tkyc marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Deletes the contents of a table.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@
@RunWith(JUnitPlatform.class)
public class DatabaseMetaDataTest extends AbstractTest {

private static final String uuid = UUID.randomUUID().toString().replaceAll("-", "");
private static final String tableName = RandomUtil.getIdentifier("DBMetadataTable");
private static final String functionName = RandomUtil.getIdentifier("DBMetadataFunction");
private static final String schema = "schema_demo" + uuid;
private static final String escapedSchema = "schema\\_demo" + uuid;
private static final String tableNameWithSchema = schema + ".resource";
private static final String sprocWithSchema = schema + ".updateresource";
private static Map<Integer, String> getColumnsDWColumns = null;
private static Map<Integer, String> getImportedKeysDWColumns = null;
private static final String TABLE_CAT = "TABLE_CAT";
Expand Down Expand Up @@ -991,6 +996,25 @@ public void testValidateColumnMetadata() throws SQLException {
}
}

@Test
public void shouldEscapeSchemaName() throws SQLException {
try (Connection con = getConnection()) {
DatabaseMetaData md = con.getMetaData();
try (ResultSet procedures = md.getProcedures(
null, escapedSchema, "updateresource")) {
if (!procedures.next()) {
fail("Escaped schema pattern did not succeed. No results found.");
}
}

try (ResultSet columns = md.getProcedureColumns(null, escapedSchema, "updateresource", null)) {
if (!columns.next()) {
fail("Escaped schema pattern did not succeed. No results found.");
}
}
}
}

@BeforeAll
public static void setupTable() throws Exception {
setConnection();
Expand All @@ -1000,6 +1024,9 @@ public static void setupTable() throws Exception {
+ " ([col_1] int NOT NULL, [col%2] varchar(200), [col[3] decimal(15,2))");
stmt.execute("CREATE FUNCTION " + AbstractSQLGenerator.escapeIdentifier(functionName)
+ " (@p1 INT, @p2 INT) RETURNS INT AS BEGIN DECLARE @result INT; SET @result = @p1 + @p2; RETURN @result; END");
stmt.execute("CREATE SCHEMA " + schema);
stmt.execute("CREATE TABLE " + tableNameWithSchema + " (id UNIQUEIDENTIFIER, name NVARCHAR(400));");
stmt.execute("CREATE PROCEDURE " + sprocWithSchema + "(@id UNIQUEIDENTIFIER, @name VARCHAR(400)) AS BEGIN SET TRANSACTION ISOLATION LEVEL SERIALIZABLE BEGIN TRANSACTION UPDATE " + tableNameWithSchema + " SET name = @name WHERE id = @id COMMIT END");
tkyc marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -1008,6 +1035,9 @@ public static void terminate() throws SQLException {
try (Statement stmt = connection.createStatement()) {
TestUtils.dropTableIfExists(tableName, stmt);
TestUtils.dropFunctionIfExists(functionName, stmt);
TestUtils.dropTableWithSchemaIfExists(tableNameWithSchema, stmt);
TestUtils.dropProcedureWithSchemaIfExists(sprocWithSchema, stmt);
TestUtils.dropSchemaIfExists(schema, stmt);
}
}
}
Loading