-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetTopics.sql
More file actions
138 lines (126 loc) · 5.01 KB
/
GetTopics.sql
File metadata and controls
138 lines (126 loc) · 5.01 KB
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
--------------------------------------------------------------------------------------------------------------------------------
-- GET TOPICS
--------------------------------------------------------------------------------------------------------------------------------
-- Gets the tree of current topics rooted FROM the provided TopicID. If no TopicID is provided then the sproc returns
-- everything under the topic with the lowest id.
--------------------------------------------------------------------------------------------------------------------------------
CREATE PROCEDURE [dbo].[GetTopics]
@TopicID int = -1,
@DeepLoad bit = 1,
@TopicKey nvarchar(255) = null
AS
--------------------------------------------------------------------------------------------------------------------------------
-- GET TOPIC ID IF UNKNOWN.
--------------------------------------------------------------------------------------------------------------------------------
IF @TopicKey IS NOT NULL
BEGIN
SET @TopicID = dbo.GetTopicID(@TopicKey)
END
IF @TopicID < 0
BEGIN
SET @TopicID = dbo.GetTopicID('Root')
END
--------------------------------------------------------------------------------------------------------------------------------
-- CREATE TEMP TABLES
--------------------------------------------------------------------------------------------------------------------------------
CREATE
TABLE #Topics (
TopicID INT,
SortOrder INT
)
CREATE
CLUSTERED INDEX IX_C_Topics_TopicID
ON #Topics(
TopicID
)
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT TOPIC AND DESCENDENTS
--------------------------------------------------------------------------------------------------------------------------------
IF @DeepLoad = 1
BEGIN
INSERT #Topics (
TopicID,
SortOrder
)
SELECT T1.TopicID,
T1.RangeLeft
FROM Topics AS T1
INNER JOIN Topics AS T2
ON T1.RangeLeft
BETWEEN T2.RangeLeft
AND ISNULL(T2.RangeRight, 0)
AND T2.TopicID = @TopicID
ORDER BY T1.RangeLeft
OPTION (
OPTIMIZE
FOR ( @TopicID = 1
)
)
END
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT TOPIC ONLY
--------------------------------------------------------------------------------------------------------------------------------
ELSE
BEGIN
INSERT #Topics (
TopicID,
SortOrder
)
SELECT TopicID,
1
FROM Topics
WHERE TopicID = @TopicID
OPTION (
OPTIMIZE
FOR ( @TopicID UNKNOWN
)
)
END
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT KEY ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
SELECT TopicIndex.TopicID,
TopicIndex.ContentType,
TopicIndex.ParentID,
TopicIndex.TopicKey,
Storage.SortOrder
FROM TopicIndex AS TopicIndex
JOIN #Topics AS Storage
ON Storage.TopicID = TopicIndex.TopicID
ORDER BY SortOrder
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT TOPIC ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
SELECT Attributes.TopicID,
Attributes.AttributeKey,
Attributes.AttributeValue,
Attributes.Version
FROM AttributeIndex Attributes
JOIN #Topics AS Storage
ON Storage.TopicID = Attributes.TopicID
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT EXTENDED ATTRIBUTES
--------------------------------------------------------------------------------------------------------------------------------
SELECT Attributes.TopicID,
Attributes.AttributesXml,
Attributes.Version
FROM ExtendedAttributeIndex AS Attributes
JOIN #Topics AS Storage
ON Storage.TopicID = Attributes.TopicID
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT RELATIONSHIPS
--------------------------------------------------------------------------------------------------------------------------------
SELECT Relationships.Source_TopicID,
Relationships.RelationshipKey,
Relationships.Target_TopicID
FROM Relationships Relationships
JOIN #Topics AS Storage
ON Storage.TopicID = Relationships.Source_TopicID
--------------------------------------------------------------------------------------------------------------------------------
-- SELECT HISTORY
--------------------------------------------------------------------------------------------------------------------------------
SELECT VersionHistory.TopicID,
VersionHistory.Version
FROM VersionHistoryIndex VersionHistory
JOIN #Topics AS Storage
ON Storage.TopicID = VersionHistory.TopicID;