-
Notifications
You must be signed in to change notification settings - Fork 0
/
DB-Script.Sql-Server_improved.sql
1948 lines (1791 loc) · 60.7 KB
/
DB-Script.Sql-Server_improved.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2008 Kemal ERDOGAN
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, version 3 of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[EdgeExists]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [EdgeExists] (
@StartVertexId varchar(100),
@EndVertexId varchar(100),
@Source varchar(150) )
AS
BEGIN
IF EXISTS (
SELECT Hops
FROM Edge
WHERE StartVertex = @StartVertexId
AND EndVertex = @EndVertexId
AND Source = @Source )
BEGIN
SELECT CONVERT(BIT, 1) AS Result
RETURN 1
END ELSE
BEGIN
SELECT CONVERT(BIT, 0) AS Result
RETURN 0
END
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[AccessPredicate]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [AccessPredicate](
[PrincipalId] [varchar](256) NOT NULL,
[OperationId] [varchar](10) NOT NULL,
[ResourceId] [varchar](256) NOT NULL,
[PredicateType] [tinyint] NOT NULL,
[Modified] [Datetime] DEFAULT GetUTcDate(),
[Deleted] [Datetime] DEFAULT '1753-01-01'
CONSTRAINT [PK_AccessPredicate_1] PRIMARY KEY CLUSTERED
(
[PrincipalId] ASC,
[OperationId] ASC,
[ResourceId] ASC,
[PredicateType] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING ON
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Principal]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Principal](
[ObjectId] [uniqueidentifier] NOT NULL,
[PrincipalType] [smallint] NOT NULL,
[Name] [varchar](256) NOT NULL,
[Email] [varchar](500) NOT NULL,
[DisplayName] [varchar](500) NOT NULL,
[Description] [varchar](500) NOT NULL,
[DataSource] [varchar](100) NOT NULL,
CONSTRAINT [PK_Principal_1] PRIMARY KEY CLUSTERED
(
[ObjectId] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Principal]') AND name = N'IX_Principal_1')
CREATE UNIQUE NONCLUSTERED INDEX [IX_Principal_1] ON [Principal]
(
[Name] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Sync_Edge]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Sync_Edge](
[StartVertex] [varchar](256) NOT NULL,
[EndVertex] [varchar](256) NOT NULL,
[Source] [varchar](150) NOT NULL,
CONSTRAINT [PK_EdgeSyncList] PRIMARY KEY CLUSTERED
(
[StartVertex] ASC,
[EndVertex] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING ON
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Sync_Principal]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Sync_Principal](
[ObjectId] [uniqueidentifier] NOT NULL,
[PrincipalType] [smallint] NOT NULL,
[Name] [varchar](256) NOT NULL,
[Email] [varchar](500) NOT NULL,
[DisplayName] [varchar](500) NOT NULL,
[Description] [varchar](500) NOT NULL,
[DataSource] [varchar](100) NOT NULL,
PRIMARY KEY CLUSTERED
(
[ObjectId] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING ON
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Operation]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Operation](
[Id] [varchar](10) NOT NULL,
[Name] [varchar](250) NOT NULL,
[Description] [varchar](500) NOT NULL,
CONSTRAINT [PK_Operation_1] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING ON
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[AddEdgeWithSpaceSavings]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'
CREATE PROC [AddEdgeWithSpaceSavings]
@StartVertexId varchar(256),
@EndVertexId varchar(256),
@Source varchar(150)
AS
BEGIN
SET NOCOUNT ON
IF EXISTS(SELECT Hops
FROM Edge
WHERE StartVertex = @StartVertexId
AND EndVertex = @EndVertexId
AND Source = @Source
AND Hops = 0)
BEGIN
RETURN 0 -- DO NOTHING!!!
END
IF @StartVertexId = @EndVertexId
OR EXISTS (SELECT Hops
FROM Edge
WHERE StartVertex = @EndVertexId
AND EndVertex = @StartVertexId
AND Source = @Source)
BEGIN
RAISERROR (''Attempt to create a circular relation detected!'', 16, 1)
RETURN 0
END
CREATE TABLE #Candidates (
StartVertex varchar(256),
EndVertex varchar(256) )
INSERT INTO #Candidates
SELECT StartVertex
, @EndVertexId
FROM Edge
WHERE EndVertex = @StartVertexId
AND Source = @Source
UNION
SELECT @StartVertexId
, EndVertex
FROM Edge
WHERE StartVertex = @EndVertexId
AND Source = @Source
UNION
SELECT A.StartVertex
, B.EndVertex
FROM Edge A
CROSS JOIN Edge B
WHERE A.EndVertex = @StartVertexId
AND B.StartVertex = @EndVertexId
AND A.Source = @Source
AND B.Source = @Source
INSERT INTO Edge (
StartVertex,
EndVertex,
Hops,
Source)
VALUES (
@StartVertexId
, @EndVertexId
, 0
, @Source )
update E
set E.Modified = GetUtcDate(),
E.Deleted = null -- remove deletion flag
from EDGE E inner join #Candidates C on E.StartVertex = C.StartVertex
AND E.EndVertex = C.EndVertex
AND E.Hops = 1
INSERT INTO Edge (
StartVertex,
EndVertex,
Hops,
Source,
Modified)
SELECT StartVertex,
EndVertex,
1,
@Source,
GETUTCDATE()
FROM #Candidates C
WHERE NOT EXISTS (
SELECT Hops
FROM Edge E
WHERE E.StartVertex = C.StartVertex
AND E.EndVertex = C.EndVertex
AND E.Hops = 1)
END '
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Edge]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Edge](
[StartVertex] [varchar](256) NOT NULL,
[EndVertex] [varchar](256) NOT NULL,
[Hops] [int] NOT NULL,
[Source] [varchar](150) NOT NULL,
[DelMark] [bit] NOT NULL,
[Modified] [Datetime] DEFAULT GetUTcDate(),
[Deleted] [Datetime] DEFAULT '1753-01-01'
) ON [PRIMARY]
END
GO
--ALTER TABLE [Edge] ADD CONSTRAINT [Modified] DEFAULT GetUtcDate() FOR [Modified]
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Edge]') AND name = N'IX_Edge_1')
CREATE CLUSTERED INDEX [IX_Edge_1] ON [Edge]
(
[StartVertex] ASC,
[EndVertex] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Edge]') AND name = N'IX_Edge_2')
CREATE NONCLUSTERED INDEX [IX_Edge_2] ON [Edge]
(
[EndVertex] ASC,
[StartVertex] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Edge]') AND name = N'IX_Edge_3')
CREATE NONCLUSTERED INDEX [IX_Edge_3] ON [Edge]
(
[Hops] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Edge]') AND name = N'IX_Edge_4')
CREATE NONCLUSTERED INDEX [IX_Edge_4] ON [Edge]
(
[Source] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
GO
IF NOT EXISTS (SELECT * FROM dbo.sysindexes WHERE id = OBJECT_ID(N'[Edge]') AND name = N'IX_Edge_5')
CREATE NONCLUSTERED INDEX [IX_Edge_5] ON [Edge]
(
[DelMark] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Resource]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [Resource](
[Id] [varchar](36) NOT NULL,
[Name] [varchar](100) NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH FILLFACTOR = 90 ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_PADDING ON
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[TEST_EDGE_DATA]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [TEST_EDGE_DATA](
[StartVertex] [varchar](256) NOT NULL,
[EndVertex] [varchar](256) NOT NULL,
[Hops] [int] NOT NULL,
[Source] [varchar](150) NOT NULL,
[DelMark] [bit] NOT NULL
) ON [PRIMARY]
END
GO
SET ANSI_PADDING ON
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[TEST_EDGE_DEL_DATA]') AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
BEGIN
CREATE TABLE [TEST_EDGE_DEL_DATA](
[StartVertex] [varchar](256) NOT NULL,
[EndVertex] [varchar](256) NOT NULL,
[Source] [varchar](150) NOT NULL
) ON [PRIMARY]
END
GO
SET ANSI_PADDING ON
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FlatGrantList]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N' CREATE VIEW [FlatGrantList]
AS
SELECT DISTINCT
PL.StartVertex AS PrincipalId
,OL.StartVertex AS OperationId
,RL.StartVertex AS ResourceId
FROM ( SELECT E.EndVertex, E.StartVertex
FROM Edge E
INNER JOIN AccessPredicate AP
ON E.EndVertex = AP.PrincipalId
WHERE E.Source = ''Principal''
UNION
SELECT PrincipalId, PrincipalId
FROM AccessPredicate) PL
CROSS JOIN (
SELECT E.EndVertex, E.StartVertex
FROM Edge E
INNER JOIN AccessPredicate AP
ON E.EndVertex = AP.OperationId
WHERE E.Source = ''Operation''
UNION
SELECT OperationId, OperationId
FROM AccessPredicate) OL
CROSS JOIN (
SELECT E.EndVertex, E.StartVertex
FROM Edge E
INNER JOIN AccessPredicate AP
ON E.EndVertex = AP.ResourceId
WHERE E.Source = ''Resource''
UNION
SELECT ResourceId, ResourceId
FROM AccessPredicate) RL
INNER JOIN AccessPredicate ACL
ON PL.EndVertex = ACL.PrincipalId
AND RL.EndVertex = ACL.ResourceId
AND OL.EndVertex = ACL.OperationId
'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FlatGrantListWithFlatResources]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N' CREATE VIEW [FlatGrantListWithFlatResources]
AS
SELECT
PL.StartVertex AS PrincipalId
,OL.StartVertex AS OperationId
,ACL.ResourceId
FROM ( SELECT E.EndVertex, E.StartVertex
FROM Edge E
INNER JOIN AccessPredicate AP
ON E.EndVertex = AP.PrincipalId
WHERE E.Source = ''Principal''
UNION
SELECT PrincipalId, PrincipalId
FROM AccessPredicate) PL
CROSS JOIN (
SELECT E.EndVertex, E.StartVertex
FROM Edge E
INNER JOIN AccessPredicate AP
ON E.EndVertex = AP.OperationId
WHERE E.Source = ''Operation''
UNION
SELECT OperationId, OperationId
FROM AccessPredicate) OL
INNER JOIN AccessPredicate ACL
ON PL.EndVertex = ACL.PrincipalId
AND OL.EndVertex = ACL.OperationId
'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[GetAuthorizedResources]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [GetAuthorizedResources] (
@PrincipalId varchar(36),
@OperationId varchar(10)
)
AS
BEGIN
SELECT DISTINCT
ResourceId
FROM FlatGrantList
WHERE PrincipalId = @PrincipalId
AND OperationId = @OperationId
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FlatGrantListWithFlatResourcesAndOperations]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N' CREATE VIEW [FlatGrantListWithFlatResourcesAndOperations]
AS
SELECT
PL.StartVertex AS PrincipalId
,ACL.OperationId
,ACL.ResourceId
FROM ( SELECT E.EndVertex, E.StartVertex
FROM Edge E
INNER JOIN AccessPredicate AP
ON E.EndVertex = AP.PrincipalId
WHERE E.Source = ''Principal''
UNION
SELECT PrincipalId, PrincipalId
FROM AccessPredicate) PL
INNER JOIN AccessPredicate ACL
ON PL.EndVertex = ACL.PrincipalId'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[GetAuthorizedOperations]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [GetAuthorizedOperations] (
@PrincipalId varchar(36),
@ResourceId varchar(100)
)
AS
BEGIN
SELECT DISTINCT
OperationId
FROM FlatGrantList
WHERE PrincipalId = @PrincipalId
AND ResourceId = @ResourceId
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[GetAuthorizationDigest]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [GetAuthorizationDigest] (
@PrincipalId varchar(36)
)
AS
BEGIN
SELECT DISTINCT
OperationId,
ResourceId,
CONVERT(INT, 0) as PredicateType
FROM FlatGrantList
WHERE PrincipalId = @PrincipalId
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[IsAuthorized]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [IsAuthorized] (
@PrincipalId varchar(36),
@OperationId varchar(10),
@ResourceId varchar(100)
)
AS
BEGIN
IF @ResourceId IS NULL
SELECT @ResourceId = ''''
IF EXISTS (
SELECT PL.PrincipalId, OL.OperationId, RL.ResourceId
FROM ( SELECT EndVertex as PrincipalId -- groups of @PrincipalId
FROM Edge
WHERE Source = ''Principal''
AND StartVertex = @PrincipalId
UNION
SELECT @PrincipalId as PrincipalId -- @PrincipalId itself
) PL -- Part 1
CROSS JOIN (
SELECT EndVertex as OperationId -- parent operations of @OperationId
FROM Edge
WHERE Source = ''Operation''
AND StartVertex = @OperationId
UNION
SELECT @OperationId as OperationId -- @OperationId itself
) OL -- Part 2
CROSS JOIN (
SELECT EndVertex as ResourceId -- parent resources of @ResourceId
FROM Edge
WHERE Source = ''Resource''
AND StartVertex = @ResourceId
UNION
SELECT @ResourceId as ResourceId -- @ResourceId itself
) RL -- Part 3
INNER JOIN AccessPredicate ACL
ON PL.PrincipalId = ACL.PrincipalId
AND OL.OperationId = ACL.OperationId
AND RL.ResourceId = ACL.ResourceId )
BEGIN
SELECT CONVERT(BIT, 1) AS Result
RETURN 1
END ELSE
BEGIN
SELECT CONVERT(BIT, 0) AS Result
RETURN 0
END
END'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[IsMemberOf]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [IsMemberOf] (
@GroupId varchar(36),
@MemberId varchar(36)
)
AS
BEGIN
IF EXISTS(
SELECT EndVertex
FROM Edge
WHERE Source = ''Principal''
AND StartVertex = @MemberId
AND EndVertex = @GroupId )
BEGIN
SELECT CONVERT(BIT, 1) AS Result
RETURN 1
END ELSE
BEGIN
SELECT CONVERT(BIT, 0) AS Result
RETURN 0
END
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[AddOrUpdateOperation]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [AddOrUpdateOperation] (
@Id varchar(10),
@Name varchar(250),
@Description varchar(500) )
AS
BEGIN
UPDATE Operation
SET Name = ISNULL(@Name, @Id)
,Description = ISNULL(@Description, '''')
WHERE Id = @Id
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO Operation
(Id
,Name
,Description)
VALUES
(@Id
,ISNULL(@Name, @Id)
,ISNULL(@Description, ''''))
END
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[GetOperationList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [GetOperationList]
AS
BEGIN
SELECT Id
,Name
,Description
FROM Operation
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[GetSubOperationsList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [GetSubOperationsList] (
@OperationId varchar(10),
@IsFlat int
)
AS
BEGIN
SELECT DISTINCT
O.Id
,O.Name
,O.Description
FROM Operation O
INNER JOIN Edge E
ON O.Id = E.StartVertex
WHERE E.EndVertex = @OperationId
AND E.Source = ''Operation''
AND ( @IsFlat = 1 OR E.Hops = @IsFlat )
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Test_DeleteAllData]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [Test_DeleteAllData]
AS
BEGIN
truncate table Principal
truncate table Operation
truncate table Edge
truncate table AccessPredicate
truncate table Sync_Principal
truncate table Sync_Edge
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Test_Performance_AddEdge_CreateBigGraph]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [Test_Performance_AddEdge_CreateBigGraph] (
@MaxNodes int,
@MaxTries int
) AS
BEGIN
TRUNCATE TABLE Edge
IF @MaxNodes > 1E4
BEGIN
RAISERROR (''Supports up to 10,000 vertices''
,16
,1
)
RETURN
END
DECLARE @idx int
DECLARE @jdx int
DECLARE @StartVertex int
DECLARE @EndVertex int
DECLARE @totalTime bigint
DECLARE @start datetime
DECLARE @cnt int
-- BOTH RANDOM
SELECT @idx = 1, @jdx = 1, @totalTime = 0, @cnt = 0
WHILE @idx <= @MaxTries
BEGIN
SELECT @StartVertex = CONVERT(int, RAND() * 1E6) % @MaxNodes + 1
,@EndVertex = CONVERT(int, RAND() * 1E6) % @MaxNodes + 1
IF @StartVertex <> @EndVertex
AND NOT EXISTS (SELECT Hops FROM Edge WHERE EndVertex = @StartVertex AND StartVertex = @EndVertex)
BEGIN
SELECT @start = GETDATE()
EXEC AddEdgeWithSpaceSavings @StartVertex, @EndVertex, ''BigGraph''
SELECT @TotalTime = @TotalTime + DATEDIFF(ms, @start, GETDATE())
SELECT @cnt = @cnt + 1
END
SELECT @jdx = @jdx + 1
SELECT @idx = @idx + 1
END
-- FIXED START WITH RANDOM END
SELECT @idx = 1, @jdx = 1
WHILE @idx <= @MaxNodes
BEGIN
WHILE @jdx <= @MaxTries
BEGIN
SELECT @StartVertex = @idx
,@EndVertex = CONVERT(int, RAND() * 1E6) % @MaxNodes + 1
IF @StartVertex <> @EndVertex
AND NOT EXISTS (SELECT Hops FROM Edge WHERE EndVertex = @StartVertex AND StartVertex = @EndVertex)
BEGIN
SELECT @start = GETDATE()
EXEC AddEdgeWithSpaceSavings @StartVertex, @EndVertex, ''BigGraph''
SELECT @TotalTime = @TotalTime + DATEDIFF(ms, @start, GETDATE())
SELECT @cnt = @cnt + 1
END
SELECT @jdx = @jdx + 1
END
SELECT @idx = @idx + 1
END
-- FIXED END WITH RANDOM START
SELECT @idx = 1, @jdx = 1
WHILE @idx <= @MaxNodes
BEGIN
WHILE @jdx <= @MaxTries
BEGIN
SELECT @StartVertex = CONVERT(int, RAND() * 1E6) % @MaxNodes + 1
,@EndVertex = @idx
IF @StartVertex <> @EndVertex
AND NOT EXISTS (SELECT Hops FROM Edge WHERE EndVertex = @StartVertex AND StartVertex = @EndVertex)
BEGIN
SELECT @start = GETDATE()
EXEC AddEdgeWithSpaceSavings @StartVertex, @EndVertex, ''BigGraph''
SELECT @TotalTime = @TotalTime + DATEDIFF(ms, @start, GETDATE())
SELECT @cnt = @cnt + 1
END
SELECT @jdx = @jdx + 1
END
SELECT @idx = @idx + 1
END
-- COMBINATIONS
SELECT @idx = 1, @jdx = @MaxNodes
WHILE @idx <= @MaxNodes
BEGIN
WHILE @jdx >= 1
BEGIN
SELECT @StartVertex = @idx
,@EndVertex = @jdx
IF @StartVertex <> @EndVertex
AND NOT EXISTS (SELECT Hops FROM Edge WHERE EndVertex = @StartVertex AND StartVertex = @EndVertex)
BEGIN
SELECT @start = GETDATE()
EXEC AddEdgeWithSpaceSavings @StartVertex, @EndVertex, ''BigGraph''
SELECT @TotalTime = @TotalTime + DATEDIFF(ms, @start, GETDATE())
SELECT @cnt = @cnt + 1
END
SELECT @jdx = @jdx - 1
END
SELECT @idx = @idx + 1
END
PRINT ''AVG INSERTION TIME (milliseconds) = '' + CONVERT( varchar(10), @totalTime/@cnt )
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[Test_Performance_AddEdge]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [Test_Performance_AddEdge]
AS
BEGIN
DECLARE @StartVertexId varchar(256)
DECLARE @EndVertexId varchar(256)
DECLARE @Source varchar(150)
DECLARE @start datetime
DECLARE @totalTime bigint
DECLARE @testCnt int
DECLARE EdgeCursor CURSOR FOR
SELECT StartVertex, EndVertex, Source
FROM TEST_EDGE_DATA loc
WHERE Hops = 0
TRUNCATE TABLE Edge
OPEN EdgeCursor
FETCH NEXT FROM EdgeCursor INTO @StartVertexId, @EndVertexId, @Source
IF @@FETCH_STATUS = 0
BEGIN
SELECT @testCnt = 0, @totalTime = 0
WHILE @@FETCH_STATUS = 0
BEGIN
SELECT @start = GETDATE()
EXEC AddEdgeWithSpaceSavings @StartVertexId, @EndVertexId, @Source
SELECT @totalTime = @totalTime + DATEDIFF(ms, @start, GETDATE())
FETCH NEXT FROM EdgeCursor INTO @StartVertexId, @EndVertexId, @Source
SELECT @testCnt = @testCnt + 1
END
PRINT ''AVG INSERTION TIME (milliseconds) = '' + CONVERT( varchar(10), @totalTime/@testCnt )
END ELSE
BEGIN
PRINT ''NO DATA FOUND FOR PERFORMANCE TESTING!''
END
CLOSE EdgeCursor
DEALLOCATE EdgeCursor
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[AddAccessPredicate]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [AddAccessPredicate] (
@PrincipalId varchar(256),
@OperationId varchar(10),
@ResourceId varchar(256),
@PredicateType int )
AS
BEGIN
INSERT INTO AccessPredicate
(PrincipalId
,OperationId
,ResourceId
,PredicateType)
VALUES
(@PrincipalId
,@OperationId
,@ResourceId
,@PredicateType)
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[FlatGrantListWithFlatOperations]') AND OBJECTPROPERTY(id, N'IsView') = 1)
EXEC dbo.sp_executesql @statement = N' CREATE VIEW [FlatGrantListWithFlatOperations]
AS
SELECT DISTINCT
PL.StartVertex AS PrincipalId
,ACL.OperationId
,RL.StartVertex AS ResourceId
FROM ( SELECT E.EndVertex, E.StartVertex
FROM Edge E
INNER JOIN AccessPredicate AP
ON E.EndVertex = AP.PrincipalId
WHERE E.Source = ''Principal''
UNION
SELECT PrincipalId, PrincipalId
FROM AccessPredicate) PL
CROSS JOIN (
SELECT E.EndVertex, E.StartVertex
FROM Edge E
INNER JOIN AccessPredicate AP
ON E.EndVertex = AP.ResourceId
WHERE E.Source = ''Resource''
UNION
SELECT ResourceId, ResourceId
FROM AccessPredicate) RL
INNER JOIN AccessPredicate ACL
ON PL.EndVertex = ACL.PrincipalId
AND RL.EndVertex = ACL.ResourceId
'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[GetMembersList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [GetMembersList] (
@GroupName varchar(256),
@IsFlat int
)
AS
BEGIN
SELECT DISTINCT
P.Name
,P.DisplayName
,P.PrincipalType
,P.Email
,P.Description
FROM Principal P
INNER JOIN Edge E
ON P.Name = E.StartVertex
WHERE E.EndVertex = @GroupName
AND E.Source = ''Principal''
AND ( @IsFlat = 1 OR E.Hops = @IsFlat )
END
'
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[GetPrincipalList]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROC [GetPrincipalList] (
@PrincipalType int = NULL
) AS