forked from stetard/DNN.Repository
-
Notifications
You must be signed in to change notification settings - Fork 1
/
03.01.14.SqlDataProvider
477 lines (376 loc) · 14.7 KB
/
03.01.14.SqlDataProvider
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
/* =====================================================================*/
/***** Initialization Script 03.01.14 *****/
/* =====================================================================*/
/* =====================================================================*/
/* DELETE ANY EXISTING FOREIGN KEYS */
/* =====================================================================*/
if exists (select * from sys.indexes
where name = 'FK_{objectQualifier}grmRepositoryAttributeValues_grmRepositoryAttributes')
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryAttributeValues]
DROP CONSTRAINT [FK_{objectQualifier}grmRepositoryAttributeValues_grmRepositoryAttributes]
if exists (select * from sys.indexes
where name = 'DF_{objectQualifier}grmRepositoryCategories_Parent')
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryCategories]
DROP CONSTRAINT [DF_{objectQualifier}grmRepositoryCategories_Parent]
/* =====================================================================*/
/* DELETE ANY EXISTING PRIMARY KEYS */
/* =====================================================================*/
if exists (select * from sys.indexes
where name = 'PK_{objectQualifier}grmRepositoryAttributes')
DROP INDEX [PK_{objectQualifier}grmRepositoryAttributes] ON
{databaseOwner}[{objectQualifier}grmRepositoryAttributes]
if exists (select * from sys.indexes
where name = 'PK_{objectQualifier}grmRepositoryCategories')
DROP INDEX [PK_{objectQualifier}grmRepositoryCategories] ON
{databaseOwner}[{objectQualifier}grmRepositoryCategories]
if exists (select * from sys.indexes
where name = 'PK_{objectQualifier}grmRepositoryComments')
DROP INDEX [PK_{objectQualifier}grmRepositoryComments] ON
{databaseOwner}[{objectQualifier}grmRepositoryComments]
if exists (select * from sys.indexes
where name = 'PK_{objectQualifier}grmRepositoryObjectValues')
DROP INDEX [PK_{objectQualifier}grmRepositoryObjectValues] ON
{databaseOwner}[{objectQualifier}grmRepositoryObjectValues]
if exists (select * from sys.indexes
where name = 'PK_{objectQualifier}grmRepositoryObjects')
DROP INDEX [PK_{objectQualifier}grmRepositoryObjects] ON
{databaseOwner}[{objectQualifier}grmRepositoryObjects]
if exists (select * from sys.indexes
where name = 'PK_{objectQualifier}grmRepositoryAttributeValues')
DROP INDEX [PK_{objectQualifier}grmRepositoryAttributeValues] ON
{databaseOwner}[{objectQualifier}grmRepositoryAttributeValues]
/* =====================================================================*/
/* DELETE ANY EXISTING INDEXES */
/* =====================================================================*/
if exists (select * from sys.indexes
where name = 'IX_{objectQualifier}grmRepositoryAttributes')
DROP INDEX [IX_{objectQualifier}grmRepositoryAttributes]
ON {databaseOwner}[{objectQualifier}grmRepositoryAttributes]
if exists (select * from sys.indexes
where name = 'IX_{objectQualifier}grmRepositoryCategories')
DROP INDEX [IX_{objectQualifier}grmRepositoryCategories]
ON {databaseOwner}[{objectQualifier}grmRepositoryCategories]
if exists (select * from sys.indexes
where name = 'IX_{objectQualifier}grmRepositoryComments')
DROP INDEX [IX_{objectQualifier}grmRepositoryComments]
ON {databaseOwner}[{objectQualifier}grmRepositoryComments]
if exists (select * from sys.indexes
where name = 'IX_{objectQualifier}grmRepositoryObjectValues')
DROP INDEX [IX_{objectQualifier}grmRepositoryObjectValues]
ON {databaseOwner}[{objectQualifier}grmRepositoryObjectValues]
if exists (select * from sys.indexes
where name = 'IX_{objectQualifier}grmRepositoryObjects')
DROP INDEX [IX_{objectQualifier}grmRepositoryObjects]
ON {databaseOwner}[{objectQualifier}grmRepositoryObjects]
if exists (select * from sys.indexes
where name = 'IX_{objectQualifier}grmRepositoryAttributeValues')
DROP INDEX [IX_{objectQualifier}grmRepositoryAttributeValues]
ON {databaseOwner}[{objectQualifier}grmRepositoryAttributeValues]
GO
/* =====================================================================*/
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryAttributes]
ADD CONSTRAINT [PK_{objectQualifier}grmRepositoryAttributes]
PRIMARY KEY CLUSTERED ([ItemID])
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryCategories]
ADD CONSTRAINT [PK_{objectQualifier}grmRepositoryCategories]
PRIMARY KEY CLUSTERED ([ItemId])
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryComments]
ADD CONSTRAINT [PK_{objectQualifier}grmRepositoryComments]
PRIMARY KEY CLUSTERED ([ItemID])
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryObjectValues]
ADD CONSTRAINT [PK_{objectQualifier}grmRepositoryObjectValues]
PRIMARY KEY CLUSTERED ([ItemID])
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryObjects]
ADD CONSTRAINT [PK_{objectQualifier}grmRepositoryObjects]
PRIMARY KEY CLUSTERED ([ItemID])
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryAttributeValues]
ADD CONSTRAINT [PK_{objectQualifier}grmRepositoryAttributeValues]
PRIMARY KEY CLUSTERED ([ItemID])
GO
/* =====================================================================*/
CREATE INDEX [IX_{objectQualifier}grmRepositoryAttributes]
ON {databaseOwner}[{objectQualifier}grmRepositoryAttributes]([ModuleID])
GO
if exists (select * from dbo.sysobjects
where id = object_id(N'[DF_{objectQualifier}grmRepositoryCategories]')
and OBJECTPROPERTY(id, N'IsConstraint') = 1)
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryCategories]
ADD CONSTRAINT [DF_{objectQualifier}grmRepositoryCategories_Parent]
DEFAULT ((-1)) FOR [Parent]
GO
CREATE INDEX [IX_{objectQualifier}grmRepositoryCategories]
ON {databaseOwner}[{objectQualifier}grmRepositoryCategories]([ModuleId])
GO
CREATE INDEX [IX_{objectQualifier}grmRepositoryComments]
ON {databaseOwner}[{objectQualifier}grmRepositoryComments]([ObjectID])
GO
CREATE INDEX [IX_{objectQualifier}grmRepositoryObjectValues]
ON {databaseOwner}[{objectQualifier}grmRepositoryObjectValues]([ObjectID])
GO
CREATE INDEX [IX_{objectQualifier}grmRepositoryObjects]
ON {databaseOwner}[{objectQualifier}grmRepositoryObjects]([ModuleID])
GO
CREATE INDEX [IX_{objectQualifier}grmRepositoryAttributeValues]
ON {databaseOwner}[{objectQualifier}grmRepositoryAttributeValues]([AttributeID])
GO
/* =====================================================================*/
ALTER TABLE {databaseOwner}[{objectQualifier}grmRepositoryAttributeValues]
ADD CONSTRAINT [FK_{objectQualifier}grmRepositoryAttributeValues_grmRepositoryAttributes]
FOREIGN KEY ([AttributeID]) REFERENCES {databaseOwner}[{objectQualifier}grmRepositoryAttributes] ([ItemID])
GO
/* =====================================================================*/
if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}grmGetSingleRepositoryObject') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}grmGetSingleRepositoryObject
GO
if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}grmGetRepositoryObjects') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}grmGetRepositoryObjects
GO
if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}grmGetCategoryItemCount') and xtype in (N'FN', N'IF', N'TF'))
drop function {databaseOwner}{objectQualifier}grmGetCategoryItemCount
GO
/* =====================================================================*/
if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}grmAddRepositoryComment') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}grmAddRepositoryComment
GO
if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}grmDeleteRepositoryComment') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}grmDeleteRepositoryComment
GO
if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}grmGetRepositoryComments') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}grmGetRepositoryComments
GO
if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}grmGetSingleRepositoryComment') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}grmGetSingleRepositoryComment
GO
if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}grmUpdateRepositoryComment') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}grmUpdateRepositoryComment
GO
/* =====================================================================*/
CREATE procedure {databaseOwner}{objectQualifier}grmGetSingleRepositoryObject
@ItemID int
AS
SELECT a.*,
(
SELECT
COUNT({databaseOwner}{objectQualifier}grmRepositoryComments.ItemID)
FROM {databaseOwner}{objectQualifier}grmRepositoryComments
WHERE {databaseOwner}{objectQualifier}grmRepositoryComments.ObjectID=a.ItemID
) AS CommentCount
FROM {databaseOwner}{objectQualifier}grmRepositoryObjects a
where a.ItemID = @ItemID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE procedure {databaseOwner}{objectQualifier}grmGetRepositoryObjects
(
@ModuleID int,
@sFilter nvarchar(256),
@sSort nvarchar(100),
@Approved int,
@CategoryId int,
@Attributes nvarchar(150),
@RowCount int
)
AS
BEGIN
IF @RowCount > 0
SET ROWCOUNT @RowCount
END
DECLARE @sql NVARCHAR(4000)
DECLARE @SearchToken nvarchar(128)
DECLARE @SearchClause nvarchar(50)
BEGIN
SELECT @sql = 'SELECT a.*, (SELECT COUNT({databaseOwner}{objectQualifier}grmRepositoryComments.ItemID) FROM {databaseOwner}{objectQualifier}grmRepositoryComments ' +
'WHERE {databaseOwner}{objectQualifier}grmRepositoryComments.ObjectID=a.ItemID) AS CommentCount '
IF @CategoryId = -1
BEGIN
SET @sql = @sql + 'FROM {databaseOwner}{objectQualifier}grmRepositoryObjects a '
END
ELSE
BEGIN
SET @sql = @sql + 'FROM {databaseOwner}{objectQualifier}grmRepositoryObjects a, {databaseOwner}{objectQualifier}grmRepositoryObjectCategories b '
END
SET @sql = @sql +
'WHERE a.ModuleID = ' + CONVERT(nVarChar, @ModuleID) + ' ' +
'AND ( CONVERT(nVarChar,a.Approved) = ' + CONVERT(nVarChar, @Approved) + ') ' +
'AND ( ''' + @Attributes + ''' = '''' OR {databaseOwner}{objectQualifier}grmCheckAllAttributes(''' + @Attributes + ''','';'', a.ItemID) = 1) '
IF @CategoryId <> -1
BEGIN
SET @sql = @sql + 'AND (a.ItemID = b.ObjectID AND b.CategoryId = ' + CONVERT(nVarChar, @CategoryId) + ') '
END
IF @sFilter <> ''
BEGIN
SET @sql = @sql + 'AND ( '
-- inject a set of search filters for each search word or phrase
DECLARE TCUR CURSOR FOR
SELECT [Value] FROM {databaseOwner}{objectQualifier}grmParseDelimitedStrings(@sFilter, '|')
SET @SearchClause = 'false'
OPEN TCUR
FETCH NEXT FROM TCUR INTO @SearchToken
WHILE @@FETCH_STATUS = 0
BEGIN
IF @SearchClause <> 'true'
BEGIN
SET @SearchClause = 'true'
SET @sql = @sql +
'(( a.Description like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) OR ( a.Summary like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) ' +
' OR ( a.Name like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) OR ( a.FileName like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) ' +
' OR ( a.Author like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) OR ( a.AuthorEmail like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) ' +
' ) '
END
ELSE
BEGIN
SET @sql = @sql +
' OR (( a.Description like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) OR ( a.Summary like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) ' +
' OR ( a.Name like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) OR ( a.FileName like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) ' +
' OR ( a.Author like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) OR ( a.AuthorEmail like ''%' + CONVERT(nVarChar, @SearchToken) + '%'' ) ' +
' ) '
END
FETCH NEXT FROM TCUR INTO @SearchToken
END
CLOSE TCUR
DEALLOCATE TCUR
SET @sql = @sql + ' ) '
END
IF @sSort = 'Name'
BEGIN
SET @sql = @sql + 'ORDER BY a.Name ASC '
END
IF @sSort = 'Author'
BEGIN
SET @sql = @sql + 'ORDER BY a.Author ASC '
END
IF @sSort = 'Downloads'
BEGIN
SET @sql = @sql + 'ORDER BY a.Downloads DESC '
END
IF @sSort = 'RatingAverage'
BEGIN
SET @sql = @sql + 'ORDER BY a.RatingAverage DESC '
END
IF @sSort = 'UpdatedDate'
BEGIN
SET @sql = @sql + 'ORDER BY a.UpdatedDate DESC '
END
IF @sSort = 'CreatedDate'
BEGIN
SET @sql = @sql + 'ORDER BY a.CreatedDate DESC '
END
EXEC(@sql)
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE FUNCTION {databaseOwner}{objectQualifier}grmGetCategoryItemCount (
@CategoryId int
)
RETURNS INT
AS
BEGIN
RETURN(SELECT COUNT(a.ItemID) FROM
{databaseOwner}{objectQualifier}grmRepositoryObjectCategories a,
{databaseOwner}{objectQualifier}grmRepositoryObjects b
WHERE a.ObjectID=b.ItemID AND b.Approved=1 AND CategoryId = @CategoryId)
END
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
CREATE procedure {databaseOwner}{objectQualifier}grmAddRepositoryComment
@ObjectID int,
@ModuleID int,
@UserName nvarchar(100),
@Comment ntext
as
insert into {databaseOwner}{objectQualifier}grmRepositoryComments (
ObjectID,
CreatedByUser,
CreatedDate,
Comment
)
values (
@ObjectID,
@UserName,
getdate(),
@Comment
)
select SCOPE_IDENTITY()
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure {databaseOwner}{objectQualifier}grmDeleteRepositoryComment
@ItemID int,
@ModuleID int
as
delete
from {databaseOwner}{objectQualifier}grmRepositoryComments
where ItemID = @ItemID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure {databaseOwner}{objectQualifier}grmGetRepositoryComments
@ItemID int,
@ModuleID int
as
select *
from {databaseOwner}{objectQualifier}grmRepositoryComments
where ObjectID = @ItemID
order by CreatedDate desc
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure {databaseOwner}{objectQualifier}grmGetSingleRepositoryComment
@ItemID int,
@ModuleID int
as
select *
from {databaseOwner}{objectQualifier}grmRepositoryComments
where ItemID = @ItemID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO
CREATE procedure {databaseOwner}{objectQualifier}grmUpdateRepositoryComment
@ItemID int,
@ModuleID int,
@Username nvarchar(100),
@Comment ntext
as
update {databaseOwner}{objectQualifier}grmRepositoryComments
set
CreatedDate = getdate(),
CreatedByUser = @Username,
Comment = @Comment
where ItemID = @ItemID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO