diff --git a/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql b/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql index 4ed0f5c4cb..1da46ef62a 100644 --- a/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql +++ b/contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql @@ -993,6 +993,9 @@ CREATE OR REPLACE PROCEDURE sys.sp_tables ( AS $$ BEGIN + -- Temporary variable to hold the current database name + DECLARE @current_db_name sys.sysname; + -- Handle special case: Enumerate all databases when name and owner are blank but qualifier is '%' IF (@table_qualifier = '%' AND @table_owner = '' AND @table_name = '') BEGIN @@ -1007,7 +1010,9 @@ BEGIN RETURN; END; - IF (@table_qualifier != '' AND LOWER(@table_qualifier) != LOWER(sys.db_name())) + SELECT @current_db_name = sys.db_name(); + + IF (@table_qualifier != '' AND LOWER(@table_qualifier) != LOWER(@current_db_name)) BEGIN THROW 33557097, N'The database name component of the object qualifier must be the name of the current database.', 1; END diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.7.0--3.8.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.7.0--3.8.0.sql index 2ce4c5b916..a94f614c3d 100644 --- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.7.0--3.8.0.sql +++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.7.0--3.8.0.sql @@ -157,6 +157,9 @@ CREATE OR REPLACE PROCEDURE sys.sp_tables ( AS $$ BEGIN + -- Temporary variable to hold the current database name + DECLARE @current_db_name sys.sysname; + -- Handle special case: Enumerate all databases when name and owner are blank but qualifier is '%' IF (@table_qualifier = '%' AND @table_owner = '' AND @table_name = '') BEGIN @@ -171,7 +174,9 @@ BEGIN RETURN; END; - IF (@table_qualifier != '' AND LOWER(@table_qualifier) != LOWER(sys.db_name())) + SELECT @current_db_name = sys.db_name(); + + IF (@table_qualifier != '' AND LOWER(@table_qualifier) != LOWER(@current_db_name)) BEGIN THROW 33557097, N'The database name component of the object qualifier must be the name of the current database.', 1; END diff --git a/test/JDBC/expected/SP_TABLES-cross-db-vu-cleanup.out b/test/JDBC/expected/SP_TABLES-cross-db-vu-cleanup.out new file mode 100644 index 0000000000..f1e150bbcf --- /dev/null +++ b/test/JDBC/expected/SP_TABLES-cross-db-vu-cleanup.out @@ -0,0 +1,20 @@ +USE babel_5263_vu_prepare_db1; +GO + +DROP FUNCTION IF EXISTS babel_5263_vu_prepare_f1; +GO + +DROP PROCEDURE IF EXISTS babel_5263_vu_prepare_p1; +GO + +DROP VIEW IF EXISTS babel_5263_vu_prepare_v1; +GO + +DROP TABLE IF EXISTS babel_5263_vu_prepare_t1; +GO + +USE master; +GO + +DROP DATABASE IF EXISTS babel_5263_vu_prepare_db1; +GO diff --git a/test/JDBC/expected/SP_TABLES-cross-db-vu-prepare.out b/test/JDBC/expected/SP_TABLES-cross-db-vu-prepare.out new file mode 100644 index 0000000000..6f870c8475 --- /dev/null +++ b/test/JDBC/expected/SP_TABLES-cross-db-vu-prepare.out @@ -0,0 +1,17 @@ +CREATE DATABASE babel_5263_vu_prepare_db1; +GO + +USE babel_5263_vu_prepare_db1; +GO + +CREATE TABLE babel_5263_vu_prepare_t1 (a INT); +GO + +CREATE PROCEDURE babel_5263_vu_prepare_p1 AS SELECT 1; +GO + +CREATE FUNCTION babel_5263_vu_prepare_f1() RETURNS INT AS BEGIN RETURN 1 END; +GO + +CREATE VIEW babel_5263_vu_prepare_v1 AS SELECT 1; +GO diff --git a/test/JDBC/expected/SP_TABLES-cross-db-vu-verify.out b/test/JDBC/expected/SP_TABLES-cross-db-vu-verify.out new file mode 100644 index 0000000000..c59b1ff8c9 --- /dev/null +++ b/test/JDBC/expected/SP_TABLES-cross-db-vu-verify.out @@ -0,0 +1,68 @@ +-- Case 1: Correct Database Context +USE babel_5263_vu_prepare_db1; +GO + +-- Enumerate matching objects +EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1; +GO +~~START~~ +varchar#!#varchar#!#varchar#!#varchar#!#varchar +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_t1#!#TABLE#!# +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!# +babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!# +~~END~~ + + +-- Case 2: Mismatched Database Context +USE master; +GO + +-- Enumerate matching objects +EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1; +GO +~~START~~ +varchar#!#varchar#!#varchar#!#varchar#!#varchar +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_t1#!#TABLE#!# +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!# +babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!# +~~END~~ + + +-- Case 3: No Table Qualifier - Current Database Assumed +USE babel_5263_vu_prepare_db1; +GO + +EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, NULL, NULL, 1; +GO +~~START~~ +varchar#!#varchar#!#varchar#!#varchar#!#varchar +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_t1#!#TABLE#!# +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!# +babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!# +~~END~~ + + +-- Case 4: Cross-database Access - Mismatch +USE babel_5263_vu_prepare_db1; +GO + +EXEC master.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1; +GO +~~ERROR (Code: 33557097)~~ + +~~ERROR (Message: The database name component of the object qualifier must be the name of the current database.)~~ + + +-- Case 5: Case Sensitivity in Table Qualifier +USE master; +GO + +EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_VU_prepARe_db1', NULL, 1; +GO +~~START~~ +varchar#!#varchar#!#varchar#!#varchar#!#varchar +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_t1#!#TABLE#!# +babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!# +babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!# +~~END~~ + diff --git a/test/JDBC/input/SP_TABLES-cross-db-vu-cleanup.sql b/test/JDBC/input/SP_TABLES-cross-db-vu-cleanup.sql new file mode 100644 index 0000000000..f1e150bbcf --- /dev/null +++ b/test/JDBC/input/SP_TABLES-cross-db-vu-cleanup.sql @@ -0,0 +1,20 @@ +USE babel_5263_vu_prepare_db1; +GO + +DROP FUNCTION IF EXISTS babel_5263_vu_prepare_f1; +GO + +DROP PROCEDURE IF EXISTS babel_5263_vu_prepare_p1; +GO + +DROP VIEW IF EXISTS babel_5263_vu_prepare_v1; +GO + +DROP TABLE IF EXISTS babel_5263_vu_prepare_t1; +GO + +USE master; +GO + +DROP DATABASE IF EXISTS babel_5263_vu_prepare_db1; +GO diff --git a/test/JDBC/input/SP_TABLES-cross-db-vu-prepare.sql b/test/JDBC/input/SP_TABLES-cross-db-vu-prepare.sql new file mode 100644 index 0000000000..6f870c8475 --- /dev/null +++ b/test/JDBC/input/SP_TABLES-cross-db-vu-prepare.sql @@ -0,0 +1,17 @@ +CREATE DATABASE babel_5263_vu_prepare_db1; +GO + +USE babel_5263_vu_prepare_db1; +GO + +CREATE TABLE babel_5263_vu_prepare_t1 (a INT); +GO + +CREATE PROCEDURE babel_5263_vu_prepare_p1 AS SELECT 1; +GO + +CREATE FUNCTION babel_5263_vu_prepare_f1() RETURNS INT AS BEGIN RETURN 1 END; +GO + +CREATE VIEW babel_5263_vu_prepare_v1 AS SELECT 1; +GO diff --git a/test/JDBC/input/SP_TABLES-cross-db-vu-verify.sql b/test/JDBC/input/SP_TABLES-cross-db-vu-verify.sql new file mode 100644 index 0000000000..468b130e0d --- /dev/null +++ b/test/JDBC/input/SP_TABLES-cross-db-vu-verify.sql @@ -0,0 +1,36 @@ +-- Case 1: Correct Database Context +USE babel_5263_vu_prepare_db1; +GO + +-- Enumerate matching objects +EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1; +GO + +-- Case 2: Mismatched Database Context +USE master; +GO + +-- Enumerate matching objects +EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1; +GO + +-- Case 3: No Table Qualifier - Current Database Assumed +USE babel_5263_vu_prepare_db1; +GO + +EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, NULL, NULL, 1; +GO + +-- Case 4: Cross-database Access - Mismatch +USE babel_5263_vu_prepare_db1; +GO + +EXEC master.sys.sp_tables NULL, NULL, 'babel_5263_vu_prepare_db1', NULL, 1; +GO + +-- Case 5: Case Sensitivity in Table Qualifier +USE master; +GO + +EXEC babel_5263_vu_prepare_db1.sys.sp_tables NULL, NULL, 'babel_5263_VU_prepARe_db1', NULL, 1; +GO diff --git a/test/JDBC/upgrade/latest/schedule b/test/JDBC/upgrade/latest/schedule index 39b17a7232..bf12deecea 100644 --- a/test/JDBC/upgrade/latest/schedule +++ b/test/JDBC/upgrade/latest/schedule @@ -188,6 +188,7 @@ BABEL-SP_STORED_PROCEDURES-dep BABEL-SP_TABLE_PRIVILIGES BABEL-SP_TABLES SP_TABLES-dep +SP_TABLES-cross-db babel_sqlvariant_cast_compare BABEL-SQUARE BABEL-TABLEOPTIONS