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 issue with sp_tables failing for cross-database table qualifiers (#2961) #2984

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
7 changes: 6 additions & 1 deletion contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions test/JDBC/expected/SP_TABLES-cross-db-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions test/JDBC/expected/SP_TABLES-cross-db-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -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
68 changes: 68 additions & 0 deletions test/JDBC/expected/SP_TABLES-cross-db-vu-verify.out
Original file line number Diff line number Diff line change
@@ -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#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!#<NULL>
~~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#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!#<NULL>
~~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#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!#<NULL>
~~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#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#babel_5263_vu_prepare_v1#!#VIEW#!#<NULL>
babel_5263_vu_prepare_db1#!#dbo#!#sysdatabases#!#VIEW#!#<NULL>
~~END~~

20 changes: 20 additions & 0 deletions test/JDBC/input/SP_TABLES-cross-db-vu-cleanup.sql
Original file line number Diff line number Diff line change
@@ -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
17 changes: 17 additions & 0 deletions test/JDBC/input/SP_TABLES-cross-db-vu-prepare.sql
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions test/JDBC/input/SP_TABLES-cross-db-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions test/JDBC/upgrade/latest/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading