-
Notifications
You must be signed in to change notification settings - Fork 0
/
query.go
207 lines (182 loc) · 5.8 KB
/
query.go
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
package mptt
import (
"gorm.io/gorm"
"math"
)
// RefreshNode 刷新节点信息,在Insert, Move, Delete操作之后,
// 有可能导致某个节点的信息已发生变化,需要通过id get到节点来刷新位置信息
func (t *tree) RefreshNode(n interface{}) error {
return t.Table(t.tableName).Where(t.colID()+" = ?", t.getNodeID(n)).Find(n).Error
}
func (t *tree) getDescendantCount(n interface{}) int {
return int(math.Floor(float64(t.getRight(n)-t.getLeft(n)-1) / 2))
}
func (t *tree) GetDescendantCount() int {
return t.getDescendantCount(t.node)
}
func (t *tree) GetAncestors(outListPtr interface{}, ascending, includeSelf bool) error {
var (
leftCol = t.colLeft()
)
order := leftCol + " desc"
if ascending {
order = leftCol + " asc"
}
whereSql := "[tree_id] = ?"
if includeSelf {
whereSql += " AND [left] <= ? AND [right] >= ?"
} else {
whereSql += " AND [left] < ? AND [right] > ?"
}
return t.Model(reflectNew(t.node)).
Where(t.replacePlaceholder(whereSql),
t.getTreeID(t.node),
t.getLeft(t.node),
t.getRight(t.node),
).Order(order).Find(outListPtr).Error
}
func (t *tree) GetDescendants(outListPtr interface{}, includeSelf bool) error {
whereSql := "[tree_id] = ?"
if includeSelf {
whereSql += " AND [left] >= ? AND [right] <= ?"
} else {
whereSql += " AND [left] > ? AND [right] < ?"
}
return t.Model(reflectNew(t.node)).
Where(t.replacePlaceholder(whereSql),
t.getTreeID(t.node),
t.getLeft(t.node),
t.getRight(t.node),
).Order(t.colLeft() + " asc").Find(outListPtr).Error
}
func (t *tree) GetFamily(outListPtr interface{}) error {
var (
treeId = t.getTreeID(t.node)
left = t.getLeft(t.node)
right = t.getRight(t.node)
)
whereSql := "[tree_id] = ? AND (([left] <= ? AND [right] >= ?) OR ( [left] > ? AND [right] < ? ))"
return t.Where(
t.replacePlaceholder(whereSql),
treeId,
left,
right,
left,
right,
).Order(t.colLeft() + " ASC").Find(outListPtr).Error
}
func (t *tree) GetChildren(outListPtr interface{}) error {
whereSql := "[tree_id] = ? AND [parent_id] = ?"
return t.Model(reflectNew(t.node)).
Where(t.replacePlaceholder(whereSql),
t.getTreeID(t.node),
t.getNodeID(t.node),
).
Order(t.colLeft() + " asc").
Find(outListPtr).Error
}
func (t *tree) GetLeafNodes(outListPtr interface{}) error {
var (
leftCol = t.colLeft()
rightCol = t.colRight()
treeId = t.getTreeID(t.node)
left = t.getLeft(t.node)
right = t.getRight(t.node)
)
whereSql := "[tree_id] = ? AND [left] > ? AND [right] < ?"
return t.Model(reflectNew(t.node)).
Where(t.replacePlaceholder(whereSql), treeId, left, right).
Where(gorm.Expr(rightCol + " - " + leftCol + " = 1")).
Order(t.colLeft() + " asc").
Find(outListPtr).Error
}
func (t *tree) GetSiblings(outListPtr interface{}, includeSelf bool) error {
tx := t.Model(reflectNew(t.node)).
Where(t.colParent()+" = ?", t.getParentID(t.node))
if !includeSelf {
tx = tx.Where(t.colID()+" <> ?", t.getNodeID(t.node))
}
return tx.Order(t.colLeft() + " asc").Find(outListPtr).Error
}
func (t *tree) nextSibling(tx *gorm.DB, node interface{}) *gorm.DB {
whereSql := t.replacePlaceholder("[parent_id] = ? AND [left] > ?")
return tx.Where(whereSql, t.getParentID(node), t.getRight(node)).
Order(t.colLeft() + " asc")
}
func (t *tree) GetNextSibling(outPtr interface{}, conds ...interface{}) error {
if t.isRootNode(t.node) {
return t.Where(t.colTree()+"> ?", t.getTreeID(t.node)).First(outPtr, conds...).Error
}
return t.Scopes(func(tx *gorm.DB) *gorm.DB {
return t.nextSibling(tx, t.node)
}).First(outPtr, conds...).Error
}
func (t *tree) previousSibling(tx *gorm.DB, node interface{}) *gorm.DB {
whereSql := t.replacePlaceholder("[parent_id] = ? AND [right] < ?")
return tx.Where(whereSql, t.getParentID(node), t.getLeft(node)).
Order(t.colRight() + " desc")
}
func (t *tree) GetPreviousSibling(outPtr interface{}, conds ...interface{}) error {
if t.isRootNode(t.node) {
return t.Where(t.colTree()+"< ?", t.getTreeID(t.node)).First(outPtr, conds...).Error
}
return t.Scopes(func(tx *gorm.DB) *gorm.DB {
return t.previousSibling(tx, t.node)
}).First(outPtr, conds...).Error
}
func (t *tree) GetRoot(outPtr interface{}) error {
return t.RootNode(t.getTreeID(t.node), outPtr)
}
func (t *tree) GetLevel() int {
return t.getLevel(t.node)
}
func (t *tree) IsChildNode() bool {
return t.isChildNode(t.node)
}
func (t *tree) isChildNode(n interface{}) bool {
return !t.isRootNode(n)
}
func (t *tree) IsRootNode() bool {
return t.isRootNode(t.node)
}
func (t *tree) isRootNode(n interface{}) bool {
return isEmpty(t.getParentID(n))
}
func (t *tree) IsLeafNode() bool {
return t.getDescendantCount(t.node) == 0
}
func (t *tree) IsDescendantOf(other interface{}, includeSelf bool) bool {
var (
otherID = t.getNodeID(other)
curID = t.getNodeID(t.node)
)
if includeSelf && t.equalIDValue(curID, otherID) {
return true
}
if t.getTreeID(other) != t.getTreeID(t.node) {
return false
}
return t.getLeft(t.node) > t.getLeft(other) && t.getRight(t.node) < t.getRight(other)
}
func (t *tree) IsAncestorOf(other interface{}, includeSelf bool) bool {
var (
otherID = t.getNodeID(other)
curID = t.getNodeID(t.node)
)
if includeSelf && t.equalIDValue(curID, otherID) {
return true
}
if t.getTreeID(other) != t.getTreeID(t.node) {
return false
}
return t.getLeft(t.node) < t.getLeft(other) && t.getRight(t.node) > t.getRight(other)
}
func (t *tree) RootNodes(outListPtr interface{}) error {
emptyNode := reflectNew(t.node)
return t.Model(emptyNode).Where(t.colParent()+" = ?", t.getParentID(emptyNode)).Find(outListPtr).Error
}
func (t *tree) RootNode(treeID int, outPtr interface{}) error {
emptyNode := reflectNew(t.node)
whereSql := t.replacePlaceholder("[parent_id] = ? AND [tree_id] = ?")
return t.Model(emptyNode).Where(whereSql, t.getParentID(emptyNode), treeID).Find(outPtr).Error
}