From 9749d1e3a819404b4eb9893cf645d18904b47cb1 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 11 Jan 2017 22:43:03 -0500 Subject: [PATCH] Grant table for identity persisted grants --- src/Sql/Sql.sqlproj | 8 +++ .../Stored Procedures/Grant_DeleteByKey.sql | 12 ++++ .../Grant_DeleteBySubjectIdClientId.sql | 14 ++++ .../Grant_DeleteBySubjectIdClientIdType.sql | 16 +++++ .../dbo/Stored Procedures/Grant_ReadByKey.sql | 13 ++++ .../Grant_ReadBySubjectId.sql | 13 ++++ src/Sql/dbo/Stored Procedures/Grant_Save.sql | 70 +++++++++++++++++++ src/Sql/dbo/Tables/Grant.sql | 16 +++++ src/Sql/dbo/Views/GrantView.sql | 6 ++ 9 files changed, 168 insertions(+) create mode 100644 src/Sql/dbo/Stored Procedures/Grant_DeleteByKey.sql create mode 100644 src/Sql/dbo/Stored Procedures/Grant_DeleteBySubjectIdClientId.sql create mode 100644 src/Sql/dbo/Stored Procedures/Grant_DeleteBySubjectIdClientIdType.sql create mode 100644 src/Sql/dbo/Stored Procedures/Grant_ReadByKey.sql create mode 100644 src/Sql/dbo/Stored Procedures/Grant_ReadBySubjectId.sql create mode 100644 src/Sql/dbo/Stored Procedures/Grant_Save.sql create mode 100644 src/Sql/dbo/Tables/Grant.sql create mode 100644 src/Sql/dbo/Views/GrantView.sql diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj index d7fb61420b..a49d52fb10 100644 --- a/src/Sql/Sql.sqlproj +++ b/src/Sql/Sql.sqlproj @@ -69,6 +69,7 @@ + @@ -94,5 +95,12 @@ + + + + + + + \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Grant_DeleteByKey.sql b/src/Sql/dbo/Stored Procedures/Grant_DeleteByKey.sql new file mode 100644 index 0000000000..eb9b1474b0 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Grant_DeleteByKey.sql @@ -0,0 +1,12 @@ +CREATE PROCEDURE [dbo].[Grant_DeleteByKey] + @Key NVARCHAR(200) +AS +BEGIN + SET NOCOUNT ON + + DELETE + FROM + [dbo].[Grant] + WHERE + [Key] = @Key +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Grant_DeleteBySubjectIdClientId.sql b/src/Sql/dbo/Stored Procedures/Grant_DeleteBySubjectIdClientId.sql new file mode 100644 index 0000000000..7b98477e98 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Grant_DeleteBySubjectIdClientId.sql @@ -0,0 +1,14 @@ +CREATE PROCEDURE [dbo].[Grant_DeleteBySubjectIdClientId] + @SubjectId NVARCHAR(50), + @ClientId NVARCHAR(50) +AS +BEGIN + SET NOCOUNT ON + + DELETE + FROM + [dbo].[Grant] + WHERE + [SubjectId] = @SubjectId + AND [ClientId] = @ClientId +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Grant_DeleteBySubjectIdClientIdType.sql b/src/Sql/dbo/Stored Procedures/Grant_DeleteBySubjectIdClientIdType.sql new file mode 100644 index 0000000000..0e75baa373 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Grant_DeleteBySubjectIdClientIdType.sql @@ -0,0 +1,16 @@ +CREATE PROCEDURE [dbo].[Grant_DeleteBySubjectIdClientIdType] + @SubjectId NVARCHAR(50), + @ClientId NVARCHAR(50), + @Type NVARCHAR(50) +AS +BEGIN + SET NOCOUNT ON + + DELETE + FROM + [dbo].[Grant] + WHERE + [SubjectId] = @SubjectId + AND [ClientId] = @ClientId + AND [Type] = @Type +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Grant_ReadByKey.sql b/src/Sql/dbo/Stored Procedures/Grant_ReadByKey.sql new file mode 100644 index 0000000000..414388b12a --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Grant_ReadByKey.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[Grant_ReadByKey] + @Key NVARCHAR(200) +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[GrantView] + WHERE + [Key] = @Key +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Grant_ReadBySubjectId.sql b/src/Sql/dbo/Stored Procedures/Grant_ReadBySubjectId.sql new file mode 100644 index 0000000000..738ec57f00 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Grant_ReadBySubjectId.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[Grant_ReadBySubjectId] + @SubjectId NVARCHAR(50) +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[GrantView] + WHERE + [SubjectId] = @SubjectId +END \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Grant_Save.sql b/src/Sql/dbo/Stored Procedures/Grant_Save.sql new file mode 100644 index 0000000000..af6b2a7e70 --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Grant_Save.sql @@ -0,0 +1,70 @@ +CREATE PROCEDURE [dbo].[Grant_Save] + @Key NVARCHAR(200), + @Type NVARCHAR(50), + @SubjectId NVARCHAR(50), + @ClientId NVARCHAR(50), + @CreationDate DATETIME2, + @ExpirationDate DATETIME2, + @Data NVARCHAR(MAX) +AS +BEGIN + SET NOCOUNT ON + + MERGE + [dbo].[Grant] AS [Target] + USING + ( + VALUES + ( + @Key, + @Type, + @SubjectId, + @ClientId, + @CreationDate, + @ExpirationDate, + @Data + ) + ) AS [Source] + ( + [Key], + [Type], + [SubjectId], + [ClientId], + [CreationDate], + [ExpirationDate], + [Data] + ) + ON + [Target].[Key] = [Source].[Key] + WHEN MATCHED THEN + UPDATE + SET + [Type] = [Source].[Type], + [SubjectId] = [Source].[SubjectId], + [ClientId] = [Source].[ClientId], + [CreationDate] = [Source].[CreationDate], + [ExpirationDate] = [Source].[ExpirationDate], + [Data] = [Source].[Data] + WHEN NOT MATCHED THEN + INSERT + ( + [Key], + [Type], + [SubjectId], + [ClientId], + [CreationDate], + [ExpirationDate], + [Data] + ) + VALUES + ( + [Source].[Key], + [Source].[Type], + [Source].[SubjectId], + [Source].[ClientId], + [Source].[CreationDate], + [Source].[ExpirationDate], + [Source].[Data] + ) + ; +END \ No newline at end of file diff --git a/src/Sql/dbo/Tables/Grant.sql b/src/Sql/dbo/Tables/Grant.sql new file mode 100644 index 0000000000..d63c881a71 --- /dev/null +++ b/src/Sql/dbo/Tables/Grant.sql @@ -0,0 +1,16 @@ +CREATE TABLE [dbo].[Grant] ( + [Key] NVARCHAR (200) NOT NULL, + [Type] NVARCHAR (50) NULL, + [SubjectId] NVARCHAR (50) NULL, + [ClientId] NVARCHAR (50) NOT NULL, + [CreationDate] DATETIME2 (7) NOT NULL, + [ExpirationDate] DATETIME2 (7) NULL, + [Data] NVARCHAR (MAX) NOT NULL, + CONSTRAINT [PK_Grant] PRIMARY KEY CLUSTERED ([Key] ASC) +); + + +GO +CREATE NONCLUSTERED INDEX [IX_Grant_SubjectId_ClientId_Type] + ON [dbo].[Grant]([SubjectId] ASC, [ClientId] ASC, [Type] ASC); + diff --git a/src/Sql/dbo/Views/GrantView.sql b/src/Sql/dbo/Views/GrantView.sql new file mode 100644 index 0000000000..cb9f964b8b --- /dev/null +++ b/src/Sql/dbo/Views/GrantView.sql @@ -0,0 +1,6 @@ +CREATE VIEW [dbo].[GrantView] +AS +SELECT + * +FROM + [dbo].[Grant] \ No newline at end of file