Skip to content

Commit

Permalink
revert changes of sp-proc-columns-views
Browse files Browse the repository at this point in the history
Signed-off-by: Sandeep Kumawat <[email protected]>
  • Loading branch information
Sandeep Kumawat committed Dec 28, 2023
1 parent 5d1ee5e commit 8c7b113
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 375 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 @@ -2671,7 +2671,7 @@ CASE
WHEN st.is_table_type = 1 THEN 0
ELSE st.precision
END
AS sys.tinyint) AS PRECISION
AS sys.int) AS PRECISION
, CAST(
CASE
WHEN ss.n IS NULL THEN
Expand All @@ -2695,7 +2695,7 @@ CASE
WHEN st.is_table_type = 1 THEN NULL
ELSE st.scale
END
AS sys.tinyint) AS SCALE
AS smallint) AS SCALE
, CAST(
CASE
WHEN ss.n IS NULL THEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -900,344 +900,6 @@ AND X.indislive ;

GRANT SELECT ON sys.sp_special_columns_view TO PUBLIC;

ALTER VIEW sys.sp_sproc_columns_view RENAME TO sp_sproc_columns_view_deprecated_3_4;

CREATE OR REPLACE VIEW sys.sp_sproc_columns_view
AS
SELECT
CAST(sys.db_name() AS sys.sysname) AS PROCEDURE_QUALIFIER -- This will always be objects in current database
, CAST(ss.schema_name AS sys.sysname) AS PROCEDURE_OWNER
, CAST(
CASE
WHEN ss.prokind = 'p' THEN CONCAT(ss.proname, ';1')
ELSE CONCAT(ss.proname, ';0')
END
AS sys.nvarchar(134)) AS PROCEDURE_NAME
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN '@TABLE_RETURN_VALUE'
ELSE '@RETURN_VALUE'
END
ELSE COALESCE(ss.proargnames[n], '')
END
AS sys.SYSNAME) AS COLUMN_NAME
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN 3
ELSE 5
END
WHEN ss.proargmodes[n] in ('o', 'b') THEN 2
ELSE 1
END
AS smallint) AS COLUMN_TYPE
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.prokind = 'p' THEN (SELECT data_type FROM sys.spt_datatype_info_table WHERE type_name = 'int')
WHEN ss.proretset THEN NULL
ELSE sdit.data_type
END
WHEN st.is_table_type = 1 THEN -153
ELSE sdit.data_type
END
AS smallint) AS DATA_TYPE
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN 'table'
WHEN ss.prokind = 'p' THEN 'int'
ELSE st.name
END
ELSE st.name
END
AS sys.sysname) AS TYPE_NAME
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN 0
WHEN ss.prokind = 'p' THEN (SELECT precision FROM sys.types WHERE name = 'int')
ELSE st.precision
END
WHEN st.is_table_type = 1 THEN 0
ELSE st.precision
END
AS sys.tinyint) AS PRECISION
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN 0
WHEN ss.prokind = 'p' THEN (SELECT max_length FROM sys.types WHERE name = 'int')
ELSE st.max_length
END
WHEN st.is_table_type = 1 THEN 2147483647
ELSE st.max_length
END
AS sys.int) AS LENGTH
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN 0
WHEN ss.prokind = 'p' THEN (SELECT scale FROM sys.types WHERE name = 'int')
ELSE st.scale
END
WHEN st.is_table_type = 1 THEN NULL
ELSE st.scale
END
AS sys.tinyint) AS SCALE
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN 0
WHEN ss.prokind = 'p' THEN (SELECT num_prec_radix FROM sys.spt_datatype_info_table WHERE type_name = 'int')
ELSE sdit.num_prec_radix
END
WHEN st.is_table_type = 1 THEN NULL
ELSE sdit.num_prec_radix
END
AS smallint) AS RADIX
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset OR ss.prokind = 'p' THEN 0
ELSE sdit.nullable
END
WHEN st.is_table_type = 1 THEN 1
ELSE sdit.nullable
END
AS smallint) AS NULLABLE
, CAST(
CASE
WHEN ss.n IS NULL AND ss.proretset THEN 'Result table returned by table valued function'
ELSE NULL
END
AS sys.varchar(254)) COLLATE sys.database_default AS REMARKS
, CAST(NULL AS sys.nvarchar(4000)) AS COLUMN_DEF
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN NULL
WHEN ss.prokind = 'p' THEN (SELECT sql_data_type FROM sys.spt_datatype_info_table WHERE type_name = 'int')
ELSE sdit.sql_data_type
END
WHEN st.is_table_type = 1 THEN -153
ELSE sdit.sql_data_type
END
AS smallint) AS SQL_DATA_TYPE
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN 0
WHEN ss.prokind = 'p' THEN (SELECT sql_datetime_sub FROM sys.spt_datatype_info_table WHERE type_name = 'int')
ELSE sdit.sql_datetime_sub
END
ELSE sdit.sql_datetime_sub
END
AS smallint) AS SQL_DATETIME_SUB
, CAST(
CASE
WHEN ss.n IS NOT NULL AND st.is_table_type = 1 THEN 2147483647
ELSE NULL
END
AS sys.int) AS CHAR_OCTET_LENGTH
, CAST(
CASE
WHEN ss.n IS NULL THEN 0
ELSE n
END
AS sys.int) AS ORDINAL_POSITION
, CAST(
CASE
WHEN ss.n IS NULL AND ss.proretset THEN 'NO'
WHEN st.is_table_type = 1 THEN 'YES'
WHEN sdit.nullable = 1 THEN 'YES'
ELSE 'NO'
END
AS sys.varchar(254)) COLLATE sys.database_default AS IS_NULLABLE
, CAST(
CASE
WHEN ss.n IS NULL THEN
CASE
WHEN ss.proretset THEN 0
WHEN ss.prokind = 'p' THEN 56
ELSE sdit.ss_data_type
END
WHEN st.is_table_type = 1 THEN 0
ELSE sdit.ss_data_type
END
AS sys.tinyint) AS SS_DATA_TYPE
, CAST(ss.proname AS sys.sysname) AS original_procedure_name
FROM
(
-- CTE to query procedures related to bbf
WITH bbf_proc AS (
SELECT
p.proname as proname,
p.proargnames as proargnames,
p.proargmodes as proargmodes,
p.prokind as prokind,
p.proretset as proretset,
p.prorettype as prorettype,
p.proallargtypes as proallargtypes,
p.proargtypes as proargtypes,
s.name as schema_name
FROM
pg_proc p
INNER JOIN (
SELECT name as name, schema_id as id FROM sys.schemas
UNION ALL
SELECT CAST(nspname as sys.sysname) as name, CAST(oid as int) as id
from pg_namespace WHERE nspname in ('sys', 'information_schema')
) as s ON p.pronamespace = s.id
WHERE (
(pg_has_role(p.proowner, 'USAGE') OR has_function_privilege(p.oid, 'EXECUTE'))
AND (s.name != 'sys'
OR p.proname like 'sp\_%' -- filter out internal babelfish-specific procs in sys schema
OR p.proname like 'xp\_%'
OR p.proname like 'dm\_%'
OR p.proname like 'fn\_%'))
)

SELECT *
FROM (
SELECT -- Selects all parameters (input and output), but NOT return values
p.proname as proname,
p.proargnames as proargnames,
p.proargmodes as proargmodes,
p.prokind as prokind,
p.proretset as proretset,
p.prorettype as prorettype,
p.schema_name as schema_name,
(information_schema._pg_expandarray(
COALESCE(p.proallargtypes,
CASE
WHEN p.prokind = 'f' THEN (CAST(p.proargtypes AS oid[]))
ELSE CAST(p.proargtypes AS oid[])
END
))).x AS x,
(information_schema._pg_expandarray(
COALESCE(p.proallargtypes,
CASE
WHEN p.prokind = 'f' THEN (CAST(p.proargtypes AS oid[]))
ELSE CAST(p.proargtypes AS oid[])
END
))).n AS n
FROM bbf_proc p) AS t
WHERE (t.proargmodes[t.n] in ('i', 'o', 'b') OR t.proargmodes is NULL)

UNION ALL

SELECT -- Selects all return values (this is because inline-table functions could cause duplicate outputs)
p.proname as proname,
p.proargnames as proargnames,
p.proargmodes as proargmodes,
p.prokind as prokind,
p.proretset as proretset,
p.prorettype as prorettype,
p.schema_name as schema_name,
p.prorettype AS x,
NULL AS n -- null value indicates that we are retrieving the return values of the proc/func
FROM bbf_proc p
) ss
LEFT JOIN sys.types st ON ss.x = st.user_type_id -- left joined because return type of table-valued functions may not have an entry in sys.types
-- Because spt_datatype_info_table does contain user-defind types and their names,
-- the join below allows us to retrieve the name of the base type of the user-defined type
LEFT JOIN sys.spt_datatype_info_table sdit ON sdit.type_name = sys.translate_pg_type_to_tsql(st.system_type_id);
GRANT SELECT ON sys.sp_sproc_columns_view TO PUBLIC;

CREATE OR REPLACE PROCEDURE sys.sp_sproc_columns(
"@procedure_name" sys.nvarchar(390) = '%',
"@procedure_owner" sys.nvarchar(384) = NULL,
"@procedure_qualifier" sys.sysname = NULL,
"@column_name" sys.nvarchar(384) = NULL,
"@odbcver" int = 2,
"@fusepattern" sys.bit = '1'
)
AS $$
SELECT @procedure_name = LOWER(COALESCE(@procedure_name, ''))
SELECT @procedure_owner = LOWER(COALESCE(@procedure_owner, ''))
SELECT @procedure_qualifier = LOWER(COALESCE(@procedure_qualifier, ''))
SELECT @column_name = LOWER(COALESCE(@column_name, ''))
BEGIN
IF (@procedure_qualifier != '' AND (SELECT LOWER(sys.db_name())) != @procedure_qualifier)
BEGIN
THROW 33557097, N'The database name component of the object qualifier must be the name of the current database.', 1;
END
IF @fusepattern = '1'
BEGIN
SELECT PROCEDURE_QUALIFIER,
PROCEDURE_OWNER,
PROCEDURE_NAME,
COLUMN_NAME,
COLUMN_TYPE,
DATA_TYPE,
TYPE_NAME,
PRECISION,
LENGTH,
SCALE,
RADIX,
NULLABLE,
REMARKS,
COLUMN_DEF,
SQL_DATA_TYPE,
SQL_DATETIME_SUB,
CHAR_OCTET_LENGTH,
ORDINAL_POSITION,
IS_NULLABLE,
SS_DATA_TYPE
FROM sys.sp_sproc_columns_view
WHERE (@procedure_name = '' OR original_procedure_name LIKE @procedure_name)
AND (@procedure_owner = '' OR procedure_owner LIKE @procedure_owner)
AND (@column_name = '' OR column_name LIKE @column_name)
AND (@procedure_qualifier = '' OR procedure_qualifier = @procedure_qualifier)
ORDER BY procedure_qualifier, procedure_owner, procedure_name, ordinal_position;
END
ELSE
BEGIN
SELECT PROCEDURE_QUALIFIER,
PROCEDURE_OWNER,
PROCEDURE_NAME,
COLUMN_NAME,
COLUMN_TYPE,
DATA_TYPE,
TYPE_NAME,
PRECISION,
LENGTH,
SCALE,
RADIX,
NULLABLE,
REMARKS,
COLUMN_DEF,
SQL_DATA_TYPE,
SQL_DATETIME_SUB,
CHAR_OCTET_LENGTH,
ORDINAL_POSITION,
IS_NULLABLE,
SS_DATA_TYPE
FROM sys.sp_sproc_columns_view
WHERE (@procedure_name = '' OR original_procedure_name = @procedure_name)
AND (@procedure_owner = '' OR procedure_owner = @procedure_owner)
AND (@column_name = '' OR column_name = @column_name)
AND (@procedure_qualifier = '' OR procedure_qualifier = @procedure_qualifier)
ORDER BY procedure_qualifier, procedure_owner, procedure_name, ordinal_position;
END
END;
$$
LANGUAGE 'pltsql';
GRANT ALL ON PROCEDURE sys.sp_sproc_columns TO PUBLIC;

CREATE OR REPLACE VIEW sys.assembly_types
AS
SELECT
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
exec babel_sp_sproc_columns_dep_vu_prepare_p2
go
~~START~~
varchar#!#varchar#!#nvarchar#!#varchar#!#smallint#!#smallint#!#varchar#!#tinyint#!#int#!#tinyint#!#smallint#!#smallint#!#varchar#!#nvarchar#!#smallint#!#smallint#!#int#!#int#!#varchar#!#tinyint
varchar#!#varchar#!#nvarchar#!#varchar#!#smallint#!#smallint#!#varchar#!#int#!#int#!#smallint#!#smallint#!#smallint#!#varchar#!#nvarchar#!#smallint#!#smallint#!#int#!#int#!#varchar#!#tinyint
master#!#dbo#!#babel_sp_sproc_columns_dep_vu_prepare_p1;1#!#@RETURN_VALUE#!#5#!#4#!#int#!#10#!#4#!#0#!#10#!#0#!#<NULL>#!#<NULL>#!#4#!#<NULL>#!#<NULL>#!#0#!#NO#!#56
~~END~~

Loading

0 comments on commit 8c7b113

Please sign in to comment.