edmug
/
Meetup-2018-01-23-Building-Modern-Enterprise-Level-CMS-Solutions-using-the-Episerver-Platform
Public
forked from episerver/AlloyDemoKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMigrate.sql
24585 lines (23937 loc) · 861 KB
/
Migrate.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
IF NOT EXISTS (SELECT * FROM sys.database_principals WHERE name = N'state_persistence_users' AND type = 'R')
CREATE ROLE [state_persistence_users]
GO
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = N'state_persistence_users')
EXEC sys.sp_executesql N'CREATE SCHEMA [state_persistence_users]'
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'ChangeNotificationGuidTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[ChangeNotificationGuidTable] AS TABLE(
[Value] [uniqueidentifier] NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'ChangeNotificationIntTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[ChangeNotificationIntTable] AS TABLE(
[Value] [int] NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'ChangeNotificationStringTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[ChangeNotificationStringTable] AS TABLE(
[Value] [nvarchar](450) COLLATE Latin1_General_BIN2 NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'ContentReferenceTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[ContentReferenceTable] AS TABLE(
[ID] [int] NULL,
[WorkID] [int] NULL,
[Provider] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'DateTimeConversion_DateTimeOffset' AND ss.name = N'dbo')
CREATE TYPE [dbo].[DateTimeConversion_DateTimeOffset] AS TABLE(
[IntervalStart] [datetime] NOT NULL,
[IntervalEnd] [datetime] NOT NULL,
[Offset] [float] NOT NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'GuidParameterTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[GuidParameterTable] AS TABLE(
[Id] [uniqueidentifier] NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'HostDefinitionTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[HostDefinitionTable] AS TABLE(
[Name] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Type] [int] NULL,
[Language] [varchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Https] [bit] NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'IDTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[IDTable] AS TABLE(
[ID] [int] NOT NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'ProjectItemTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[ProjectItemTable] AS TABLE(
[ID] [int] NULL,
[ProjectID] [int] NULL,
[ContentLinkID] [int] NULL,
[ContentLinkWorkID] [int] NULL,
[ContentLinkProvider] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Language] [nvarchar](17) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Category] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'ProjectMemberTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[ProjectMemberTable] AS TABLE(
[ID] [int] NULL,
[Name] [varchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Type] [smallint] NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'StringParameterTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[StringParameterTable] AS TABLE(
[String] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
)
GO
IF NOT EXISTS (SELECT * FROM sys.types st JOIN sys.schemas ss ON st.schema_id = ss.schema_id WHERE st.name = N'UriPartsTable' AND ss.name = N'dbo')
CREATE TYPE [dbo].[UriPartsTable] AS TABLE(
[Host] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Path] [nvarchar](2048) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL
)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblLanguageBranch]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblLanguageBranch](
[pkID] [int] IDENTITY(1,1) NOT NULL,
[LanguageID] [nchar](17) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[Name] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[SortIndex] [int] NOT NULL,
[SystemIconPath] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[URLSegment] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ACL] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Enabled] [bit] NOT NULL CONSTRAINT [DF__tblLanguageBranch__Enabled] DEFAULT ((1)),
CONSTRAINT [PK_tblLanguageBranch] PRIMARY KEY CLUSTERED
(
[pkID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ,
CONSTRAINT [IX_tblLanguageBranch] UNIQUE NONCLUSTERED
(
[LanguageID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblContentType]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblContentType](
[pkID] [int] IDENTITY(1,1) NOT NULL,
[ContentTypeGUID] [uniqueidentifier] NOT NULL CONSTRAINT [DF_tblContentType_ContentTypeGUID] DEFAULT (newid()),
[Created] [datetime] NOT NULL,
[DefaultWebFormTemplate] [nvarchar](1024) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DefaultMvcController] [nvarchar](1024) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DefaultMvcPartialView] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Filename] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ModelType] [nvarchar](1024) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Name] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[DisplayName] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Description] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[IdString] [nvarchar](50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[Available] [bit] NULL,
[SortOrder] [int] NULL,
[MetaDataInherit] [int] NOT NULL CONSTRAINT [DF_tblContentType_MetaDataInherit] DEFAULT ((0)),
[MetaDataDefault] [int] NOT NULL CONSTRAINT [DF_tblContentType_MetaDataDefault] DEFAULT ((0)),
[WorkflowEditFields] [bit] NULL,
[ACL] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ContentType] [int] NOT NULL CONSTRAINT [DF_tblContentType_ContentType] DEFAULT ((0)),
CONSTRAINT [PK_tblContentType] PRIMARY KEY CLUSTERED
(
[pkID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblContentType]') AND name = N'IDX_tblContentType_ContentTypeGUID')
CREATE UNIQUE NONCLUSTERED INDEX [IDX_tblContentType_ContentTypeGUID] ON [dbo].[tblContentType]
(
[ContentTypeGUID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblContentType]') AND name = N'IDX_tblContentType_Name')
CREATE UNIQUE NONCLUSTERED INDEX [IDX_tblContentType_Name] ON [dbo].[tblContentType]
(
[Name] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblContent]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblContent](
[pkID] [int] IDENTITY(1,1) NOT NULL,
[fkContentTypeID] [int] NOT NULL,
[fkParentID] [int] NULL,
[ArchiveContentGUID] [uniqueidentifier] NULL,
[CreatorName] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ContentGUID] [uniqueidentifier] NOT NULL CONSTRAINT [DF__tblContent__ContentGUID] DEFAULT (newid()),
[VisibleInMenu] [bit] NOT NULL CONSTRAINT [DF__tblContent__Visible__2E06CDA9] DEFAULT ((1)),
[Deleted] [bit] NOT NULL CONSTRAINT [DF__tblContent__Deleted__2EFAF1E2] DEFAULT ((0)),
[ChildOrderRule] [int] NOT NULL CONSTRAINT [DF__tblContent__ChildOr__35A7EF71] DEFAULT ((1)),
[PeerOrder] [int] NOT NULL CONSTRAINT [DF__tblContent__PeerOrd__369C13AA] DEFAULT ((100)),
[ContentAssetsID] [uniqueidentifier] NULL,
[ContentOwnerID] [uniqueidentifier] NULL,
[DeletedBy] [nvarchar](255) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[DeletedDate] [datetime] NULL,
[fkMasterLanguageBranchID] [int] NOT NULL CONSTRAINT [DF__tblContent__fkMasterLangaugeBranchID] DEFAULT ((1)),
[ContentPath] [varchar](900) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ContentType] [int] NOT NULL CONSTRAINT [DF_tblContent_ContentType] DEFAULT ((0)),
[IsLeafNode] [bit] NOT NULL CONSTRAINT [DF_tblContent_IsLeafNode] DEFAULT ((1)),
CONSTRAINT [PK_tblContent] PRIMARY KEY CLUSTERED
(
[pkID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblContent]') AND name = N'IDX_tblContent_ArchiveContentGUID')
CREATE NONCLUSTERED INDEX [IDX_tblContent_ArchiveContentGUID] ON [dbo].[tblContent]
(
[ArchiveContentGUID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblContent]') AND name = N'IDX_tblContent_ContentGUID')
CREATE UNIQUE NONCLUSTERED INDEX [IDX_tblContent_ContentGUID] ON [dbo].[tblContent]
(
[ContentGUID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblContent]') AND name = N'IDX_tblContent_ContentPath')
CREATE NONCLUSTERED INDEX [IDX_tblContent_ContentPath] ON [dbo].[tblContent]
(
[ContentPath] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblContent]') AND name = N'IDX_tblContent_ContentType')
CREATE NONCLUSTERED INDEX [IDX_tblContent_ContentType] ON [dbo].[tblContent]
(
[ContentType] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblContent]') AND name = N'IDX_tblContent_fkContentTypeID')
CREATE NONCLUSTERED INDEX [IDX_tblContent_fkContentTypeID] ON [dbo].[tblContent]
(
[fkContentTypeID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblContent]') AND name = N'IDX_tblContent_fkParentID')
CREATE NONCLUSTERED INDEX [IDX_tblContent_fkParentID] ON [dbo].[tblContent]
(
[fkParentID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.views WHERE object_id = OBJECT_ID(N'[dbo].[tblPage]'))
EXEC dbo.sp_executesql @statement = N'CREATE VIEW [dbo].[tblPage]
AS
SELECT [pkID],
[fkContentTypeID] AS fkPageTypeID,
[fkParentID],
[ArchiveContentGUID] AS ArchivePageGUID,
[CreatorName],
[ContentGUID] AS PageGUID,
[VisibleInMenu],
[Deleted],
CAST (0 AS BIT) AS PendingPublish,
[ChildOrderRule],
[PeerOrder],
[ContentAssetsID],
[ContentOwnerID],
NULL as PublishedVersion,
[fkMasterLanguageBranchID],
[ContentPath] AS PagePath,
[ContentType],
[DeletedBy],
[DeletedDate]
FROM dbo.tblContent
'
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[admDatabaseStatistics]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[admDatabaseStatistics] AS'
END
GO
ALTER PROCEDURE [dbo].[admDatabaseStatistics]
AS
BEGIN
SET NOCOUNT ON
SELECT
(SELECT Count(*) FROM tblPage) AS PageCount
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BigTableDeleteAll]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[BigTableDeleteAll] AS'
END
GO
ALTER PROCEDURE [dbo].[BigTableDeleteAll]
@ViewName nvarchar(4000)
AS
BEGIN
CREATE TABLE #deletes(Id BIGINT, NestLevel INT, ObjectPath varchar(max))
CREATE INDEX IX_Deletes_Id ON #deletes(Id)
CREATE INDEX IX_Deletes_Id_NestLevel ON #deletes(Id, NestLevel)
INSERT INTO #deletes(Id, NestLevel, ObjectPath)
EXEC ('SELECT [StoreId], 1, ''/'' + CAST([StoreId] AS VARCHAR) + ''/'' FROM ' + @ViewName)
EXEC ('BigTableDeleteItemInternal 1')
DROP INDEX IX_Deletes_Id_NestLevel ON #deletes
DROP INDEX IX_Deletes_Id ON #deletes
DROP TABLE #deletes
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblBigTableIdentity]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblBigTableIdentity](
[pkId] [bigint] IDENTITY(1,1) NOT NULL,
[Guid] [uniqueidentifier] NOT NULL CONSTRAINT [DF_tblBigTableIdentity_Guid] DEFAULT (newid()),
[StoreName] [nvarchar](375) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
CONSTRAINT [PK_tblBigTableIdentity] PRIMARY KEY CLUSTERED
(
[pkId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblBigTableIdentity]') AND name = N'IDX_tblBigTableIdentity_Guid')
CREATE NONCLUSTERED INDEX [IDX_tblBigTableIdentity_Guid] ON [dbo].[tblBigTableIdentity]
(
[Guid] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblBigTableReference]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblBigTableReference](
[pkId] [bigint] NOT NULL,
[Type] [int] NOT NULL,
[PropertyName] [nvarchar](75) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[CollectionType] [nvarchar](2000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ElementType] [nvarchar](2000) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[ElementStoreName] [nvarchar](375) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[IsKey] [bit] NOT NULL,
[Index] [int] NOT NULL CONSTRAINT [tblBigTableReference_Index] DEFAULT ((1)),
[BooleanValue] [bit] NULL,
[IntegerValue] [int] NULL,
[LongValue] [bigint] NULL,
[DateTimeValue] [datetime] NULL,
[GuidValue] [uniqueidentifier] NULL,
[FloatValue] [float] NULL,
[StringValue] [nvarchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[BinaryValue] [varbinary](max) NULL,
[RefIdValue] [bigint] NULL,
[ExternalIdValue] [bigint] NULL,
[DecimalValue] [decimal](18, 3) NULL,
CONSTRAINT [PK_tblBigTableReference] PRIMARY KEY CLUSTERED
(
[pkId] ASC,
[PropertyName] ASC,
[IsKey] ASC,
[Index] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblBigTableReference]') AND name = N'IDX_tblBigTableReference_RefIdValue')
CREATE NONCLUSTERED INDEX [IDX_tblBigTableReference_RefIdValue] ON [dbo].[tblBigTableReference]
(
[RefIdValue] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BigTableDeleteExcessReferences]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[BigTableDeleteExcessReferences] AS'
END
GO
ALTER PROCEDURE [dbo].[BigTableDeleteExcessReferences]
@Id bigint,
@PropertyName nvarchar(75),
@StartIndex int
AS
BEGIN
BEGIN TRAN
IF @StartIndex > -1
BEGIN
-- Creates temporary store with id's of references that has no other reference
CREATE TABLE #deletes(Id BIGINT, NestLevel INT, ObjectPath varchar(max))
CREATE INDEX IX_Deletes_Id ON #deletes(Id)
CREATE INDEX IX_Deletes_Id_NestLevel ON #deletes(Id, NestLevel)
INSERT INTO #deletes(Id, NestLevel, ObjectPath)
SELECT DISTINCT R1.RefIdValue, 1, '/' + CAST(R1.RefIdValue AS VARCHAR) + '/' FROM tblBigTableReference AS R1
LEFT OUTER JOIN tblBigTableReference AS R2 ON R1.RefIdValue = R2.pkId
WHERE R1.pkId = @Id AND R1.PropertyName = @PropertyName AND R1.[Index] >= @StartIndex AND
R1.RefIdValue IS NOT NULL AND R2.RefIdValue IS NULL
-- Remove reference on main store
DELETE FROM tblBigTableReference WHERE pkId = @Id and PropertyName = @PropertyName and [Index] >= @StartIndex
IF((select count(*) from #deletes) > 0)
BEGIN
EXEC ('BigTableDeleteItemInternal')
END
DROP INDEX IX_Deletes_Id_NestLevel ON #deletes
DROP INDEX IX_Deletes_Id ON #deletes
DROP TABLE #deletes
END
ELSE
-- Remove reference on main store
DELETE FROM tblBigTableReference WHERE pkId = @Id and PropertyName = @PropertyName and [Index] >= @StartIndex
COMMIT TRAN
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BigTableDeleteItem]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[BigTableDeleteItem] AS'
END
GO
ALTER PROCEDURE [dbo].[BigTableDeleteItem]
@StoreId BIGINT = NULL,
@ExternalId uniqueidentifier = NULL
AS
BEGIN
IF @StoreId IS NULL
BEGIN
SELECT @StoreId = pkId FROM tblBigTableIdentity WHERE [Guid] = @ExternalId
END
IF @StoreId IS NULL RAISERROR(N'No object exists for the unique identifier passed', 1, 1)
CREATE TABLE #deletes(Id BIGINT, NestLevel INT, ObjectPath varchar(max))
CREATE INDEX IX_Deletes_Id ON #deletes(Id)
CREATE INDEX IX_Deletes_Id_NestLevel ON #deletes(Id, NestLevel)
INSERT INTO #deletes(Id, NestLevel, ObjectPath) VALUES(@StoreId, 1, '/' + CAST(@StoreId AS varchar) + '/')
EXEC ('BigTableDeleteItemInternal')
DROP INDEX IX_Deletes_Id_NestLevel ON #deletes
DROP INDEX IX_Deletes_Id ON #deletes
DROP TABLE #deletes
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblBigTableStoreConfig]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblBigTableStoreConfig](
[pkId] [bigint] IDENTITY(1,1) NOT NULL,
[StoreName] [nvarchar](375) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[TableName] [nvarchar](128) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,
[EntityTypeId] [int] NULL,
[DateTimeKind] [int] NOT NULL DEFAULT ((0)),
CONSTRAINT [PK_tblBigTableStoreConfig] PRIMARY KEY CLUSTERED
(
[pkId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
SET ANSI_PADDING ON
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblBigTableStoreConfig]') AND name = N'IDX_tblBigTableStoreConfig_StoreName')
CREATE NONCLUSTERED INDEX [IDX_tblBigTableStoreConfig_StoreName] ON [dbo].[tblBigTableStoreConfig]
(
[StoreName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BigTableDeleteItemInternal]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[BigTableDeleteItemInternal] AS'
END
GO
ALTER PROCEDURE [dbo].[BigTableDeleteItemInternal]
@forceDelete bit = 0
AS
BEGIN
DECLARE @nestLevel int
SET @nestLevel = 1
WHILE @@ROWCOUNT > 0
BEGIN
SET @nestLevel = @nestLevel + 1
-- insert all items contained in the ones matching the _previous_ nestlevel and give them _this_ nestLevel
-- exclude those items that are also referred by some other item not already in #deletes
-- IMPORTANT: Make sure that this insert is the last statement that can affect @@ROWCOUNT in the while-loop
INSERT INTO #deletes(Id, NestLevel, ObjectPath)
SELECT DISTINCT RefIdValue, @nestLevel, #deletes.ObjectPath + '/' + CAST(RefIdValue AS VARCHAR) + '/'
FROM tblBigTableReference R1
INNER JOIN #deletes ON #deletes.Id=R1.pkId
WHERE #deletes.NestLevel=@nestLevel-1
AND RefIdValue NOT IN(SELECT Id FROM #deletes)
END
DELETE #deletes FROM #deletes
INNER JOIN
(
SELECT innerDelete.Id
FROM #deletes as innerDelete
INNER JOIN tblBigTableReference ON tblBigTableReference.RefIdValue=innerDelete.Id
WHERE NOT EXISTS(SELECT * FROM #deletes WHERE #deletes.Id=tblBigTableReference.pkId)
) ReferencedObjects ON #deletes.ObjectPath LIKE '%/' + CAST(ReferencedObjects.Id AS VARCHAR) + '/%'
WHERE @forceDelete = 0 OR #deletes.NestLevel > 1
BEGIN TRAN
DELETE FROM tblBigTableReference WHERE RefIdValue in (SELECT Id FROM #deletes)
DELETE FROM tblBigTableReference WHERE pkId in (SELECT Id FROM #deletes)
-- Go through each big table and delete any rows associated with the item being deleted
DECLARE @tableName NVARCHAR(128)
DECLARE tableNameCursor CURSOR READ_ONLY
FOR SELECT DISTINCT TableName FROM tblBigTableStoreConfig WHERE TableName IS NOT NULL
OPEN tableNameCursor
FETCH NEXT FROM tableNameCursor INTO @tableName
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC ('DELETE FROM ' + @tableName + ' WHERE pkId in (SELECT Id FROM #deletes)')
FETCH NEXT FROM tableNameCursor INTO @tableName
END
CLOSE tableNameCursor
DEALLOCATE tableNameCursor
DELETE FROM tblBigTableIdentity WHERE pkId in (SELECT Id FROM #deletes)
COMMIT TRAN
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[BigTableSaveReference]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[BigTableSaveReference] AS'
END
GO
ALTER PROCEDURE [dbo].[BigTableSaveReference]
@Id bigint,
@Type int,
@PropertyName nvarchar(75),
@CollectionType nvarchar(2000) = NULL,
@ElementType nvarchar(2000) = NULL,
@ElementStoreName nvarchar(375) = null,
@IsKey bit,
@Index int,
@BooleanValue bit = NULL,
@IntegerValue int = NULL,
@LongValue bigint = NULL,
@DateTimeValue datetime = NULL,
@GuidValue uniqueidentifier = NULL,
@FloatValue float = NULL,
@StringValue nvarchar(max) = NULL,
@BinaryValue varbinary(max) = NULL,
@RefIdValue bigint = NULL,
@ExternalIdValue bigint = NULL,
@DecimalValue decimal(18, 3) = NULL
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
if not exists(select * from tblBigTableReference where pkId = @Id and PropertyName = @PropertyName and IsKey = @IsKey and [Index] = @Index)
begin
-- insert
insert into tblBigTableReference
values
(
@Id,
@Type,
@PropertyName,
@CollectionType,
@ElementType,
@ElementStoreName,
@IsKey,
@Index,
@BooleanValue,
@IntegerValue,
@LongValue,
@DateTimeValue,
@GuidValue,
@FloatValue,
@StringValue,
@BinaryValue,
@RefIdValue,
@ExternalIdValue,
@DecimalValue
)
end
else
begin
-- update
update tblBigTableReference
set
CollectionType = @CollectionType,
ElementType = @ElementType,
ElementStoreName = @ElementStoreName,
BooleanValue = @BooleanValue,
IntegerValue = @IntegerValue,
LongValue = @LongValue,
DateTimeValue = @DateTimeValue,
GuidValue = @GuidValue,
FloatValue = @FloatValue,
StringValue = @StringValue,
BinaryValue = @BinaryValue,
RefIdValue = @RefIdValue,
ExternalIdValue = @ExternalIdValue,
DecimalValue = @DecimalValue
where
pkId = @Id and PropertyName = @PropertyName and IsKey = @IsKey and [Index] = @Index
end
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblChangeNotificationProcessor]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblChangeNotificationProcessor](
[ProcessorId] [uniqueidentifier] NOT NULL,
[ChangeNotificationDataType] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ProcessorName] [nvarchar](4000) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[ProcessorStatus] [nvarchar](30) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL,
[NextQueueOrderValue] [int] NOT NULL,
[LastConsistentDbUtc] [datetime] NULL,
CONSTRAINT [PK_ChangeNotificationProcessor] PRIMARY KEY CLUSTERED
(
[ProcessorId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblChangeNotificationConnection]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblChangeNotificationConnection](
[ConnectionId] [uniqueidentifier] NOT NULL,
[ProcessorId] [uniqueidentifier] NOT NULL,
[IsOpen] [bit] NOT NULL,
[LastActivityDbUtc] [datetime] NOT NULL,
CONSTRAINT [PK_ChangeNotificationConnection] PRIMARY KEY CLUSTERED
(
[ConnectionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ChangeNotificationAccessConnectionWorker]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ChangeNotificationAccessConnectionWorker] AS'
END
GO
ALTER PROCEDURE [dbo].[ChangeNotificationAccessConnectionWorker]
@connectionId uniqueidentifier,
@expectedChangeNotificationDataType nvarchar(30) = null
as
begin
update tblChangeNotificationConnection
set LastActivityDbUtc = GETUTCDATE()
where ConnectionId = @connectionId
declare @processorId uniqueidentifier
declare @queuedDataType nvarchar(30)
declare @processorStatus nvarchar(30)
declare @nextQueueOrderValue int
declare @lastConsistentDbUtc datetime
declare @isOpen bit
select @processorId = p.ProcessorId, @queuedDataType = p.ChangeNotificationDataType, @processorStatus = p.ProcessorStatus, @nextQueueOrderValue = p.NextQueueOrderValue, @lastConsistentDbUtc = p.LastConsistentDbUtc, @isOpen = c.IsOpen
from tblChangeNotificationProcessor p
join tblChangeNotificationConnection c on p.ProcessorId = c.ProcessorId
where c.ConnectionId = @connectionId
if (@processorId is null)
begin
set @processorStatus = 'closed'
end
else if (@expectedChangeNotificationDataType is not null and @expectedChangeNotificationDataType != @queuedDataType)
begin
set @processorStatus = 'type_mismatch'
end
else if (@processorStatus = 'invalid' or @isOpen = 1)
begin
-- the queue is invalid, or the current connection is valid.
-- all pending connection requests may be considered open.
update tblChangeNotificationConnection
set IsOpen = 1
where ProcessorId = @processorId and IsOpen = 0
if (@processorStatus = 'valid' and @nextQueueOrderValue = 0)
begin
set @lastConsistentDbUtc = GETUTCDATE()
end
end
else if (@isOpen = 0 and @processorStatus != 'invalid')
begin
set @processorStatus = 'opening'
end
select @processorId as ProcessorId, @processorStatus as ProcessorStatus, @lastConsistentDbUtc
end
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblChangeNotificationQueuedGuid]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblChangeNotificationQueuedGuid](
[ProcessorId] [uniqueidentifier] NOT NULL,
[ConnectionId] [uniqueidentifier] NULL,
[QueueOrder] [int] NOT NULL,
[Value] [uniqueidentifier] NOT NULL
)
END
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblChangeNotificationQueuedGuid]') AND name = N'IDX_tblChangeNotificationQueuedGuid')
CREATE CLUSTERED INDEX [IDX_tblChangeNotificationQueuedGuid] ON [dbo].[tblChangeNotificationQueuedGuid]
(
[ProcessorId] ASC,
[QueueOrder] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblChangeNotificationQueuedInt]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblChangeNotificationQueuedInt](
[ProcessorId] [uniqueidentifier] NOT NULL,
[ConnectionId] [uniqueidentifier] NULL,
[QueueOrder] [int] NOT NULL,
[Value] [int] NOT NULL
)
END
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblChangeNotificationQueuedInt]') AND name = N'IDX_tblChangeNotificationQueuedInt')
CREATE CLUSTERED INDEX [IDX_tblChangeNotificationQueuedInt] ON [dbo].[tblChangeNotificationQueuedInt]
(
[ProcessorId] ASC,
[QueueOrder] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tblChangeNotificationQueuedString]') AND type in (N'U'))
BEGIN
CREATE TABLE [dbo].[tblChangeNotificationQueuedString](
[ProcessorId] [uniqueidentifier] NOT NULL,
[ConnectionId] [uniqueidentifier] NULL,
[QueueOrder] [int] NOT NULL,
[Value] [nvarchar](450) COLLATE Latin1_General_BIN2 NOT NULL
)
END
GO
IF NOT EXISTS (SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID(N'[dbo].[tblChangeNotificationQueuedString]') AND name = N'IDX_tblChangeNotificationQueuedString')
CREATE CLUSTERED INDEX [IDX_tblChangeNotificationQueuedString] ON [dbo].[tblChangeNotificationQueuedString]
(
[ProcessorId] ASC,
[QueueOrder] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ChangeNotificationCloseConnection]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ChangeNotificationCloseConnection] AS'
END
GO
ALTER PROCEDURE [dbo].[ChangeNotificationCloseConnection]
@connectionId uniqueidentifier
as
begin
begin try
begin transaction
declare @processorId uniqueidentifier
declare @processorStatus nvarchar(30)
select @processorId = p.ProcessorId, @processorStatus = p.ProcessorStatus
from tblChangeNotificationConnection c
join tblChangeNotificationProcessor p on c.ProcessorId = p.ProcessorId
where c.ConnectionId = @connectionId
update tblChangeNotificationQueuedInt set ConnectionId = null where ConnectionId = @connectionId
update tblChangeNotificationQueuedGuid set ConnectionId = null where ConnectionId = @connectionId
update tblChangeNotificationQueuedString set ConnectionId = null where ConnectionId = @connectionId
delete from tblChangeNotificationConnection where ConnectionId = @connectionId
if (@processorStatus != 'valid' and not exists (select 1 from tblChangeNotificationConnection where ProcessorId = @processorId))
begin
-- if there are no connections to the queue and it is not in a valid state, remove it from persistent storage.
delete from tblChangeNotificationQueuedInt where ProcessorId = @processorId
delete from tblChangeNotificationQueuedGuid where ProcessorId = @processorId
delete from tblChangeNotificationQueuedString where ProcessorId = @processorId
delete from tblChangeNotificationProcessor where ProcessorId = @processorId
end
commit transaction
select @connectionId as ConnectionId
end try
begin catch
declare @msg nvarchar(4000), @sev int, @stt int
select @msg = ERROR_MESSAGE(), @sev = ERROR_SEVERITY(), @stt = ERROR_STATE()
rollback transaction
raiserror(@msg, @sev, @stt)
end catch
end
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ChangeNotificationCompleteBatchGuid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ChangeNotificationCompleteBatchGuid] AS'
END
GO
ALTER PROCEDURE [dbo].[ChangeNotificationCompleteBatchGuid]
@connectionId uniqueidentifier,
@success bit
as
begin
begin try
begin transaction
declare @processorId uniqueidentifier
declare @processorStatus nvarchar(30)
declare @processorStatusTable table (ProcessorId uniqueidentifier, ProcessorStatus nvarchar(30), LastConsistentDbUtc datetime)
insert into @processorStatusTable (ProcessorId, ProcessorStatus, LastConsistentDbUtc)
exec ChangeNotificationAccessConnectionWorker @connectionId, 'Guid'
select @processorId = ProcessorId, @processorStatus = ProcessorStatus
from @processorStatusTable
if (@processorStatus != 'invalid' and @processorStatus != 'closed')
begin
if (@success = 1)
begin
delete from tblChangeNotificationQueuedGuid
where ConnectionId = @connectionId
if not exists (select 1 from tblChangeNotificationQueuedGuid where ProcessorId = @processorId)
begin
update tblChangeNotificationProcessor
set NextQueueOrderValue = 0, LastConsistentDbUtc = GETUTCDATE()
where ProcessorId = @processorId
end
end
else
begin
declare @queueOrder int
update tblChangeNotificationProcessor
set @queueOrder = NextQueueOrderValue = NextQueueOrderValue + 1
where ProcessorId = @processorId
update tblChangeNotificationQueuedGuid
set QueueOrder = @queueOrder, ConnectionId = null
where ConnectionId = @connectionId
end
end
commit transaction
end try
begin catch
declare @msg nvarchar(4000), @sev int, @stt int
select @msg = ERROR_MESSAGE(), @sev = ERROR_SEVERITY(), @stt = ERROR_STATE()
rollback transaction
raiserror(@msg, @sev, @stt)
end catch
end
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ChangeNotificationCompleteBatchInt]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ChangeNotificationCompleteBatchInt] AS'
END
GO
ALTER PROCEDURE [dbo].[ChangeNotificationCompleteBatchInt]
@connectionId uniqueidentifier,
@success bit
as
begin
begin try
begin transaction
declare @processorId uniqueidentifier
declare @processorStatus nvarchar(30)
declare @processorStatusTable table (ProcessorId uniqueidentifier, ProcessorStatus nvarchar(30), LastConsistentDbUtc datetime)
insert into @processorStatusTable (ProcessorId, ProcessorStatus, LastConsistentDbUtc)
exec ChangeNotificationAccessConnectionWorker @connectionId, 'Int'
select @processorId = ProcessorId, @processorStatus = ProcessorStatus
from @processorStatusTable
if (@processorStatus != 'invalid' and @processorStatus != 'closed')
begin
if (@success = 1)
begin
delete from tblChangeNotificationQueuedInt
where ConnectionId = @connectionId
if not exists (select 1 from tblChangeNotificationQueuedInt where ProcessorId = @processorId)
begin
update tblChangeNotificationProcessor
set NextQueueOrderValue = 0, LastConsistentDbUtc = GETUTCDATE()
where ProcessorId = @processorId
end
end
else
begin
declare @queueOrder int
update tblChangeNotificationProcessor
set @queueOrder = NextQueueOrderValue = NextQueueOrderValue + 1
where ProcessorId = @processorId
update tblChangeNotificationQueuedInt
set QueueOrder = @queueOrder, ConnectionId = null
where ConnectionId = @connectionId
end
end
commit transaction
end try
begin catch
declare @msg nvarchar(4000), @sev int, @stt int
select @msg = ERROR_MESSAGE(), @sev = ERROR_SEVERITY(), @stt = ERROR_STATE()
rollback transaction
raiserror(@msg, @sev, @stt)
end catch
end
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ChangeNotificationCompleteBatchString]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ChangeNotificationCompleteBatchString] AS'
END
GO
ALTER PROCEDURE [dbo].[ChangeNotificationCompleteBatchString]
@connectionId uniqueidentifier,
@success bit
as
begin
begin try
begin transaction
declare @processorId uniqueidentifier
declare @processorStatus nvarchar(30)
declare @processorStatusTable table (ProcessorId uniqueidentifier, ProcessorStatus nvarchar(30), LastConsistentDbUtc datetime)
insert into @processorStatusTable (ProcessorId, ProcessorStatus, LastConsistentDbUtc)
exec ChangeNotificationAccessConnectionWorker @connectionId, 'String'
select @processorId = ProcessorId, @processorStatus = ProcessorStatus
from @processorStatusTable
if (@processorStatus != 'invalid' and @processorStatus != 'closed')
begin
if (@success = 1)
begin
delete from tblChangeNotificationQueuedString
where ConnectionId = @connectionId
if not exists (select 1 from tblChangeNotificationQueuedString where ProcessorId = @processorId)
begin
update tblChangeNotificationProcessor
set NextQueueOrderValue = 0, LastConsistentDbUtc = GETUTCDATE()
where ProcessorId = @processorId
end
end
else
begin
declare @queueOrder int
update tblChangeNotificationProcessor
set @queueOrder = NextQueueOrderValue = NextQueueOrderValue + 1
where ProcessorId = @processorId
update tblChangeNotificationQueuedString
set QueueOrder = @queueOrder, ConnectionId = null
where ConnectionId = @connectionId
end
end
commit transaction
end try
begin catch
declare @msg nvarchar(4000), @sev int, @stt int
select @msg = ERROR_MESSAGE(), @sev = ERROR_SEVERITY(), @stt = ERROR_STATE()
rollback transaction
raiserror(@msg, @sev, @stt)
end catch
end
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ChangeNotificationDequeueGuid]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'CREATE PROCEDURE [dbo].[ChangeNotificationDequeueGuid] AS'
END
GO
ALTER PROCEDURE [dbo].[ChangeNotificationDequeueGuid]
@connectionId uniqueidentifier,
@maxItems int
as
begin
begin try
begin transaction
declare @processorId uniqueidentifier
declare @processorStatus nvarchar(30)