Skip to content

Commit

Permalink
Support double-escaping and unbalanced quotes in sp_tables table_type…
Browse files Browse the repository at this point in the history
… parameter 3x

Signed-off-by: Alex Kasko <[email protected]>
  • Loading branch information
staticlibs committed Apr 8, 2024
1 parent 022e8a6 commit 2ade013
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 2 deletions.
4 changes: 2 additions & 2 deletions contrib/babelfishpg_tsql/sql/babelfishpg_tsql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1000,10 +1000,10 @@ CREATE OR REPLACE FUNCTION sys.sp_tables_internal(
DECLARE opt_view sys.varchar(16) = '';
DECLARE cs_as_in_table_type varchar COLLATE "C" = in_table_type;
BEGIN
IF (SELECT count(*) FROM unnest(string_to_array(cs_as_in_table_type, ',')) WHERE upper(trim(unnest)) = '''TABLE''' OR upper(trim(unnest)) = '''''''TABLE''''''') >= 1 THEN
IF upper(cs_as_in_table_type) LIKE '%''TABLE''%' THEN
opt_table = 'TABLE';
END IF;
IF (SELECT count(*) from unnest(string_to_array(cs_as_in_table_type, ',')) WHERE upper(trim(unnest)) = '''VIEW''' OR upper(trim(unnest)) = '''''''VIEW''''''') >= 1 THEN
IF upper(cs_as_in_table_type) LIKE '%''VIEW''%' THEN
opt_view = 'VIEW';
END IF;
IF in_fusepattern = 1 THEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,64 @@ CALL sys.analyze_babelfish_catalogs();

-- Reset search_path to not affect any subsequent scripts
SELECT set_config('search_path', trim(leading 'sys, ' from current_setting('search_path')), false);

CREATE OR REPLACE FUNCTION sys.sp_tables_internal(
in_table_name sys.nvarchar(384) = '',
in_table_owner sys.nvarchar(384) = '',
in_table_qualifier sys.sysname = '',
in_table_type sys.varchar(100) = '',
in_fusepattern sys.bit = '1')
RETURNS TABLE (
out_table_qualifier sys.sysname,
out_table_owner sys.sysname,
out_table_name sys.sysname,
out_table_type sys.varchar(32),
out_remarks sys.varchar(254)
)
AS $$
DECLARE opt_table sys.varchar(16) = '';
DECLARE opt_view sys.varchar(16) = '';
DECLARE cs_as_in_table_type varchar COLLATE "C" = in_table_type;
BEGIN
IF upper(cs_as_in_table_type) LIKE '%''TABLE''%' THEN
opt_table = 'TABLE';
END IF;
IF upper(cs_as_in_table_type) LIKE '%''VIEW''%' THEN
opt_view = 'VIEW';
END IF;
IF in_fusepattern = 1 THEN
RETURN query
SELECT
CAST(table_qualifier AS sys.sysname) AS TABLE_QUALIFIER,
CAST(table_owner AS sys.sysname) AS TABLE_OWNER,
CAST(table_name AS sys.sysname) AS TABLE_NAME,
CAST(table_type AS sys.varchar(32)) AS TABLE_TYPE,
CAST(remarks AS sys.varchar(254)) AS REMARKS
FROM sys.sp_tables_view
WHERE ((SELECT coalesce(in_table_name,'')) = '' OR table_name LIKE in_table_name collate sys.database_default)
AND ((SELECT coalesce(in_table_owner,'')) = '' OR table_owner LIKE in_table_owner collate sys.database_default)
AND ((SELECT coalesce(in_table_qualifier,'')) = '' OR table_qualifier LIKE in_table_qualifier collate sys.database_default)
AND ((SELECT coalesce(cs_as_in_table_type,'')) = ''
OR table_type = opt_table
OR table_type = opt_view)
ORDER BY table_qualifier, table_owner, table_name;
ELSE
RETURN query
SELECT
CAST(table_qualifier AS sys.sysname) AS TABLE_QUALIFIER,
CAST(table_owner AS sys.sysname) AS TABLE_OWNER,
CAST(table_name AS sys.sysname) AS TABLE_NAME,
CAST(table_type AS sys.varchar(32)) AS TABLE_TYPE,
CAST(remarks AS sys.varchar(254)) AS REMARKS
FROM sys.sp_tables_view
WHERE ((SELECT coalesce(in_table_name,'')) = '' OR table_name = in_table_name collate sys.database_default)
AND ((SELECT coalesce(in_table_owner,'')) = '' OR table_owner = in_table_owner collate sys.database_default)
AND ((SELECT coalesce(in_table_qualifier,'')) = '' OR table_qualifier = in_table_qualifier collate sys.database_default)
AND ((SELECT coalesce(cs_as_in_table_type,'')) = ''
OR table_type = opt_table
OR table_type = opt_view)
ORDER BY table_qualifier, table_owner, table_name;
END IF;
END;
$$
LANGUAGE plpgsql STABLE;
75 changes: 75 additions & 0 deletions test/JDBC/expected/BABEL-SP_TABLES.out
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,81 @@ db1#!#dbo#!#t_sptables#!#TABLE#!#<NULL>
~~END~~


-- table_type list
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE'',''VIEW'''
go
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
db1#!#dbo#!#t_sptables#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables2#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables5#!#VIEW#!#<NULL>
~~END~~

-- table_type list with unsupported type
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE'',''VIEW'',''SYSTEM TABLE'''
go
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
db1#!#dbo#!#t_sptables#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables2#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables5#!#VIEW#!#<NULL>
~~END~~

-- table_type list without tables
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''VIEW'',''SYSTEM TABLE'''
go
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
db1#!#dbo#!#t_sptables5#!#VIEW#!#<NULL>
~~END~~

-- table_type list without views
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE'',''SYSTEM TABLE'''
go
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
db1#!#dbo#!#t_sptables#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables2#!#TABLE#!#<NULL>
~~END~~

-- table_type list with double-escaping
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''''''TABLE'''',''''VIEW'''',''''SYSTEM TABLE'''''''
go
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
db1#!#dbo#!#t_sptables#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables2#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables5#!#VIEW#!#<NULL>
~~END~~

-- table_type list with unbalanced quotes
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE'''''',''''VIEW'',''SYSTEM TABLE'''
go
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
db1#!#dbo#!#t_sptables#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables2#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables5#!#VIEW#!#<NULL>
~~END~~

-- table_type list with spaces
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE '','' VIEW'',''SYSTEM TABLE'''
go
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
~~END~~

-- table_type list with mixed case
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''Table'',''View'',''System Table'''
go
~~START~~
varchar#!#varchar#!#varchar#!#varchar#!#varchar
db1#!#dbo#!#t_sptables#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables2#!#TABLE#!#<NULL>
db1#!#dbo#!#t_sptables5#!#VIEW#!#<NULL>
~~END~~


drop view t_sptables5
go
drop table t_sptables
Expand Down
25 changes: 25 additions & 0 deletions test/JDBC/input/BABEL-SP_TABLES.sql
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,31 @@ go
exec [sys].sp_tables N't_sptables',N'dbo',NULL,N'''TABLE''',@fUsePattern=1;
go

-- table_type list
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE'',''VIEW'''
go
-- table_type list with unsupported type
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE'',''VIEW'',''SYSTEM TABLE'''
go
-- table_type list without tables
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''VIEW'',''SYSTEM TABLE'''
go
-- table_type list without views
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE'',''SYSTEM TABLE'''
go
-- table_type list with double-escaping
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''''''TABLE'''',''''VIEW'''',''''SYSTEM TABLE'''''''
go
-- table_type list with unbalanced quotes
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE'''''',''''VIEW'',''SYSTEM TABLE'''
go
-- table_type list with spaces
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''TABLE '','' VIEW'',''SYSTEM TABLE'''
go
-- table_type list with mixed case
exec [sys].sp_tables 't_sptable%','dbo',NULL,'''Table'',''View'',''System Table'''
go

drop view t_sptables5
go
drop table t_sptables
Expand Down

0 comments on commit 2ade013

Please sign in to comment.