Skip to content

Commit

Permalink
Support SSMS scripting for Sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashish Prasad committed Oct 5, 2023
1 parent 30520ee commit c2b315f
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 0 deletions.
36 changes: 36 additions & 0 deletions contrib/babelfishpg_tsql/sql/sys_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3004,3 +3004,39 @@ SELECT
CAST(0 as sys.BIT) AS is_ambiguous
WHERE FALSE;
GRANT SELECT ON sys.sql_expression_dependencies TO PUBLIC;


create or replace view sys.sequences as
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
, CAST(ps.seqstart as sql_variant ) as start_value
, CAST(ps.seqincrement as sql_variant ) as increment
, CAST(ps.seqmin as sql_variant ) as minimum_value
, CAST(ps.seqmax as sql_variant ) as maximum_value
, CASE ps.seqcycle when 't' then CAST(1 as sys.bit) else CAST(0 as sys.bit) end as is_cycling
, CAST(ps.seqcache as sys.bit ) as is_cached
, CAST(0 as int ) as cache_size
, CAST(ps.seqtypid as int ) as system_type_id
, CAST(ps.seqtypid as int ) as user_type_id
, CAST(0 as sys.tinyint ) as precision
, CAST(0 as sys.tinyint ) as scale
, CAST('ABC' as sql_variant ) as current_value
, CAST(0 as sys.bit ) as is_exhausted
, CAST('ABC' as sql_variant ) as last_used_value
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')
inner join pg_sequence ps on ps.seqrelid = p.oid;
GRANT SELECT ON sys.sequences TO PUBLIC;
Original file line number Diff line number Diff line change
Expand Up @@ -1115,6 +1115,41 @@ END;
$body$
LANGUAGE plpgsql IMMUTABLE;

create or replace view sys.sequences as
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
, CAST(ps.seqstart as sql_variant ) as start_value
, CAST(ps.seqincrement as sql_variant ) as increment
, CAST(ps.seqmin as sql_variant ) as minimum_value
, CAST(ps.seqmax as sql_variant ) as maximum_value
, CASE ps.seqcycle when 't' then CAST(1 as sys.bit) else CAST(0 as sys.bit) end as is_cycling
, CAST(ps.seqcache as sys.bit ) as is_cached
, CAST(0 as int ) as cache_size
, CAST(ps.seqtypid as int ) as system_type_id
, CAST(ps.seqtypid as int ) as user_type_id
, CAST(0 as sys.tinyint ) as precision
, CAST(0 as sys.tinyint ) as scale
, CAST('ABC' as sql_variant ) as current_value
, CAST(0 as sys.bit ) as is_exhausted
, CAST('ABC' as sql_variant ) as last_used_value
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')
inner join pg_sequence ps on ps.seqrelid = p.oid;
GRANT SELECT ON sys.sequences TO PUBLIC;


-- This is a temporary procedure which is called during upgrade to update guest schema
-- for the guest users in the already existing databases
Expand Down
11 changes: 11 additions & 0 deletions test/JDBC/expected/sys_sequences-vu-cleanup.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
USE master
GO

DROP VIEW sys_sequences_vu_prepare_view
GO

DROP PROC sys_sequences_vu_prepare_proc
GO

DROP FUNCTION sys_sequences_vu_prepare_func
GO
18 changes: 18 additions & 0 deletions test/JDBC/expected/sys_sequences-vu-prepare.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
USE master
GO

CREATE VIEW sys_sequences_vu_prepare_view AS
SELECT * FROM sys.sequences
GO

CREATE PROC sys_sequences_vu_prepare_proc AS
SELECT * FROM sys.sequences
GO

CREATE FUNCTION sys_sequences_vu_prepare_func()
RETURNS INT
AS
BEGIN
RETURN (SELECT COUNT(*) FROM sys.sequences WHERE is_cycling= 0)
END
GO
31 changes: 31 additions & 0 deletions test/JDBC/expected/sys_sequences-vu-verify.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
USE master
GO

SELECT * FROM sys.sequences
GO
~~START~~
varchar#!#int#!#int#!#int#!#int#!#char#!#nvarchar#!#datetime#!#datetime#!#bit#!#bit#!#bit#!#sql_variant#!#sql_variant#!#sql_variant#!#sql_variant#!#bit#!#bit#!#int#!#int#!#int#!#tinyint#!#tinyint#!#sql_variant#!#bit#!#sql_variant
~~END~~


SELECT * FROM sys_sequences_vu_prepare_view
GO
~~START~~
varchar#!#int#!#int#!#int#!#int#!#char#!#nvarchar#!#datetime#!#datetime#!#bit#!#bit#!#bit#!#sql_variant#!#sql_variant#!#sql_variant#!#sql_variant#!#bit#!#bit#!#int#!#int#!#int#!#tinyint#!#tinyint#!#sql_variant#!#bit#!#sql_variant
~~END~~


EXEC sys_sequences_vu_prepare_proc
GO
~~START~~
varchar#!#int#!#int#!#int#!#int#!#char#!#nvarchar#!#datetime#!#datetime#!#bit#!#bit#!#bit#!#sql_variant#!#sql_variant#!#sql_variant#!#sql_variant#!#bit#!#bit#!#int#!#int#!#int#!#tinyint#!#tinyint#!#sql_variant#!#bit#!#sql_variant
~~END~~


SELECT sys_sequences_vu_prepare_func()
GO
~~START~~
int
0
~~END~~

11 changes: 11 additions & 0 deletions test/JDBC/input/views/sys_sequences-vu-cleanup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
USE master
GO

DROP VIEW sys_sequences_vu_prepare_view
GO

DROP PROC sys_sequences_vu_prepare_proc
GO

DROP FUNCTION sys_sequences_vu_prepare_func
GO
18 changes: 18 additions & 0 deletions test/JDBC/input/views/sys_sequences-vu-prepare.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
USE master
GO

CREATE VIEW sys_sequences_vu_prepare_view AS
SELECT * FROM sys.sequences
GO

CREATE PROC sys_sequences_vu_prepare_proc AS
SELECT * FROM sys.sequences
GO

CREATE FUNCTION sys_sequences_vu_prepare_func()
RETURNS INT
AS
BEGIN
RETURN (SELECT COUNT(*) FROM sys.sequences WHERE is_cycling= 0)
END
GO
14 changes: 14 additions & 0 deletions test/JDBC/input/views/sys_sequences-vu-verify.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
USE master
GO

SELECT * FROM sys.sequences
GO

SELECT * FROM sys_sequences_vu_prepare_view
GO

EXEC sys_sequences_vu_prepare_proc
GO

SELECT sys_sequences_vu_prepare_func()
GO
1 change: 1 addition & 0 deletions test/JDBC/upgrade/latest/schedule
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,4 @@ Test_ISNULL
BABEL-4270
BABEL-4410
BABEL-4231
sys_sequences

0 comments on commit c2b315f

Please sign in to comment.