-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathFindTopicIDs.sql
110 lines (100 loc) · 3.95 KB
/
FindTopicIDs.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
--------------------------------------------------------------------------------------------------------------------------------
-- FIND TOPIC IDS
--------------------------------------------------------------------------------------------------------------------------------
-- Given an attribute key and attribute value, returns the TopicID of each child topic that has an attribute with that value.
--------------------------------------------------------------------------------------------------------------------------------
CREATE
FUNCTION [dbo].[FindTopicIDs]
(
@TopicID INT,
@AttributeKey VARCHAR(255),
@AttributeValue NVARCHAR(255),
@IsExtendedAttribute BIT = NULL,
@UsePartialMatch BIT = 0
)
RETURNS @Topics TABLE
(
TopicID INT
)
AS
BEGIN
------------------------------------------------------------------------------------------------------------------------------
-- DEFINE VARIABLES
------------------------------------------------------------------------------------------------------------------------------
DECLARE @RangeLeft INT = 0
DECLARE @RangeRight INT = 0
SELECT @RangeLeft = RangeLeft,
@RangeRight = RangeRight
FROM Topics
WHERE TopicID = @TopicID
------------------------------------------------------------------------------------------------------------------------------
-- SET DEFAULTS
------------------------------------------------------------------------------------------------------------------------------
IF (@AttributeValue IS NULL)
BEGIN
SET @AttributeValue = ''
END
------------------------------------------------------------------------------------------------------------------------------
-- RETRIEVE KEY ATTRIBUTES
------------------------------------------------------------------------------------------------------------------------------
IF (@AttributeKey IN ('Key', 'ContentType', 'ParentID'))
BEGIN
INSERT
INTO @Topics
SELECT TopicID
FROM Topics
WHERE ( @AttributeKey = 'Key'
AND TopicKey = @AttributeValue
OR @AttributeKey = 'ContentType'
AND ContentType = @AttributeValue
OR @AttributeKey = 'ParentID'
AND ISNULL(ParentID, '') = @AttributeValue
)
RETURN
END
------------------------------------------------------------------------------------------------------------------------------
-- RETRIEVE INDEXED ATTRIBUTES
------------------------------------------------------------------------------------------------------------------------------
IF (ISNULL(@IsExtendedAttribute, 0) = 0)
INSERT
INTO @Topics
SELECT Attributes.TopicID
FROM AttributeIndex Attributes
JOIN Topics
ON Topics.TopicID = Attributes.TopicID
WHERE AttributeKey = @AttributeKey
AND RangeLeft >= @RangeLeft
AND RangeRight <= @RangeRight
AND ( @UsePartialMatch = 1
AND AttributeValue LIKE '%' + @AttributeValue + '%'
OR AttributeValue = @AttributeValue
)
------------------------------------------------------------------------------------------------------------------------------
-- RETRIEVE EXTENDED ATTRIBUTES
------------------------------------------------------------------------------------------------------------------------------
IF (ISNULL(@IsExtendedAttribute, 1) = 1)
INSERT
INTO @Topics
SELECT Attributes.TopicID
FROM ExtendedAttributeIndex Attributes
JOIN Topics
ON Topics.TopicID = Attributes.TopicID
WHERE RangeLeft >= @RangeLeft
AND RangeRight <= @RangeRight
AND ( @UsePartialMatch = 1
AND AttributesXml
.exist(
'/attributes/attribute[
@key=sql:variable("@AttributeKey") and
text()[contains(.,sql:variable("@AttributeValue"))]
]') = 1
OR @UsePartialMatch = 0
AND AttributesXml
.exist(
'/attributes/attribute[
@key=sql:variable("@AttributeKey") and
text() = sql:variable("@AttributeValue")
]') = 1
)
RETURN
END