diff --git a/contrib/babelfishpg_tsql/sql/import_export_compatibility.sql b/contrib/babelfishpg_tsql/sql/import_export_compatibility.sql index 6e12de8723d..08f8930cce2 100644 --- a/contrib/babelfishpg_tsql/sql/import_export_compatibility.sql +++ b/contrib/babelfishpg_tsql/sql/import_export_compatibility.sql @@ -17,7 +17,7 @@ GRANT SELECT ON sys.assemblies TO PUBLIC; CREATE OR REPLACE VIEW sys.assembly_types AS SELECT - CAST(t.name as sys.sysname) AS name, + t.name AS name, -- 'system_type_id' is specified as type INT here, and not TINYINT per SQL Server documentation. -- This is because the IDs of generated SQL Server system type values generated by B -- Babelfish installation will exceed the size of TINYINT. @@ -28,7 +28,7 @@ SELECT CAST(t.max_length as smallint) AS max_length, CAST(t.precision as sys.tinyint) AS precision, CAST(t.scale as sys.tinyint) AS scale, - CAST(t.collation_name as sys.sysname) AS collation_name, + t.collation_name AS collation_name, CAST(t.is_nullable as sys.bit) AS is_nullable, CAST(t.is_user_defined as sys.bit) AS is_user_defined, CAST(t.is_assembly_type as sys.bit) AS is_assembly_type, diff --git a/contrib/babelfishpg_tsql/sql/sys_views.sql b/contrib/babelfishpg_tsql/sql/sys_views.sql index f7634c7145f..4e96c497049 100644 --- a/contrib/babelfishpg_tsql/sql/sys_views.sql +++ b/contrib/babelfishpg_tsql/sql/sys_views.sql @@ -18,7 +18,7 @@ select , CAST(NULL as int) as principal_id , CAST(t.relnamespace as int) as schema_id , 0 as parent_object_id - , CAST('U' as CHAR(2)) as type + , CAST('U' as sys.bpchar(2)) as type , CAST('USER_TABLE' as sys.nvarchar(60)) as type_desc , CAST((select string_agg( case @@ -106,21 +106,21 @@ GRANT SELECT ON sys.shipped_objects_not_in_sys TO PUBLIC; create or replace view sys.views as select - t.relname as name - , t.oid as object_id + CAST(t.relname as sys.sysname) as name + , t.oid::int as object_id , null::integer as principal_id - , sch.schema_id as schema_id + , sch.schema_id::int as schema_id , 0 as parent_object_id - , 'V'::varchar(2) as type - , 'VIEW'::varchar(60) as type_desc - , vd.create_date::timestamp as create_date - , vd.create_date::timestamp as modify_date - , 0 as is_ms_shipped - , 0 as is_published - , 0 as is_schema_published - , 0 as with_check_option - , 0 as is_date_correlation_view - , 0 as is_tracked_by_cdc + , 'V'::sys.bpchar(2) as type + , 'VIEW'::sys.nvarchar(60) as type_desc + , vd.create_date::sys.datetime as create_date + , vd.create_date::sys.datetime as modify_date + , CAST(0 as sys.BIT) as is_ms_shipped + , CAST(0 as sys.BIT) as is_published + , CAST(0 as sys.BIT) as is_schema_published + , CAST(0 as sys.BIT) as with_check_option + , CAST(0 as sys.BIT) as is_date_correlation_view + , CAST(0 as sys.BIT) as is_tracked_by_cdc from pg_class t inner join sys.schemas sch on (t.relnamespace = sch.schema_id) left join sys.shipped_objects_not_in_sys nis on (nis.name = t.relname and nis.schemaid = sch.schema_id and nis.type = 'V') left outer join sys.babelfish_view_def vd on t.relname::sys.sysname = vd.object_name and sch.name = vd.schema_name and vd.dbid = sys.db_id() @@ -709,7 +709,7 @@ SELECT , CAST(NULL AS INT) AS principal_id , CAST(sch.schema_id AS INT) AS schema_id , CAST(c.conrelid AS INT) AS parent_object_id -, CAST('F' AS CHAR(2)) AS type +, CAST('F' AS sys.bpchar(2)) AS type , CAST('FOREIGN_KEY_CONSTRAINT' AS NVARCHAR(60)) AS type_desc , CAST(NULL AS sys.DATETIME) AS create_date , CAST(NULL AS sys.DATETIME) AS modify_date @@ -919,10 +919,10 @@ SELECT , CAST(c.conrelid AS INT) AS parent_object_id , CAST( (CASE contype - WHEN 'p' THEN 'PK' - WHEN 'u' THEN 'UQ' + WHEN 'p' THEN CAST('PK' as sys.bpchar(2)) + WHEN 'u' THEN CAST('UQ' as sys.bpchar(2)) END) - AS CHAR(2)) AS type + AS sys.bpchar(2)) AS type , CAST( (CASE contype WHEN 'p' THEN 'PRIMARY_KEY_CONSTRAINT' @@ -987,10 +987,9 @@ GRANT SELECT ON sys.procedures TO PUBLIC; create or replace view sys.sysforeignkeys as select - c.conname as name - , c.oid as object_id - , c.conrelid as fkeyid - , c.confrelid as rkeyid + CAST(c.oid as int) as constid + , CAST(c.conrelid as int) as fkeyid + , CAST(c.confrelid as int) as rkeyid , a_con.attnum as fkey , a_conf.attnum as rkey , a_conf.attnum as keyno @@ -1039,37 +1038,37 @@ GRANT SELECT ON sys.sysindexes TO PUBLIC; create or replace view sys.sysprocesses as select a.pid as spid - , null::integer as kpid + , null::smallint as kpid , coalesce(blocking_activity.pid, 0) as blocked - , null::bytea as waittype - , 0 as waittime - , a.wait_event_type as lastwaittype - , null::text as waitresource - , coalesce(t.database_id, 0)::oid as dbid + , null::sys.binary(2) as waittype + , 0::bigint as waittime + , CAST(a.wait_event_type as sys.nchar(32)) as lastwaittype + , null::sys.nchar(256) as waitresource + , coalesce(t.database_id, 0)::int as dbid , a.usesysid as uid - , 0 as cpu - , 0 as physical_io - , 0 as memusage - , a.backend_start as login_time - , a.query_start as last_batch - , 0 as ecid - , 0 as open_tran - , a.state as status - , null::bytea as sid + , 0::int as cpu + , 0::bigint as physical_io + , 0::int as memusage + , cast(a.backend_start as sys.datetime) as login_time + , cast(a.query_start as sys.datetime) as last_batch + , 0::smallint as ecid + , 0::smallint as open_tran + , CAST(a.state as sys.nchar(30)) as status + , null::sys.binary(86) as sid , CAST(t.host_name AS sys.nchar(128)) as hostname - , a.application_name as program_name - , t.client_pid::varchar(10) as hostprocess - , a.query as cmd - , null::varchar(128) as nt_domain - , null::varchar(128) as nt_username - , null::varchar(12) as net_address - , null::varchar(12) as net_library - , a.usename as loginname - , t.context_info::bytea as context_info - , null::bytea as sql_handle - , 0 as stmt_start - , 0 as stmt_end - , 0 as request_id + , CAST(a.application_name as sys.nchar(128)) as program_name + , t.client_pid::sys.nchar(10) as hostprocess + , CAST(a.query as sys.nchar(52)) as cmd + , null::sys.nchar(128) as nt_domain + , null::sys.nchar(128) as nt_username + , null::sys.nchar(12) as net_address + , null::sys.nchar(12) as net_library + , CAST(a.usename as sys.nchar(128)) as loginname + , t.context_info as context_info + , null::sys.binary(20) as sql_handle + , 0::int as stmt_start + , 0::int as stmt_end + , 0::int as request_id from pg_stat_activity a left join sys.tsql_stat_get_activity('sessions') as t on a.pid = t.procid left join pg_catalog.pg_locks as blocked_locks on a.pid = blocked_locks.pid @@ -1091,65 +1090,65 @@ GRANT SELECT ON sys.sysprocesses TO PUBLIC; create or replace view sys.types As -- For System types -select - tsql_type_name as name - , t.oid as system_type_id - , t.oid as user_type_id - , s.oid as schema_id +select + CAST(tsql_type_name as sys.sysname) as name + , cast(t.oid as int) as system_type_id + , cast(t.oid as int) as user_type_id + , cast(s.oid as int) as schema_id , cast(NULL as INT) as principal_id , sys.tsql_type_max_length_helper(tsql_type_name, t.typlen, t.typtypmod, true) as max_length - , cast(sys.tsql_type_precision_helper(tsql_type_name, t.typtypmod) as int) as precision - , cast(sys.tsql_type_scale_helper(tsql_type_name, t.typtypmod, false) as int) as scale + , sys.tsql_type_precision_helper(tsql_type_name, t.typtypmod) as precision + , sys.tsql_type_scale_helper(tsql_type_name, t.typtypmod, false) as scale , CASE c.collname WHEN 'default' THEN default_collation_name - ELSE c.collname + ELSE CAST(c.collname as sys.sysname) END as collation_name , case when typnotnull then cast(0 as sys.bit) else cast(1 as sys.bit) end as is_nullable - , 0 as is_user_defined - , 0 as is_assembly_type - , 0 as default_object_id - , 0 as rule_object_id - , 0 as is_table_type + , CAST(0 as sys.bit) as is_user_defined + , CAST(0 as sys.bit) as is_assembly_type + , CAST(0 as int) as default_object_id + , CAST(0 as int) as rule_object_id + , CAST(0 as sys.bit) as is_table_type from pg_type t inner join pg_namespace s on s.oid = t.typnamespace left join pg_collation c on c.oid = t.typcollation , sys.translate_pg_type_to_tsql(t.oid) AS tsql_type_name -,cast(current_setting('babelfishpg_tsql.server_collation_name') as name) as default_collation_name +,cast(current_setting('babelfishpg_tsql.server_collation_name') as sys.sysname) as default_collation_name where -tsql_type_name IS NOT NULL +tsql_type_name IS NOT NULL and pg_type_is_visible(t.oid) and (s.nspname = 'pg_catalog' OR s.nspname = 'sys') union all -- For User Defined Types -select cast(t.typname as text) as name - , t.typbasetype as system_type_id - , t.oid as user_type_id - , t.typnamespace as schema_id +select cast(t.typname as sys.sysname) as name + , cast(t.typbasetype as int) as system_type_id + , cast(t.oid as int) as user_type_id + , cast(t.typnamespace as int) as schema_id , null::integer as principal_id , case when tt.typrelid is not null then -1::smallint else sys.tsql_type_max_length_helper(tsql_base_type_name, t.typlen, t.typtypmod) end as max_length - , case when tt.typrelid is not null then 0::smallint else cast(sys.tsql_type_precision_helper(tsql_base_type_name, t.typtypmod) as int) end as precision - , case when tt.typrelid is not null then 0::smallint else cast(sys.tsql_type_scale_helper(tsql_base_type_name, t.typtypmod, false) as int) end as scale + , case when tt.typrelid is not null then 0::sys.tinyint else sys.tsql_type_precision_helper(tsql_base_type_name, t.typtypmod) end as precision + , case when tt.typrelid is not null then 0::sys.tinyint else sys.tsql_type_scale_helper(tsql_base_type_name, t.typtypmod, false) end as scale , CASE c.collname WHEN 'default' THEN default_collation_name - ELSE c.collname + ELSE CAST(c.collname as sys.sysname) END as collation_name , case when tt.typrelid is not null then cast(0 as sys.bit) else case when typnotnull then cast(0 as sys.bit) else cast(1 as sys.bit) end end as is_nullable -- CREATE TYPE ... FROM is implemented as CREATE DOMAIN in babel - , 1 as is_user_defined - , 0 as is_assembly_type - , 0 as default_object_id - , 0 as rule_object_id - , case when tt.typrelid is not null then 1 else 0 end as is_table_type + , CAST(1 as sys.bit) as is_user_defined + , CAST(0 as sys.bit) as is_assembly_type + , CAST(0 as int) as default_object_id + , CAST(0 as int) as rule_object_id + , case when tt.typrelid is not null then CAST(1 as sys.bit) else CAST(0 as sys.bit) end as is_table_type from pg_type t join sys.schemas sch on t.typnamespace = sch.schema_id left join pg_collation c on c.oid = t.typcollation left join sys.table_types_internal tt on t.typrelid = tt.typrelid , sys.translate_pg_type_to_tsql(t.oid) AS tsql_type_name , sys.translate_pg_type_to_tsql(t.typbasetype) AS tsql_base_type_name -, cast(current_setting('babelfishpg_tsql.server_collation_name') as name) as default_collation_name +, cast(current_setting('babelfishpg_tsql.server_collation_name') as sys.sysname) as default_collation_name -- we want to show details of user defined datatypes created under babelfish database where tsql_type_name IS NULL @@ -1182,7 +1181,7 @@ END; $$ LANGUAGE plpgsql IMMUTABLE STRICT; CREATE OR REPLACE VIEW sys.systypes AS -SELECT CAST(name as sys.sysname) as name +SELECT name , CAST(system_type_id as int) as xtype , CAST((case when is_nullable = 1 then 0 else 1 end) as sys.tinyint) as status , CAST((case when user_type_id < 32767 then user_type_id::int else null end) as smallint) as xusertype @@ -1201,10 +1200,10 @@ SELECT CAST(name as sys.sysname) as name , CAST(is_nullable as sys.bit) as allownulls , CAST(system_type_id as int) as type , CAST(null as sys.varchar(255)) as printfmt - , (case when precision <> 0::smallint then precision + , (case when precision <> 0::sys.tinyint then precision::smallint else sys.systypes_precision_helper(sys.translate_pg_type_to_tsql(system_type_id), max_length) end) as prec , CAST(scale as sys.tinyint) as scale - , CAST(collation_name as sys.sysname) as collation + , collation_name as collation FROM sys.types; GRANT SELECT ON sys.systypes TO PUBLIC; @@ -1224,7 +1223,7 @@ select CAST(('DF_' || tab.name || '_' || d.oid) as sys.sysname) as name , CAST(null as int) as principal_id , CAST(tab.schema_id as int) as schema_id , CAST(d.adrelid as int) as parent_object_id - , CAST('D' as char(2)) as type + , CAST('D' as sys.bpchar(2)) as type , CAST('DEFAULT_CONSTRAINT' as sys.nvarchar(60)) AS type_desc , CAST(null as sys.datetime) as create_date , CAST(null as sys.datetime) as modified_date @@ -1248,7 +1247,7 @@ SELECT CAST(c.conname as sys.sysname) as name , CAST(NULL as integer) as principal_id , CAST(c.connamespace as integer) as schema_id , CAST(conrelid as integer) as parent_object_id - , CAST('C' as char(2)) as type + , CAST('C' as sys.bpchar(2)) as type , CAST('CHECK_CONSTRAINT' as sys.nvarchar(60)) as type_desc , CAST(null as sys.datetime) as create_date , CAST(null as sys.datetime) as modify_date @@ -1675,7 +1674,11 @@ left join sys.shipped_objects_not_in_sys nis on nis.name = ('TT_' || tt.name || GRANT SELECT ON sys.all_objects TO PUBLIC; create or replace view sys.system_objects as -select * from sys.all_objects o +select + name, object_id, principal_id, schema_id, + parent_object_id, type, type_desc, create_date, + modify_date, is_ms_shipped, is_published, is_schema_published + from sys.all_objects o inner join pg_namespace s on s.oid = o.schema_id where s.nspname = 'sys'; GRANT SELECT ON sys.system_objects TO PUBLIC; @@ -2204,7 +2207,7 @@ SELECT out_name as name case out_is_identity::int when 1 then 128 else 0 end)::sys.tinyint as status , out_system_type_id as type , (case when out_user_type_id < 32767 then out_user_type_id else null end)::smallint as usertype - , null::varchar(255) as printfmt + , null::sys.varchar(255) as printfmt , out_precision::smallint as prec , out_scale::int as scale , out_is_computed::int as iscomputed @@ -2234,7 +2237,7 @@ SELECT p.name , 0::smallint as offset , collationid , (case p.isoutparam when 1 then 64 else 0 end)::sys.tinyint as status - , p.xtype as type + , p.xtype type , (case when p.xtype < 32767 then p.xtype else null end)::smallint as usertype , null::varchar(255) as printfmt , p.prec @@ -2310,7 +2313,7 @@ create or replace view sys.dm_exec_connections , 'TCP'::sys.nvarchar(40) as net_transport , 'TSQL'::sys.nvarchar(40) as protocol_type , d.protocol_version as protocol_version - , 4 as endpoint_id + , CAST(4 as int) as endpoint_id , d.encrypyt_option::sys.nvarchar(40) as encrypt_option , null::sys.nvarchar(40) as auth_scheme , null::smallint as node_affinity @@ -2319,9 +2322,9 @@ create or replace view sys.dm_exec_connections , null::sys.datetime as last_read , null::sys.datetime as last_write , d.packet_size as net_packet_size - , a.client_addr::varchar(48) as client_net_address + , a.client_addr::sys.varchar(48) as client_net_address , a.client_port as client_tcp_port - , null::varchar(48) as local_net_address + , null::sys.varchar(48) as local_net_address , null::int as local_tcp_port , null::sys.uniqueidentifier as connection_id , null::sys.uniqueidentifier as parent_connection_id @@ -2448,7 +2451,7 @@ AS SELECT CAST('PRIMARY' as SYSNAME) AS name, CAST(1 as INT) AS data_space_id, - CAST('FG' as CHAR(2)) AS type, + CAST('FG' as sys.bpchar(2)) AS type, CAST('ROWS_FILEGROUP' as NVARCHAR(60)) AS type_desc, CAST(1 as sys.BIT) AS is_default, CAST(0 as sys.BIT) AS is_system; @@ -2584,7 +2587,7 @@ SELECT , CAST(idx.filter_definition AS sys.nvarchar(4000)) AS filter_definition , CAST(idx.auto_created AS sys.bit) AS auto_created , CAST(NULL AS INT) AS using_xml_index_id - , CAST(NULL AS char(1)) AS secondary_type + , CAST(NULL AS sys.bpchar(1)) AS secondary_type , CAST(NULL AS sys.nvarchar(60)) AS secondary_type_desc , CAST(0 AS sys.tinyint) AS xml_index_type , CAST(NULL AS sys.nvarchar(60)) AS xml_index_type_description @@ -2802,7 +2805,7 @@ SELECT CAST(0 as sys.BIT) AS is_incremental, CAST(0 as sys.BIT) AS has_persisted_sample, CAST(0 as INT) AS stats_generation_method, - CAST('' as VARCHAR(255)) AS stats_generation_method_desc + CAST('' as sys.VARCHAR(255)) AS stats_generation_method_desc WHERE FALSE; GRANT SELECT ON sys.stats TO PUBLIC; diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.4.0--3.5.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.4.0--3.5.0.sql index b5d7c6cf3a8..64d6f703ff4 100644 --- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.4.0--3.5.0.sql +++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--3.4.0--3.5.0.sql @@ -152,6 +152,1876 @@ EXCEPTION WHEN OTHERS THEN END; $$; + +ALTER VIEW sys.sysforeignkeys RENAME TO sysforeignkeys_deprecated_3_5_0; + +create or replace view sys.sysforeignkeys as +select + CAST(c.oid as int) as constid + , CAST(c.conrelid as int) as fkeyid + , CAST(c.confrelid as int) as rkeyid + , a_con.attnum as fkey + , a_conf.attnum as rkey + , a_conf.attnum as keyno +from pg_constraint c +inner join pg_attribute a_con on a_con.attrelid = c.conrelid and a_con.attnum = any(c.conkey) +inner join pg_attribute a_conf on a_conf.attrelid = c.confrelid and a_conf.attnum = any(c.confkey) +where c.contype = 'f' +and (c.connamespace in (select schema_id from sys.schemas)) +and has_schema_privilege(c.connamespace, 'USAGE'); +GRANT SELECT ON sys.sysforeignkeys TO PUBLIC; + +ALTER VIEW sys.system_objects RENAME TO system_objects_deprecated_3_5_0; + +create or replace view sys.system_objects as +select + name, object_id, principal_id, schema_id, + parent_object_id, type, type_desc, create_date, + modify_date, is_ms_shipped, is_published, is_schema_published + from sys.all_objects o +inner join pg_namespace s on s.oid = o.schema_id +where s.nspname = 'sys'; +GRANT SELECT ON sys.system_objects TO PUBLIC; + +ALTER VIEW sys.syscolumns RENAME TO syscolumns_deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.syscolumns AS +SELECT out_name as name + , out_object_id as id + , out_system_type_id as xtype + , 0::sys.tinyint as typestat + , (case when out_user_type_id < 32767 then out_user_type_id else null end)::smallint as xusertype + , out_max_length as length + , 0::sys.tinyint as xprec + , 0::sys.tinyint as xscale + , out_column_id::smallint as colid + , 0::smallint as xoffset + , 0::sys.tinyint as bitpos + , 0::sys.tinyint as reserved + , 0::smallint as colstat + , out_default_object_id::int as cdefault + , out_rule_object_id::int as domain + , 0::smallint as number + , 0::smallint as colorder + , null::sys.varbinary(8000) as autoval + , out_offset as offset + , out_collation_id as collationid + , (case out_is_nullable::int when 1 then 8 else 0 end + + case out_is_identity::int when 1 then 128 else 0 end)::sys.tinyint as status + , out_system_type_id as type + , (case when out_user_type_id < 32767 then out_user_type_id else null end)::smallint as usertype + , null::sys.varchar(255) as printfmt + , out_precision::smallint as prec + , out_scale::int as scale + , out_is_computed::int as iscomputed + , 0::int as isoutparam + , out_is_nullable::int as isnullable + , out_collation_name::sys.sysname as collation +FROM sys.columns_internal() +union all +SELECT p.name + , p.id + , p.xtype + , 0::sys.tinyint as typestat + , (case when p.xtype < 32767 then p.xtype else null end)::smallint as xusertype + , null as length + , 0::sys.tinyint as xprec + , 0::sys.tinyint as xscale + , p.colid + , 0::smallint as xoffset + , 0::sys.tinyint as bitpos + , 0::sys.tinyint as reserved + , 0::smallint as colstat + , null::int as cdefault + , null::int as domain + , 0::smallint as number + , 0::smallint as colorder + , null::sys.varbinary(8000) as autoval + , 0::smallint as offset + , collationid + , (case p.isoutparam when 1 then 64 else 0 end)::sys.tinyint as status + , p.xtype type + , (case when p.xtype < 32767 then p.xtype else null end)::smallint as usertype + , null::varchar(255) as printfmt + , p.prec + , p.scale + , 0::int as iscomputed + , p.isoutparam + , 1::int as isnullable + , p.collation +FROM sys.proc_param_helper() as p; +GRANT SELECT ON sys.syscolumns TO PUBLIC; + +ALTER VIEW sys.dm_exec_connections RENAME TO dm_exec_connections_deprecated_3_5_0; + +create or replace view sys.dm_exec_connections + as + select a.pid as session_id + , a.pid as most_recent_session_id + , a.backend_start::sys.datetime as connect_time + , 'TCP'::sys.nvarchar(40) as net_transport + , 'TSQL'::sys.nvarchar(40) as protocol_type + , d.protocol_version as protocol_version + , CAST(4 as int) as endpoint_id + , d.encrypyt_option::sys.nvarchar(40) as encrypt_option + , null::sys.nvarchar(40) as auth_scheme + , null::smallint as node_affinity + , null::int as num_reads + , null::int as num_writes + , null::sys.datetime as last_read + , null::sys.datetime as last_write + , d.packet_size as net_packet_size + , a.client_addr::sys.varchar(48) as client_net_address + , a.client_port as client_tcp_port + , null::sys.varchar(48) as local_net_address + , null::int as local_tcp_port + , null::sys.uniqueidentifier as connection_id + , null::sys.uniqueidentifier as parent_connection_id + , a.pid::sys.varbinary(64) as most_recent_sql_handle + from pg_catalog.pg_stat_activity AS a + RIGHT JOIN sys.tsql_stat_get_activity('connections') AS d ON (a.pid = d.procid); + GRANT SELECT ON sys.dm_exec_connections TO PUBLIC; + +ALTER VIEW sys.xml_indexes RENAME TO xml_indexes_connections_deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.xml_indexes +AS +SELECT + CAST(idx.object_id AS INT) AS object_id + , CAST(idx.name AS sys.sysname) AS name + , CAST(idx.index_id AS INT) AS index_id + , CAST(idx.type AS sys.tinyint) AS type + , CAST(idx.type_desc AS sys.nvarchar(60)) AS type_desc + , CAST(idx.is_unique AS sys.bit) AS is_unique + , CAST(idx.data_space_id AS int) AS data_space_id + , CAST(idx.ignore_dup_key AS sys.bit) AS ignore_dup_key + , CAST(idx.is_primary_key AS sys.bit) AS is_primary_key + , CAST(idx.is_unique_constraint AS sys.bit) AS is_unique_constraint + , CAST(idx.fill_factor AS sys.tinyint) AS fill_factor + , CAST(idx.is_padded AS sys.bit) AS is_padded + , CAST(idx.is_disabled AS sys.bit) AS is_disabled + , CAST(idx.is_hypothetical AS sys.bit) AS is_hypothetical + , CAST(idx.allow_row_locks AS sys.bit) AS allow_row_locks + , CAST(idx.allow_page_locks AS sys.bit) AS allow_page_locks + , CAST(idx.has_filter AS sys.bit) AS has_filter + , CAST(idx.filter_definition AS sys.nvarchar(4000)) AS filter_definition + , CAST(idx.auto_created AS sys.bit) AS auto_created + , CAST(NULL AS INT) AS using_xml_index_id + , CAST(NULL AS sys.bpchar(1)) AS secondary_type + , CAST(NULL AS sys.nvarchar(60)) AS secondary_type_desc + , CAST(0 AS sys.tinyint) AS xml_index_type + , CAST(NULL AS sys.nvarchar(60)) AS xml_index_type_description + , CAST(NULL AS INT) AS path_id +FROM sys.indexes idx +WHERE idx.type = 3; -- 3 is of type XML +GRANT SELECT ON sys.xml_indexes TO PUBLIC; + +ALTER VIEW sys.stats RENAME TO stats__deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.stats +AS +SELECT + CAST(0 as INT) AS object_id, + CAST('' as SYSNAME) AS name, + CAST(0 as INT) AS stats_id, + CAST(0 as sys.BIT) AS auto_created, + CAST(0 as sys.BIT) AS user_created, + CAST(0 as sys.BIT) AS no_recompute, + CAST(0 as sys.BIT) AS has_filter, + CAST('' as sys.NVARCHAR(4000)) AS filter_definition, + CAST(0 as sys.BIT) AS is_temporary, + CAST(0 as sys.BIT) AS is_incremental, + CAST(0 as sys.BIT) AS has_persisted_sample, + CAST(0 as INT) AS stats_generation_method, + CAST('' as sys.VARCHAR(255)) AS stats_generation_method_desc +WHERE FALSE; +GRANT SELECT ON sys.stats TO PUBLIC; + +ALTER VIEW sys.data_spaces RENAME TO data_spaces_deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.data_spaces +AS +SELECT + CAST('PRIMARY' as SYSNAME) AS name, + CAST(1 as INT) AS data_space_id, + CAST('FG' as sys.bpchar(2)) AS type, + CAST('ROWS_FILEGROUP' as NVARCHAR(60)) AS type_desc, + CAST(1 as sys.BIT) AS is_default, + CAST(0 as sys.BIT) AS is_system; +GRANT SELECT ON sys.data_spaces TO PUBLIC; + +CREATE OR REPLACE VIEW sys.filegroups +AS +SELECT + CAST(ds.name AS sys.SYSNAME), + CAST(ds.data_space_id AS INT), + CAST(ds.type AS sys.BPCHAR(2)) COLLATE sys.database_default, + CAST(ds.type_desc AS sys.NVARCHAR(60)), + CAST(ds.is_default AS sys.BIT), + CAST(ds.is_system AS sys.BIT), + CAST(NULL as sys.UNIQUEIDENTIFIER) AS filegroup_guid, + CAST(0 as INT) AS log_filegroup_id, + CAST(0 as sys.BIT) AS is_read_only, + CAST(0 as sys.BIT) AS is_autogrow_all_files +FROM sys.data_spaces ds WHERE type = 'FG'; +GRANT SELECT ON sys.filegroups TO PUBLIC; + +ALTER VIEW sys.sysprocesses RENAME TO sysprocesses_deprecated_3_5_0; + +create or replace view sys.sysprocesses as +select + a.pid as spid + , null::smallint as kpid + , coalesce(blocking_activity.pid, 0) as blocked + , null::sys.binary(2) as waittype + , 0::bigint as waittime + , CAST(a.wait_event_type as sys.nchar(32)) as lastwaittype + , null::sys.nchar(256) as waitresource + , coalesce(t.database_id, 0)::int as dbid + , a.usesysid as uid + , 0::int as cpu + , 0::bigint as physical_io + , 0::int as memusage + , cast(a.backend_start as sys.datetime) as login_time + , cast(a.query_start as sys.datetime) as last_batch + , 0::smallint as ecid + , 0::smallint as open_tran + , CAST(a.state as sys.nchar(30)) as status + , null::sys.binary(86) as sid + , CAST(t.host_name AS sys.nchar(128)) as hostname + , CAST(a.application_name as sys.nchar(128)) as program_name + , t.client_pid::sys.nchar(10) as hostprocess + , CAST(a.query as sys.nchar(52)) as cmd + , null::sys.nchar(128) as nt_domain + , null::sys.nchar(128) as nt_username + , null::sys.nchar(12) as net_address + , null::sys.nchar(12) as net_library + , CAST(a.usename as sys.nchar(128)) as loginname + , t.context_info as context_info + , null::sys.binary(20) as sql_handle + , 0::int as stmt_start + , 0::int as stmt_end + , 0::int as request_id +from pg_stat_activity a +left join sys.tsql_stat_get_activity('sessions') as t on a.pid = t.procid +left join pg_catalog.pg_locks as blocked_locks on a.pid = blocked_locks.pid +left join pg_catalog.pg_locks blocking_locks + ON blocking_locks.locktype = blocked_locks.locktype + AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE + AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation + AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page + AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple + AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid + AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid + AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid + AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid + AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid + AND blocking_locks.pid != blocked_locks.pid + left join pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid + where a.datname = current_database(); /* current physical database will always be babelfish database */ +GRANT SELECT ON sys.sysprocesses TO PUBLIC; + +CREATE OR REPLACE PROCEDURE sys.sp_who( + IN "@loginame" sys.sysname DEFAULT NULL, + IN "@option" sys.VARCHAR(30) DEFAULT NULL) +LANGUAGE 'pltsql' +AS $$ +BEGIN + SET NOCOUNT ON + DECLARE @msg sys.VARCHAR(200) + DECLARE @show_pg BIT = 0 + DECLARE @hide_col sys.VARCHAR(50) + + IF @option IS NOT NULL + BEGIN + IF LOWER(TRIM(@option)) <> 'postgres' + BEGIN + RAISERROR('Parameter @option can only be ''postgres''', 16, 1) + RETURN + END + END + + -- Take a copy of sysprocesses so that we reference it only once + SELECT DISTINCT * INTO #sp_who_sysprocesses FROM sys.sysprocesses + + -- Get the executing statement for each spid and extract the main stmt type + -- This is for informational purposes only + SELECT pid, query INTO #sp_who_tmp FROM pg_stat_activity pgsa + + UPDATE #sp_who_tmp SET query = ' ' + TRIM(UPPER(query)) + UPDATE #sp_who_tmp SET query = sys.REPLACE(query, chr(9), ' ') + UPDATE #sp_who_tmp SET query = sys.REPLACE(query, chr(10), ' ') + UPDATE #sp_who_tmp SET query = sys.REPLACE(query, chr(13), ' ') + WHILE (SELECT count(*) FROM #sp_who_tmp WHERE sys.CHARINDEX(' ',query)>0) > 0 + BEGIN + UPDATE #sp_who_tmp SET query = sys.REPLACE(query, ' ', ' ') + END + + -- Determine type of stmt to report by sp_who: very basic only + -- NB: not handling presence of comments in the query string + UPDATE #sp_who_tmp + SET query = + CASE + WHEN PATINDEX('%[^a-zA-Z0-9_]UPDATE[^a-zA-Z0-9_]%', query) > 0 THEN 'UPDATE' + WHEN PATINDEX('%[^a-zA-Z0-9_]DELETE[^a-zA-Z0-9_]%', query) > 0 THEN 'DELETE' + WHEN PATINDEX('%[^a-zA-Z0-9_]INSERT[^a-zA-Z0-9_]%', query) > 0 THEN 'INSERT' + WHEN PATINDEX('%[^a-zA-Z0-9_]SELECT[^a-zA-Z0-9_]%', query) > 0 THEN 'SELECT' + WHEN PATINDEX('%[^a-zA-Z0-9_]WAITFOR[^a-zA-Z0-9_]%', query) > 0 THEN 'WAITFOR' + WHEN PATINDEX('%[^a-zA-Z0-9_]CREATE ]%', query) > 0 THEN sys.SUBSTRING(query,1,sys.CHARINDEX('CREATE ', query)) + WHEN PATINDEX('%[^a-zA-Z0-9_]ALTER ]%', query) > 0 THEN sys.SUBSTRING(query,1,sys.CHARINDEX('ALTER ', query)) + WHEN PATINDEX('%[^a-zA-Z0-9_]DROP ]%', query) > 0 THEN sys.SUBSTRING(query,1,sys.CHARINDEX('DROP ', query)) + ELSE sys.SUBSTRING(query, 1, sys.CHARINDEX(' ', query)) + END + + UPDATE #sp_who_tmp + SET query = sys.SUBSTRING(query,1, 8-1 + sys.CHARINDEX(' ', sys.SUBSTRING(query,8,99))) + WHERE query LIKE 'CREATE %' OR query LIKE 'ALTER %' OR query LIKE 'DROP %' + + -- The executing spid is always shown as doing a SELECT + UPDATE #sp_who_tmp SET query = 'SELECT' WHERE pid = @@spid + UPDATE #sp_who_tmp SET query = TRIM(query) + + -- Get all current connections + SELECT + spid, + MAX(blocked) AS blocked, + 0 AS ecid, + CAST('' AS sys.VARCHAR(100)) AS status, + CAST('' AS sys.VARCHAR(100)) AS loginname, + CAST('' AS sys.VARCHAR(100)) AS hostname, + 0 AS dbid, + CAST('' AS sys.VARCHAR(100)) AS cmd, + 0 AS request_id, + CAST('TDS' AS sys.VARCHAR(20)) AS connection, + hostprocess + INTO #sp_who_proc + FROM #sp_who_sysprocesses + GROUP BY spid, status, hostprocess + + -- Add attributes to each connection + UPDATE #sp_who_proc + SET ecid = sp.ecid, + status = sp.status, + loginname = sp.loginname, + hostname = sp.hostname, + dbid = sp.dbid, + request_id = sp.request_id + FROM #sp_who_sysprocesses sp + WHERE #sp_who_proc.spid = sp.spid + + -- Identify PG connections: the hostprocess PID comes from the TDS login packet + -- and therefore PG connections do not have a value here + UPDATE #sp_who_proc + SET connection = 'PostgreSQL' + WHERE hostprocess IS NULL + + -- Keep or delete PG connections + IF (LOWER(@loginame) = 'postgres' OR LOWER(@option) = 'postgres') + begin + -- Show PG connections; these have dbid = 0 + -- This is a Babelfish-specific enhancement, since PG connections may also be active in the Babelfish DB + -- and it may be useful to see these displayed + SET @show_pg = 1 + + -- blank out the loginame parameter for the tests below + IF LOWER(@loginame) = 'postgres' SET @loginame = NULL + END + + -- By default, do not show the column indicating the connection type since SQL Server does not have this column + SET @hide_col = 'connection' + + IF (@show_pg = 1) + BEGIN + SET @hide_col = '' + END + ELSE + BEGIN + -- Delete PG connections + DELETE #sp_who_proc + WHERE dbid = 0 + END + + -- Apply filter if specified + IF (@loginame IS NOT NULL) + BEGIN + IF (TRIM(@loginame) = '') + BEGIN + -- Raise error + SET @msg = ''''+@loginame+''' is not a valid login or you do not have permission.' + RAISERROR(@msg, 16, 1) + RETURN + END + + IF (sys.ISNUMERIC(@loginame) = 1) + BEGIN + -- Remove all connections except the specified one + DELETE #sp_who_proc + WHERE spid <> CAST(@loginame AS INT) + END + ELSE + BEGIN + IF (LOWER(@loginame) = 'active') + BEGIN + -- Remove all 'idle' connections + DELETE #sp_who_proc + WHERE status = 'idle' + END + ELSE + BEGIN + -- Verify the specified login name exists + IF (sys.SUSER_ID(@loginame) IS NULL) + BEGIN + SET @msg = ''''+@loginame+''' is not a valid login or you do not have permission.' + RAISERROR(@msg, 16, 1) + RETURN + END + ELSE + BEGIN + -- Keep only connections for the specified login + DELETE #sp_who_proc + WHERE sys.SUSER_ID(loginname) <> sys.SUSER_ID(@loginame) + END + END + END + END + + -- Create final result set; use DISTINCT since there are usually duplicate rows from the PG catalogs + SELECT distinct + p.spid AS spid, + p.ecid AS ecid, + CAST(LEFT(p.status,20) AS sys.VARCHAR(20)) AS status, + CAST(LEFT(p.loginname,40) AS sys.VARCHAR(40)) AS loginame, + CAST(LEFT(p.hostname,60) AS sys.VARCHAR(60)) AS hostname, + p.blocked AS blk, + CAST(LEFT(db_name(p.dbid),40) AS sys.VARCHAR(40)) AS dbname, + CAST(LEFT(#sp_who_tmp.query,30)as sys.VARCHAR(30)) AS cmd, + p.request_id AS request_id, + connection + INTO #sp_who_tmp2 + FROM #sp_who_proc p, #sp_who_tmp + WHERE p.spid = #sp_who_tmp.pid + ORDER BY spid + + -- Patch up remaining cases + UPDATE #sp_who_tmp2 + SET cmd = 'AWAITING COMMAND' + WHERE TRIM(ISNULL(cmd,'')) = '' AND status = 'idle' + + UPDATE #sp_who_tmp2 + SET cmd = 'UNKNOWN' + WHERE TRIM(cmd) = '' + + -- Format the result set as narrow as possible for readability + SET @hide_col += ',hostprocess' + EXECUTE sys.sp_babelfish_autoformat @tab='#sp_who_tmp2', @orderby='ORDER BY spid', @hiddencols=@hide_col, @printrc=0 + RETURN +END +$$; +GRANT EXECUTE ON PROCEDURE sys.sp_who(IN sys.sysname, IN sys.VARCHAR(30)) TO PUBLIC; + +ALTER VIEW sys.foreign_keys RENAME TO foreign_keys_deprecated_3_5_0; + +CREATE OR replace view sys.foreign_keys AS +SELECT + CAST(c.conname AS sys.SYSNAME) AS name +, CAST(c.oid AS INT) AS object_id +, CAST(NULL AS INT) AS principal_id +, CAST(sch.schema_id AS INT) AS schema_id +, CAST(c.conrelid AS INT) AS parent_object_id +, CAST('F' AS sys.bpchar(2)) AS type +, CAST('FOREIGN_KEY_CONSTRAINT' AS NVARCHAR(60)) AS type_desc +, CAST(NULL AS sys.DATETIME) AS create_date +, CAST(NULL AS sys.DATETIME) AS modify_date +, CAST(0 AS sys.BIT) AS is_ms_shipped +, CAST(0 AS sys.BIT) AS is_published +, CAST(0 AS sys.BIT) as is_schema_published +, CAST(c.confrelid AS INT) AS referenced_object_id +, CAST(c.conindid AS INT) AS key_index_id +, CAST(0 AS sys.BIT) AS is_disabled +, CAST(0 AS sys.BIT) AS is_not_for_replication +, CAST(0 AS sys.BIT) AS is_not_trusted +, CAST( + (CASE c.confdeltype + WHEN 'a' THEN 0 + WHEN 'r' THEN 0 + WHEN 'c' THEN 1 + WHEN 'n' THEN 2 + WHEN 'd' THEN 3 + END) + AS sys.TINYINT) AS delete_referential_action +, CAST( + (CASE c.confdeltype + WHEN 'a' THEN 'NO_ACTION' + WHEN 'r' THEN 'NO_ACTION' + WHEN 'c' THEN 'CASCADE' + WHEN 'n' THEN 'SET_NULL' + WHEN 'd' THEN 'SET_DEFAULT' + END) + AS sys.NVARCHAR(60)) AS delete_referential_action_desc +, CAST( + (CASE c.confupdtype + WHEN 'a' THEN 0 + WHEN 'r' THEN 0 + WHEN 'c' THEN 1 + WHEN 'n' THEN 2 + WHEN 'd' THEN 3 + END) + AS sys.TINYINT) AS update_referential_action +, CAST( + (CASE c.confupdtype + WHEN 'a' THEN 'NO_ACTION' + WHEN 'r' THEN 'NO_ACTION' + WHEN 'c' THEN 'CASCADE' + WHEN 'n' THEN 'SET_NULL' + WHEN 'd' THEN 'SET_DEFAULT' + END) + AS sys.NVARCHAR(60)) update_referential_action_desc +, CAST(1 AS sys.BIT) AS is_system_named +FROM pg_constraint c +INNER JOIN sys.schemas sch ON sch.schema_id = c.connamespace +WHERE has_schema_privilege(sch.schema_id, 'USAGE') +AND c.contype = 'f'; +GRANT SELECT ON sys.foreign_keys TO PUBLIC; + +ALTER VIEW sys.key_constraints RENAME TO key_constraints_deprecated_3_5_0; + +CREATE OR replace view sys.key_constraints AS +SELECT + CAST(c.conname AS SYSNAME) AS name + , CAST(c.oid AS INT) AS object_id + , CAST(0 AS INT) AS principal_id + , CAST(sch.schema_id AS INT) AS schema_id + , CAST(c.conrelid AS INT) AS parent_object_id + , CAST( + (CASE contype + WHEN 'p' THEN CAST('PK' as sys.bpchar(2)) + WHEN 'u' THEN CAST('UQ' as sys.bpchar(2)) + END) + AS sys.bpchar(2)) AS type + , CAST( + (CASE contype + WHEN 'p' THEN 'PRIMARY_KEY_CONSTRAINT' + WHEN 'u' THEN 'UNIQUE_CONSTRAINT' + END) + AS NVARCHAR(60)) AS type_desc + , CAST(NULL AS DATETIME) AS create_date + , CAST(NULL AS DATETIME) AS modify_date + , CAST(c.conindid AS INT) AS unique_index_id + , CAST(0 AS sys.BIT) AS is_ms_shipped + , CAST(0 AS sys.BIT) AS is_published + , CAST(0 AS sys.BIT) AS is_schema_published + , CAST(1 as sys.BIT) as is_system_named +FROM pg_constraint c +INNER JOIN sys.schemas sch ON sch.schema_id = c.connamespace +WHERE has_schema_privilege(sch.schema_id, 'USAGE') +AND c.contype IN ('p', 'u'); +GRANT SELECT ON sys.key_constraints TO PUBLIC; + +ALTER VIEW sys.views RENAME TO views_deprecated_3_5_0; + +create or replace view sys.views as +select + CAST(t.relname as sys.sysname) as name + , t.oid::int as object_id + , null::integer as principal_id + , sch.schema_id::int as schema_id + , 0 as parent_object_id + , 'V'::sys.bpchar(2) as type + , 'VIEW'::sys.nvarchar(60) as type_desc + , vd.create_date::sys.datetime as create_date + , vd.create_date::sys.datetime as modify_date + , CAST(0 as sys.BIT) as is_ms_shipped + , CAST(0 as sys.BIT) as is_published + , CAST(0 as sys.BIT) as is_schema_published + , CAST(0 as sys.BIT) as with_check_option + , CAST(0 as sys.BIT) as is_date_correlation_view + , CAST(0 as sys.BIT) as is_tracked_by_cdc +from pg_class t inner join sys.schemas sch on (t.relnamespace = sch.schema_id) +left join sys.shipped_objects_not_in_sys nis on (nis.name = t.relname and nis.schemaid = sch.schema_id and nis.type = 'V') +left outer join sys.babelfish_view_def vd on t.relname::sys.sysname = vd.object_name and sch.name = vd.schema_name and vd.dbid = sys.db_id() +where t.relkind = 'v' +and nis.name is null +and has_schema_privilege(sch.schema_id, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER'); +GRANT SELECT ON sys.views TO PUBLIC; + +ALTER VIEW sys.tables RENAME TO tables_deprecated_3_5_0; + +create or replace view sys.tables as +select + CAST(t.relname as sys._ci_sysname) as name + , CAST(t.oid as int) as object_id + , CAST(NULL as int) as principal_id + , CAST(t.relnamespace as int) as schema_id + , 0 as parent_object_id + , CAST('U' as sys.bpchar(2)) as type + , CAST('USER_TABLE' as sys.nvarchar(60)) as type_desc + , CAST((select string_agg( + case + when option like 'bbf_rel_create_date=%%' then substring(option, 21) + else NULL + end, ',') + from unnest(t.reloptions) as option) + as sys.datetime) as create_date + , CAST((select string_agg( + case + when option like 'bbf_rel_create_date=%%' then substring(option, 21) + else NULL + end, ',') + from unnest(t.reloptions) as option) + as sys.datetime) as modify_date + , CAST(0 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published + , case reltoastrelid when 0 then 0 else 1 end as lob_data_space_id + , CAST(NULL as int) as filestream_data_space_id + , CAST(relnatts as int) as max_column_id_used + , CAST(0 as sys.bit) as lock_on_bulk_load + , CAST(1 as sys.bit) as uses_ansi_nulls + , CAST(0 as sys.bit) as is_replicated + , CAST(0 as sys.bit) as has_replication_filter + , CAST(0 as sys.bit) as is_merge_published + , CAST(0 as sys.bit) as is_sync_tran_subscribed + , CAST(0 as sys.bit) as has_unchecked_assembly_data + , 0 as text_in_row_limit + , CAST(0 as sys.bit) as large_value_types_out_of_row + , CAST(0 as sys.bit) as is_tracked_by_cdc + , CAST(0 as sys.tinyint) as lock_escalation + , CAST('TABLE' as sys.nvarchar(60)) as lock_escalation_desc + , CAST(0 as sys.bit) as is_filetable + , CAST(0 as sys.tinyint) as durability + , CAST('SCHEMA_AND_DATA' as sys.nvarchar(60)) as durability_desc + , CAST(0 as sys.bit) is_memory_optimized + , case relpersistence when 't' then CAST(2 as sys.tinyint) else CAST(0 as sys.tinyint) end as temporal_type + , case relpersistence when 't' then CAST('SYSTEM_VERSIONED_TEMPORAL_TABLE' as sys.nvarchar(60)) else CAST('NON_TEMPORAL_TABLE' as sys.nvarchar(60)) end as temporal_type_desc + , CAST(null as integer) as history_table_id + , CAST(0 as sys.bit) as is_remote_data_archive_enabled + , CAST(0 as sys.bit) as is_external +from pg_class t +inner join sys.schemas sch on sch.schema_id = t.relnamespace +left join sys.table_types_internal tt on t.oid = tt.typrelid +where tt.typrelid is null +and t.relkind = 'r' +and has_schema_privilege(t.relnamespace, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER'); +GRANT SELECT ON sys.tables TO PUBLIC; + +ALTER VIEW sys.default_constraints RENAME TO default_constraints_deprecated_3_5_0; + +create or replace view sys.default_constraints +AS +select CAST(('DF_' || tab.name || '_' || d.oid) as sys.sysname) as name + , CAST(d.oid as int) as object_id + , CAST(null as int) as principal_id + , CAST(tab.schema_id as int) as schema_id + , CAST(d.adrelid as int) as parent_object_id + , CAST('D' as sys.bpchar(2)) as type + , CAST('DEFAULT_CONSTRAINT' as sys.nvarchar(60)) AS type_desc + , CAST(null as sys.datetime) as create_date + , CAST(null as sys.datetime) as modified_date + , CAST(0 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published + , CAST(d.adnum as int) as parent_column_id + , CAST(tsql_get_expr(d.adbin, d.adrelid) as sys.nvarchar(4000)) as definition + , CAST(1 as sys.bit) as is_system_named +from pg_catalog.pg_attrdef as d +inner join pg_attribute a on a.attrelid = d.adrelid and d.adnum = a.attnum +inner join sys.tables tab on d.adrelid = tab.object_id +WHERE a.atthasdef = 't' and a.attgenerated = '' +AND has_schema_privilege(tab.schema_id, 'USAGE') +AND has_column_privilege(a.attrelid, a.attname, 'SELECT,INSERT,UPDATE,REFERENCES'); +GRANT SELECT ON sys.default_constraints TO PUBLIC; + +ALTER VIEW sys.check_constraints RENAME TO check_constraints_deprecated_3_5_0; + +CREATE or replace VIEW sys.check_constraints AS +SELECT CAST(c.conname as sys.sysname) as name + , CAST(oid as integer) as object_id + , CAST(NULL as integer) as principal_id + , CAST(c.connamespace as integer) as schema_id + , CAST(conrelid as integer) as parent_object_id + , CAST('C' as sys.bpchar(2)) as type + , CAST('CHECK_CONSTRAINT' as sys.nvarchar(60)) as type_desc + , CAST(null as sys.datetime) as create_date + , CAST(null as sys.datetime) as modify_date + , CAST(0 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published + , CAST(0 as sys.bit) as is_disabled + , CAST(0 as sys.bit) as is_not_for_replication + , CAST(0 as sys.bit) as is_not_trusted + , CAST(c.conkey[1] as integer) AS parent_column_id + , CAST(tsql_get_constraintdef(c.oid) as sys.nvarchar(4000)) AS definition + , CAST(1 as sys.bit) as uses_database_collation + , CAST(0 as sys.bit) as is_system_named +FROM pg_catalog.pg_constraint as c +INNER JOIN sys.schemas s on c.connamespace = s.schema_id +WHERE has_schema_privilege(s.schema_id, 'USAGE') +AND c.contype = 'c' and c.conrelid != 0; +GRANT SELECT ON sys.check_constraints TO PUBLIC; + +ALTER VIEW sys.types RENAME TO types_deprecated_3_5_0; + +create or replace view sys.types As +-- For System types +select + CAST(tsql_type_name as sys.sysname) as name + , cast(t.oid as int) as system_type_id + , cast(t.oid as int) as user_type_id + , cast(s.oid as int) as schema_id + , cast(NULL as INT) as principal_id + , sys.tsql_type_max_length_helper(tsql_type_name, t.typlen, t.typtypmod, true) as max_length + , sys.tsql_type_precision_helper(tsql_type_name, t.typtypmod) as precision + , sys.tsql_type_scale_helper(tsql_type_name, t.typtypmod, false) as scale + , CASE c.collname + WHEN 'default' THEN default_collation_name + ELSE CAST(c.collname as sys.sysname) + END as collation_name + , case when typnotnull then cast(0 as sys.bit) else cast(1 as sys.bit) end as is_nullable + , CAST(0 as sys.bit) as is_user_defined + , CAST(0 as sys.bit) as is_assembly_type + , CAST(0 as int) as default_object_id + , CAST(0 as int) as rule_object_id + , CAST(0 as sys.bit) as is_table_type +from pg_type t +inner join pg_namespace s on s.oid = t.typnamespace +left join pg_collation c on c.oid = t.typcollation +, sys.translate_pg_type_to_tsql(t.oid) AS tsql_type_name +,cast(current_setting('babelfishpg_tsql.server_collation_name') as sys.sysname) as default_collation_name +where +tsql_type_name IS NOT NULL +and pg_type_is_visible(t.oid) +and (s.nspname = 'pg_catalog' OR s.nspname = 'sys') +union all +-- For User Defined Types +select cast(t.typname as sys.sysname) as name + , cast(t.typbasetype as int) as system_type_id + , cast(t.oid as int) as user_type_id + , cast(t.typnamespace as int) as schema_id + , null::integer as principal_id + , case when tt.typrelid is not null then -1::smallint else sys.tsql_type_max_length_helper(tsql_base_type_name, t.typlen, t.typtypmod) end as max_length + , case when tt.typrelid is not null then 0::sys.tinyint else sys.tsql_type_precision_helper(tsql_base_type_name, t.typtypmod) end as precision + , case when tt.typrelid is not null then 0::sys.tinyint else sys.tsql_type_scale_helper(tsql_base_type_name, t.typtypmod, false) end as scale + , CASE c.collname + WHEN 'default' THEN default_collation_name + ELSE CAST(c.collname as sys.sysname) + END as collation_name + , case when tt.typrelid is not null then cast(0 as sys.bit) + else case when typnotnull then cast(0 as sys.bit) else cast(1 as sys.bit) end + end + as is_nullable + -- CREATE TYPE ... FROM is implemented as CREATE DOMAIN in babel + , CAST(1 as sys.bit) as is_user_defined + , CAST(0 as sys.bit) as is_assembly_type + , CAST(0 as int) as default_object_id + , CAST(0 as int) as rule_object_id + , case when tt.typrelid is not null then CAST(1 as sys.bit) else CAST(0 as sys.bit) end as is_table_type +from pg_type t +join sys.schemas sch on t.typnamespace = sch.schema_id +left join pg_collation c on c.oid = t.typcollation +left join sys.table_types_internal tt on t.typrelid = tt.typrelid +, sys.translate_pg_type_to_tsql(t.oid) AS tsql_type_name +, sys.translate_pg_type_to_tsql(t.typbasetype) AS tsql_base_type_name +, cast(current_setting('babelfishpg_tsql.server_collation_name') as sys.sysname) as default_collation_name +-- we want to show details of user defined datatypes created under babelfish database +where + tsql_type_name IS NULL +and + ( + -- show all user defined datatypes created under babelfish database except table types + t.typtype = 'd' + or + -- only for table types + tt.typrelid is not null + ); +GRANT SELECT ON sys.types TO PUBLIC; + +CREATE OR REPLACE VIEW sys.sp_special_columns_view AS +SELECT +CAST(1 AS SMALLINT) AS SCOPE, +CAST(coalesce (split_part(a.attoptions[1] COLLATE "C", '=', 2) ,a.attname) AS sys.sysname) AS COLUMN_NAME, -- get original column name if exists +CAST(t6.data_type AS SMALLINT) AS DATA_TYPE, + +CASE -- cases for when they are of type identity. + WHEN a.attidentity <> ''::"char" AND (t1.name = 'decimal' OR t1.name = 'numeric') + THEN CAST(CONCAT(t1.name, '() identity') AS sys.sysname) + WHEN a.attidentity <> ''::"char" AND (t1.name != 'decimal' AND t1.name != 'numeric') + THEN CAST(CONCAT(t1.name, ' identity') AS sys.sysname) + ELSE CAST(t1.name AS sys.sysname) +END AS TYPE_NAME, + +CAST(sys.sp_special_columns_precision_helper(COALESCE(tsql_type_name, tsql_base_type_name), c1.precision, c1.max_length, t6."PRECISION") AS INT) AS PRECISION, +CAST(sys.sp_special_columns_length_helper(coalesce(tsql_type_name, tsql_base_type_name), c1.precision, c1.max_length, t6."PRECISION") AS INT) AS LENGTH, +CAST(sys.sp_special_columns_scale_helper(coalesce(tsql_type_name, tsql_base_type_name), c1.scale) AS SMALLINT) AS SCALE, +CAST(1 AS smallint) AS PSEUDO_COLUMN, +CASE + WHEN a.attnotnull + THEN CAST(0 AS INT) + ELSE CAST(1 AS INT) END +AS IS_NULLABLE, +CAST(nsp_ext.dbname AS sys.sysname) AS TABLE_QUALIFIER, +CAST(s1.name AS sys.sysname) AS TABLE_OWNER, +CAST(C.relname AS sys.sysname) AS TABLE_NAME, + +CASE + WHEN X.indisprimary + THEN CAST('p' AS sys.sysname) + ELSE CAST('u' AS sys.sysname) -- if it is a unique index, then we should cast it as 'u' for filtering purposes +END AS CONSTRAINT_TYPE, +CAST(I.relname AS sys.sysname) CONSTRAINT_NAME, +CAST(X.indexrelid AS int) AS INDEX_ID + +FROM( pg_index X +JOIN pg_class C ON X.indrelid = C.oid +JOIN pg_class I ON I.oid = X.indexrelid +CROSS JOIN LATERAL unnest(X.indkey) AS ak(k) + LEFT JOIN pg_attribute a + ON (a.attrelid = X.indrelid AND a.attnum = ak.k) +) +LEFT JOIN sys.pg_namespace_ext nsp_ext ON C.relnamespace = nsp_ext.oid +LEFT JOIN sys.schemas s1 ON s1.schema_id = C.relnamespace +LEFT JOIN sys.columns c1 ON c1.object_id = X.indrelid AND cast(a.attname AS sys.sysname) = c1.name COLLATE sys.database_default +LEFT JOIN pg_catalog.pg_type AS T ON T.oid = c1.system_type_id +LEFT JOIN sys.types AS t1 ON a.atttypid = t1.user_type_id +LEFT JOIN sys.sp_datatype_info_helper(2::smallint, false) AS t6 ON T.typname = t6.pg_type_name OR T.typname = t6.type_name --need in order to get accurate DATA_TYPE value +, sys.translate_pg_type_to_tsql(t1.user_type_id) AS tsql_type_name +, sys.translate_pg_type_to_tsql(t1.system_type_id) AS tsql_base_type_name +WHERE has_schema_privilege(s1.schema_id, 'USAGE') +AND X.indislive ; + +GRANT SELECT ON sys.sp_special_columns_view TO PUBLIC; + +CREATE OR REPLACE VIEW sys.assembly_types +AS +SELECT + t.name AS name, + -- 'system_type_id' is specified as type INT here, and not TINYINT per SQL Server documentation. + -- This is because the IDs of generated SQL Server system type values generated by B + -- Babelfish installation will exceed the size of TINYINT. + CAST(t.system_type_id as int) AS system_type_id, + CAST(t.user_type_id as int) AS user_type_id, + CAST(t.schema_id as int) AS schema_id, + CAST(t.principal_id as int) AS principal_id, + CAST(t.max_length as smallint) AS max_length, + CAST(t.precision as sys.tinyint) AS precision, + CAST(t.scale as sys.tinyint) AS scale, + t.collation_name AS collation_name, + CAST(t.is_nullable as sys.bit) AS is_nullable, + CAST(t.is_user_defined as sys.bit) AS is_user_defined, + CAST(t.is_assembly_type as sys.bit) AS is_assembly_type, + CAST(t.default_object_id as int) AS default_object_id, + CAST(t.rule_object_id as int) AS rule_object_id, + CAST(NULL as int) AS assembly_id, + CAST(NULL as sys.sysname) AS assembly_class, + CAST(NULL as sys.bit) AS is_binary_ordered, + CAST(NULL as sys.bit) AS is_fixed_length, + CAST(NULL as sys.nvarchar(40)) AS prog_id, + CAST(NULL as sys.nvarchar(4000)) AS assembly_qualified_name, + CAST(t.is_table_type as sys.bit) AS is_table_type +FROM sys.types t +WHERE t.is_assembly_type = 1; +GRANT SELECT ON sys.assembly_types TO PUBLIC; + +ALTER VIEW sys.systypes RENAME TO systypes_deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.systypes AS +SELECT name + , CAST(system_type_id as int) as xtype + , CAST((case when is_nullable = 1 then 0 else 1 end) as sys.tinyint) as status + , CAST((case when user_type_id < 32767 then user_type_id::int else null end) as smallint) as xusertype + , max_length as length + , CAST(precision as sys.tinyint) as xprec + , CAST(scale as sys.tinyint) as xscale + , CAST(default_object_id as int) as tdefault + , CAST(rule_object_id as int) as domain + , CAST((case when schema_id < 32767 then schema_id::int else null end) as smallint) as uid + , CAST(0 as smallint) as reserved + , CAST(sys.CollationProperty(collation_name, 'CollationId') as int) as collationid + , CAST((case when user_type_id < 32767 then user_type_id::int else null end) as smallint) as usertype + , CAST((case when (coalesce(sys.translate_pg_type_to_tsql(system_type_id), sys.translate_pg_type_to_tsql(user_type_id)) + in ('nvarchar', 'varchar', 'sysname', 'varbinary')) then 1 + else 0 end) as sys.bit) as variable + , CAST(is_nullable as sys.bit) as allownulls + , CAST(system_type_id as int) as type + , CAST(null as sys.varchar(255)) as printfmt + , (case when precision <> 0::sys.tinyint then precision::smallint + else sys.systypes_precision_helper(sys.translate_pg_type_to_tsql(system_type_id), max_length) end) as prec + , CAST(scale as sys.tinyint) as scale + , collation_name as collation +FROM sys.types; +GRANT SELECT ON sys.systypes TO PUBLIC; + +CREATE OR REPLACE PROCEDURE sys.sp_babelfish_autoformat( + IN "@tab" sys.VARCHAR(257) DEFAULT NULL, + IN "@orderby" sys.VARCHAR(1000) DEFAULT '', + IN "@printrc" sys.bit DEFAULT 1, + IN "@hiddencols" sys.VARCHAR(1000) DEFAULT NULL) +LANGUAGE 'pltsql' +AS $$ +BEGIN + SET NOCOUNT ON + DECLARE @rc INT + DECLARE @id INT + DECLARE @objtype sys.VARCHAR(2) + DECLARE @msg sys.VARCHAR(200) + + IF @tab IS NULL + BEGIN + RAISERROR('Must specify table name', 16, 1) + RETURN + END + + IF TRIM(@tab) = '' + BEGIN + RAISERROR('Must specify table name', 16, 1) + RETURN + END + + -- Since we cannot find #tmp tables in the Babelfish catalogs, we cannot check + -- their existence other than by trying to select from them + -- Function sys.babelfish_get_enr_list() could be used to determine if a #tmp table + -- exists but the columns and datatypes can still not be retrieved, it would be of + -- little use here. + -- NB: not handling uncommon but valid T-SQL syntax '.#tmp' for #tmp tables + IF sys.SUBSTRING(@tab,1,1) <> '#' + BEGIN + SET @id = sys.OBJECT_ID(@tab) + IF @id IS NULL + BEGIN + IF sys.SUBSTRING(UPPER(@tab),1,4) = 'DBO.' + BEGIN + SET @id = sys.OBJECT_ID('SYS.' + sys.SUBSTRING(@tab,5)) + END + IF @id IS NULL + BEGIN + SET @msg = 'Table or view '''+@tab+''' not found' + RAISERROR(@msg, 16, 1) + RETURN + END + END + END + + SELECT @objtype = type COLLATE DATABASE_DEFAULT FROM sys.sysobjects WHERE id = @id + IF @objtype NOT IN ('U', 'S', 'V') + BEGIN + SET @msg = ''''+@tab+''' is not a table or view' + RAISERROR(@msg, 16, 1) + RETURN + END + + -- check for 'ORDER BY', if specified + SET @orderby = TRIM(@orderby) + IF @orderby <> '' + BEGIN + IF UPPER(@orderby) NOT LIKE 'ORDER BY%' + BEGIN + RAISERROR('@orderby parameter must start with ''ORDER BY''', 16, 1) + RETURN + END + END + + -- columns to hide in final client output + -- assuming delimited column names do not contain spaces or commas inside the name + -- remove any spaces around the commas: + WHILE (sys.CHARINDEX(' ,', @hiddencols) > 0) or (sys.CHARINDEX(', ', @hiddencols) > 0) + BEGIN + SET @hiddencols = sys.REPLACE(@hiddencols, ' ,', ',') + SET @hiddencols = sys.REPLACE(@hiddencols, ', ', ',') + END + IF sys.LEN(@hiddencols) IS NOT NULL SET @hiddencols = ',' + @hiddencols + ',' + SET @hiddencols = UPPER(@hiddencols) + + -- Need to use a guaranteed-uniquely named table as intermediate step since we cannot + -- access the metadata in case a #tmp table is passed as argument + -- But when we copy the #tmp table into another table, we get all the attributes and metadata + DECLARE @tmptab sys.VARCHAR(63) = 'sp_babelfish_autoformat' + sys.REPLACE(NEWID(), '-', '') + DECLARE @tmptab2 sys.VARCHAR(63) = 'sp_babelfish_autoformat' + sys.REPLACE(NEWID(), '-', '') + DECLARE @cmd sys.VARCHAR(1000) = 'SELECT * INTO ' + @tmptab + ' FROM ' + @tab + + BEGIN TRY + -- create the first work table + EXECUTE(@cmd) + + -- Get the columns + SELECT + c.name AS colname, c.colid AS colid, t.name AS basetype, 0 AS maxlen + INTO #sp_bbf_autoformat + FROM sys.syscolumns c left join sys.systypes t + ON c.xusertype = t.xusertype + WHERE c.id = sys.OBJECT_ID(@tmptab) + ORDER BY c.colid + + -- Get max length for each column based on the data + DECLARE @colname sys.VARCHAR(63), @basetype sys.VARCHAR(63), @maxlen int + DECLARE c CURSOR FOR SELECT colname, basetype, maxlen FROM #sp_bbf_autoformat ORDER BY colid + OPEN c + WHILE 1=1 + BEGIN + FETCH c INTO @colname, @basetype, @maxlen + IF @@fetch_status <> 0 BREAK + SET @cmd = 'DECLARE @i INT SELECT @i=ISNULL(MAX(sys.LEN(CAST([' + @colname + '] AS sys.VARCHAR(500)))),4) FROM ' + @tmptab + ' UPDATE #sp_bbf_autoformat SET maxlen = @i WHERE colname = ''' + @colname + '''' + EXECUTE(@cmd) + END + CLOSE c + DEALLOCATE c + + -- Generate the final SELECT + DECLARE @selectlist sys.VARCHAR(8000) = '' + DECLARE @collist sys.VARCHAR(8000) = '' + DECLARE @fmtstart sys.VARCHAR(30) = '' + DECLARE @fmtend sys.VARCHAR(30) = '' + OPEN c + WHILE 1=1 + BEGIN + FETCH c INTO @colname, @basetype, @maxlen + IF @@fetch_status <> 0 BREAK + IF sys.LEN(@colname) > @maxlen SET @maxlen = sys.LEN(@colname) + IF @maxlen <= 0 SET @maxlen = 1 + + IF (sys.CHARINDEX(',' + UPPER(@colname) + ',', @hiddencols) > 0) OR (sys.CHARINDEX(',[' + UPPER(@colname) + '],', @hiddencols) > 0) + BEGIN + SET @selectlist += ' [' + @colname + '],' + END + ELSE + BEGIN + SET @fmtstart = '' + SET @fmtend = '' + IF @basetype IN ('tinyint', 'smallint', 'int', 'bigint', 'decimal', 'numeric', 'real', 'float') + BEGIN + SET @fmtstart = 'CAST(right(space('+CAST(@maxlen AS sys.VARCHAR)+')+' + SET @fmtend = ','+CAST(@maxlen AS sys.VARCHAR)+') AS sys.VARCHAR(' + CAST(@maxlen AS sys.VARCHAR) + '))' + END + + SET @selectlist += ' '+@fmtstart+'CAST([' + @colname + '] AS sys.VARCHAR(' + CAST(@maxlen AS sys.VARCHAR) + '))'+@fmtend+' AS [' + @colname + '],' + SET @collist += '['+@colname + '],' + END + END + CLOSE c + DEALLOCATE c + + -- Remove redundant commas + SET @collist = sys.SUBSTRING(@collist, 1, sys.LEN(@collist)-1) + SET @selectlist = sys.SUBSTRING(@selectlist, 1, sys.LEN(@selectlist)-1) + SET @selectlist = 'SELECT ' + @selectlist + ' INTO ' + @tmptab2 + ' FROM ' + @tmptab + ' ' + @orderby + + -- create the second work table + EXECUTE(@selectlist) + + -- perform the final SELECT to generate the result set for the client + EXECUTE('SELECT ' + @collist + ' FROM ' + @tmptab2) + + -- PRINT rowcount if desired + SET @rc = @@rowcount + IF @printrc = 1 + BEGIN + PRINT ' ' + SET @cmd = '(' + CAST(@rc AS sys.VARCHAR) + ' rows affected)' + PRINT @cmd + END + + -- Cleanup: these work tables are permanent tables after all + EXECUTE('DROP TABLE IF EXISTS ' + @tmptab) + EXECUTE('DROP TABLE IF EXISTS ' + @tmptab2) + END TRY + BEGIN CATCH + -- Cleanup in case of an unexpected error + EXECUTE('DROP TABLE IF EXISTS ' + @tmptab) + EXECUTE('DROP TABLE IF EXISTS ' + @tmptab2) + END CATCH + + RETURN +END +$$; +GRANT EXECUTE ON PROCEDURE sys.sp_babelfish_autoformat(IN sys.VARCHAR(257), IN sys.VARCHAR(1000), sys.bit, sys.VARCHAR(1000)) TO PUBLIC; + +ALTER VIEW sys.table_types RENAME TO table_types_deprecated_3_5_0; + +create or replace view sys.table_types as +select st.* + , pt.typrelid::int as type_table_object_id + , 0::sys.bit as is_memory_optimized -- return 0 until we support in-memory tables +from sys.types st +inner join pg_catalog.pg_type pt on st.user_type_id = pt.oid +where is_table_type = 1; +GRANT SELECT ON sys.table_types TO PUBLIC; + +-- sys.all_objects and sys.objects +create or replace view sys.all_objects as +select + name collate sys.database_default + , cast (object_id as integer) + , cast ( principal_id as integer) + , cast (schema_id as integer) + , cast (parent_object_id as integer) + , type collate sys.database_default + , cast (type_desc as sys.nvarchar(60)) + , cast (create_date as sys.datetime) + , cast (modify_date as sys.datetime) + , is_ms_shipped + , cast (is_published as sys.bit) + , cast (is_schema_published as sys.bit) +from +( +-- Currently for pg_class, pg_proc UNIONs, we separated user defined objects and system objects because the +-- optimiser will be able to make a better estimation of number of rows(in case the query contains a filter on +-- is_ms_shipped column) and in turn chooses a better query plan. + +-- details of system tables +select + t.relname::sys.sysname as name + , t.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'U'::char(2) as type + , 'USER_TABLE' as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 1::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class t inner join pg_namespace s on s.oid = t.relnamespace +left join sys.table_types_internal tt on t.oid = tt.typrelid +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'U' +where t.relpersistence in ('p', 'u', 't') +and t.relkind = 'r' +and (s.nspname = 'sys' or (nis.name is not null and ext.nspname is not null)) +and tt.typrelid is null +and has_schema_privilege(s.oid, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER') + +union all +-- details of user defined tables +select + t.relname::sys.sysname as name + , t.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'U'::char(2) as type + , 'USER_TABLE' as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 0::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class t inner join pg_namespace s on s.oid = t.relnamespace +left join sys.table_types_internal tt on t.oid = tt.typrelid +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'U' +where t.relpersistence in ('p', 'u', 't') +and t.relkind = 'r' +and s.nspname <> 'sys' and nis.name is null +and ext.nspname is not null +and tt.typrelid is null +and has_schema_privilege(s.oid, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER') + +union all +-- details of system views +select + t.relname::sys.sysname as name + , t.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'V'::char(2) as type + , 'VIEW'::varchar(60) as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 1::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class t inner join pg_namespace s on s.oid = t.relnamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'V' +where t.relkind = 'v' +and (s.nspname = 'sys' or (nis.name is not null and ext.nspname is not null)) +and has_schema_privilege(s.oid, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER') +union all +-- Details of user defined views +select + t.relname::sys.sysname as name + , t.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'V'::char(2) as type + , 'VIEW'::varchar(60) as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 0::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class t inner join pg_namespace s on s.oid = t.relnamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'V' +where t.relkind = 'v' +and s.nspname <> 'sys' and nis.name is null +and ext.nspname is not null +and has_schema_privilege(s.oid, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER') +union all +-- details of user defined and system foreign key constraints +select + c.conname::sys.sysname as name + , c.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , c.conrelid as parent_object_id + , 'F'::char(2) as type + , 'FOREIGN_KEY_CONSTRAINT' + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_constraint c +inner join pg_namespace s on s.oid = c.connamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = c.conname and nis.schemaid = s.oid and nis.type = 'F' +where has_schema_privilege(s.oid, 'USAGE') +and c.contype = 'f' +and (s.nspname = 'sys' or ext.nspname is not null) +union all +-- details of user defined and system primary key constraints +select + c.conname::sys.sysname as name + , c.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , c.conrelid as parent_object_id + , 'PK'::char(2) as type + , 'PRIMARY_KEY_CONSTRAINT' as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_constraint c +inner join pg_namespace s on s.oid = c.connamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = c.conname and nis.schemaid = s.oid and nis.type = 'PK' +where has_schema_privilege(s.oid, 'USAGE') +and c.contype = 'p' +and (s.nspname = 'sys' or ext.nspname is not null) +union all +-- details of system defined procedures +select + p.proname::sys.sysname as name + , p.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , cast (case when tr.tgrelid is not null + then tr.tgrelid + else 0 end as int) + as parent_object_id + , case p.prokind + when 'p' then 'P'::char(2) + when 'a' then 'AF'::char(2) + else + case + when t.typname = 'trigger' + then 'TR'::char(2) + when p.proretset then + case + when t.typtype = 'c' + then 'TF'::char(2) + else 'IF'::char(2) + end + else 'FN'::char(2) + end + end as type + , case p.prokind + when 'p' then 'SQL_STORED_PROCEDURE'::varchar(60) + when 'a' then 'AGGREGATE_FUNCTION'::varchar(60) + else + case + when t.typname = 'trigger' + then 'SQL_TRIGGER'::varchar(60) + when p.proretset then + case + when t.typtype = 'c' + then 'SQL_TABLE_VALUED_FUNCTION'::varchar(60) + else 'SQL_INLINE_TABLE_VALUED_FUNCTION'::varchar(60) + end + else 'SQL_SCALAR_FUNCTION'::varchar(60) + end + end as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 1::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_proc p +inner join pg_namespace s on s.oid = p.pronamespace +inner join pg_catalog.pg_type t on t.oid = p.prorettype +left join pg_trigger tr on tr.tgfoid = p.oid +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = p.proname and nis.schemaid = s.oid +and nis.type = (case p.prokind + when 'p' then 'P'::char(2) + when 'a' then 'AF'::char(2) + else + case + when t.typname = 'trigger' + then 'TR'::char(2) + when p.proretset then + case + when t.typtype = 'c' + then 'TF'::char(2) + else 'IF'::char(2) + end + else 'FN'::char(2) + end + end) +where (s.nspname = 'sys' or (nis.name is not null and ext.nspname is not null)) +and has_schema_privilege(s.oid, 'USAGE') +and has_function_privilege(p.oid, 'EXECUTE') + +union all +-- details of user defined procedures +select + p.proname::sys.sysname as name + , p.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , cast (case when tr.tgrelid is not null + then tr.tgrelid + else 0 end as int) + as parent_object_id + , case p.prokind + when 'p' then 'P'::char(2) + when 'a' then 'AF'::char(2) + else + case + when t.typname = 'trigger' + then 'TR'::char(2) + when p.proretset then + case + when t.typtype = 'c' + then 'TF'::char(2) + else 'IF'::char(2) + end + else 'FN'::char(2) + end + end as type + , case p.prokind + when 'p' then 'SQL_STORED_PROCEDURE'::varchar(60) + when 'a' then 'AGGREGATE_FUNCTION'::varchar(60) + else + case + when t.typname = 'trigger' + then 'SQL_TRIGGER'::varchar(60) + when p.proretset then + case + when t.typtype = 'c' + then 'SQL_TABLE_VALUED_FUNCTION'::varchar(60) + else 'SQL_INLINE_TABLE_VALUED_FUNCTION'::varchar(60) + end + else 'SQL_SCALAR_FUNCTION'::varchar(60) + end + end as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 0::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_proc p +inner join pg_namespace s on s.oid = p.pronamespace +inner join pg_catalog.pg_type t on t.oid = p.prorettype +left join pg_trigger tr on tr.tgfoid = p.oid +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = p.proname and nis.schemaid = s.oid +and nis.type = (case p.prokind + when 'p' then 'P'::char(2) + when 'a' then 'AF'::char(2) + else + case + when t.typname = 'trigger' + then 'TR'::char(2) + when p.proretset then + case + when t.typtype = 'c' + then 'TF'::char(2) + else 'IF'::char(2) + end + else 'FN'::char(2) + end + end) +where s.nspname <> 'sys' and nis.name is null +and ext.nspname is not null +and has_schema_privilege(s.oid, 'USAGE') +and has_function_privilege(p.oid, 'EXECUTE') + +union all +-- details of all default constraints +select + ('DF_' || o.relname || '_' || d.oid)::sys.sysname as name + , d.oid as object_id + , null::int as principal_id + , o.relnamespace as schema_id + , d.adrelid as parent_object_id + , 'D'::char(2) as type + , 'DEFAULT_CONSTRAINT'::sys.nvarchar(60) AS type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_catalog.pg_attrdef d +inner join pg_attribute a on a.attrelid = d.adrelid and d.adnum = a.attnum +inner join pg_class o on d.adrelid = o.oid +inner join pg_namespace s on s.oid = o.relnamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = ('DF_' || o.relname || '_' || d.oid) and nis.schemaid = s.oid and nis.type = 'D' +where a.atthasdef = 't' and a.attgenerated = '' +and (s.nspname = 'sys' or ext.nspname is not null) +and has_schema_privilege(s.oid, 'USAGE') +and has_column_privilege(a.attrelid, a.attname, 'SELECT,INSERT,UPDATE,REFERENCES') +union all +-- details of all check constraints +select + c.conname::sys.sysname + , c.oid::integer as object_id + , NULL::integer as principal_id + , s.oid as schema_id + , c.conrelid::integer as parent_object_id + , 'C'::char(2) as type + , 'CHECK_CONSTRAINT'::sys.nvarchar(60) as type_desc + , null::sys.datetime as create_date + , null::sys.datetime as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_catalog.pg_constraint as c +inner join pg_namespace s on s.oid = c.connamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = c.conname and nis.schemaid = s.oid and nis.type = 'C' +where has_schema_privilege(s.oid, 'USAGE') +and c.contype = 'c' and c.conrelid != 0 +and (s.nspname = 'sys' or ext.nspname is not null) +union all +-- details of user defined and system defined sequence objects +select + p.relname::sys.sysname as name + , p.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'SO'::char(2) as type + , 'SEQUENCE_OBJECT'::varchar(60) as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class p +inner join pg_namespace s on s.oid = p.relnamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = p.relname and nis.schemaid = s.oid and nis.type = 'SO' +where p.relkind = 'S' +and (s.nspname = 'sys' or ext.nspname is not null) +and has_schema_privilege(s.oid, 'USAGE') +union all +-- details of user defined table types +select + ('TT_' || tt.name || '_' || tt.type_table_object_id)::sys.sysname as name + , tt.type_table_object_id as object_id + , tt.principal_id as principal_id + , tt.schema_id as schema_id + , 0 as parent_object_id + , 'TT'::char(2) as type + , 'TABLE_TYPE'::varchar(60) as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (tt.schema_id::regnamespace::text = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from sys.table_types tt +left join sys.shipped_objects_not_in_sys nis on nis.name = ('TT_' || tt.name || '_' || tt.type_table_object_id)::name and nis.schemaid = tt.schema_id and nis.type = 'TT' +) ot; +GRANT SELECT ON sys.all_objects TO PUBLIC; + +create or replace view sys.objects as +select + CAST(t.name as sys.sysname) as name + , CAST(t.object_id as int) as object_id + , CAST(t.principal_id as int) as principal_id + , CAST(t.schema_id as int) as schema_id + , CAST(t.parent_object_id as int) as parent_object_id + , CAST('U' as char(2)) as type + , CAST('USER_TABLE' as sys.nvarchar(60)) as type_desc + , CAST(t.create_date as sys.datetime) as create_date + , CAST(t.modify_date as sys.datetime) as modify_date + , CAST(t.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(t.is_published as sys.bit) as is_published + , CAST(t.is_schema_published as sys.bit) as is_schema_published +from sys.tables t +union all +select + CAST(v.name as sys.sysname) as name + , CAST(v.object_id as int) as object_id + , CAST(v.principal_id as int) as principal_id + , CAST(v.schema_id as int) as schema_id + , CAST(v.parent_object_id as int) as parent_object_id + , CAST('V' as char(2)) as type + , CAST('VIEW' as sys.nvarchar(60)) as type_desc + , CAST(v.create_date as sys.datetime) as create_date + , CAST(v.modify_date as sys.datetime) as modify_date + , CAST(v.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(v.is_published as sys.bit) as is_published + , CAST(v.is_schema_published as sys.bit) as is_schema_published +from sys.views v +union all +select + CAST(f.name as sys.sysname) as name + , CAST(f.object_id as int) as object_id + , CAST(f.principal_id as int) as principal_id + , CAST(f.schema_id as int) as schema_id + , CAST(f.parent_object_id as int) as parent_object_id + , CAST('F' as char(2)) as type + , CAST('FOREIGN_KEY_CONSTRAINT' as sys.nvarchar(60)) as type_desc + , CAST(f.create_date as sys.datetime) as create_date + , CAST(f.modify_date as sys.datetime) as modify_date + , CAST(f.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(f.is_published as sys.bit) as is_published + , CAST(f.is_schema_published as sys.bit) as is_schema_published + from sys.foreign_keys f +union all +select + CAST(p.name as sys.sysname) as name + , CAST(p.object_id as int) as object_id + , CAST(p.principal_id as int) as principal_id + , CAST(p.schema_id as int) as schema_id + , CAST(p.parent_object_id as int) as parent_object_id + , CAST('PK' as char(2)) as type + , CAST('PRIMARY_KEY_CONSTRAINT' as sys.nvarchar(60)) as type_desc + , CAST(p.create_date as sys.datetime) as create_date + , CAST(p.modify_date as sys.datetime) as modify_date + , CAST(p.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(p.is_published as sys.bit) as is_published + , CAST(p.is_schema_published as sys.bit) as is_schema_published +from sys.key_constraints p +where p.type = 'PK' +union all +select + CAST(pr.name as sys.sysname) as name + , CAST(pr.object_id as int) as object_id + , CAST(pr.principal_id as int) as principal_id + , CAST(pr.schema_id as int) as schema_id + , CAST(pr.parent_object_id as int) as parent_object_id + , CAST(pr.type as char(2)) as type + , CAST(pr.type_desc as sys.nvarchar(60)) as type_desc + , CAST(pr.create_date as sys.datetime) as create_date + , CAST(pr.modify_date as sys.datetime) as modify_date + , CAST(pr.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(pr.is_published as sys.bit) as is_published + , CAST(pr.is_schema_published as sys.bit) as is_schema_published + from sys.procedures pr +union all +select + CAST(tr.name as sys.sysname) as name + , CAST(tr.object_id as int) as object_id + , CAST(NULL as int) as principal_id + , CAST(p.pronamespace as int) as schema_id + , CAST(tr.parent_id as int) as parent_object_id + , CAST(tr.type as char(2)) as type + , CAST(tr.type_desc as sys.nvarchar(60)) as type_desc + , CAST(tr.create_date as sys.datetime) as create_date + , CAST(tr.modify_date as sys.datetime) as modify_date + , CAST(tr.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published + from sys.triggers tr + inner join pg_proc p on p.oid = tr.object_id +union all +select + CAST(def.name as sys.sysname) as name + , CAST(def.object_id as int) as object_id + , CAST(def.principal_id as int) as principal_id + , CAST(def.schema_id as int) as schema_id + , CAST(def.parent_object_id as int) as parent_object_id + , CAST(def.type as char(2)) as type + , CAST(def.type_desc as sys.nvarchar(60)) as type_desc + , CAST(def.create_date as sys.datetime) as create_date + , CAST(def.modified_date as sys.datetime) as modify_date + , CAST(def.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(def.is_published as sys.bit) as is_published + , CAST(def.is_schema_published as sys.bit) as is_schema_published + from sys.default_constraints def +union all +select + CAST(chk.name as sys.sysname) as name + , CAST(chk.object_id as int) as object_id + , CAST(chk.principal_id as int) as principal_id + , CAST(chk.schema_id as int) as schema_id + , CAST(chk.parent_object_id as int) as parent_object_id + , CAST(chk.type as char(2)) as type + , CAST(chk.type_desc as sys.nvarchar(60)) as type_desc + , CAST(chk.create_date as sys.datetime) as create_date + , CAST(chk.modify_date as sys.datetime) as modify_date + , CAST(chk.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(chk.is_published as sys.bit) as is_published + , CAST(chk.is_schema_published as sys.bit) as is_schema_published + from sys.check_constraints chk +union all +select + CAST(p.relname as sys.sysname) as name + , CAST(p.oid as int) as object_id + , CAST(null as int) as principal_id + , CAST(s.schema_id as int) as schema_id + , CAST(0 as int) as parent_object_id + , CAST('SO' as char(2)) as type + , CAST('SEQUENCE_OBJECT' as sys.nvarchar(60)) as type_desc + , CAST(null as sys.datetime) as create_date + , CAST(null as sys.datetime) as modify_date + , CAST(0 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published +from pg_class p +inner join sys.schemas s on s.schema_id = p.relnamespace +and p.relkind = 'S' +and has_schema_privilege(s.schema_id, 'USAGE') +union all +select + CAST(('TT_' || tt.name collate "C" || '_' || tt.type_table_object_id) as sys.sysname) as name + , CAST(tt.type_table_object_id as int) as object_id + , CAST(tt.principal_id as int) as principal_id + , CAST(tt.schema_id as int) as schema_id + , CAST(0 as int) as parent_object_id + , CAST('TT' as char(2)) as type + , CAST('TABLE_TYPE' as sys.nvarchar(60)) as type_desc + , CAST((select string_agg( + case + when option like 'bbf_rel_create_date=%%' then substring(option, 21) + else NULL + end, ',') + from unnest(c.reloptions) as option) + as sys.datetime) as create_date + , CAST((select string_agg( + case + when option like 'bbf_rel_create_date=%%' then substring(option, 21) + else NULL + end, ',') + from unnest(c.reloptions) as option) + as sys.datetime) as modify_date + , CAST(1 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published +from sys.table_types tt +inner join pg_class c on tt.type_table_object_id = c.oid; +GRANT SELECT ON sys.objects TO PUBLIC; + +CREATE OR REPLACE VIEW sys.all_parameters +AS +SELECT + CAST(ss.p_oid AS INT) AS object_id + , CAST(COALESCE(ss.proargnames[(ss.x).n], '') AS sys.SYSNAME) AS name + , CAST( + CASE + WHEN is_out_scalar = 1 THEN 0 -- param_id = 0 for output of scalar function + ELSE (ss.x).n + END + AS INT) AS parameter_id + -- 'system_type_id' is specified as type INT here, and not TINYINT per SQL Server documentation. + -- This is because the IDs of system type values generated by + -- Babelfish installation will exceed the size of TINYINT + , CAST(st.system_type_id AS INT) AS system_type_id + , CAST(st.user_type_id AS INT) AS user_type_id + , CAST( + CASE + WHEN st.is_table_type = 1 THEN -1 -- TVP case + WHEN st.is_user_defined = 1 THEN st.max_length -- UDT case + ELSE sys.tsql_type_max_length_helper(st.name, t.typlen, typmod, true, true) + END + AS smallint) AS max_length + , CAST( + CASE + WHEN st.is_table_type = 1 THEN 0 -- TVP case + WHEN st.is_user_defined = 1 THEN st.precision -- UDT case + ELSE sys.tsql_type_precision_helper(st.name, typmod) + END + AS sys.tinyint) AS precision + , CAST( + CASE + WHEN st.is_table_type = 1 THEN 0 -- TVP case + WHEN st.is_user_defined = 1 THEN st.scale + ELSE sys.tsql_type_scale_helper(st.name, typmod,false) + END + AS sys.tinyint) AS scale + , CAST( + CASE + WHEN is_out_scalar = 1 THEN 1 -- Output of a scalar function + WHEN ss.proargmodes[(ss.x).n] in ('o', 'b', 't') THEN 1 + ELSE 0 + END + AS sys.bit) AS is_output + , CAST(0 AS sys.bit) AS is_cursor_ref + , CAST(0 AS sys.bit) AS has_default_value + , CAST(0 AS sys.bit) AS is_xml_document + , CAST(NULL AS sys.sql_variant) AS default_value + , CAST(0 AS int) AS xml_collection_id + , CAST(0 AS sys.bit) AS is_readonly + , CAST(1 AS sys.bit) AS is_nullable + , CAST(NULL AS int) AS encryption_type + , CAST(NULL AS sys.nvarchar(64)) AS encryption_type_desc + , CAST(NULL AS sys.sysname) AS encryption_algorithm_name + , CAST(NULL AS int) AS column_encryption_key_id + , CAST(NULL AS sys.sysname) AS column_encryption_key_database_name +FROM pg_type t + INNER JOIN sys.types st ON st.user_type_id = t.oid + INNER JOIN + ( + SELECT + p.oid AS p_oid, + p.proargnames, + p.proargmodes, + p.prokind, + json_extract_path(CAST(p.probin as json), 'typmod_array') AS typmod_array, + information_schema._pg_expandarray( + COALESCE(p.proallargtypes, + CASE + WHEN p.prokind = 'f' THEN (CAST( p.proargtypes AS oid[]) || p.prorettype) -- Adds return type if not present on proallargtypes + ELSE CAST(p.proargtypes AS oid[]) + END + )) AS x + FROM pg_proc p + WHERE ( + p.pronamespace in (select schema_id from sys.schemas union all select oid from pg_namespace where nspname = 'sys') + AND (pg_has_role(p.proowner, 'USAGE') OR has_function_privilege(p.oid, 'EXECUTE')) + AND p.probin like '{%typmod_array%}') -- Needs to have a typmod array in JSON format + ) ss ON t.oid = (ss.x).x, + COALESCE(pg_get_function_result(ss.p_oid), '') AS return_type, + CAST(ss.typmod_array->>(ss.x).n-1 AS INT) AS typmod, + CAST( + CASE + WHEN ss.prokind = 'f' AND ss.proargnames[(ss.x).n] IS NULL THEN 1 -- checks if param is output of scalar function + ELSE 0 + END + AS INT) AS is_out_scalar +WHERE ( -- If it is a Table function, we only want the inputs + return_type NOT LIKE 'TABLE(%' OR + (return_type LIKE 'TABLE(%' AND ss.proargmodes[(ss.x).n] = 'i')); +GRANT SELECT ON sys.all_parameters TO PUBLIC; + +CREATE OR REPLACE PROCEDURE sys.sp_rename( + IN "@objname" sys.nvarchar(776) = NULL, + IN "@newname" sys.SYSNAME = NULL, + IN "@objtype" sys.varchar(13) DEFAULT NULL +) +LANGUAGE 'pltsql' +AS $$ +BEGIN + If @objtype IS NULL + BEGIN + THROW 33557097, N'Please provide @objtype that is supported in Babelfish', 1; + END + ELSE IF @objtype = 'INDEX' + BEGIN + THROW 33557097, N'Feature not supported: renaming object type Index', 1; + END + ELSE IF @objtype = 'STATISTICS' + BEGIN + THROW 33557097, N'Feature not supported: renaming object type Statistics', 1; + END + ELSE + BEGIN + DECLARE @subname sys.nvarchar(776); + DECLARE @schemaname sys.nvarchar(776); + DECLARE @dbname sys.nvarchar(776); + DECLARE @curr_relname sys.nvarchar(776); + + EXEC sys.babelfish_sp_rename_word_parse @objname, @objtype, @subname OUT, @curr_relname OUT, @schemaname OUT, @dbname OUT; + + DECLARE @currtype char(2); + + IF @objtype = 'COLUMN' + BEGIN + DECLARE @col_count INT; + SELECT @col_count = COUNT(*)FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @curr_relname and COLUMN_NAME = @subname; + IF @col_count < 0 + BEGIN + THROW 33557097, N'There is no object with the given @objname.', 1; + END + SET @currtype = 'CO'; + END + ELSE IF @objtype = 'USERDATATYPE' + BEGIN + DECLARE @alias_count INT; + SELECT @alias_count = COUNT(*) FROM sys.types t1 INNER JOIN sys.schemas s1 ON t1.schema_id = s1.schema_id + WHERE s1.name = @schemaname AND t1.name = @subname; + IF @alias_count > 1 + BEGIN + THROW 33557097, N'There are multiple objects with the given @objname.', 1; + END + IF @alias_count < 1 + BEGIN + THROW 33557097, N'There is no object with the given @objname.', 1; + END + SET @currtype = 'AL'; + END + ELSE IF @objtype = 'OBJECT' + BEGIN + DECLARE @count INT; + SELECT type INTO #tempTable FROM sys.objects o1 INNER JOIN sys.schemas s1 ON o1.schema_id = s1.schema_id + WHERE s1.name = @schemaname AND o1.name = @subname; + SELECT @count = COUNT(*) FROM #tempTable; + + IF @count > 1 + BEGIN + THROW 33557097, N'There are multiple objects with the given @objname.', 1; + END + IF @count < 1 + BEGIN + -- TABLE TYPE: check if there is a match in sys.table_types (if we cannot alter sys.objects table_type naming) + SELECT @count = COUNT(*) FROM sys.table_types tt1 INNER JOIN sys.schemas s1 ON tt1.schema_id = s1.schema_id + WHERE s1.name = @schemaname AND tt1.name = @subname; + IF @count > 1 + BEGIN + THROW 33557097, N'There are multiple objects with the given @objname.', 1; + END + ELSE IF @count < 1 + BEGIN + THROW 33557097, N'There is no object with the given @objname.', 1; + END + ELSE + BEGIN + SET @currtype = 'TT' + END + END + IF @currtype IS NULL + BEGIN + SELECT @currtype = type from #tempTable; + END + IF @currtype = 'TR' OR @currtype = 'TA' + BEGIN + DECLARE @physical_schema_name sys.nvarchar(776) = ''; + SELECT @physical_schema_name = nspname FROM sys.babelfish_namespace_ext WHERE dbid = sys.db_id() AND orig_name = @schemaname; + SELECT @curr_relname = relname FROM pg_catalog.pg_trigger tr LEFT JOIN pg_catalog.pg_class c ON tr.tgrelid = c.oid LEFT JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid + WHERE tr.tgname = @subname AND n.nspname = @physical_schema_name; + END + END + ELSE + BEGIN + THROW 33557097, N'Provided @objtype is not currently supported in Babelfish', 1; + END + EXEC sys.babelfish_sp_rename_internal @subname, @newname, @schemaname, @currtype, @curr_relname; + PRINT 'Caution: Changing any part of an object name could break scripts and stored procedures.'; + END +END; +$$; +GRANT EXECUTE on PROCEDURE sys.sp_rename(IN sys.nvarchar(776), IN sys.SYSNAME, IN sys.varchar(13)) TO PUBLIC; + CREATE OR REPLACE VIEW sys.sp_pkeys_view AS SELECT CAST(t4."TABLE_CATALOG" AS sys.sysname) AS TABLE_QUALIFIER, @@ -178,7 +2048,7 @@ DECLARE exception_message text; BEGIN -- Rename parsename for dependencies - ALTER FUNCTION sys.parsename(sys.VARCHAR, INT) RENAME TO parsename_deprecated_in_3_5_0; + ALTER FUNCTION sys.parsename(sys.VARCHAR, INT) RENAME TO parsename_deprecated_in_3_5_0_0; EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -192,7 +2062,7 @@ DECLARE exception_message text; BEGIN -- Rename sp_set_session_context for dependencies - ALTER PROCEDURE sys.sp_set_session_context(sys.SYSNAME, sys.SQL_VARIANT, sys.BIT) RENAME TO sp_set_session_context_deprecated_in_3_5_0; + ALTER PROCEDURE sys.sp_set_session_context(sys.SYSNAME, sys.SQL_VARIANT, sys.BIT) RENAME TO sp_set_session_context_deprecated_in_3_5_0_0; EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -206,7 +2076,7 @@ DECLARE exception_message text; BEGIN -- Rename session_context for dependencies - ALTER FUNCTION sys.session_context(sys.SYSNAME) RENAME TO session_context_deprecated_in_3_5_0; + ALTER FUNCTION sys.session_context(sys.SYSNAME) RENAME TO session_context_deprecated_in_3_5_0_0; EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -243,8 +2113,8 @@ DO $$ DECLARE exception_message text; BEGIN - -- === DROP parsename_deprecated_in_3_5_0 - CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'parsename_deprecated_in_3_5_0'); + -- === DROP parsename_deprecated_in_3_5_0_0 + CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'parsename_deprecated_in_3_5_0_0'); EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -257,8 +2127,8 @@ DO $$ DECLARE exception_message text; BEGIN - -- === DROP sp_set_session_context_deprecated_in_3_5_0 - CALL sys.babelfish_drop_deprecated_object('procedure', 'sys', 'sp_set_session_context_deprecated_in_3_5_0'); + -- === DROP sp_set_session_context_deprecated_in_3_5_0_0 + CALL sys.babelfish_drop_deprecated_object('procedure', 'sys', 'sp_set_session_context_deprecated_in_3_5_0_0'); EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -271,8 +2141,8 @@ DO $$ DECLARE exception_message text; BEGIN - -- === DROP session_context_deprecated_in_3_5_0 - CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'session_context_deprecated_in_3_5_0'); + -- === DROP session_context_deprecated_in_3_5_0_0 + CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'session_context_deprecated_in_3_5_0_0'); EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -331,6 +2201,23 @@ AS SELECT WHERE FALSE; GRANT SELECT ON sys.availability_groups TO PUBLIC; +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'sysforeignkeys_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'system_objects_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'syscolumns_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'dm_exec_connections_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'xml_indexes_connections_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'stats__deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'data_spaces_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'sysprocesses_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'key_constraints_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'foreign_keys_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'views_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'tables_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'default_constraints_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'check_constraints_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'types_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'table_types_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'systypes_deprecated_3_5_0'); -- Drops the temporary procedure used by the upgrade script. -- Please have this be one of the last statements executed in this upgrade script. DROP PROCEDURE sys.babelfish_drop_deprecated_object(varchar, varchar, varchar); diff --git a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.0.0--4.1.0.sql b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.0.0--4.1.0.sql index c76214e5a9d..7da8e472389 100644 --- a/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.0.0--4.1.0.sql +++ b/contrib/babelfishpg_tsql/sql/upgrades/babelfishpg_tsql--4.0.0--4.1.0.sql @@ -152,6 +152,1876 @@ EXCEPTION WHEN OTHERS THEN END; $$; + +ALTER VIEW sys.sysforeignkeys RENAME TO sysforeignkeys_deprecated_3_5_0; + +create or replace view sys.sysforeignkeys as +select + CAST(c.oid as int) as constid + , CAST(c.conrelid as int) as fkeyid + , CAST(c.confrelid as int) as rkeyid + , a_con.attnum as fkey + , a_conf.attnum as rkey + , a_conf.attnum as keyno +from pg_constraint c +inner join pg_attribute a_con on a_con.attrelid = c.conrelid and a_con.attnum = any(c.conkey) +inner join pg_attribute a_conf on a_conf.attrelid = c.confrelid and a_conf.attnum = any(c.confkey) +where c.contype = 'f' +and (c.connamespace in (select schema_id from sys.schemas)) +and has_schema_privilege(c.connamespace, 'USAGE'); +GRANT SELECT ON sys.sysforeignkeys TO PUBLIC; + +ALTER VIEW sys.system_objects RENAME TO system_objects_deprecated_3_5_0; + +create or replace view sys.system_objects as +select + name, object_id, principal_id, schema_id, + parent_object_id, type, type_desc, create_date, + modify_date, is_ms_shipped, is_published, is_schema_published + from sys.all_objects o +inner join pg_namespace s on s.oid = o.schema_id +where s.nspname = 'sys'; +GRANT SELECT ON sys.system_objects TO PUBLIC; + +ALTER VIEW sys.syscolumns RENAME TO syscolumns_deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.syscolumns AS +SELECT out_name as name + , out_object_id as id + , out_system_type_id as xtype + , 0::sys.tinyint as typestat + , (case when out_user_type_id < 32767 then out_user_type_id else null end)::smallint as xusertype + , out_max_length as length + , 0::sys.tinyint as xprec + , 0::sys.tinyint as xscale + , out_column_id::smallint as colid + , 0::smallint as xoffset + , 0::sys.tinyint as bitpos + , 0::sys.tinyint as reserved + , 0::smallint as colstat + , out_default_object_id::int as cdefault + , out_rule_object_id::int as domain + , 0::smallint as number + , 0::smallint as colorder + , null::sys.varbinary(8000) as autoval + , out_offset as offset + , out_collation_id as collationid + , (case out_is_nullable::int when 1 then 8 else 0 end + + case out_is_identity::int when 1 then 128 else 0 end)::sys.tinyint as status + , out_system_type_id as type + , (case when out_user_type_id < 32767 then out_user_type_id else null end)::smallint as usertype + , null::sys.varchar(255) as printfmt + , out_precision::smallint as prec + , out_scale::int as scale + , out_is_computed::int as iscomputed + , 0::int as isoutparam + , out_is_nullable::int as isnullable + , out_collation_name::sys.sysname as collation +FROM sys.columns_internal() +union all +SELECT p.name + , p.id + , p.xtype + , 0::sys.tinyint as typestat + , (case when p.xtype < 32767 then p.xtype else null end)::smallint as xusertype + , null as length + , 0::sys.tinyint as xprec + , 0::sys.tinyint as xscale + , p.colid + , 0::smallint as xoffset + , 0::sys.tinyint as bitpos + , 0::sys.tinyint as reserved + , 0::smallint as colstat + , null::int as cdefault + , null::int as domain + , 0::smallint as number + , 0::smallint as colorder + , null::sys.varbinary(8000) as autoval + , 0::smallint as offset + , collationid + , (case p.isoutparam when 1 then 64 else 0 end)::sys.tinyint as status + , p.xtype type + , (case when p.xtype < 32767 then p.xtype else null end)::smallint as usertype + , null::varchar(255) as printfmt + , p.prec + , p.scale + , 0::int as iscomputed + , p.isoutparam + , 1::int as isnullable + , p.collation +FROM sys.proc_param_helper() as p; +GRANT SELECT ON sys.syscolumns TO PUBLIC; + +ALTER VIEW sys.dm_exec_connections RENAME TO dm_exec_connections_deprecated_3_5_0; + +create or replace view sys.dm_exec_connections + as + select a.pid as session_id + , a.pid as most_recent_session_id + , a.backend_start::sys.datetime as connect_time + , 'TCP'::sys.nvarchar(40) as net_transport + , 'TSQL'::sys.nvarchar(40) as protocol_type + , d.protocol_version as protocol_version + , CAST(4 as int) as endpoint_id + , d.encrypyt_option::sys.nvarchar(40) as encrypt_option + , null::sys.nvarchar(40) as auth_scheme + , null::smallint as node_affinity + , null::int as num_reads + , null::int as num_writes + , null::sys.datetime as last_read + , null::sys.datetime as last_write + , d.packet_size as net_packet_size + , a.client_addr::sys.varchar(48) as client_net_address + , a.client_port as client_tcp_port + , null::sys.varchar(48) as local_net_address + , null::int as local_tcp_port + , null::sys.uniqueidentifier as connection_id + , null::sys.uniqueidentifier as parent_connection_id + , a.pid::sys.varbinary(64) as most_recent_sql_handle + from pg_catalog.pg_stat_activity AS a + RIGHT JOIN sys.tsql_stat_get_activity('connections') AS d ON (a.pid = d.procid); + GRANT SELECT ON sys.dm_exec_connections TO PUBLIC; + +ALTER VIEW sys.xml_indexes RENAME TO xml_indexes_connections_deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.xml_indexes +AS +SELECT + CAST(idx.object_id AS INT) AS object_id + , CAST(idx.name AS sys.sysname) AS name + , CAST(idx.index_id AS INT) AS index_id + , CAST(idx.type AS sys.tinyint) AS type + , CAST(idx.type_desc AS sys.nvarchar(60)) AS type_desc + , CAST(idx.is_unique AS sys.bit) AS is_unique + , CAST(idx.data_space_id AS int) AS data_space_id + , CAST(idx.ignore_dup_key AS sys.bit) AS ignore_dup_key + , CAST(idx.is_primary_key AS sys.bit) AS is_primary_key + , CAST(idx.is_unique_constraint AS sys.bit) AS is_unique_constraint + , CAST(idx.fill_factor AS sys.tinyint) AS fill_factor + , CAST(idx.is_padded AS sys.bit) AS is_padded + , CAST(idx.is_disabled AS sys.bit) AS is_disabled + , CAST(idx.is_hypothetical AS sys.bit) AS is_hypothetical + , CAST(idx.allow_row_locks AS sys.bit) AS allow_row_locks + , CAST(idx.allow_page_locks AS sys.bit) AS allow_page_locks + , CAST(idx.has_filter AS sys.bit) AS has_filter + , CAST(idx.filter_definition AS sys.nvarchar(4000)) AS filter_definition + , CAST(idx.auto_created AS sys.bit) AS auto_created + , CAST(NULL AS INT) AS using_xml_index_id + , CAST(NULL AS sys.bpchar(1)) AS secondary_type + , CAST(NULL AS sys.nvarchar(60)) AS secondary_type_desc + , CAST(0 AS sys.tinyint) AS xml_index_type + , CAST(NULL AS sys.nvarchar(60)) AS xml_index_type_description + , CAST(NULL AS INT) AS path_id +FROM sys.indexes idx +WHERE idx.type = 3; -- 3 is of type XML +GRANT SELECT ON sys.xml_indexes TO PUBLIC; + +ALTER VIEW sys.stats RENAME TO stats__deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.stats +AS +SELECT + CAST(0 as INT) AS object_id, + CAST('' as SYSNAME) AS name, + CAST(0 as INT) AS stats_id, + CAST(0 as sys.BIT) AS auto_created, + CAST(0 as sys.BIT) AS user_created, + CAST(0 as sys.BIT) AS no_recompute, + CAST(0 as sys.BIT) AS has_filter, + CAST('' as sys.NVARCHAR(4000)) AS filter_definition, + CAST(0 as sys.BIT) AS is_temporary, + CAST(0 as sys.BIT) AS is_incremental, + CAST(0 as sys.BIT) AS has_persisted_sample, + CAST(0 as INT) AS stats_generation_method, + CAST('' as sys.VARCHAR(255)) AS stats_generation_method_desc +WHERE FALSE; +GRANT SELECT ON sys.stats TO PUBLIC; + +ALTER VIEW sys.data_spaces RENAME TO data_spaces_deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.data_spaces +AS +SELECT + CAST('PRIMARY' as SYSNAME) AS name, + CAST(1 as INT) AS data_space_id, + CAST('FG' as sys.bpchar(2)) AS type, + CAST('ROWS_FILEGROUP' as NVARCHAR(60)) AS type_desc, + CAST(1 as sys.BIT) AS is_default, + CAST(0 as sys.BIT) AS is_system; +GRANT SELECT ON sys.data_spaces TO PUBLIC; + +CREATE OR REPLACE VIEW sys.filegroups +AS +SELECT + CAST(ds.name AS sys.SYSNAME), + CAST(ds.data_space_id AS INT), + CAST(ds.type AS sys.BPCHAR(2)) COLLATE sys.database_default, + CAST(ds.type_desc AS sys.NVARCHAR(60)), + CAST(ds.is_default AS sys.BIT), + CAST(ds.is_system AS sys.BIT), + CAST(NULL as sys.UNIQUEIDENTIFIER) AS filegroup_guid, + CAST(0 as INT) AS log_filegroup_id, + CAST(0 as sys.BIT) AS is_read_only, + CAST(0 as sys.BIT) AS is_autogrow_all_files +FROM sys.data_spaces ds WHERE type = 'FG'; +GRANT SELECT ON sys.filegroups TO PUBLIC; + +ALTER VIEW sys.sysprocesses RENAME TO sysprocesses_deprecated_3_5_0; + +create or replace view sys.sysprocesses as +select + a.pid as spid + , null::smallint as kpid + , coalesce(blocking_activity.pid, 0) as blocked + , null::sys.binary(2) as waittype + , 0::bigint as waittime + , CAST(a.wait_event_type as sys.nchar(32)) as lastwaittype + , null::sys.nchar(256) as waitresource + , coalesce(t.database_id, 0)::int as dbid + , a.usesysid as uid + , 0::int as cpu + , 0::bigint as physical_io + , 0::int as memusage + , cast(a.backend_start as sys.datetime) as login_time + , cast(a.query_start as sys.datetime) as last_batch + , 0::smallint as ecid + , 0::smallint as open_tran + , CAST(a.state as sys.nchar(30)) as status + , null::sys.binary(86) as sid + , CAST(t.host_name AS sys.nchar(128)) as hostname + , CAST(a.application_name as sys.nchar(128)) as program_name + , t.client_pid::sys.nchar(10) as hostprocess + , CAST(a.query as sys.nchar(52)) as cmd + , null::sys.nchar(128) as nt_domain + , null::sys.nchar(128) as nt_username + , null::sys.nchar(12) as net_address + , null::sys.nchar(12) as net_library + , CAST(a.usename as sys.nchar(128)) as loginname + , t.context_info as context_info + , null::sys.binary(20) as sql_handle + , 0::int as stmt_start + , 0::int as stmt_end + , 0::int as request_id +from pg_stat_activity a +left join sys.tsql_stat_get_activity('sessions') as t on a.pid = t.procid +left join pg_catalog.pg_locks as blocked_locks on a.pid = blocked_locks.pid +left join pg_catalog.pg_locks blocking_locks + ON blocking_locks.locktype = blocked_locks.locktype + AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE + AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation + AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page + AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple + AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid + AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid + AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid + AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid + AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid + AND blocking_locks.pid != blocked_locks.pid + left join pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid + where a.datname = current_database(); /* current physical database will always be babelfish database */ +GRANT SELECT ON sys.sysprocesses TO PUBLIC; + +CREATE OR REPLACE PROCEDURE sys.sp_who( + IN "@loginame" sys.sysname DEFAULT NULL, + IN "@option" sys.VARCHAR(30) DEFAULT NULL) +LANGUAGE 'pltsql' +AS $$ +BEGIN + SET NOCOUNT ON + DECLARE @msg sys.VARCHAR(200) + DECLARE @show_pg BIT = 0 + DECLARE @hide_col sys.VARCHAR(50) + + IF @option IS NOT NULL + BEGIN + IF LOWER(TRIM(@option)) <> 'postgres' + BEGIN + RAISERROR('Parameter @option can only be ''postgres''', 16, 1) + RETURN + END + END + + -- Take a copy of sysprocesses so that we reference it only once + SELECT DISTINCT * INTO #sp_who_sysprocesses FROM sys.sysprocesses + + -- Get the executing statement for each spid and extract the main stmt type + -- This is for informational purposes only + SELECT pid, query INTO #sp_who_tmp FROM pg_stat_activity pgsa + + UPDATE #sp_who_tmp SET query = ' ' + TRIM(UPPER(query)) + UPDATE #sp_who_tmp SET query = sys.REPLACE(query, chr(9), ' ') + UPDATE #sp_who_tmp SET query = sys.REPLACE(query, chr(10), ' ') + UPDATE #sp_who_tmp SET query = sys.REPLACE(query, chr(13), ' ') + WHILE (SELECT count(*) FROM #sp_who_tmp WHERE sys.CHARINDEX(' ',query)>0) > 0 + BEGIN + UPDATE #sp_who_tmp SET query = sys.REPLACE(query, ' ', ' ') + END + + -- Determine type of stmt to report by sp_who: very basic only + -- NB: not handling presence of comments in the query string + UPDATE #sp_who_tmp + SET query = + CASE + WHEN PATINDEX('%[^a-zA-Z0-9_]UPDATE[^a-zA-Z0-9_]%', query) > 0 THEN 'UPDATE' + WHEN PATINDEX('%[^a-zA-Z0-9_]DELETE[^a-zA-Z0-9_]%', query) > 0 THEN 'DELETE' + WHEN PATINDEX('%[^a-zA-Z0-9_]INSERT[^a-zA-Z0-9_]%', query) > 0 THEN 'INSERT' + WHEN PATINDEX('%[^a-zA-Z0-9_]SELECT[^a-zA-Z0-9_]%', query) > 0 THEN 'SELECT' + WHEN PATINDEX('%[^a-zA-Z0-9_]WAITFOR[^a-zA-Z0-9_]%', query) > 0 THEN 'WAITFOR' + WHEN PATINDEX('%[^a-zA-Z0-9_]CREATE ]%', query) > 0 THEN sys.SUBSTRING(query,1,sys.CHARINDEX('CREATE ', query)) + WHEN PATINDEX('%[^a-zA-Z0-9_]ALTER ]%', query) > 0 THEN sys.SUBSTRING(query,1,sys.CHARINDEX('ALTER ', query)) + WHEN PATINDEX('%[^a-zA-Z0-9_]DROP ]%', query) > 0 THEN sys.SUBSTRING(query,1,sys.CHARINDEX('DROP ', query)) + ELSE sys.SUBSTRING(query, 1, sys.CHARINDEX(' ', query)) + END + + UPDATE #sp_who_tmp + SET query = sys.SUBSTRING(query,1, 8-1 + sys.CHARINDEX(' ', sys.SUBSTRING(query,8,99))) + WHERE query LIKE 'CREATE %' OR query LIKE 'ALTER %' OR query LIKE 'DROP %' + + -- The executing spid is always shown as doing a SELECT + UPDATE #sp_who_tmp SET query = 'SELECT' WHERE pid = @@spid + UPDATE #sp_who_tmp SET query = TRIM(query) + + -- Get all current connections + SELECT + spid, + MAX(blocked) AS blocked, + 0 AS ecid, + CAST('' AS sys.VARCHAR(100)) AS status, + CAST('' AS sys.VARCHAR(100)) AS loginname, + CAST('' AS sys.VARCHAR(100)) AS hostname, + 0 AS dbid, + CAST('' AS sys.VARCHAR(100)) AS cmd, + 0 AS request_id, + CAST('TDS' AS sys.VARCHAR(20)) AS connection, + hostprocess + INTO #sp_who_proc + FROM #sp_who_sysprocesses + GROUP BY spid, status, hostprocess + + -- Add attributes to each connection + UPDATE #sp_who_proc + SET ecid = sp.ecid, + status = sp.status, + loginname = sp.loginname, + hostname = sp.hostname, + dbid = sp.dbid, + request_id = sp.request_id + FROM #sp_who_sysprocesses sp + WHERE #sp_who_proc.spid = sp.spid + + -- Identify PG connections: the hostprocess PID comes from the TDS login packet + -- and therefore PG connections do not have a value here + UPDATE #sp_who_proc + SET connection = 'PostgreSQL' + WHERE hostprocess IS NULL + + -- Keep or delete PG connections + IF (LOWER(@loginame) = 'postgres' OR LOWER(@option) = 'postgres') + begin + -- Show PG connections; these have dbid = 0 + -- This is a Babelfish-specific enhancement, since PG connections may also be active in the Babelfish DB + -- and it may be useful to see these displayed + SET @show_pg = 1 + + -- blank out the loginame parameter for the tests below + IF LOWER(@loginame) = 'postgres' SET @loginame = NULL + END + + -- By default, do not show the column indicating the connection type since SQL Server does not have this column + SET @hide_col = 'connection' + + IF (@show_pg = 1) + BEGIN + SET @hide_col = '' + END + ELSE + BEGIN + -- Delete PG connections + DELETE #sp_who_proc + WHERE dbid = 0 + END + + -- Apply filter if specified + IF (@loginame IS NOT NULL) + BEGIN + IF (TRIM(@loginame) = '') + BEGIN + -- Raise error + SET @msg = ''''+@loginame+''' is not a valid login or you do not have permission.' + RAISERROR(@msg, 16, 1) + RETURN + END + + IF (sys.ISNUMERIC(@loginame) = 1) + BEGIN + -- Remove all connections except the specified one + DELETE #sp_who_proc + WHERE spid <> CAST(@loginame AS INT) + END + ELSE + BEGIN + IF (LOWER(@loginame) = 'active') + BEGIN + -- Remove all 'idle' connections + DELETE #sp_who_proc + WHERE status = 'idle' + END + ELSE + BEGIN + -- Verify the specified login name exists + IF (sys.SUSER_ID(@loginame) IS NULL) + BEGIN + SET @msg = ''''+@loginame+''' is not a valid login or you do not have permission.' + RAISERROR(@msg, 16, 1) + RETURN + END + ELSE + BEGIN + -- Keep only connections for the specified login + DELETE #sp_who_proc + WHERE sys.SUSER_ID(loginname) <> sys.SUSER_ID(@loginame) + END + END + END + END + + -- Create final result set; use DISTINCT since there are usually duplicate rows from the PG catalogs + SELECT distinct + p.spid AS spid, + p.ecid AS ecid, + CAST(LEFT(p.status,20) AS sys.VARCHAR(20)) AS status, + CAST(LEFT(p.loginname,40) AS sys.VARCHAR(40)) AS loginame, + CAST(LEFT(p.hostname,60) AS sys.VARCHAR(60)) AS hostname, + p.blocked AS blk, + CAST(LEFT(db_name(p.dbid),40) AS sys.VARCHAR(40)) AS dbname, + CAST(LEFT(#sp_who_tmp.query,30)as sys.VARCHAR(30)) AS cmd, + p.request_id AS request_id, + connection + INTO #sp_who_tmp2 + FROM #sp_who_proc p, #sp_who_tmp + WHERE p.spid = #sp_who_tmp.pid + ORDER BY spid + + -- Patch up remaining cases + UPDATE #sp_who_tmp2 + SET cmd = 'AWAITING COMMAND' + WHERE TRIM(ISNULL(cmd,'')) = '' AND status = 'idle' + + UPDATE #sp_who_tmp2 + SET cmd = 'UNKNOWN' + WHERE TRIM(cmd) = '' + + -- Format the result set as narrow as possible for readability + SET @hide_col += ',hostprocess' + EXECUTE sys.sp_babelfish_autoformat @tab='#sp_who_tmp2', @orderby='ORDER BY spid', @hiddencols=@hide_col, @printrc=0 + RETURN +END +$$; +GRANT EXECUTE ON PROCEDURE sys.sp_who(IN sys.sysname, IN sys.VARCHAR(30)) TO PUBLIC; + +ALTER VIEW sys.foreign_keys RENAME TO foreign_keys_deprecated_3_5_0; + +CREATE OR replace view sys.foreign_keys AS +SELECT + CAST(c.conname AS sys.SYSNAME) AS name +, CAST(c.oid AS INT) AS object_id +, CAST(NULL AS INT) AS principal_id +, CAST(sch.schema_id AS INT) AS schema_id +, CAST(c.conrelid AS INT) AS parent_object_id +, CAST('F' AS sys.bpchar(2)) AS type +, CAST('FOREIGN_KEY_CONSTRAINT' AS NVARCHAR(60)) AS type_desc +, CAST(NULL AS sys.DATETIME) AS create_date +, CAST(NULL AS sys.DATETIME) AS modify_date +, CAST(0 AS sys.BIT) AS is_ms_shipped +, CAST(0 AS sys.BIT) AS is_published +, CAST(0 AS sys.BIT) as is_schema_published +, CAST(c.confrelid AS INT) AS referenced_object_id +, CAST(c.conindid AS INT) AS key_index_id +, CAST(0 AS sys.BIT) AS is_disabled +, CAST(0 AS sys.BIT) AS is_not_for_replication +, CAST(0 AS sys.BIT) AS is_not_trusted +, CAST( + (CASE c.confdeltype + WHEN 'a' THEN 0 + WHEN 'r' THEN 0 + WHEN 'c' THEN 1 + WHEN 'n' THEN 2 + WHEN 'd' THEN 3 + END) + AS sys.TINYINT) AS delete_referential_action +, CAST( + (CASE c.confdeltype + WHEN 'a' THEN 'NO_ACTION' + WHEN 'r' THEN 'NO_ACTION' + WHEN 'c' THEN 'CASCADE' + WHEN 'n' THEN 'SET_NULL' + WHEN 'd' THEN 'SET_DEFAULT' + END) + AS sys.NVARCHAR(60)) AS delete_referential_action_desc +, CAST( + (CASE c.confupdtype + WHEN 'a' THEN 0 + WHEN 'r' THEN 0 + WHEN 'c' THEN 1 + WHEN 'n' THEN 2 + WHEN 'd' THEN 3 + END) + AS sys.TINYINT) AS update_referential_action +, CAST( + (CASE c.confupdtype + WHEN 'a' THEN 'NO_ACTION' + WHEN 'r' THEN 'NO_ACTION' + WHEN 'c' THEN 'CASCADE' + WHEN 'n' THEN 'SET_NULL' + WHEN 'd' THEN 'SET_DEFAULT' + END) + AS sys.NVARCHAR(60)) update_referential_action_desc +, CAST(1 AS sys.BIT) AS is_system_named +FROM pg_constraint c +INNER JOIN sys.schemas sch ON sch.schema_id = c.connamespace +WHERE has_schema_privilege(sch.schema_id, 'USAGE') +AND c.contype = 'f'; +GRANT SELECT ON sys.foreign_keys TO PUBLIC; + +ALTER VIEW sys.key_constraints RENAME TO key_constraints_deprecated_3_5_0; + +CREATE OR replace view sys.key_constraints AS +SELECT + CAST(c.conname AS SYSNAME) AS name + , CAST(c.oid AS INT) AS object_id + , CAST(0 AS INT) AS principal_id + , CAST(sch.schema_id AS INT) AS schema_id + , CAST(c.conrelid AS INT) AS parent_object_id + , CAST( + (CASE contype + WHEN 'p' THEN CAST('PK' as sys.bpchar(2)) + WHEN 'u' THEN CAST('UQ' as sys.bpchar(2)) + END) + AS sys.bpchar(2)) AS type + , CAST( + (CASE contype + WHEN 'p' THEN 'PRIMARY_KEY_CONSTRAINT' + WHEN 'u' THEN 'UNIQUE_CONSTRAINT' + END) + AS NVARCHAR(60)) AS type_desc + , CAST(NULL AS DATETIME) AS create_date + , CAST(NULL AS DATETIME) AS modify_date + , CAST(c.conindid AS INT) AS unique_index_id + , CAST(0 AS sys.BIT) AS is_ms_shipped + , CAST(0 AS sys.BIT) AS is_published + , CAST(0 AS sys.BIT) AS is_schema_published + , CAST(1 as sys.BIT) as is_system_named +FROM pg_constraint c +INNER JOIN sys.schemas sch ON sch.schema_id = c.connamespace +WHERE has_schema_privilege(sch.schema_id, 'USAGE') +AND c.contype IN ('p', 'u'); +GRANT SELECT ON sys.key_constraints TO PUBLIC; + +ALTER VIEW sys.views RENAME TO views_deprecated_3_5_0; + +create or replace view sys.views as +select + CAST(t.relname as sys.sysname) as name + , t.oid::int as object_id + , null::integer as principal_id + , sch.schema_id::int as schema_id + , 0 as parent_object_id + , 'V'::sys.bpchar(2) as type + , 'VIEW'::sys.nvarchar(60) as type_desc + , vd.create_date::sys.datetime as create_date + , vd.create_date::sys.datetime as modify_date + , CAST(0 as sys.BIT) as is_ms_shipped + , CAST(0 as sys.BIT) as is_published + , CAST(0 as sys.BIT) as is_schema_published + , CAST(0 as sys.BIT) as with_check_option + , CAST(0 as sys.BIT) as is_date_correlation_view + , CAST(0 as sys.BIT) as is_tracked_by_cdc +from pg_class t inner join sys.schemas sch on (t.relnamespace = sch.schema_id) +left join sys.shipped_objects_not_in_sys nis on (nis.name = t.relname and nis.schemaid = sch.schema_id and nis.type = 'V') +left outer join sys.babelfish_view_def vd on t.relname::sys.sysname = vd.object_name and sch.name = vd.schema_name and vd.dbid = sys.db_id() +where t.relkind = 'v' +and nis.name is null +and has_schema_privilege(sch.schema_id, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER'); +GRANT SELECT ON sys.views TO PUBLIC; + +ALTER VIEW sys.tables RENAME TO tables_deprecated_3_5_0; + +create or replace view sys.tables as +select + CAST(t.relname as sys._ci_sysname) as name + , CAST(t.oid as int) as object_id + , CAST(NULL as int) as principal_id + , CAST(t.relnamespace as int) as schema_id + , 0 as parent_object_id + , CAST('U' as sys.bpchar(2)) as type + , CAST('USER_TABLE' as sys.nvarchar(60)) as type_desc + , CAST((select string_agg( + case + when option like 'bbf_rel_create_date=%%' then substring(option, 21) + else NULL + end, ',') + from unnest(t.reloptions) as option) + as sys.datetime) as create_date + , CAST((select string_agg( + case + when option like 'bbf_rel_create_date=%%' then substring(option, 21) + else NULL + end, ',') + from unnest(t.reloptions) as option) + as sys.datetime) as modify_date + , CAST(0 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published + , case reltoastrelid when 0 then 0 else 1 end as lob_data_space_id + , CAST(NULL as int) as filestream_data_space_id + , CAST(relnatts as int) as max_column_id_used + , CAST(0 as sys.bit) as lock_on_bulk_load + , CAST(1 as sys.bit) as uses_ansi_nulls + , CAST(0 as sys.bit) as is_replicated + , CAST(0 as sys.bit) as has_replication_filter + , CAST(0 as sys.bit) as is_merge_published + , CAST(0 as sys.bit) as is_sync_tran_subscribed + , CAST(0 as sys.bit) as has_unchecked_assembly_data + , 0 as text_in_row_limit + , CAST(0 as sys.bit) as large_value_types_out_of_row + , CAST(0 as sys.bit) as is_tracked_by_cdc + , CAST(0 as sys.tinyint) as lock_escalation + , CAST('TABLE' as sys.nvarchar(60)) as lock_escalation_desc + , CAST(0 as sys.bit) as is_filetable + , CAST(0 as sys.tinyint) as durability + , CAST('SCHEMA_AND_DATA' as sys.nvarchar(60)) as durability_desc + , CAST(0 as sys.bit) is_memory_optimized + , case relpersistence when 't' then CAST(2 as sys.tinyint) else CAST(0 as sys.tinyint) end as temporal_type + , case relpersistence when 't' then CAST('SYSTEM_VERSIONED_TEMPORAL_TABLE' as sys.nvarchar(60)) else CAST('NON_TEMPORAL_TABLE' as sys.nvarchar(60)) end as temporal_type_desc + , CAST(null as integer) as history_table_id + , CAST(0 as sys.bit) as is_remote_data_archive_enabled + , CAST(0 as sys.bit) as is_external +from pg_class t +inner join sys.schemas sch on sch.schema_id = t.relnamespace +left join sys.table_types_internal tt on t.oid = tt.typrelid +where tt.typrelid is null +and t.relkind = 'r' +and has_schema_privilege(t.relnamespace, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER'); +GRANT SELECT ON sys.tables TO PUBLIC; + +ALTER VIEW sys.default_constraints RENAME TO default_constraints_deprecated_3_5_0; + +create or replace view sys.default_constraints +AS +select CAST(('DF_' || tab.name || '_' || d.oid) as sys.sysname) as name + , CAST(d.oid as int) as object_id + , CAST(null as int) as principal_id + , CAST(tab.schema_id as int) as schema_id + , CAST(d.adrelid as int) as parent_object_id + , CAST('D' as sys.bpchar(2)) as type + , CAST('DEFAULT_CONSTRAINT' as sys.nvarchar(60)) AS type_desc + , CAST(null as sys.datetime) as create_date + , CAST(null as sys.datetime) as modified_date + , CAST(0 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published + , CAST(d.adnum as int) as parent_column_id + , CAST(tsql_get_expr(d.adbin, d.adrelid) as sys.nvarchar(4000)) as definition + , CAST(1 as sys.bit) as is_system_named +from pg_catalog.pg_attrdef as d +inner join pg_attribute a on a.attrelid = d.adrelid and d.adnum = a.attnum +inner join sys.tables tab on d.adrelid = tab.object_id +WHERE a.atthasdef = 't' and a.attgenerated = '' +AND has_schema_privilege(tab.schema_id, 'USAGE') +AND has_column_privilege(a.attrelid, a.attname, 'SELECT,INSERT,UPDATE,REFERENCES'); +GRANT SELECT ON sys.default_constraints TO PUBLIC; + +ALTER VIEW sys.check_constraints RENAME TO check_constraints_deprecated_3_5_0; + +CREATE or replace VIEW sys.check_constraints AS +SELECT CAST(c.conname as sys.sysname) as name + , CAST(oid as integer) as object_id + , CAST(NULL as integer) as principal_id + , CAST(c.connamespace as integer) as schema_id + , CAST(conrelid as integer) as parent_object_id + , CAST('C' as sys.bpchar(2)) as type + , CAST('CHECK_CONSTRAINT' as sys.nvarchar(60)) as type_desc + , CAST(null as sys.datetime) as create_date + , CAST(null as sys.datetime) as modify_date + , CAST(0 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published + , CAST(0 as sys.bit) as is_disabled + , CAST(0 as sys.bit) as is_not_for_replication + , CAST(0 as sys.bit) as is_not_trusted + , CAST(c.conkey[1] as integer) AS parent_column_id + , CAST(tsql_get_constraintdef(c.oid) as sys.nvarchar(4000)) AS definition + , CAST(1 as sys.bit) as uses_database_collation + , CAST(0 as sys.bit) as is_system_named +FROM pg_catalog.pg_constraint as c +INNER JOIN sys.schemas s on c.connamespace = s.schema_id +WHERE has_schema_privilege(s.schema_id, 'USAGE') +AND c.contype = 'c' and c.conrelid != 0; +GRANT SELECT ON sys.check_constraints TO PUBLIC; + +ALTER VIEW sys.types RENAME TO types_deprecated_3_5_0; + +create or replace view sys.types As +-- For System types +select + CAST(tsql_type_name as sys.sysname) as name + , cast(t.oid as int) as system_type_id + , cast(t.oid as int) as user_type_id + , cast(s.oid as int) as schema_id + , cast(NULL as INT) as principal_id + , sys.tsql_type_max_length_helper(tsql_type_name, t.typlen, t.typtypmod, true) as max_length + , sys.tsql_type_precision_helper(tsql_type_name, t.typtypmod) as precision + , sys.tsql_type_scale_helper(tsql_type_name, t.typtypmod, false) as scale + , CASE c.collname + WHEN 'default' THEN default_collation_name + ELSE CAST(c.collname as sys.sysname) + END as collation_name + , case when typnotnull then cast(0 as sys.bit) else cast(1 as sys.bit) end as is_nullable + , CAST(0 as sys.bit) as is_user_defined + , CAST(0 as sys.bit) as is_assembly_type + , CAST(0 as int) as default_object_id + , CAST(0 as int) as rule_object_id + , CAST(0 as sys.bit) as is_table_type +from pg_type t +inner join pg_namespace s on s.oid = t.typnamespace +left join pg_collation c on c.oid = t.typcollation +, sys.translate_pg_type_to_tsql(t.oid) AS tsql_type_name +,cast(current_setting('babelfishpg_tsql.server_collation_name') as sys.sysname) as default_collation_name +where +tsql_type_name IS NOT NULL +and pg_type_is_visible(t.oid) +and (s.nspname = 'pg_catalog' OR s.nspname = 'sys') +union all +-- For User Defined Types +select cast(t.typname as sys.sysname) as name + , cast(t.typbasetype as int) as system_type_id + , cast(t.oid as int) as user_type_id + , cast(t.typnamespace as int) as schema_id + , null::integer as principal_id + , case when tt.typrelid is not null then -1::smallint else sys.tsql_type_max_length_helper(tsql_base_type_name, t.typlen, t.typtypmod) end as max_length + , case when tt.typrelid is not null then 0::sys.tinyint else sys.tsql_type_precision_helper(tsql_base_type_name, t.typtypmod) end as precision + , case when tt.typrelid is not null then 0::sys.tinyint else sys.tsql_type_scale_helper(tsql_base_type_name, t.typtypmod, false) end as scale + , CASE c.collname + WHEN 'default' THEN default_collation_name + ELSE CAST(c.collname as sys.sysname) + END as collation_name + , case when tt.typrelid is not null then cast(0 as sys.bit) + else case when typnotnull then cast(0 as sys.bit) else cast(1 as sys.bit) end + end + as is_nullable + -- CREATE TYPE ... FROM is implemented as CREATE DOMAIN in babel + , CAST(1 as sys.bit) as is_user_defined + , CAST(0 as sys.bit) as is_assembly_type + , CAST(0 as int) as default_object_id + , CAST(0 as int) as rule_object_id + , case when tt.typrelid is not null then CAST(1 as sys.bit) else CAST(0 as sys.bit) end as is_table_type +from pg_type t +join sys.schemas sch on t.typnamespace = sch.schema_id +left join pg_collation c on c.oid = t.typcollation +left join sys.table_types_internal tt on t.typrelid = tt.typrelid +, sys.translate_pg_type_to_tsql(t.oid) AS tsql_type_name +, sys.translate_pg_type_to_tsql(t.typbasetype) AS tsql_base_type_name +, cast(current_setting('babelfishpg_tsql.server_collation_name') as sys.sysname) as default_collation_name +-- we want to show details of user defined datatypes created under babelfish database +where + tsql_type_name IS NULL +and + ( + -- show all user defined datatypes created under babelfish database except table types + t.typtype = 'd' + or + -- only for table types + tt.typrelid is not null + ); +GRANT SELECT ON sys.types TO PUBLIC; + +CREATE OR REPLACE VIEW sys.sp_special_columns_view AS +SELECT +CAST(1 AS SMALLINT) AS SCOPE, +CAST(coalesce (split_part(a.attoptions[1] COLLATE "C", '=', 2) ,a.attname) AS sys.sysname) AS COLUMN_NAME, -- get original column name if exists +CAST(t6.data_type AS SMALLINT) AS DATA_TYPE, + +CASE -- cases for when they are of type identity. + WHEN a.attidentity <> ''::"char" AND (t1.name = 'decimal' OR t1.name = 'numeric') + THEN CAST(CONCAT(t1.name, '() identity') AS sys.sysname) + WHEN a.attidentity <> ''::"char" AND (t1.name != 'decimal' AND t1.name != 'numeric') + THEN CAST(CONCAT(t1.name, ' identity') AS sys.sysname) + ELSE CAST(t1.name AS sys.sysname) +END AS TYPE_NAME, + +CAST(sys.sp_special_columns_precision_helper(COALESCE(tsql_type_name, tsql_base_type_name), c1.precision, c1.max_length, t6."PRECISION") AS INT) AS PRECISION, +CAST(sys.sp_special_columns_length_helper(coalesce(tsql_type_name, tsql_base_type_name), c1.precision, c1.max_length, t6."PRECISION") AS INT) AS LENGTH, +CAST(sys.sp_special_columns_scale_helper(coalesce(tsql_type_name, tsql_base_type_name), c1.scale) AS SMALLINT) AS SCALE, +CAST(1 AS smallint) AS PSEUDO_COLUMN, +CASE + WHEN a.attnotnull + THEN CAST(0 AS INT) + ELSE CAST(1 AS INT) END +AS IS_NULLABLE, +CAST(nsp_ext.dbname AS sys.sysname) AS TABLE_QUALIFIER, +CAST(s1.name AS sys.sysname) AS TABLE_OWNER, +CAST(C.relname AS sys.sysname) AS TABLE_NAME, + +CASE + WHEN X.indisprimary + THEN CAST('p' AS sys.sysname) + ELSE CAST('u' AS sys.sysname) -- if it is a unique index, then we should cast it as 'u' for filtering purposes +END AS CONSTRAINT_TYPE, +CAST(I.relname AS sys.sysname) CONSTRAINT_NAME, +CAST(X.indexrelid AS int) AS INDEX_ID + +FROM( pg_index X +JOIN pg_class C ON X.indrelid = C.oid +JOIN pg_class I ON I.oid = X.indexrelid +CROSS JOIN LATERAL unnest(X.indkey) AS ak(k) + LEFT JOIN pg_attribute a + ON (a.attrelid = X.indrelid AND a.attnum = ak.k) +) +LEFT JOIN sys.pg_namespace_ext nsp_ext ON C.relnamespace = nsp_ext.oid +LEFT JOIN sys.schemas s1 ON s1.schema_id = C.relnamespace +LEFT JOIN sys.columns c1 ON c1.object_id = X.indrelid AND cast(a.attname AS sys.sysname) = c1.name COLLATE sys.database_default +LEFT JOIN pg_catalog.pg_type AS T ON T.oid = c1.system_type_id +LEFT JOIN sys.types AS t1 ON a.atttypid = t1.user_type_id +LEFT JOIN sys.sp_datatype_info_helper(2::smallint, false) AS t6 ON T.typname = t6.pg_type_name OR T.typname = t6.type_name --need in order to get accurate DATA_TYPE value +, sys.translate_pg_type_to_tsql(t1.user_type_id) AS tsql_type_name +, sys.translate_pg_type_to_tsql(t1.system_type_id) AS tsql_base_type_name +WHERE has_schema_privilege(s1.schema_id, 'USAGE') +AND X.indislive ; + +GRANT SELECT ON sys.sp_special_columns_view TO PUBLIC; + +CREATE OR REPLACE VIEW sys.assembly_types +AS +SELECT + t.name AS name, + -- 'system_type_id' is specified as type INT here, and not TINYINT per SQL Server documentation. + -- This is because the IDs of generated SQL Server system type values generated by B + -- Babelfish installation will exceed the size of TINYINT. + CAST(t.system_type_id as int) AS system_type_id, + CAST(t.user_type_id as int) AS user_type_id, + CAST(t.schema_id as int) AS schema_id, + CAST(t.principal_id as int) AS principal_id, + CAST(t.max_length as smallint) AS max_length, + CAST(t.precision as sys.tinyint) AS precision, + CAST(t.scale as sys.tinyint) AS scale, + t.collation_name AS collation_name, + CAST(t.is_nullable as sys.bit) AS is_nullable, + CAST(t.is_user_defined as sys.bit) AS is_user_defined, + CAST(t.is_assembly_type as sys.bit) AS is_assembly_type, + CAST(t.default_object_id as int) AS default_object_id, + CAST(t.rule_object_id as int) AS rule_object_id, + CAST(NULL as int) AS assembly_id, + CAST(NULL as sys.sysname) AS assembly_class, + CAST(NULL as sys.bit) AS is_binary_ordered, + CAST(NULL as sys.bit) AS is_fixed_length, + CAST(NULL as sys.nvarchar(40)) AS prog_id, + CAST(NULL as sys.nvarchar(4000)) AS assembly_qualified_name, + CAST(t.is_table_type as sys.bit) AS is_table_type +FROM sys.types t +WHERE t.is_assembly_type = 1; +GRANT SELECT ON sys.assembly_types TO PUBLIC; + +ALTER VIEW sys.systypes RENAME TO systypes_deprecated_3_5_0; + +CREATE OR REPLACE VIEW sys.systypes AS +SELECT name + , CAST(system_type_id as int) as xtype + , CAST((case when is_nullable = 1 then 0 else 1 end) as sys.tinyint) as status + , CAST((case when user_type_id < 32767 then user_type_id::int else null end) as smallint) as xusertype + , max_length as length + , CAST(precision as sys.tinyint) as xprec + , CAST(scale as sys.tinyint) as xscale + , CAST(default_object_id as int) as tdefault + , CAST(rule_object_id as int) as domain + , CAST((case when schema_id < 32767 then schema_id::int else null end) as smallint) as uid + , CAST(0 as smallint) as reserved + , CAST(sys.CollationProperty(collation_name, 'CollationId') as int) as collationid + , CAST((case when user_type_id < 32767 then user_type_id::int else null end) as smallint) as usertype + , CAST((case when (coalesce(sys.translate_pg_type_to_tsql(system_type_id), sys.translate_pg_type_to_tsql(user_type_id)) + in ('nvarchar', 'varchar', 'sysname', 'varbinary')) then 1 + else 0 end) as sys.bit) as variable + , CAST(is_nullable as sys.bit) as allownulls + , CAST(system_type_id as int) as type + , CAST(null as sys.varchar(255)) as printfmt + , (case when precision <> 0::sys.tinyint then precision::smallint + else sys.systypes_precision_helper(sys.translate_pg_type_to_tsql(system_type_id), max_length) end) as prec + , CAST(scale as sys.tinyint) as scale + , collation_name as collation +FROM sys.types; +GRANT SELECT ON sys.systypes TO PUBLIC; + +CREATE OR REPLACE PROCEDURE sys.sp_babelfish_autoformat( + IN "@tab" sys.VARCHAR(257) DEFAULT NULL, + IN "@orderby" sys.VARCHAR(1000) DEFAULT '', + IN "@printrc" sys.bit DEFAULT 1, + IN "@hiddencols" sys.VARCHAR(1000) DEFAULT NULL) +LANGUAGE 'pltsql' +AS $$ +BEGIN + SET NOCOUNT ON + DECLARE @rc INT + DECLARE @id INT + DECLARE @objtype sys.VARCHAR(2) + DECLARE @msg sys.VARCHAR(200) + + IF @tab IS NULL + BEGIN + RAISERROR('Must specify table name', 16, 1) + RETURN + END + + IF TRIM(@tab) = '' + BEGIN + RAISERROR('Must specify table name', 16, 1) + RETURN + END + + -- Since we cannot find #tmp tables in the Babelfish catalogs, we cannot check + -- their existence other than by trying to select from them + -- Function sys.babelfish_get_enr_list() could be used to determine if a #tmp table + -- exists but the columns and datatypes can still not be retrieved, it would be of + -- little use here. + -- NB: not handling uncommon but valid T-SQL syntax '.#tmp' for #tmp tables + IF sys.SUBSTRING(@tab,1,1) <> '#' + BEGIN + SET @id = sys.OBJECT_ID(@tab) + IF @id IS NULL + BEGIN + IF sys.SUBSTRING(UPPER(@tab),1,4) = 'DBO.' + BEGIN + SET @id = sys.OBJECT_ID('SYS.' + sys.SUBSTRING(@tab,5)) + END + IF @id IS NULL + BEGIN + SET @msg = 'Table or view '''+@tab+''' not found' + RAISERROR(@msg, 16, 1) + RETURN + END + END + END + + SELECT @objtype = type COLLATE DATABASE_DEFAULT FROM sys.sysobjects WHERE id = @id + IF @objtype NOT IN ('U', 'S', 'V') + BEGIN + SET @msg = ''''+@tab+''' is not a table or view' + RAISERROR(@msg, 16, 1) + RETURN + END + + -- check for 'ORDER BY', if specified + SET @orderby = TRIM(@orderby) + IF @orderby <> '' + BEGIN + IF UPPER(@orderby) NOT LIKE 'ORDER BY%' + BEGIN + RAISERROR('@orderby parameter must start with ''ORDER BY''', 16, 1) + RETURN + END + END + + -- columns to hide in final client output + -- assuming delimited column names do not contain spaces or commas inside the name + -- remove any spaces around the commas: + WHILE (sys.CHARINDEX(' ,', @hiddencols) > 0) or (sys.CHARINDEX(', ', @hiddencols) > 0) + BEGIN + SET @hiddencols = sys.REPLACE(@hiddencols, ' ,', ',') + SET @hiddencols = sys.REPLACE(@hiddencols, ', ', ',') + END + IF sys.LEN(@hiddencols) IS NOT NULL SET @hiddencols = ',' + @hiddencols + ',' + SET @hiddencols = UPPER(@hiddencols) + + -- Need to use a guaranteed-uniquely named table as intermediate step since we cannot + -- access the metadata in case a #tmp table is passed as argument + -- But when we copy the #tmp table into another table, we get all the attributes and metadata + DECLARE @tmptab sys.VARCHAR(63) = 'sp_babelfish_autoformat' + sys.REPLACE(NEWID(), '-', '') + DECLARE @tmptab2 sys.VARCHAR(63) = 'sp_babelfish_autoformat' + sys.REPLACE(NEWID(), '-', '') + DECLARE @cmd sys.VARCHAR(1000) = 'SELECT * INTO ' + @tmptab + ' FROM ' + @tab + + BEGIN TRY + -- create the first work table + EXECUTE(@cmd) + + -- Get the columns + SELECT + c.name AS colname, c.colid AS colid, t.name AS basetype, 0 AS maxlen + INTO #sp_bbf_autoformat + FROM sys.syscolumns c left join sys.systypes t + ON c.xusertype = t.xusertype + WHERE c.id = sys.OBJECT_ID(@tmptab) + ORDER BY c.colid + + -- Get max length for each column based on the data + DECLARE @colname sys.VARCHAR(63), @basetype sys.VARCHAR(63), @maxlen int + DECLARE c CURSOR FOR SELECT colname, basetype, maxlen FROM #sp_bbf_autoformat ORDER BY colid + OPEN c + WHILE 1=1 + BEGIN + FETCH c INTO @colname, @basetype, @maxlen + IF @@fetch_status <> 0 BREAK + SET @cmd = 'DECLARE @i INT SELECT @i=ISNULL(MAX(sys.LEN(CAST([' + @colname + '] AS sys.VARCHAR(500)))),4) FROM ' + @tmptab + ' UPDATE #sp_bbf_autoformat SET maxlen = @i WHERE colname = ''' + @colname + '''' + EXECUTE(@cmd) + END + CLOSE c + DEALLOCATE c + + -- Generate the final SELECT + DECLARE @selectlist sys.VARCHAR(8000) = '' + DECLARE @collist sys.VARCHAR(8000) = '' + DECLARE @fmtstart sys.VARCHAR(30) = '' + DECLARE @fmtend sys.VARCHAR(30) = '' + OPEN c + WHILE 1=1 + BEGIN + FETCH c INTO @colname, @basetype, @maxlen + IF @@fetch_status <> 0 BREAK + IF sys.LEN(@colname) > @maxlen SET @maxlen = sys.LEN(@colname) + IF @maxlen <= 0 SET @maxlen = 1 + + IF (sys.CHARINDEX(',' + UPPER(@colname) + ',', @hiddencols) > 0) OR (sys.CHARINDEX(',[' + UPPER(@colname) + '],', @hiddencols) > 0) + BEGIN + SET @selectlist += ' [' + @colname + '],' + END + ELSE + BEGIN + SET @fmtstart = '' + SET @fmtend = '' + IF @basetype IN ('tinyint', 'smallint', 'int', 'bigint', 'decimal', 'numeric', 'real', 'float') + BEGIN + SET @fmtstart = 'CAST(right(space('+CAST(@maxlen AS sys.VARCHAR)+')+' + SET @fmtend = ','+CAST(@maxlen AS sys.VARCHAR)+') AS sys.VARCHAR(' + CAST(@maxlen AS sys.VARCHAR) + '))' + END + + SET @selectlist += ' '+@fmtstart+'CAST([' + @colname + '] AS sys.VARCHAR(' + CAST(@maxlen AS sys.VARCHAR) + '))'+@fmtend+' AS [' + @colname + '],' + SET @collist += '['+@colname + '],' + END + END + CLOSE c + DEALLOCATE c + + -- Remove redundant commas + SET @collist = sys.SUBSTRING(@collist, 1, sys.LEN(@collist)-1) + SET @selectlist = sys.SUBSTRING(@selectlist, 1, sys.LEN(@selectlist)-1) + SET @selectlist = 'SELECT ' + @selectlist + ' INTO ' + @tmptab2 + ' FROM ' + @tmptab + ' ' + @orderby + + -- create the second work table + EXECUTE(@selectlist) + + -- perform the final SELECT to generate the result set for the client + EXECUTE('SELECT ' + @collist + ' FROM ' + @tmptab2) + + -- PRINT rowcount if desired + SET @rc = @@rowcount + IF @printrc = 1 + BEGIN + PRINT ' ' + SET @cmd = '(' + CAST(@rc AS sys.VARCHAR) + ' rows affected)' + PRINT @cmd + END + + -- Cleanup: these work tables are permanent tables after all + EXECUTE('DROP TABLE IF EXISTS ' + @tmptab) + EXECUTE('DROP TABLE IF EXISTS ' + @tmptab2) + END TRY + BEGIN CATCH + -- Cleanup in case of an unexpected error + EXECUTE('DROP TABLE IF EXISTS ' + @tmptab) + EXECUTE('DROP TABLE IF EXISTS ' + @tmptab2) + END CATCH + + RETURN +END +$$; +GRANT EXECUTE ON PROCEDURE sys.sp_babelfish_autoformat(IN sys.VARCHAR(257), IN sys.VARCHAR(1000), sys.bit, sys.VARCHAR(1000)) TO PUBLIC; + +ALTER VIEW sys.table_types RENAME TO table_types_deprecated_3_5_0; + +create or replace view sys.table_types as +select st.* + , pt.typrelid::int as type_table_object_id + , 0::sys.bit as is_memory_optimized -- return 0 until we support in-memory tables +from sys.types st +inner join pg_catalog.pg_type pt on st.user_type_id = pt.oid +where is_table_type = 1; +GRANT SELECT ON sys.table_types TO PUBLIC; + +-- sys.all_objects and sys.objects +create or replace view sys.all_objects as +select + name collate sys.database_default + , cast (object_id as integer) + , cast ( principal_id as integer) + , cast (schema_id as integer) + , cast (parent_object_id as integer) + , type collate sys.database_default + , cast (type_desc as sys.nvarchar(60)) + , cast (create_date as sys.datetime) + , cast (modify_date as sys.datetime) + , is_ms_shipped + , cast (is_published as sys.bit) + , cast (is_schema_published as sys.bit) +from +( +-- Currently for pg_class, pg_proc UNIONs, we separated user defined objects and system objects because the +-- optimiser will be able to make a better estimation of number of rows(in case the query contains a filter on +-- is_ms_shipped column) and in turn chooses a better query plan. + +-- details of system tables +select + t.relname::sys.sysname as name + , t.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'U'::char(2) as type + , 'USER_TABLE' as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 1::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class t inner join pg_namespace s on s.oid = t.relnamespace +left join sys.table_types_internal tt on t.oid = tt.typrelid +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'U' +where t.relpersistence in ('p', 'u', 't') +and t.relkind = 'r' +and (s.nspname = 'sys' or (nis.name is not null and ext.nspname is not null)) +and tt.typrelid is null +and has_schema_privilege(s.oid, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER') + +union all +-- details of user defined tables +select + t.relname::sys.sysname as name + , t.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'U'::char(2) as type + , 'USER_TABLE' as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 0::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class t inner join pg_namespace s on s.oid = t.relnamespace +left join sys.table_types_internal tt on t.oid = tt.typrelid +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'U' +where t.relpersistence in ('p', 'u', 't') +and t.relkind = 'r' +and s.nspname <> 'sys' and nis.name is null +and ext.nspname is not null +and tt.typrelid is null +and has_schema_privilege(s.oid, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER') + +union all +-- details of system views +select + t.relname::sys.sysname as name + , t.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'V'::char(2) as type + , 'VIEW'::varchar(60) as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 1::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class t inner join pg_namespace s on s.oid = t.relnamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'V' +where t.relkind = 'v' +and (s.nspname = 'sys' or (nis.name is not null and ext.nspname is not null)) +and has_schema_privilege(s.oid, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER') +union all +-- Details of user defined views +select + t.relname::sys.sysname as name + , t.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'V'::char(2) as type + , 'VIEW'::varchar(60) as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 0::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class t inner join pg_namespace s on s.oid = t.relnamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = t.relname and nis.schemaid = s.oid and nis.type = 'V' +where t.relkind = 'v' +and s.nspname <> 'sys' and nis.name is null +and ext.nspname is not null +and has_schema_privilege(s.oid, 'USAGE') +and has_table_privilege(t.oid, 'SELECT,INSERT,UPDATE,DELETE,TRUNCATE,TRIGGER') +union all +-- details of user defined and system foreign key constraints +select + c.conname::sys.sysname as name + , c.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , c.conrelid as parent_object_id + , 'F'::char(2) as type + , 'FOREIGN_KEY_CONSTRAINT' + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_constraint c +inner join pg_namespace s on s.oid = c.connamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = c.conname and nis.schemaid = s.oid and nis.type = 'F' +where has_schema_privilege(s.oid, 'USAGE') +and c.contype = 'f' +and (s.nspname = 'sys' or ext.nspname is not null) +union all +-- details of user defined and system primary key constraints +select + c.conname::sys.sysname as name + , c.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , c.conrelid as parent_object_id + , 'PK'::char(2) as type + , 'PRIMARY_KEY_CONSTRAINT' as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_constraint c +inner join pg_namespace s on s.oid = c.connamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = c.conname and nis.schemaid = s.oid and nis.type = 'PK' +where has_schema_privilege(s.oid, 'USAGE') +and c.contype = 'p' +and (s.nspname = 'sys' or ext.nspname is not null) +union all +-- details of system defined procedures +select + p.proname::sys.sysname as name + , p.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , cast (case when tr.tgrelid is not null + then tr.tgrelid + else 0 end as int) + as parent_object_id + , case p.prokind + when 'p' then 'P'::char(2) + when 'a' then 'AF'::char(2) + else + case + when t.typname = 'trigger' + then 'TR'::char(2) + when p.proretset then + case + when t.typtype = 'c' + then 'TF'::char(2) + else 'IF'::char(2) + end + else 'FN'::char(2) + end + end as type + , case p.prokind + when 'p' then 'SQL_STORED_PROCEDURE'::varchar(60) + when 'a' then 'AGGREGATE_FUNCTION'::varchar(60) + else + case + when t.typname = 'trigger' + then 'SQL_TRIGGER'::varchar(60) + when p.proretset then + case + when t.typtype = 'c' + then 'SQL_TABLE_VALUED_FUNCTION'::varchar(60) + else 'SQL_INLINE_TABLE_VALUED_FUNCTION'::varchar(60) + end + else 'SQL_SCALAR_FUNCTION'::varchar(60) + end + end as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 1::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_proc p +inner join pg_namespace s on s.oid = p.pronamespace +inner join pg_catalog.pg_type t on t.oid = p.prorettype +left join pg_trigger tr on tr.tgfoid = p.oid +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = p.proname and nis.schemaid = s.oid +and nis.type = (case p.prokind + when 'p' then 'P'::char(2) + when 'a' then 'AF'::char(2) + else + case + when t.typname = 'trigger' + then 'TR'::char(2) + when p.proretset then + case + when t.typtype = 'c' + then 'TF'::char(2) + else 'IF'::char(2) + end + else 'FN'::char(2) + end + end) +where (s.nspname = 'sys' or (nis.name is not null and ext.nspname is not null)) +and has_schema_privilege(s.oid, 'USAGE') +and has_function_privilege(p.oid, 'EXECUTE') + +union all +-- details of user defined procedures +select + p.proname::sys.sysname as name + , p.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , cast (case when tr.tgrelid is not null + then tr.tgrelid + else 0 end as int) + as parent_object_id + , case p.prokind + when 'p' then 'P'::char(2) + when 'a' then 'AF'::char(2) + else + case + when t.typname = 'trigger' + then 'TR'::char(2) + when p.proretset then + case + when t.typtype = 'c' + then 'TF'::char(2) + else 'IF'::char(2) + end + else 'FN'::char(2) + end + end as type + , case p.prokind + when 'p' then 'SQL_STORED_PROCEDURE'::varchar(60) + when 'a' then 'AGGREGATE_FUNCTION'::varchar(60) + else + case + when t.typname = 'trigger' + then 'SQL_TRIGGER'::varchar(60) + when p.proretset then + case + when t.typtype = 'c' + then 'SQL_TABLE_VALUED_FUNCTION'::varchar(60) + else 'SQL_INLINE_TABLE_VALUED_FUNCTION'::varchar(60) + end + else 'SQL_SCALAR_FUNCTION'::varchar(60) + end + end as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , 0::sys.bit as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_proc p +inner join pg_namespace s on s.oid = p.pronamespace +inner join pg_catalog.pg_type t on t.oid = p.prorettype +left join pg_trigger tr on tr.tgfoid = p.oid +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = p.proname and nis.schemaid = s.oid +and nis.type = (case p.prokind + when 'p' then 'P'::char(2) + when 'a' then 'AF'::char(2) + else + case + when t.typname = 'trigger' + then 'TR'::char(2) + when p.proretset then + case + when t.typtype = 'c' + then 'TF'::char(2) + else 'IF'::char(2) + end + else 'FN'::char(2) + end + end) +where s.nspname <> 'sys' and nis.name is null +and ext.nspname is not null +and has_schema_privilege(s.oid, 'USAGE') +and has_function_privilege(p.oid, 'EXECUTE') + +union all +-- details of all default constraints +select + ('DF_' || o.relname || '_' || d.oid)::sys.sysname as name + , d.oid as object_id + , null::int as principal_id + , o.relnamespace as schema_id + , d.adrelid as parent_object_id + , 'D'::char(2) as type + , 'DEFAULT_CONSTRAINT'::sys.nvarchar(60) AS type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_catalog.pg_attrdef d +inner join pg_attribute a on a.attrelid = d.adrelid and d.adnum = a.attnum +inner join pg_class o on d.adrelid = o.oid +inner join pg_namespace s on s.oid = o.relnamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = ('DF_' || o.relname || '_' || d.oid) and nis.schemaid = s.oid and nis.type = 'D' +where a.atthasdef = 't' and a.attgenerated = '' +and (s.nspname = 'sys' or ext.nspname is not null) +and has_schema_privilege(s.oid, 'USAGE') +and has_column_privilege(a.attrelid, a.attname, 'SELECT,INSERT,UPDATE,REFERENCES') +union all +-- details of all check constraints +select + c.conname::sys.sysname + , c.oid::integer as object_id + , NULL::integer as principal_id + , s.oid as schema_id + , c.conrelid::integer as parent_object_id + , 'C'::char(2) as type + , 'CHECK_CONSTRAINT'::sys.nvarchar(60) as type_desc + , null::sys.datetime as create_date + , null::sys.datetime as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_catalog.pg_constraint as c +inner join pg_namespace s on s.oid = c.connamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = c.conname and nis.schemaid = s.oid and nis.type = 'C' +where has_schema_privilege(s.oid, 'USAGE') +and c.contype = 'c' and c.conrelid != 0 +and (s.nspname = 'sys' or ext.nspname is not null) +union all +-- details of user defined and system defined sequence objects +select + p.relname::sys.sysname as name + , p.oid as object_id + , null::integer as principal_id + , s.oid as schema_id + , 0 as parent_object_id + , 'SO'::char(2) as type + , 'SEQUENCE_OBJECT'::varchar(60) as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (s.nspname = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from pg_class p +inner join pg_namespace s on s.oid = p.relnamespace +left join sys.babelfish_namespace_ext ext on (s.nspname = ext.nspname and ext.dbid = sys.db_id()) +left join sys.shipped_objects_not_in_sys nis on nis.name = p.relname and nis.schemaid = s.oid and nis.type = 'SO' +where p.relkind = 'S' +and (s.nspname = 'sys' or ext.nspname is not null) +and has_schema_privilege(s.oid, 'USAGE') +union all +-- details of user defined table types +select + ('TT_' || tt.name || '_' || tt.type_table_object_id)::sys.sysname as name + , tt.type_table_object_id as object_id + , tt.principal_id as principal_id + , tt.schema_id as schema_id + , 0 as parent_object_id + , 'TT'::char(2) as type + , 'TABLE_TYPE'::varchar(60) as type_desc + , null::timestamp as create_date + , null::timestamp as modify_date + , CAST (case when (tt.schema_id::regnamespace::text = 'sys' or nis.name is not null) then 1 + else 0 end as sys.bit ) as is_ms_shipped + , 0 as is_published + , 0 as is_schema_published +from sys.table_types tt +left join sys.shipped_objects_not_in_sys nis on nis.name = ('TT_' || tt.name || '_' || tt.type_table_object_id)::name and nis.schemaid = tt.schema_id and nis.type = 'TT' +) ot; +GRANT SELECT ON sys.all_objects TO PUBLIC; + +create or replace view sys.objects as +select + CAST(t.name as sys.sysname) as name + , CAST(t.object_id as int) as object_id + , CAST(t.principal_id as int) as principal_id + , CAST(t.schema_id as int) as schema_id + , CAST(t.parent_object_id as int) as parent_object_id + , CAST('U' as char(2)) as type + , CAST('USER_TABLE' as sys.nvarchar(60)) as type_desc + , CAST(t.create_date as sys.datetime) as create_date + , CAST(t.modify_date as sys.datetime) as modify_date + , CAST(t.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(t.is_published as sys.bit) as is_published + , CAST(t.is_schema_published as sys.bit) as is_schema_published +from sys.tables t +union all +select + CAST(v.name as sys.sysname) as name + , CAST(v.object_id as int) as object_id + , CAST(v.principal_id as int) as principal_id + , CAST(v.schema_id as int) as schema_id + , CAST(v.parent_object_id as int) as parent_object_id + , CAST('V' as char(2)) as type + , CAST('VIEW' as sys.nvarchar(60)) as type_desc + , CAST(v.create_date as sys.datetime) as create_date + , CAST(v.modify_date as sys.datetime) as modify_date + , CAST(v.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(v.is_published as sys.bit) as is_published + , CAST(v.is_schema_published as sys.bit) as is_schema_published +from sys.views v +union all +select + CAST(f.name as sys.sysname) as name + , CAST(f.object_id as int) as object_id + , CAST(f.principal_id as int) as principal_id + , CAST(f.schema_id as int) as schema_id + , CAST(f.parent_object_id as int) as parent_object_id + , CAST('F' as char(2)) as type + , CAST('FOREIGN_KEY_CONSTRAINT' as sys.nvarchar(60)) as type_desc + , CAST(f.create_date as sys.datetime) as create_date + , CAST(f.modify_date as sys.datetime) as modify_date + , CAST(f.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(f.is_published as sys.bit) as is_published + , CAST(f.is_schema_published as sys.bit) as is_schema_published + from sys.foreign_keys f +union all +select + CAST(p.name as sys.sysname) as name + , CAST(p.object_id as int) as object_id + , CAST(p.principal_id as int) as principal_id + , CAST(p.schema_id as int) as schema_id + , CAST(p.parent_object_id as int) as parent_object_id + , CAST('PK' as char(2)) as type + , CAST('PRIMARY_KEY_CONSTRAINT' as sys.nvarchar(60)) as type_desc + , CAST(p.create_date as sys.datetime) as create_date + , CAST(p.modify_date as sys.datetime) as modify_date + , CAST(p.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(p.is_published as sys.bit) as is_published + , CAST(p.is_schema_published as sys.bit) as is_schema_published +from sys.key_constraints p +where p.type = 'PK' +union all +select + CAST(pr.name as sys.sysname) as name + , CAST(pr.object_id as int) as object_id + , CAST(pr.principal_id as int) as principal_id + , CAST(pr.schema_id as int) as schema_id + , CAST(pr.parent_object_id as int) as parent_object_id + , CAST(pr.type as char(2)) as type + , CAST(pr.type_desc as sys.nvarchar(60)) as type_desc + , CAST(pr.create_date as sys.datetime) as create_date + , CAST(pr.modify_date as sys.datetime) as modify_date + , CAST(pr.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(pr.is_published as sys.bit) as is_published + , CAST(pr.is_schema_published as sys.bit) as is_schema_published + from sys.procedures pr +union all +select + CAST(tr.name as sys.sysname) as name + , CAST(tr.object_id as int) as object_id + , CAST(NULL as int) as principal_id + , CAST(p.pronamespace as int) as schema_id + , CAST(tr.parent_id as int) as parent_object_id + , CAST(tr.type as char(2)) as type + , CAST(tr.type_desc as sys.nvarchar(60)) as type_desc + , CAST(tr.create_date as sys.datetime) as create_date + , CAST(tr.modify_date as sys.datetime) as modify_date + , CAST(tr.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published + from sys.triggers tr + inner join pg_proc p on p.oid = tr.object_id +union all +select + CAST(def.name as sys.sysname) as name + , CAST(def.object_id as int) as object_id + , CAST(def.principal_id as int) as principal_id + , CAST(def.schema_id as int) as schema_id + , CAST(def.parent_object_id as int) as parent_object_id + , CAST(def.type as char(2)) as type + , CAST(def.type_desc as sys.nvarchar(60)) as type_desc + , CAST(def.create_date as sys.datetime) as create_date + , CAST(def.modified_date as sys.datetime) as modify_date + , CAST(def.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(def.is_published as sys.bit) as is_published + , CAST(def.is_schema_published as sys.bit) as is_schema_published + from sys.default_constraints def +union all +select + CAST(chk.name as sys.sysname) as name + , CAST(chk.object_id as int) as object_id + , CAST(chk.principal_id as int) as principal_id + , CAST(chk.schema_id as int) as schema_id + , CAST(chk.parent_object_id as int) as parent_object_id + , CAST(chk.type as char(2)) as type + , CAST(chk.type_desc as sys.nvarchar(60)) as type_desc + , CAST(chk.create_date as sys.datetime) as create_date + , CAST(chk.modify_date as sys.datetime) as modify_date + , CAST(chk.is_ms_shipped as sys.bit) as is_ms_shipped + , CAST(chk.is_published as sys.bit) as is_published + , CAST(chk.is_schema_published as sys.bit) as is_schema_published + from sys.check_constraints chk +union all +select + CAST(p.relname as sys.sysname) as name + , CAST(p.oid as int) as object_id + , CAST(null as int) as principal_id + , CAST(s.schema_id as int) as schema_id + , CAST(0 as int) as parent_object_id + , CAST('SO' as char(2)) as type + , CAST('SEQUENCE_OBJECT' as sys.nvarchar(60)) as type_desc + , CAST(null as sys.datetime) as create_date + , CAST(null as sys.datetime) as modify_date + , CAST(0 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published +from pg_class p +inner join sys.schemas s on s.schema_id = p.relnamespace +and p.relkind = 'S' +and has_schema_privilege(s.schema_id, 'USAGE') +union all +select + CAST(('TT_' || tt.name collate "C" || '_' || tt.type_table_object_id) as sys.sysname) as name + , CAST(tt.type_table_object_id as int) as object_id + , CAST(tt.principal_id as int) as principal_id + , CAST(tt.schema_id as int) as schema_id + , CAST(0 as int) as parent_object_id + , CAST('TT' as char(2)) as type + , CAST('TABLE_TYPE' as sys.nvarchar(60)) as type_desc + , CAST((select string_agg( + case + when option like 'bbf_rel_create_date=%%' then substring(option, 21) + else NULL + end, ',') + from unnest(c.reloptions) as option) + as sys.datetime) as create_date + , CAST((select string_agg( + case + when option like 'bbf_rel_create_date=%%' then substring(option, 21) + else NULL + end, ',') + from unnest(c.reloptions) as option) + as sys.datetime) as modify_date + , CAST(1 as sys.bit) as is_ms_shipped + , CAST(0 as sys.bit) as is_published + , CAST(0 as sys.bit) as is_schema_published +from sys.table_types tt +inner join pg_class c on tt.type_table_object_id = c.oid; +GRANT SELECT ON sys.objects TO PUBLIC; + +CREATE OR REPLACE VIEW sys.all_parameters +AS +SELECT + CAST(ss.p_oid AS INT) AS object_id + , CAST(COALESCE(ss.proargnames[(ss.x).n], '') AS sys.SYSNAME) AS name + , CAST( + CASE + WHEN is_out_scalar = 1 THEN 0 -- param_id = 0 for output of scalar function + ELSE (ss.x).n + END + AS INT) AS parameter_id + -- 'system_type_id' is specified as type INT here, and not TINYINT per SQL Server documentation. + -- This is because the IDs of system type values generated by + -- Babelfish installation will exceed the size of TINYINT + , CAST(st.system_type_id AS INT) AS system_type_id + , CAST(st.user_type_id AS INT) AS user_type_id + , CAST( + CASE + WHEN st.is_table_type = 1 THEN -1 -- TVP case + WHEN st.is_user_defined = 1 THEN st.max_length -- UDT case + ELSE sys.tsql_type_max_length_helper(st.name, t.typlen, typmod, true, true) + END + AS smallint) AS max_length + , CAST( + CASE + WHEN st.is_table_type = 1 THEN 0 -- TVP case + WHEN st.is_user_defined = 1 THEN st.precision -- UDT case + ELSE sys.tsql_type_precision_helper(st.name, typmod) + END + AS sys.tinyint) AS precision + , CAST( + CASE + WHEN st.is_table_type = 1 THEN 0 -- TVP case + WHEN st.is_user_defined = 1 THEN st.scale + ELSE sys.tsql_type_scale_helper(st.name, typmod,false) + END + AS sys.tinyint) AS scale + , CAST( + CASE + WHEN is_out_scalar = 1 THEN 1 -- Output of a scalar function + WHEN ss.proargmodes[(ss.x).n] in ('o', 'b', 't') THEN 1 + ELSE 0 + END + AS sys.bit) AS is_output + , CAST(0 AS sys.bit) AS is_cursor_ref + , CAST(0 AS sys.bit) AS has_default_value + , CAST(0 AS sys.bit) AS is_xml_document + , CAST(NULL AS sys.sql_variant) AS default_value + , CAST(0 AS int) AS xml_collection_id + , CAST(0 AS sys.bit) AS is_readonly + , CAST(1 AS sys.bit) AS is_nullable + , CAST(NULL AS int) AS encryption_type + , CAST(NULL AS sys.nvarchar(64)) AS encryption_type_desc + , CAST(NULL AS sys.sysname) AS encryption_algorithm_name + , CAST(NULL AS int) AS column_encryption_key_id + , CAST(NULL AS sys.sysname) AS column_encryption_key_database_name +FROM pg_type t + INNER JOIN sys.types st ON st.user_type_id = t.oid + INNER JOIN + ( + SELECT + p.oid AS p_oid, + p.proargnames, + p.proargmodes, + p.prokind, + json_extract_path(CAST(p.probin as json), 'typmod_array') AS typmod_array, + information_schema._pg_expandarray( + COALESCE(p.proallargtypes, + CASE + WHEN p.prokind = 'f' THEN (CAST( p.proargtypes AS oid[]) || p.prorettype) -- Adds return type if not present on proallargtypes + ELSE CAST(p.proargtypes AS oid[]) + END + )) AS x + FROM pg_proc p + WHERE ( + p.pronamespace in (select schema_id from sys.schemas union all select oid from pg_namespace where nspname = 'sys') + AND (pg_has_role(p.proowner, 'USAGE') OR has_function_privilege(p.oid, 'EXECUTE')) + AND p.probin like '{%typmod_array%}') -- Needs to have a typmod array in JSON format + ) ss ON t.oid = (ss.x).x, + COALESCE(pg_get_function_result(ss.p_oid), '') AS return_type, + CAST(ss.typmod_array->>(ss.x).n-1 AS INT) AS typmod, + CAST( + CASE + WHEN ss.prokind = 'f' AND ss.proargnames[(ss.x).n] IS NULL THEN 1 -- checks if param is output of scalar function + ELSE 0 + END + AS INT) AS is_out_scalar +WHERE ( -- If it is a Table function, we only want the inputs + return_type NOT LIKE 'TABLE(%' OR + (return_type LIKE 'TABLE(%' AND ss.proargmodes[(ss.x).n] = 'i')); +GRANT SELECT ON sys.all_parameters TO PUBLIC; + +CREATE OR REPLACE PROCEDURE sys.sp_rename( + IN "@objname" sys.nvarchar(776) = NULL, + IN "@newname" sys.SYSNAME = NULL, + IN "@objtype" sys.varchar(13) DEFAULT NULL +) +LANGUAGE 'pltsql' +AS $$ +BEGIN + If @objtype IS NULL + BEGIN + THROW 33557097, N'Please provide @objtype that is supported in Babelfish', 1; + END + ELSE IF @objtype = 'INDEX' + BEGIN + THROW 33557097, N'Feature not supported: renaming object type Index', 1; + END + ELSE IF @objtype = 'STATISTICS' + BEGIN + THROW 33557097, N'Feature not supported: renaming object type Statistics', 1; + END + ELSE + BEGIN + DECLARE @subname sys.nvarchar(776); + DECLARE @schemaname sys.nvarchar(776); + DECLARE @dbname sys.nvarchar(776); + DECLARE @curr_relname sys.nvarchar(776); + + EXEC sys.babelfish_sp_rename_word_parse @objname, @objtype, @subname OUT, @curr_relname OUT, @schemaname OUT, @dbname OUT; + + DECLARE @currtype char(2); + + IF @objtype = 'COLUMN' + BEGIN + DECLARE @col_count INT; + SELECT @col_count = COUNT(*)FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = @curr_relname and COLUMN_NAME = @subname; + IF @col_count < 0 + BEGIN + THROW 33557097, N'There is no object with the given @objname.', 1; + END + SET @currtype = 'CO'; + END + ELSE IF @objtype = 'USERDATATYPE' + BEGIN + DECLARE @alias_count INT; + SELECT @alias_count = COUNT(*) FROM sys.types t1 INNER JOIN sys.schemas s1 ON t1.schema_id = s1.schema_id + WHERE s1.name = @schemaname AND t1.name = @subname; + IF @alias_count > 1 + BEGIN + THROW 33557097, N'There are multiple objects with the given @objname.', 1; + END + IF @alias_count < 1 + BEGIN + THROW 33557097, N'There is no object with the given @objname.', 1; + END + SET @currtype = 'AL'; + END + ELSE IF @objtype = 'OBJECT' + BEGIN + DECLARE @count INT; + SELECT type INTO #tempTable FROM sys.objects o1 INNER JOIN sys.schemas s1 ON o1.schema_id = s1.schema_id + WHERE s1.name = @schemaname AND o1.name = @subname; + SELECT @count = COUNT(*) FROM #tempTable; + + IF @count > 1 + BEGIN + THROW 33557097, N'There are multiple objects with the given @objname.', 1; + END + IF @count < 1 + BEGIN + -- TABLE TYPE: check if there is a match in sys.table_types (if we cannot alter sys.objects table_type naming) + SELECT @count = COUNT(*) FROM sys.table_types tt1 INNER JOIN sys.schemas s1 ON tt1.schema_id = s1.schema_id + WHERE s1.name = @schemaname AND tt1.name = @subname; + IF @count > 1 + BEGIN + THROW 33557097, N'There are multiple objects with the given @objname.', 1; + END + ELSE IF @count < 1 + BEGIN + THROW 33557097, N'There is no object with the given @objname.', 1; + END + ELSE + BEGIN + SET @currtype = 'TT' + END + END + IF @currtype IS NULL + BEGIN + SELECT @currtype = type from #tempTable; + END + IF @currtype = 'TR' OR @currtype = 'TA' + BEGIN + DECLARE @physical_schema_name sys.nvarchar(776) = ''; + SELECT @physical_schema_name = nspname FROM sys.babelfish_namespace_ext WHERE dbid = sys.db_id() AND orig_name = @schemaname; + SELECT @curr_relname = relname FROM pg_catalog.pg_trigger tr LEFT JOIN pg_catalog.pg_class c ON tr.tgrelid = c.oid LEFT JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid + WHERE tr.tgname = @subname AND n.nspname = @physical_schema_name; + END + END + ELSE + BEGIN + THROW 33557097, N'Provided @objtype is not currently supported in Babelfish', 1; + END + EXEC sys.babelfish_sp_rename_internal @subname, @newname, @schemaname, @currtype, @curr_relname; + PRINT 'Caution: Changing any part of an object name could break scripts and stored procedures.'; + END +END; +$$; +GRANT EXECUTE on PROCEDURE sys.sp_rename(IN sys.nvarchar(776), IN sys.SYSNAME, IN sys.varchar(13)) TO PUBLIC; + CREATE OR REPLACE VIEW sys.sp_pkeys_view AS SELECT CAST(t4."TABLE_CATALOG" AS sys.sysname) AS TABLE_QUALIFIER, @@ -178,7 +2048,7 @@ DECLARE exception_message text; BEGIN -- Rename parsename for dependencies - ALTER FUNCTION sys.parsename(sys.VARCHAR, INT) RENAME TO parsename_deprecated_in_3_5_0; + ALTER FUNCTION sys.parsename(sys.VARCHAR, INT) RENAME TO parsename_deprecated_in_3_5_0_0; EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -192,7 +2062,7 @@ DECLARE exception_message text; BEGIN -- Rename sp_set_session_context for dependencies - ALTER PROCEDURE sys.sp_set_session_context(sys.SYSNAME, sys.SQL_VARIANT, sys.BIT) RENAME TO sp_set_session_context_deprecated_in_3_5_0; + ALTER PROCEDURE sys.sp_set_session_context(sys.SYSNAME, sys.SQL_VARIANT, sys.BIT) RENAME TO sp_set_session_context_deprecated_in_3_5_0_0; EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -206,7 +2076,7 @@ DECLARE exception_message text; BEGIN -- Rename session_context for dependencies - ALTER FUNCTION sys.session_context(sys.SYSNAME) RENAME TO session_context_deprecated_in_3_5_0; + ALTER FUNCTION sys.session_context(sys.SYSNAME) RENAME TO session_context_deprecated_in_3_5_0_0; EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -243,8 +2113,8 @@ DO $$ DECLARE exception_message text; BEGIN - -- === DROP parsename_deprecated_in_3_5_0 - CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'parsename_deprecated_in_3_5_0'); + -- === DROP parsename_deprecated_in_3_5_0_0 + CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'parsename_deprecated_in_3_5_0_0'); EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -257,8 +2127,8 @@ DO $$ DECLARE exception_message text; BEGIN - -- === DROP sp_set_session_context_deprecated_in_3_5_0 - CALL sys.babelfish_drop_deprecated_object('procedure', 'sys', 'sp_set_session_context_deprecated_in_3_5_0'); + -- === DROP sp_set_session_context_deprecated_in_3_5_0_0 + CALL sys.babelfish_drop_deprecated_object('procedure', 'sys', 'sp_set_session_context_deprecated_in_3_5_0_0'); EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -271,8 +2141,8 @@ DO $$ DECLARE exception_message text; BEGIN - -- === DROP session_context_deprecated_in_3_5_0 - CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'session_context_deprecated_in_3_5_0'); + -- === DROP session_context_deprecated_in_3_5_0_0 + CALL sys.babelfish_drop_deprecated_object('function', 'sys', 'session_context_deprecated_in_3_5_0_0'); EXCEPTION WHEN OTHERS THEN GET STACKED DIAGNOSTICS @@ -331,6 +2201,24 @@ AS SELECT WHERE FALSE; GRANT SELECT ON sys.availability_groups TO PUBLIC; +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'sysforeignkeys_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'system_objects_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'syscolumns_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'dm_exec_connections_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'xml_indexes_connections_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'stats__deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'data_spaces_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'sysprocesses_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'key_constraints_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'foreign_keys_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'views_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'tables_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'default_constraints_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'check_constraints_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'types_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'table_types_deprecated_3_5_0'); +CALL sys.babelfish_drop_deprecated_object('view', 'sys', 'systypes_deprecated_3_5_0'); + -- Drops the temporary procedure used by the upgrade script. -- Please have this be one of the last statements executed in this upgrade script. DROP PROCEDURE sys.babelfish_drop_deprecated_object(varchar, varchar, varchar); diff --git a/test/JDBC/expected/BABEL-2829.out b/test/JDBC/expected/BABEL-2829.out index cfd96b8eca7..1cebc43a8c7 100644 --- a/test/JDBC/expected/BABEL-2829.out +++ b/test/JDBC/expected/BABEL-2829.out @@ -5,32 +5,32 @@ SELECT DISTINCT db_name(dbid), loginname FROM sys.sysprocesses WHERE spid = @@SPID GO ~~START~~ -nvarchar#!#varchar -master#!#jdbc_user +nvarchar#!#nchar +master#!#jdbc_user ~~END~~ SELECT DISTINCT db_name(dbid), loginname FROM dbo.sysprocesses WHERE spid = @@SPID GO ~~START~~ -nvarchar#!#varchar -master#!#jdbc_user +nvarchar#!#nchar +master#!#jdbc_user ~~END~~ SELECT DISTINCT db_name(dbid), loginname FROM [sys].[sysprocesses] WHERE spid = @@SPID GO ~~START~~ -nvarchar#!#varchar -master#!#jdbc_user +nvarchar#!#nchar +master#!#jdbc_user ~~END~~ SELECT DISTINCT db_name(dbid), loginname FROM [DbO].[SySProcESSeS] WHERE spid = @@SPID GO ~~START~~ -nvarchar#!#varchar -master#!#jdbc_user +nvarchar#!#nchar +master#!#jdbc_user ~~END~~ @@ -41,15 +41,15 @@ GO SELECT DISTINCT db_name(dbid), loginname FROM sys.sysprocesses WHERE spid = @@SPID GO ~~START~~ -nvarchar#!#varchar -db_2829#!#jdbc_user +nvarchar#!#nchar +db_2829#!#jdbc_user ~~END~~ SELECT DISTINCT db_name(dbid), loginname FROM dbo.sysprocesses WHERE spid = @@SPID GO ~~START~~ -nvarchar#!#varchar -db_2829#!#jdbc_user +nvarchar#!#nchar +db_2829#!#jdbc_user ~~END~~ USE master @@ -58,15 +58,15 @@ GO SELECT DISTINCT db_name(dbid), loginname FROM db_2829.sys.sysprocesses WHERE spid = @@SPID GO ~~START~~ -nvarchar#!#varchar -db_2829#!#jdbc_user +nvarchar#!#nchar +db_2829#!#jdbc_user ~~END~~ SELECT DISTINCT db_name(dbid), loginname FROM db_2829.dbo.sysprocesses WHERE spid = @@SPID GO ~~START~~ -nvarchar#!#varchar -master#!#jdbc_user +nvarchar#!#nchar +master#!#jdbc_user ~~END~~ @@ -85,8 +85,8 @@ go exec procedure_2829 go ~~START~~ -nvarchar#!#varchar -master#!#jdbc_user +nvarchar#!#nchar +master#!#jdbc_user ~~END~~ @@ -119,3 +119,29 @@ GO DROP DATABASE db_2829 GO + +-- test case-sementics of sys.sysprocesses catalog. +-- lastwaittype, waitresource column can not be test as these are NULL. +-- hostprocess colum is ID in char +-- cmd, nt_domain, nt_username, net_address, net_library testing can't be done as these are NULL. +SELECT CASE WHEN (select count(status) from sys.sysprocesses where status='active') = (select count(status) from sys.sysprocesses where status='ACTIVE') THEN 1 ELSE 0 END AS result; +GO +~~START~~ +int +1 +~~END~~ + +SELECT CASE WHEN (select count(program_name) from sys.sysprocesses where program_name='sqlcmd') = (select count(program_name) from sys.sysprocesses where program_name='SQLCMD') THEN 1 ELSE 0 END AS result; +GO +~~START~~ +int +1 +~~END~~ + +SELECT CASE WHEN (select count(loginname) from sys.sysprocesses where loginname='jdbc_user') = (select count(loginname) from sys.sysprocesses where loginname='JDBC_user') THEN 1 ELSE 0 END AS result; +GO +~~START~~ +int +1 +~~END~~ + diff --git a/test/JDBC/expected/BABEL-2869.out b/test/JDBC/expected/BABEL-2869.out index b0b0a440883..aef905b9f5e 100644 --- a/test/JDBC/expected/BABEL-2869.out +++ b/test/JDBC/expected/BABEL-2869.out @@ -17,7 +17,7 @@ AND O.schema_id = SCHEMA_ID( 'dbo' ) ORDER BY T.name go ~~START~~ -text +varchar bigint binary char @@ -37,7 +37,7 @@ go Select name from sys.types order by name go ~~START~~ -text +varchar bigint binary bit diff --git a/test/JDBC/expected/BABEL-3009.out b/test/JDBC/expected/BABEL-3009.out index 3b5b0725163..37a1e7e83f5 100644 --- a/test/JDBC/expected/BABEL-3009.out +++ b/test/JDBC/expected/BABEL-3009.out @@ -3,7 +3,7 @@ LEFT JOIN sys.types tp ON tp.system_type_id = ac.system_type_id WHERE ac.object_id = object_id('sys.server_principals') ORDER BY ac.name; GO ~~START~~ -varchar#!#text +varchar#!#varchar create_date#!#datetime credential_id#!#int default_database_name#!#sysname @@ -25,7 +25,7 @@ LEFT JOIN sys.types tp ON tp.system_type_id = ac.system_type_id WHERE ac.object_id = object_id('sys.database_principals') ORDER BY ac.name; GO ~~START~~ -varchar#!#text +varchar#!#varchar allow_encrypted_value_modifications#!#bit authentication_type#!#int authentication_type_desc#!#nvarchar diff --git a/test/JDBC/expected/BABEL-4327.out b/test/JDBC/expected/BABEL-4327.out index 10d77faa805..150bf259289 100644 --- a/test/JDBC/expected/BABEL-4327.out +++ b/test/JDBC/expected/BABEL-4327.out @@ -16,7 +16,7 @@ select name from sys.types where system_type_id = ); GO ~~START~~ -text +varchar varchar ~~END~~ diff --git a/test/JDBC/expected/BABEL-4529-before-15_6-or-14_11-vu-cleanup.out b/test/JDBC/expected/BABEL-4529-before-15_6-or-14_11-vu-cleanup.out new file mode 100644 index 00000000000..53b0edc4c3c --- /dev/null +++ b/test/JDBC/expected/BABEL-4529-before-15_6-or-14_11-vu-cleanup.out @@ -0,0 +1,50 @@ +DROP VIEW IF EXISTS babel_4529_stats +GO + +DROP VIEW IF EXISTS babel_4529_dm_exec_connections +GO + +DROP VIEW IF EXISTS babel_4529_syscolumns +GO + +DROP VIEW IF EXISTS babel_4529_system_objects +GO + +DROP VIEW IF EXISTS babel_4529_xml_indexes +GO + +DROP VIEW IF EXISTS babel_4529_sysforeignkeys +GO + +DROP VIEW IF EXISTS babel_4529_data_spaces +GO + +DROP VIEW IF EXISTS babel_4529_sysprocesses +GO + +DROP VIEW IF EXISTS babel_4529_key_constraints +GO + +DROP VIEW IF EXISTS babel_4529_foreign_keys +GO + +DROP VIEW IF EXISTS babel_4529_sysviews +GO + +DROP VIEW IF EXISTS babel_4529_systables +GO + +DROP VIEW IF EXISTS babel_4529_default_constraints +GO + +DROP VIEW IF EXISTS babel_4529_check_constraints +GO + +DROP VIEW IF EXISTS babel_4529_types +GO + +DROP VIEW IF EXISTS babel_4529_sp_sproc_columns_view +GO + +DROP VIEW IF EXISTS babel_4529_systypes +GO diff --git a/test/JDBC/expected/BABEL-4529-before-15_6-or-14_11-vu-prepare.out b/test/JDBC/expected/BABEL-4529-before-15_6-or-14_11-vu-prepare.out new file mode 100644 index 00000000000..561d6d7c49f --- /dev/null +++ b/test/JDBC/expected/BABEL-4529-before-15_6-or-14_11-vu-prepare.out @@ -0,0 +1,84 @@ +CREATE VIEW babel_4529_stats +AS +SELECT stats_generation_method_desc FROM sys.stats +GO + +CREATE VIEW babel_4529_dm_exec_connections +AS +SELECT local_net_address, client_net_address, endpoint_id FROM sys.dm_exec_connections where local_net_address = 'test_address_4529'; +GO + +CREATE VIEW babel_4529_syscolumns +AS +SELECT printfmt FROM sys.syscolumns where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_system_objects +AS +SELECT * FROM sys.system_objects where name='test_key_constraints' +GO + +CREATE VIEW babel_4529_xml_indexes +AS +SELECT secondary_type FROM sys.xml_indexes +GO + +CREATE VIEW babel_4529_sysforeignkeys +AS +SELECT * FROM sys.sysforeignkeys where fkeyid IS NULL +GO + +CREATE VIEW babel_4529_data_spaces +AS +SELECT type FROM sys.data_spaces where type='FG' +GO + +CREATE VIEW babel_4529_sysprocesses +AS +SELECT * FROM sys.sysprocesses WHERE loginname = 'test_name_4529' +GO + +CREATE VIEW babel_4529_key_constraints +AS +SELECT type FROM sys.key_constraints where parent_object_id IS NULL +GO + +CREATE VIEW babel_4529_foreign_keys +AS +SELECT type FROM sys.foreign_keys where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_sysviews +AS +SELECT name, type, type_desc, create_date, modify_date, is_ms_shipped, is_published, is_schema_published, with_check_option, is_date_correlation_view, is_tracked_by_cdc, object_id, schema_id FROM sys.views where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_systables +AS +SELECT type FROM sys.tables where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_default_constraints +AS +SELECT type FROM sys.default_constraints where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_check_constraints +AS +SELECT type FROM sys.check_constraints where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_types +AS +SELECT name, collation_name, precision, scale, is_user_defined, is_assembly_type, is_table_type, default_object_id, rule_object_id, system_type_id, user_type_id, schema_id FROM sys.types where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_sp_sproc_columns_view +AS +SELECT precision, scale FROM sys.sp_sproc_columns_view where PROCEDURE_QUALIFIER = 'test_name_4529' +GO + +CREATE VIEW babel_4529_systypes +AS +SELECT prec FROM sys.systypes where name = 'test_name_4529' +GO diff --git a/test/JDBC/expected/BABEL-4529-before-15_6-or-14_11-vu-verify.out b/test/JDBC/expected/BABEL-4529-before-15_6-or-14_11-vu-verify.out new file mode 100644 index 00000000000..21d116c0aa2 --- /dev/null +++ b/test/JDBC/expected/BABEL-4529-before-15_6-or-14_11-vu-verify.out @@ -0,0 +1,119 @@ +SELECT * FROM babel_4529_stats +GO +~~START~~ +varchar +~~END~~ + + +SELECT * FROM babel_4529_dm_exec_connections +GO +~~START~~ +varchar#!#varchar#!#int +~~END~~ + + +SELECT * FROM babel_4529_syscolumns +GO +~~START~~ +varchar +~~END~~ + + +SELECT * FROM babel_4529_system_objects +GO +~~START~~ +varchar#!#int#!#int#!#int#!#int#!#char#!#nvarchar#!#datetime#!#datetime#!#bit#!#bit#!#bit#!#int#!#varchar#!#int#!#varchar +~~END~~ + + +SELECT * FROM babel_4529_xml_indexes +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_sysforeignkeys +GO +~~START~~ +varchar#!#int#!#int#!#int#!#smallint#!#smallint#!#smallint +~~END~~ + + +SELECT * FROM babel_4529_data_spaces +GO +~~START~~ +char +FG +~~END~~ + + +SELECT * FROM babel_4529_sysprocesses +GO +~~START~~ +int#!#int#!#int#!#varbinary#!#int#!#text#!#text#!#int#!#int#!#int#!#int#!#int#!#varchar#!#varchar#!#int#!#int#!#text#!#varbinary#!#nchar#!#text#!#varchar#!#text#!#varchar#!#varchar#!#varchar#!#varchar#!#varchar#!#varbinary#!#varbinary#!#int#!#int#!#int +~~END~~ + + +SELECT * FROM babel_4529_key_constraints +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_foreign_keys +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_sysviews +GO +~~START~~ +varchar#!#varchar#!#varchar#!#datetime#!#datetime#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +~~END~~ + + +SELECT * FROM babel_4529_systables +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_default_constraints +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_check_constraints +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_types +GO +~~START~~ +text#!#varchar#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int#!#int +~~END~~ + + +SELECT * FROM babel_4529_sp_sproc_columns_view +GO +~~START~~ +int#!#smallint +~~END~~ + + +SELECT * FROM babel_4529_systypes +GO +~~START~~ +int +~~END~~ + diff --git a/test/JDBC/expected/BABEL-4529-vu-cleanup.out b/test/JDBC/expected/BABEL-4529-vu-cleanup.out new file mode 100644 index 00000000000..53b0edc4c3c --- /dev/null +++ b/test/JDBC/expected/BABEL-4529-vu-cleanup.out @@ -0,0 +1,50 @@ +DROP VIEW IF EXISTS babel_4529_stats +GO + +DROP VIEW IF EXISTS babel_4529_dm_exec_connections +GO + +DROP VIEW IF EXISTS babel_4529_syscolumns +GO + +DROP VIEW IF EXISTS babel_4529_system_objects +GO + +DROP VIEW IF EXISTS babel_4529_xml_indexes +GO + +DROP VIEW IF EXISTS babel_4529_sysforeignkeys +GO + +DROP VIEW IF EXISTS babel_4529_data_spaces +GO + +DROP VIEW IF EXISTS babel_4529_sysprocesses +GO + +DROP VIEW IF EXISTS babel_4529_key_constraints +GO + +DROP VIEW IF EXISTS babel_4529_foreign_keys +GO + +DROP VIEW IF EXISTS babel_4529_sysviews +GO + +DROP VIEW IF EXISTS babel_4529_systables +GO + +DROP VIEW IF EXISTS babel_4529_default_constraints +GO + +DROP VIEW IF EXISTS babel_4529_check_constraints +GO + +DROP VIEW IF EXISTS babel_4529_types +GO + +DROP VIEW IF EXISTS babel_4529_sp_sproc_columns_view +GO + +DROP VIEW IF EXISTS babel_4529_systypes +GO diff --git a/test/JDBC/expected/BABEL-4529-vu-prepare.out b/test/JDBC/expected/BABEL-4529-vu-prepare.out new file mode 100644 index 00000000000..561d6d7c49f --- /dev/null +++ b/test/JDBC/expected/BABEL-4529-vu-prepare.out @@ -0,0 +1,84 @@ +CREATE VIEW babel_4529_stats +AS +SELECT stats_generation_method_desc FROM sys.stats +GO + +CREATE VIEW babel_4529_dm_exec_connections +AS +SELECT local_net_address, client_net_address, endpoint_id FROM sys.dm_exec_connections where local_net_address = 'test_address_4529'; +GO + +CREATE VIEW babel_4529_syscolumns +AS +SELECT printfmt FROM sys.syscolumns where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_system_objects +AS +SELECT * FROM sys.system_objects where name='test_key_constraints' +GO + +CREATE VIEW babel_4529_xml_indexes +AS +SELECT secondary_type FROM sys.xml_indexes +GO + +CREATE VIEW babel_4529_sysforeignkeys +AS +SELECT * FROM sys.sysforeignkeys where fkeyid IS NULL +GO + +CREATE VIEW babel_4529_data_spaces +AS +SELECT type FROM sys.data_spaces where type='FG' +GO + +CREATE VIEW babel_4529_sysprocesses +AS +SELECT * FROM sys.sysprocesses WHERE loginname = 'test_name_4529' +GO + +CREATE VIEW babel_4529_key_constraints +AS +SELECT type FROM sys.key_constraints where parent_object_id IS NULL +GO + +CREATE VIEW babel_4529_foreign_keys +AS +SELECT type FROM sys.foreign_keys where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_sysviews +AS +SELECT name, type, type_desc, create_date, modify_date, is_ms_shipped, is_published, is_schema_published, with_check_option, is_date_correlation_view, is_tracked_by_cdc, object_id, schema_id FROM sys.views where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_systables +AS +SELECT type FROM sys.tables where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_default_constraints +AS +SELECT type FROM sys.default_constraints where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_check_constraints +AS +SELECT type FROM sys.check_constraints where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_types +AS +SELECT name, collation_name, precision, scale, is_user_defined, is_assembly_type, is_table_type, default_object_id, rule_object_id, system_type_id, user_type_id, schema_id FROM sys.types where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_sp_sproc_columns_view +AS +SELECT precision, scale FROM sys.sp_sproc_columns_view where PROCEDURE_QUALIFIER = 'test_name_4529' +GO + +CREATE VIEW babel_4529_systypes +AS +SELECT prec FROM sys.systypes where name = 'test_name_4529' +GO diff --git a/test/JDBC/expected/BABEL-4529-vu-verify.out b/test/JDBC/expected/BABEL-4529-vu-verify.out new file mode 100644 index 00000000000..cfea6237322 --- /dev/null +++ b/test/JDBC/expected/BABEL-4529-vu-verify.out @@ -0,0 +1,119 @@ +SELECT * FROM babel_4529_stats +GO +~~START~~ +varchar +~~END~~ + + +SELECT * FROM babel_4529_dm_exec_connections +GO +~~START~~ +varchar#!#varchar#!#int +~~END~~ + + +SELECT * FROM babel_4529_syscolumns +GO +~~START~~ +varchar +~~END~~ + + +SELECT * FROM babel_4529_system_objects +GO +~~START~~ +varchar#!#int#!#int#!#int#!#int#!#char#!#nvarchar#!#datetime#!#datetime#!#bit#!#bit#!#bit +~~END~~ + + +SELECT * FROM babel_4529_xml_indexes +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_sysforeignkeys +GO +~~START~~ +int#!#int#!#int#!#smallint#!#smallint#!#smallint +~~END~~ + + +SELECT * FROM babel_4529_data_spaces +GO +~~START~~ +char +FG +~~END~~ + + +SELECT * FROM babel_4529_sysprocesses +GO +~~START~~ +int#!#smallint#!#int#!#binary#!#bigint#!#nchar#!#nchar#!#int#!#int#!#int#!#bigint#!#int#!#datetime#!#datetime#!#smallint#!#smallint#!#nchar#!#binary#!#nchar#!#nchar#!#nchar#!#nchar#!#nchar#!#nchar#!#nchar#!#nchar#!#nchar#!#varbinary#!#binary#!#int#!#int#!#int +~~END~~ + + +SELECT * FROM babel_4529_key_constraints +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_foreign_keys +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_sysviews +GO +~~START~~ +varchar#!#char#!#nvarchar#!#datetime#!#datetime#!#bit#!#bit#!#bit#!#bit#!#bit#!#bit#!#int#!#int +~~END~~ + + +SELECT * FROM babel_4529_systables +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_default_constraints +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_check_constraints +GO +~~START~~ +char +~~END~~ + + +SELECT * FROM babel_4529_types +GO +~~START~~ +varchar#!#varchar#!#tinyint#!#tinyint#!#bit#!#bit#!#bit#!#int#!#int#!#int#!#int#!#int +~~END~~ + + +SELECT * FROM babel_4529_sp_sproc_columns_view +GO +~~START~~ +int#!#smallint +~~END~~ + + +SELECT * FROM babel_4529_systypes +GO +~~START~~ +smallint +~~END~~ + diff --git a/test/JDBC/expected/BABEL_4367.out b/test/JDBC/expected/BABEL_4367.out index c4501ba4758..93057ebe04e 100644 --- a/test/JDBC/expected/BABEL_4367.out +++ b/test/JDBC/expected/BABEL_4367.out @@ -282,7 +282,7 @@ GO SELECT name FROM sys.types WHERE name in ('BABEL_4367_TYPE_1', 'BABEL_4367_TYPE_2') ORDER BY name ASC GO ~~START~~ -text +varchar ~~END~~ diff --git a/test/JDBC/expected/BABEL_4367_2.out b/test/JDBC/expected/BABEL_4367_2.out index 351aa33d9bf..b2e38dab687 100644 --- a/test/JDBC/expected/BABEL_4367_2.out +++ b/test/JDBC/expected/BABEL_4367_2.out @@ -94,7 +94,7 @@ varchar SELECT name FROM sys.types WHERE name LIKE 'babel_4367_%' ORDER BY name ASC GO ~~START~~ -text +varchar ~~END~~ @@ -121,7 +121,7 @@ babel_4367_v_2 SELECT name FROM sys.types WHERE name LIKE 'babel_4367_%' ORDER BY name ASC GO ~~START~~ -text +varchar babel_4367_t_1 babel_4367_t_2 ~~END~~ @@ -178,7 +178,7 @@ varchar SELECT name FROM sys.types WHERE name LIKE 'babel_4367_%' ORDER BY name ASC GO ~~START~~ -text +varchar ~~END~~ @@ -195,6 +195,6 @@ varchar SELECT name FROM sys.types WHERE name LIKE 'babel_4367_%' ORDER BY name ASC GO ~~START~~ -text +varchar ~~END~~ diff --git a/test/JDBC/expected/Test-sp_rename-vu-verify.out b/test/JDBC/expected/Test-sp_rename-vu-verify.out index 63cc9a2ad12..8c0b61f67d3 100644 --- a/test/JDBC/expected/Test-sp_rename-vu-verify.out +++ b/test/JDBC/expected/Test-sp_rename-vu-verify.out @@ -519,7 +519,7 @@ WHERE name LIKE '%sp_rename_vu%' ORDER BY schema_id, type_table_object_id, name; GO ~~START~~ -text +varchar sp_rename_vu_tabletype1 sp_rename_vu_tabletype1 ~~END~~ @@ -551,7 +551,7 @@ WHERE name LIKE '%sp_rename_vu%' ORDER BY schema_id, type_table_object_id, name; GO ~~START~~ -text +varchar sp_rename_vu_tabletype1_new sp_rename_vu_tabletype1_schema1_new ~~END~~ @@ -729,7 +729,7 @@ WHERE t1.is_user_defined = 1 AND t1.name LIKE '%sp_rename_vu%' ORDER BY t1.name, s1.name; GO ~~START~~ -text#!#varchar +varchar#!#varchar sp_rename_vu_alias1#!#dbo sp_rename_vu_alias1#!#sp_rename_vu_schema1 sp_rename_vu_tabletype1_new#!#dbo @@ -752,7 +752,7 @@ WHERE t1.is_user_defined = 1 AND t1.name LIKE '%sp_rename_vu%' ORDER BY t1.name, s1.name; GO ~~START~~ -text#!#varchar +varchar#!#varchar sp_rename_vu_alias2#!#dbo sp_rename_vu_alias2#!#sp_rename_vu_schema1 sp_rename_vu_tabletype1_new#!#dbo diff --git a/test/JDBC/expected/Test_ISNULL-vu-verify.out b/test/JDBC/expected/Test_ISNULL-vu-verify.out index 09b1ca40ab5..f675f2a3662 100644 --- a/test/JDBC/expected/Test_ISNULL-vu-verify.out +++ b/test/JDBC/expected/Test_ISNULL-vu-verify.out @@ -9,7 +9,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar varchar ~~END~~ @@ -114,7 +114,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar int ~~END~~ @@ -129,7 +129,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar varchar ~~END~~ @@ -144,7 +144,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar varchar ~~END~~ @@ -159,7 +159,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar varchar ~~END~~ @@ -174,7 +174,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar varchar ~~END~~ diff --git a/test/JDBC/expected/alter_authorization_change_db_owner-vu-verify.out b/test/JDBC/expected/alter_authorization_change_db_owner-vu-verify.out index e7454698da4..124f4aeb580 100644 --- a/test/JDBC/expected/alter_authorization_change_db_owner-vu-verify.out +++ b/test/JDBC/expected/alter_authorization_change_db_owner-vu-verify.out @@ -658,8 +658,9 @@ go declare @spid int, @cmd varchar(30) while (1=1) begin +-- case lower(loginname) = 'dba_login' is not working here. will have to file a seperate JIRA select @spid = spid from sys.sysprocesses where spid <> @@spid and spid not in (select killed from #killed) -and (lower(loginname) like 'new_owner_login%' or lower(loginname) like 'login6%' or lower(loginname) = 'dba_login') +and (lower(loginname) like 'new_owner_login%' or lower(loginname) like 'login6%' or loginname = 'dba_login') if @@rowcount = 0 break insert #killed values(@spid) set @cmd = 'kill ' + convert(varchar, @spid) diff --git a/test/JDBC/expected/latest__verification_cleanup__14_3__sys-types-vu-verify.out b/test/JDBC/expected/latest__verification_cleanup__14_3__sys-types-vu-verify.out index 2cb5e800187..cb98c5701c9 100644 --- a/test/JDBC/expected/latest__verification_cleanup__14_3__sys-types-vu-verify.out +++ b/test/JDBC/expected/latest__verification_cleanup__14_3__sys-types-vu-verify.out @@ -6,7 +6,7 @@ select cast(name as varchar(20)) from sys.types where is_user_defined = 0 order by name asc; GO ~~START~~ -varchar#!#smallint#!#int#!#int#!#varchar +varchar#!#smallint#!#tinyint#!#tinyint#!#varchar bigint#!#8#!#19#!#0#!# binary#!#8000#!#0#!#0#!# bit#!#1#!#1#!#0#!# @@ -53,7 +53,7 @@ select cast(name as varchar(20)) from sys.types where is_user_defined = 1 order by name asc; GO ~~START~~ -varchar#!#smallint#!#int#!#int#!#varchar +varchar#!#smallint#!#tinyint#!#tinyint#!#varchar my_type#!#4#!#10#!#0#!# my_type2#!#20#!#0#!#0#!#sql_latin1_general_cp1_ci_as tbl_type_sys_types#!#-1#!#0#!#0#!# diff --git a/test/JDBC/expected/latest__verification_cleanup__14_5__sys-types-vu-verify.out b/test/JDBC/expected/latest__verification_cleanup__14_5__sys-types-vu-verify.out index 2cb5e800187..cb98c5701c9 100644 --- a/test/JDBC/expected/latest__verification_cleanup__14_5__sys-types-vu-verify.out +++ b/test/JDBC/expected/latest__verification_cleanup__14_5__sys-types-vu-verify.out @@ -6,7 +6,7 @@ select cast(name as varchar(20)) from sys.types where is_user_defined = 0 order by name asc; GO ~~START~~ -varchar#!#smallint#!#int#!#int#!#varchar +varchar#!#smallint#!#tinyint#!#tinyint#!#varchar bigint#!#8#!#19#!#0#!# binary#!#8000#!#0#!#0#!# bit#!#1#!#1#!#0#!# @@ -53,7 +53,7 @@ select cast(name as varchar(20)) from sys.types where is_user_defined = 1 order by name asc; GO ~~START~~ -varchar#!#smallint#!#int#!#int#!#varchar +varchar#!#smallint#!#tinyint#!#tinyint#!#varchar my_type#!#4#!#10#!#0#!# my_type2#!#20#!#0#!#0#!#sql_latin1_general_cp1_ci_as tbl_type_sys_types#!#-1#!#0#!#0#!# diff --git a/test/JDBC/expected/latest__verification_cleanup__14_6__Test_ISNULL-vu-verify.out b/test/JDBC/expected/latest__verification_cleanup__14_6__Test_ISNULL-vu-verify.out index 4e04bc8de02..df454f5cef2 100644 --- a/test/JDBC/expected/latest__verification_cleanup__14_6__Test_ISNULL-vu-verify.out +++ b/test/JDBC/expected/latest__verification_cleanup__14_6__Test_ISNULL-vu-verify.out @@ -9,7 +9,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar bigint ~~END~~ @@ -106,7 +106,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar int ~~END~~ @@ -121,7 +121,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar text ~~END~~ @@ -136,7 +136,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar varchar ~~END~~ @@ -151,7 +151,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar int ~~END~~ diff --git a/test/JDBC/expected/latest__verification_cleanup__15_2__Test_ISNULL-vu-verify.out b/test/JDBC/expected/latest__verification_cleanup__15_2__Test_ISNULL-vu-verify.out index 4e04bc8de02..df454f5cef2 100644 --- a/test/JDBC/expected/latest__verification_cleanup__15_2__Test_ISNULL-vu-verify.out +++ b/test/JDBC/expected/latest__verification_cleanup__15_2__Test_ISNULL-vu-verify.out @@ -9,7 +9,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar bigint ~~END~~ @@ -106,7 +106,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar int ~~END~~ @@ -121,7 +121,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar text ~~END~~ @@ -136,7 +136,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar varchar ~~END~~ @@ -151,7 +151,7 @@ select name from sys.types where system_type_id = ) and is_user_defined = 0; GO ~~START~~ -text +varchar int ~~END~~ diff --git a/test/JDBC/expected/sys-data_spaces.out b/test/JDBC/expected/sys-data_spaces.out index e7febd2c90b..5937209056e 100644 --- a/test/JDBC/expected/sys-data_spaces.out +++ b/test/JDBC/expected/sys-data_spaces.out @@ -5,3 +5,20 @@ varchar#!#int#!#char#!#nvarchar#!#bit#!#bit PRIMARY#!#1#!#FG#!#ROWS_FILEGROUP#!#1#!#0 ~~END~~ + +-- test case-sementics of column type. +SELECT type FROM sys.data_spaces where type='FG'; +GO +~~START~~ +char +FG +~~END~~ + + +SELECT type FROM sys.data_spaces where type='fg'; +GO +~~START~~ +char +FG +~~END~~ + diff --git a/test/JDBC/expected/sys-key_constraints.out b/test/JDBC/expected/sys-key_constraints.out index 82635fb1e94..421302e768a 100644 --- a/test/JDBC/expected/sys-key_constraints.out +++ b/test/JDBC/expected/sys-key_constraints.out @@ -15,6 +15,22 @@ int ~~END~~ +select count(*) from sys.key_constraints where parent_object_id = object_id('uq_1') and type = 'UQ'; +GO +~~START~~ +int +1 +~~END~~ + + +select count(*) from sys.key_constraints where parent_object_id = object_id('uq_1') and type = 'uq'; +GO +~~START~~ +int +1 +~~END~~ + + USE master GO diff --git a/test/JDBC/expected/sys-sysforeignkeys.out b/test/JDBC/expected/sys-sysforeignkeys.out index d1e405d9518..aec2586d93b 100644 --- a/test/JDBC/expected/sys-sysforeignkeys.out +++ b/test/JDBC/expected/sys-sysforeignkeys.out @@ -52,6 +52,22 @@ int ~~END~~ +select count(*) from sys.foreign_keys where parent_object_id = object_id('fk_2') and type='f'; +GO +~~START~~ +int +1 +~~END~~ + + +select count(*) from sys.foreign_keys where parent_object_id = object_id('fk_2') and type='F'; +GO +~~START~~ +int +1 +~~END~~ + + USE master GO diff --git a/test/JDBC/expected/sys-table_types-vu-verify.out b/test/JDBC/expected/sys-table_types-vu-verify.out index 7a009f04105..7babb933db4 100644 --- a/test/JDBC/expected/sys-table_types-vu-verify.out +++ b/test/JDBC/expected/sys-table_types-vu-verify.out @@ -18,7 +18,7 @@ from sys.table_types where name = 'sys_table_types_vu_prepare_tt'; GO ~~START~~ -text#!#int#!#int#!#smallint#!#int#!#int#!#varchar#!#bit#!#int#!#int#!#int#!#int#!#int#!#bit +varchar#!#int#!#int#!#smallint#!#tinyint#!#tinyint#!#varchar#!#bit#!#bit#!#bit#!#int#!#int#!#bit#!#bit sys_table_types_vu_prepare_tt#!#0#!##!#-1#!#0#!#0#!##!#0#!#1#!#0#!#0#!#0#!#1#!#0 ~~END~~ diff --git a/test/JDBC/expected/sys-table_types.out b/test/JDBC/expected/sys-table_types.out index c4f34b547ac..4e353110d85 100644 --- a/test/JDBC/expected/sys-table_types.out +++ b/test/JDBC/expected/sys-table_types.out @@ -22,7 +22,7 @@ from sys.table_types where name = 'tt_type'; GO ~~START~~ -text#!#int#!#int#!#smallint#!#int#!#int#!#varchar#!#bit#!#int#!#int#!#int#!#int#!#int#!#bit +varchar#!#int#!#int#!#smallint#!#tinyint#!#tinyint#!#varchar#!#bit#!#bit#!#bit#!#int#!#int#!#bit#!#bit tt_type#!#0#!##!#-1#!#0#!#0#!##!#0#!#1#!#0#!#0#!#0#!#1#!#0 ~~END~~ diff --git a/test/JDBC/expected/sys-tables.out b/test/JDBC/expected/sys-tables.out index 5b67d3d3ceb..f60097069b4 100644 --- a/test/JDBC/expected/sys-tables.out +++ b/test/JDBC/expected/sys-tables.out @@ -15,6 +15,21 @@ int ~~END~~ +SELECT COUNT(*) FROM sys.tables WHERE name = 'rand_name1' and type='U'; +GO +~~START~~ +int +1 +~~END~~ + +SELECT COUNT(*) FROM sys.tables WHERE name = 'rand_name1' and type='u'; +GO +~~START~~ +int +1 +~~END~~ + + SELECT COUNT(*) FROM sys.columns WHERE name COLLATE bbf_unicode_general_ci_as = 'rand_col1'; GO ~~START~~ diff --git a/test/JDBC/expected/sys-types-vu-verify.out b/test/JDBC/expected/sys-types-vu-verify.out index b39cb384eae..e039201042b 100644 --- a/test/JDBC/expected/sys-types-vu-verify.out +++ b/test/JDBC/expected/sys-types-vu-verify.out @@ -6,7 +6,7 @@ select cast(name as varchar(20)) from sys.types where is_user_defined = 0 order by name asc; GO ~~START~~ -varchar#!#smallint#!#int#!#int#!#varchar +varchar#!#smallint#!#tinyint#!#tinyint#!#varchar bigint#!#8#!#19#!#0#!# binary#!#8000#!#0#!#0#!# bit#!#1#!#1#!#0#!# @@ -53,7 +53,7 @@ select cast(name as varchar(20)) from sys.types where is_user_defined = 1 order by name asc; GO ~~START~~ -varchar#!#smallint#!#int#!#int#!#varchar +varchar#!#smallint#!#tinyint#!#tinyint#!#varchar my_type#!#4#!#10#!#0#!# my_type2#!#20#!#0#!#0#!#bbf_unicode_cp1_ci_as tbl_type_sys_types#!#-1#!#0#!#0#!# diff --git a/test/JDBC/expected/sys-types.out b/test/JDBC/expected/sys-types.out index d1057c972c8..db2c2674f18 100644 --- a/test/JDBC/expected/sys-types.out +++ b/test/JDBC/expected/sys-types.out @@ -6,7 +6,7 @@ select cast(name as varchar(20)) from sys.types where is_user_defined = 0 order by name asc; GO ~~START~~ -varchar#!#smallint#!#int#!#int#!#varchar +varchar#!#smallint#!#tinyint#!#tinyint#!#varchar bigint#!#8#!#19#!#0#!# binary#!#8000#!#0#!#0#!# bit#!#1#!#1#!#0#!# @@ -54,6 +54,22 @@ GO CREATE TYPE my_type2 FROM varchar(20); GO +select count(*) from sys.types where collation_name='bbf_unicode_cp1_ci_as'; +GO +~~START~~ +int +7 +~~END~~ + + +select count(*) from sys.types where collation_name='BBF_unicode_CP1_ci_as'; +GO +~~START~~ +int +7 +~~END~~ + + select cast(name as varchar(20)) , max_length , precision @@ -62,7 +78,7 @@ select cast(name as varchar(20)) from sys.types where is_user_defined = 1 order by name asc; GO ~~START~~ -varchar#!#smallint#!#int#!#int#!#varchar +varchar#!#smallint#!#tinyint#!#tinyint#!#varchar my_type#!#4#!#10#!#0#!# my_type2#!#20#!#0#!#0#!#bbf_unicode_cp1_ci_as ~~END~~ @@ -76,6 +92,14 @@ int ~~END~~ +SELECT count(*) FROM sys.types WHERE name = 'MY_TYPE'; +GO +~~START~~ +int +1 +~~END~~ + + CREATE TYPE tbl_type_sys_types AS TABLE(a INT); GO @@ -86,6 +110,13 @@ int 1 ~~END~~ +SELECT count(*) FROM sys.types WHERE name = 'TBL_type_SYS_types'; +GO +~~START~~ +int +1 +~~END~~ + USE master; GO diff --git a/test/JDBC/expected/sys-views.out b/test/JDBC/expected/sys-views.out index 1e78e5952a7..9a53c0b6022 100644 --- a/test/JDBC/expected/sys-views.out +++ b/test/JDBC/expected/sys-views.out @@ -15,6 +15,46 @@ int ~~END~~ +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1'; +GO +~~START~~ +int +1 +~~END~~ + + +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1' and type='V'; +GO +~~START~~ +int +1 +~~END~~ + + +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1' and type='v'; +GO +~~START~~ +int +1 +~~END~~ + + +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1' and type_desc='VIEW'; +GO +~~START~~ +int +1 +~~END~~ + + +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1' and type_desc='view'; +GO +~~START~~ +int +1 +~~END~~ + + SELECT COUNT(*) FROM sys.objects WHERE type='V' and name = 'rand_name1'; GO ~~START~~ diff --git a/test/JDBC/input/BABEL-2829.sql b/test/JDBC/input/BABEL-2829.sql index 7771553117f..364cc26385c 100644 --- a/test/JDBC/input/BABEL-2829.sql +++ b/test/JDBC/input/BABEL-2829.sql @@ -59,4 +59,15 @@ DROP TABLE table_2829 GO DROP DATABASE db_2829 -GO \ No newline at end of file +GO + +-- test case-sementics of sys.sysprocesses catalog. +-- lastwaittype, waitresource column can not be test as these are NULL. +-- hostprocess colum is ID in char +-- cmd, nt_domain, nt_username, net_address, net_library testing can't be done as these are NULL. +SELECT CASE WHEN (select count(status) from sys.sysprocesses where status='active') = (select count(status) from sys.sysprocesses where status='ACTIVE') THEN 1 ELSE 0 END AS result; +GO +SELECT CASE WHEN (select count(program_name) from sys.sysprocesses where program_name='sqlcmd') = (select count(program_name) from sys.sysprocesses where program_name='SQLCMD') THEN 1 ELSE 0 END AS result; +GO +SELECT CASE WHEN (select count(loginname) from sys.sysprocesses where loginname='jdbc_user') = (select count(loginname) from sys.sysprocesses where loginname='JDBC_user') THEN 1 ELSE 0 END AS result; +GO diff --git a/test/JDBC/input/BABEL-4529-before-15_6-or-14_11-vu-cleanup.sql b/test/JDBC/input/BABEL-4529-before-15_6-or-14_11-vu-cleanup.sql new file mode 100644 index 00000000000..53b0edc4c3c --- /dev/null +++ b/test/JDBC/input/BABEL-4529-before-15_6-or-14_11-vu-cleanup.sql @@ -0,0 +1,50 @@ +DROP VIEW IF EXISTS babel_4529_stats +GO + +DROP VIEW IF EXISTS babel_4529_dm_exec_connections +GO + +DROP VIEW IF EXISTS babel_4529_syscolumns +GO + +DROP VIEW IF EXISTS babel_4529_system_objects +GO + +DROP VIEW IF EXISTS babel_4529_xml_indexes +GO + +DROP VIEW IF EXISTS babel_4529_sysforeignkeys +GO + +DROP VIEW IF EXISTS babel_4529_data_spaces +GO + +DROP VIEW IF EXISTS babel_4529_sysprocesses +GO + +DROP VIEW IF EXISTS babel_4529_key_constraints +GO + +DROP VIEW IF EXISTS babel_4529_foreign_keys +GO + +DROP VIEW IF EXISTS babel_4529_sysviews +GO + +DROP VIEW IF EXISTS babel_4529_systables +GO + +DROP VIEW IF EXISTS babel_4529_default_constraints +GO + +DROP VIEW IF EXISTS babel_4529_check_constraints +GO + +DROP VIEW IF EXISTS babel_4529_types +GO + +DROP VIEW IF EXISTS babel_4529_sp_sproc_columns_view +GO + +DROP VIEW IF EXISTS babel_4529_systypes +GO diff --git a/test/JDBC/input/BABEL-4529-before-15_6-or-14_11-vu-prepare.sql b/test/JDBC/input/BABEL-4529-before-15_6-or-14_11-vu-prepare.sql new file mode 100644 index 00000000000..561d6d7c49f --- /dev/null +++ b/test/JDBC/input/BABEL-4529-before-15_6-or-14_11-vu-prepare.sql @@ -0,0 +1,84 @@ +CREATE VIEW babel_4529_stats +AS +SELECT stats_generation_method_desc FROM sys.stats +GO + +CREATE VIEW babel_4529_dm_exec_connections +AS +SELECT local_net_address, client_net_address, endpoint_id FROM sys.dm_exec_connections where local_net_address = 'test_address_4529'; +GO + +CREATE VIEW babel_4529_syscolumns +AS +SELECT printfmt FROM sys.syscolumns where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_system_objects +AS +SELECT * FROM sys.system_objects where name='test_key_constraints' +GO + +CREATE VIEW babel_4529_xml_indexes +AS +SELECT secondary_type FROM sys.xml_indexes +GO + +CREATE VIEW babel_4529_sysforeignkeys +AS +SELECT * FROM sys.sysforeignkeys where fkeyid IS NULL +GO + +CREATE VIEW babel_4529_data_spaces +AS +SELECT type FROM sys.data_spaces where type='FG' +GO + +CREATE VIEW babel_4529_sysprocesses +AS +SELECT * FROM sys.sysprocesses WHERE loginname = 'test_name_4529' +GO + +CREATE VIEW babel_4529_key_constraints +AS +SELECT type FROM sys.key_constraints where parent_object_id IS NULL +GO + +CREATE VIEW babel_4529_foreign_keys +AS +SELECT type FROM sys.foreign_keys where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_sysviews +AS +SELECT name, type, type_desc, create_date, modify_date, is_ms_shipped, is_published, is_schema_published, with_check_option, is_date_correlation_view, is_tracked_by_cdc, object_id, schema_id FROM sys.views where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_systables +AS +SELECT type FROM sys.tables where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_default_constraints +AS +SELECT type FROM sys.default_constraints where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_check_constraints +AS +SELECT type FROM sys.check_constraints where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_types +AS +SELECT name, collation_name, precision, scale, is_user_defined, is_assembly_type, is_table_type, default_object_id, rule_object_id, system_type_id, user_type_id, schema_id FROM sys.types where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_sp_sproc_columns_view +AS +SELECT precision, scale FROM sys.sp_sproc_columns_view where PROCEDURE_QUALIFIER = 'test_name_4529' +GO + +CREATE VIEW babel_4529_systypes +AS +SELECT prec FROM sys.systypes where name = 'test_name_4529' +GO diff --git a/test/JDBC/input/BABEL-4529-before-15_6-or-14_11-vu-verify.sql b/test/JDBC/input/BABEL-4529-before-15_6-or-14_11-vu-verify.sql new file mode 100644 index 00000000000..2762e4f5d11 --- /dev/null +++ b/test/JDBC/input/BABEL-4529-before-15_6-or-14_11-vu-verify.sql @@ -0,0 +1,50 @@ +SELECT * FROM babel_4529_stats +GO + +SELECT * FROM babel_4529_dm_exec_connections +GO + +SELECT * FROM babel_4529_syscolumns +GO + +SELECT * FROM babel_4529_system_objects +GO + +SELECT * FROM babel_4529_xml_indexes +GO + +SELECT * FROM babel_4529_sysforeignkeys +GO + +SELECT * FROM babel_4529_data_spaces +GO + +SELECT * FROM babel_4529_sysprocesses +GO + +SELECT * FROM babel_4529_key_constraints +GO + +SELECT * FROM babel_4529_foreign_keys +GO + +SELECT * FROM babel_4529_sysviews +GO + +SELECT * FROM babel_4529_systables +GO + +SELECT * FROM babel_4529_default_constraints +GO + +SELECT * FROM babel_4529_check_constraints +GO + +SELECT * FROM babel_4529_types +GO + +SELECT * FROM babel_4529_sp_sproc_columns_view +GO + +SELECT * FROM babel_4529_systypes +GO diff --git a/test/JDBC/input/BABEL-4529-vu-cleanup.sql b/test/JDBC/input/BABEL-4529-vu-cleanup.sql new file mode 100644 index 00000000000..53b0edc4c3c --- /dev/null +++ b/test/JDBC/input/BABEL-4529-vu-cleanup.sql @@ -0,0 +1,50 @@ +DROP VIEW IF EXISTS babel_4529_stats +GO + +DROP VIEW IF EXISTS babel_4529_dm_exec_connections +GO + +DROP VIEW IF EXISTS babel_4529_syscolumns +GO + +DROP VIEW IF EXISTS babel_4529_system_objects +GO + +DROP VIEW IF EXISTS babel_4529_xml_indexes +GO + +DROP VIEW IF EXISTS babel_4529_sysforeignkeys +GO + +DROP VIEW IF EXISTS babel_4529_data_spaces +GO + +DROP VIEW IF EXISTS babel_4529_sysprocesses +GO + +DROP VIEW IF EXISTS babel_4529_key_constraints +GO + +DROP VIEW IF EXISTS babel_4529_foreign_keys +GO + +DROP VIEW IF EXISTS babel_4529_sysviews +GO + +DROP VIEW IF EXISTS babel_4529_systables +GO + +DROP VIEW IF EXISTS babel_4529_default_constraints +GO + +DROP VIEW IF EXISTS babel_4529_check_constraints +GO + +DROP VIEW IF EXISTS babel_4529_types +GO + +DROP VIEW IF EXISTS babel_4529_sp_sproc_columns_view +GO + +DROP VIEW IF EXISTS babel_4529_systypes +GO diff --git a/test/JDBC/input/BABEL-4529-vu-prepare.sql b/test/JDBC/input/BABEL-4529-vu-prepare.sql new file mode 100644 index 00000000000..561d6d7c49f --- /dev/null +++ b/test/JDBC/input/BABEL-4529-vu-prepare.sql @@ -0,0 +1,84 @@ +CREATE VIEW babel_4529_stats +AS +SELECT stats_generation_method_desc FROM sys.stats +GO + +CREATE VIEW babel_4529_dm_exec_connections +AS +SELECT local_net_address, client_net_address, endpoint_id FROM sys.dm_exec_connections where local_net_address = 'test_address_4529'; +GO + +CREATE VIEW babel_4529_syscolumns +AS +SELECT printfmt FROM sys.syscolumns where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_system_objects +AS +SELECT * FROM sys.system_objects where name='test_key_constraints' +GO + +CREATE VIEW babel_4529_xml_indexes +AS +SELECT secondary_type FROM sys.xml_indexes +GO + +CREATE VIEW babel_4529_sysforeignkeys +AS +SELECT * FROM sys.sysforeignkeys where fkeyid IS NULL +GO + +CREATE VIEW babel_4529_data_spaces +AS +SELECT type FROM sys.data_spaces where type='FG' +GO + +CREATE VIEW babel_4529_sysprocesses +AS +SELECT * FROM sys.sysprocesses WHERE loginname = 'test_name_4529' +GO + +CREATE VIEW babel_4529_key_constraints +AS +SELECT type FROM sys.key_constraints where parent_object_id IS NULL +GO + +CREATE VIEW babel_4529_foreign_keys +AS +SELECT type FROM sys.foreign_keys where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_sysviews +AS +SELECT name, type, type_desc, create_date, modify_date, is_ms_shipped, is_published, is_schema_published, with_check_option, is_date_correlation_view, is_tracked_by_cdc, object_id, schema_id FROM sys.views where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_systables +AS +SELECT type FROM sys.tables where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_default_constraints +AS +SELECT type FROM sys.default_constraints where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_check_constraints +AS +SELECT type FROM sys.check_constraints where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_types +AS +SELECT name, collation_name, precision, scale, is_user_defined, is_assembly_type, is_table_type, default_object_id, rule_object_id, system_type_id, user_type_id, schema_id FROM sys.types where name = 'test_name_4529' +GO + +CREATE VIEW babel_4529_sp_sproc_columns_view +AS +SELECT precision, scale FROM sys.sp_sproc_columns_view where PROCEDURE_QUALIFIER = 'test_name_4529' +GO + +CREATE VIEW babel_4529_systypes +AS +SELECT prec FROM sys.systypes where name = 'test_name_4529' +GO diff --git a/test/JDBC/input/BABEL-4529-vu-verify.sql b/test/JDBC/input/BABEL-4529-vu-verify.sql new file mode 100644 index 00000000000..2762e4f5d11 --- /dev/null +++ b/test/JDBC/input/BABEL-4529-vu-verify.sql @@ -0,0 +1,50 @@ +SELECT * FROM babel_4529_stats +GO + +SELECT * FROM babel_4529_dm_exec_connections +GO + +SELECT * FROM babel_4529_syscolumns +GO + +SELECT * FROM babel_4529_system_objects +GO + +SELECT * FROM babel_4529_xml_indexes +GO + +SELECT * FROM babel_4529_sysforeignkeys +GO + +SELECT * FROM babel_4529_data_spaces +GO + +SELECT * FROM babel_4529_sysprocesses +GO + +SELECT * FROM babel_4529_key_constraints +GO + +SELECT * FROM babel_4529_foreign_keys +GO + +SELECT * FROM babel_4529_sysviews +GO + +SELECT * FROM babel_4529_systables +GO + +SELECT * FROM babel_4529_default_constraints +GO + +SELECT * FROM babel_4529_check_constraints +GO + +SELECT * FROM babel_4529_types +GO + +SELECT * FROM babel_4529_sp_sproc_columns_view +GO + +SELECT * FROM babel_4529_systypes +GO diff --git a/test/JDBC/input/alter_authorization_change_db_owner-vu-verify.mix b/test/JDBC/input/alter_authorization_change_db_owner-vu-verify.mix index 113a4f93f1c..0db1a93af57 100644 --- a/test/JDBC/input/alter_authorization_change_db_owner-vu-verify.mix +++ b/test/JDBC/input/alter_authorization_change_db_owner-vu-verify.mix @@ -379,8 +379,9 @@ go declare @spid int, @cmd varchar(30) while (1=1) begin +-- case lower(loginname) = 'dba_login' is not working here. will have to file a seperate JIRA select @spid = spid from sys.sysprocesses where spid <> @@spid and spid not in (select killed from #killed) -and (lower(loginname) like 'new_owner_login%' or lower(loginname) like 'login6%' or lower(loginname) = 'dba_login') +and (lower(loginname) like 'new_owner_login%' or lower(loginname) like 'login6%' or loginname = 'dba_login') if @@rowcount = 0 break insert #killed values(@spid) set @cmd = 'kill ' + convert(varchar, @spid) diff --git a/test/JDBC/input/views/sys-check_constraints.sql b/test/JDBC/input/views/sys-check_constraints.sql index 86c1542d3ff..0eba18bcec1 100644 --- a/test/JDBC/input/views/sys-check_constraints.sql +++ b/test/JDBC/input/views/sys-check_constraints.sql @@ -9,5 +9,11 @@ GO SELECT name FROM sys.check_constraints WHERE NAME IN ('sys_check_constraints_sck_date_col_check') GO +SELECT COUNT(*) FROM sys.check_constraints WHERE NAME IN ('sys_check_constraints_sck_date_col_check') and type = 'C' +GO + +SELECT COUNT(*) FROM sys.check_constraints WHERE NAME IN ('sys_check_constraints_sck_date_col_check') and type = 'c' +GO + DROP TABLE IF EXISTS sys_check_constraints GO diff --git a/test/JDBC/input/views/sys-data_spaces.sql b/test/JDBC/input/views/sys-data_spaces.sql index 8814bd91b6d..6bf6ce26539 100644 --- a/test/JDBC/input/views/sys-data_spaces.sql +++ b/test/JDBC/input/views/sys-data_spaces.sql @@ -1,2 +1,9 @@ SELECT * FROM sys.data_spaces; GO + +-- test case-sementics of column type. +SELECT type FROM sys.data_spaces where type='FG'; +GO + +SELECT type FROM sys.data_spaces where type='fg'; +GO diff --git a/test/JDBC/input/views/sys-default_constraints.sql b/test/JDBC/input/views/sys-default_constraints.sql index 9e4bc44de9b..0df65048b4e 100644 --- a/test/JDBC/input/views/sys-default_constraints.sql +++ b/test/JDBC/input/views/sys-default_constraints.sql @@ -10,6 +10,12 @@ GO SELECT definition FROM sys.default_constraints where name LIKE '%sys_default_definitions%' GO +SELECT COUNT(*) FROM sys.default_constraints where name LIKE '%sys_default_definitions%' and type='D'; +GO + +SELECT COUNT(*) FROM sys.default_constraints where name LIKE '%sys_default_definitions%' and type='d'; +GO + SELECT COUNT(*) FROM sys.all_columns WHERE object_id = object_id('sys.default_constraints'); GO diff --git a/test/JDBC/input/views/sys-key_constraints.sql b/test/JDBC/input/views/sys-key_constraints.sql index a7378dd53ad..27aa27bf263 100644 --- a/test/JDBC/input/views/sys-key_constraints.sql +++ b/test/JDBC/input/views/sys-key_constraints.sql @@ -10,6 +10,12 @@ GO select count(*) from sys.key_constraints where parent_object_id = object_id('uq_1'); GO +select count(*) from sys.key_constraints where parent_object_id = object_id('uq_1') and type = 'UQ'; +GO + +select count(*) from sys.key_constraints where parent_object_id = object_id('uq_1') and type = 'uq'; +GO + USE master GO diff --git a/test/JDBC/input/views/sys-sysforeignkeys.sql b/test/JDBC/input/views/sys-sysforeignkeys.sql index c36799c5372..bb096315b4d 100644 --- a/test/JDBC/input/views/sys-sysforeignkeys.sql +++ b/test/JDBC/input/views/sys-sysforeignkeys.sql @@ -27,6 +27,12 @@ go select count(*) from sys.foreign_keys where parent_object_id = object_id('fk_2'); GO +select count(*) from sys.foreign_keys where parent_object_id = object_id('fk_2') and type='f'; +GO + +select count(*) from sys.foreign_keys where parent_object_id = object_id('fk_2') and type='F'; +GO + USE master GO diff --git a/test/JDBC/input/views/sys-tables.sql b/test/JDBC/input/views/sys-tables.sql index fd869de00ed..2c582130655 100644 --- a/test/JDBC/input/views/sys-tables.sql +++ b/test/JDBC/input/views/sys-tables.sql @@ -10,6 +10,11 @@ GO SELECT COUNT(*) FROM sys.tables WHERE name COLLATE bbf_unicode_general_ci_as = 'rand_name1'; GO +SELECT COUNT(*) FROM sys.tables WHERE name = 'rand_name1' and type='U'; +GO +SELECT COUNT(*) FROM sys.tables WHERE name = 'rand_name1' and type='u'; +GO + SELECT COUNT(*) FROM sys.columns WHERE name COLLATE bbf_unicode_general_ci_as = 'rand_col1'; GO diff --git a/test/JDBC/input/views/sys-types.sql b/test/JDBC/input/views/sys-types.sql index 92f73ebbb80..9c1ba9b217a 100644 --- a/test/JDBC/input/views/sys-types.sql +++ b/test/JDBC/input/views/sys-types.sql @@ -18,6 +18,12 @@ GO CREATE TYPE my_type2 FROM varchar(20); GO +select count(*) from sys.types where collation_name='bbf_unicode_cp1_ci_as'; +GO + +select count(*) from sys.types where collation_name='BBF_unicode_CP1_ci_as'; +GO + select cast(name as varchar(20)) , max_length , precision @@ -29,11 +35,16 @@ GO SELECT count(*) FROM sys.types WHERE name = 'my_type'; GO +SELECT count(*) FROM sys.types WHERE name = 'MY_TYPE'; +GO + CREATE TYPE tbl_type_sys_types AS TABLE(a INT); GO SELECT count(*) FROM sys.types WHERE name = 'tbl_type_sys_types'; GO +SELECT count(*) FROM sys.types WHERE name = 'TBL_type_SYS_types'; +GO USE master; GO diff --git a/test/JDBC/input/views/sys-views.sql b/test/JDBC/input/views/sys-views.sql index 359de286fa6..82015387270 100644 --- a/test/JDBC/input/views/sys-views.sql +++ b/test/JDBC/input/views/sys-views.sql @@ -10,6 +10,21 @@ GO SELECT COUNT(*) FROM sys.views WHERE name = 'rand_name1'; GO +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1'; +GO + +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1' and type='V'; +GO + +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1' and type='v'; +GO + +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1' and type_desc='VIEW'; +GO + +SELECT count(*) FROM sys.views WHERE name='RAND_nAME1' and type_desc='view'; +GO + SELECT COUNT(*) FROM sys.objects WHERE type='V' and name = 'rand_name1'; GO diff --git a/test/JDBC/jdbc_schedule b/test/JDBC/jdbc_schedule index b2ebe22fd3f..549dc69cc7e 100644 --- a/test/JDBC/jdbc_schedule +++ b/test/JDBC/jdbc_schedule @@ -102,6 +102,9 @@ ignore#!#forjson-datatypes-before-14_10-or-15_5-vu-cleanup ignore#!#sys-parsename-before-15_6-or-16_1-vu-prepare ignore#!#sys-parsename-before-15_6-or-16_1-vu-verify ignore#!#sys-parsename-before-15_6-or-16_1-vu-cleanup +ignore#!#BABEL-4529-before-15_6-or-14_11-vu-prepare +ignore#!#BABEL-4529-before-15_6-or-14_11-vu-verify +ignore#!#BABEL-4529-before-15_6-or-14_11-vu-cleanup ignore#!#AUTO_ANALYZE-before-15-5-or-14-10-vu-prepare ignore#!#AUTO_ANALYZE-before-15-5-or-14-10-vu-verify ignore#!#orderby-before-15_3-vu-prepare diff --git a/test/JDBC/sql_expected/sys-check_constraints.out b/test/JDBC/sql_expected/sys-check_constraints.out index 1c3ece109f7..0655d40e953 100644 --- a/test/JDBC/sql_expected/sys-check_constraints.out +++ b/test/JDBC/sql_expected/sys-check_constraints.out @@ -14,5 +14,21 @@ sys_check_constraints_sck_date_col_check ~~END~~ +SELECT COUNT(*) FROM sys.check_constraints WHERE NAME IN ('sys_check_constraints_sck_date_col_check') and type = 'C' +GO +~~START~~ +int +1 +~~END~~ + + +SELECT COUNT(*) FROM sys.check_constraints WHERE NAME IN ('sys_check_constraints_sck_date_col_check') and type = 'c' +GO +~~START~~ +int +1 +~~END~~ + + DROP TABLE IF EXISTS sys_check_constraints GO diff --git a/test/JDBC/sql_expected/sys-default_constraints.out b/test/JDBC/sql_expected/sys-default_constraints.out index 71603290a59..c0d7f992b84 100644 --- a/test/JDBC/sql_expected/sys-default_constraints.out +++ b/test/JDBC/sql_expected/sys-default_constraints.out @@ -15,6 +15,22 @@ nvarchar ~~END~~ +SELECT COUNT(*) FROM sys.default_constraints where name LIKE '%sys_default_definitions%' and type='D'; +GO +~~START~~ +int +1 +~~END~~ + + +SELECT COUNT(*) FROM sys.default_constraints where name LIKE '%sys_default_definitions%' and type='d'; +GO +~~START~~ +int +1 +~~END~~ + + SELECT COUNT(*) FROM sys.all_columns WHERE object_id = object_id('sys.default_constraints'); GO ~~START~~ diff --git a/test/JDBC/upgrade/14_10/schedule b/test/JDBC/upgrade/14_10/schedule index daed233ad7e..92567b14108 100644 --- a/test/JDBC/upgrade/14_10/schedule +++ b/test/JDBC/upgrade/14_10/schedule @@ -420,4 +420,5 @@ BABEL-2999 BABEL-4606 sys-parsename-before-15_6-or-16_1 permission_restrictions_from_pg +BABEL-4529-before-15_6-or-14_11 BABEL-730-before-15_6-or-16_1 diff --git a/test/JDBC/upgrade/15_5/schedule b/test/JDBC/upgrade/15_5/schedule index 761943860d7..bbbc07420cf 100644 --- a/test/JDBC/upgrade/15_5/schedule +++ b/test/JDBC/upgrade/15_5/schedule @@ -487,4 +487,5 @@ babel_index_nulls_order BABEL-2999 BABEL-4606 permission_restrictions_from_pg +BABEL-4529-before-15_6-or-14_11 BABEL-730-before-15_6-or-16_1 diff --git a/test/JDBC/upgrade/latest/schedule b/test/JDBC/upgrade/latest/schedule index 5cbbe8f3fa0..d0ca7d1eb4d 100644 --- a/test/JDBC/upgrade/latest/schedule +++ b/test/JDBC/upgrade/latest/schedule @@ -496,6 +496,7 @@ TestDatatypeAggSort babel_index_nulls_order BABEL-2999 BABEL-4606 +BABEL-4529 sys_availability_groups sys_availability_replicas BABEL-730 diff --git a/test/python/expected/sql_validation_framework/expected_create.out b/test/python/expected/sql_validation_framework/expected_create.out index f0dda59a882..f453abfa904 100644 --- a/test/python/expected/sql_validation_framework/expected_create.out +++ b/test/python/expected/sql_validation_framework/expected_create.out @@ -85,7 +85,6 @@ Could not find tests for view sys.sp_columns_100_view Could not find tests for view sys.sp_fkeys_view Could not find tests for view sys.sp_pkeys_view Could not find tests for view sys.sp_special_columns_view -Could not find tests for view sys.sp_sproc_columns_view Could not find tests for view sys.sp_statistics_view Could not find tests for view sys.sp_stored_procedures_view Could not find tests for view sys.sp_table_privileges_view @@ -211,7 +210,6 @@ Could not find upgrade tests for view sys.sp_columns_100_view Could not find upgrade tests for view sys.sp_fkeys_view Could not find upgrade tests for view sys.sp_pkeys_view Could not find upgrade tests for view sys.sp_special_columns_view -Could not find upgrade tests for view sys.sp_sproc_columns_view Could not find upgrade tests for view sys.sp_statistics_view Could not find upgrade tests for view sys.sp_stored_procedures_view Could not find upgrade tests for view sys.sp_table_privileges_view diff --git a/test/python/expected/upgrade_validation/expected_dependency.out b/test/python/expected/upgrade_validation/expected_dependency.out index ef3ebfe196a..666ed855026 100644 --- a/test/python/expected/upgrade_validation/expected_dependency.out +++ b/test/python/expected/upgrade_validation/expected_dependency.out @@ -1020,7 +1020,6 @@ View sys.assembly_modules View sys.babelfish_has_perms_by_name_permissions View sys.change_tracking_databases View sys.change_tracking_tables -View sys.data_spaces View sys.database_files View sys.database_filestream_options View sys.database_recovery_status @@ -1044,7 +1043,6 @@ View sys.sp_columns_100_view View sys.sp_fkeys_view View sys.sp_pkeys_view View sys.sp_special_columns_view -View sys.sp_sproc_columns_view View sys.sp_statistics_view View sys.sp_stored_procedures_view View sys.sp_table_privileges_view @@ -1052,9 +1050,7 @@ View sys.spatial_index_tessellations View sys.spatial_indexes View sys.spt_columns_view_managed View sys.spt_tablecollations_view -View sys.stats View sys.synonyms View sys.syscharsets View sys.syslanguages -View sys.xml_indexes View sys.xml_schema_collections