-
Notifications
You must be signed in to change notification settings - Fork 34
/
search_builder.go
195 lines (153 loc) · 4.76 KB
/
search_builder.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
package sqlstore
import (
"strings"
m "github.com/grafana/grafana/pkg/models"
)
// SearchBuilder is a builder/object mother that builds a dashboard search query
type SearchBuilder struct {
SqlBuilder
tags []string
isStarred bool
limit int
signedInUser *m.SignedInUser
whereDashboardIdsIn []int64
whereTitle string
whereTypeFolder bool
whereTypeDash bool
whereFolderIds []int64
permission m.PermissionType
}
func NewSearchBuilder(signedInUser *m.SignedInUser, limit int, permission m.PermissionType) *SearchBuilder {
searchBuilder := &SearchBuilder{
signedInUser: signedInUser,
limit: limit,
permission: permission,
}
return searchBuilder
}
func (sb *SearchBuilder) WithTags(tags []string) *SearchBuilder {
if len(tags) > 0 {
sb.tags = tags
}
return sb
}
func (sb *SearchBuilder) IsStarred() *SearchBuilder {
sb.isStarred = true
return sb
}
func (sb *SearchBuilder) WithDashboardIdsIn(ids []int64) *SearchBuilder {
if len(ids) > 0 {
sb.whereDashboardIdsIn = ids
}
return sb
}
func (sb *SearchBuilder) WithTitle(title string) *SearchBuilder {
sb.whereTitle = title
return sb
}
func (sb *SearchBuilder) WithType(queryType string) *SearchBuilder {
if len(queryType) > 0 && queryType == "dash-folder" {
sb.whereTypeFolder = true
}
if len(queryType) > 0 && queryType == "dash-db" {
sb.whereTypeDash = true
}
return sb
}
func (sb *SearchBuilder) WithFolderIds(folderIds []int64) *SearchBuilder {
sb.whereFolderIds = folderIds
return sb
}
// ToSql builds the sql and returns it as a string, together with the params.
func (sb *SearchBuilder) ToSql() (string, []interface{}) {
sb.params = make([]interface{}, 0)
sb.buildSelect()
if len(sb.tags) > 0 {
sb.buildTagQuery()
} else {
sb.buildMainQuery()
}
sb.sql.WriteString(`
LEFT OUTER JOIN dashboard folder on folder.id = dashboard.folder_id
LEFT OUTER JOIN dashboard_tag on dashboard.id = dashboard_tag.dashboard_id`)
sb.sql.WriteString(" ORDER BY dashboard.title ASC" + dialect.Limit(5000))
return sb.sql.String(), sb.params
}
func (sb *SearchBuilder) buildSelect() {
sb.sql.WriteString(
`SELECT
dashboard.id,
dashboard.uid,
dashboard.title,
dashboard.slug,
dashboard_tag.term,
dashboard.is_folder,
dashboard.folder_id,
folder.uid as folder_uid,
folder.slug as folder_slug,
folder.title as folder_title
FROM `)
}
func (sb *SearchBuilder) buildTagQuery() {
sb.sql.WriteString(
`(
SELECT
dashboard.id FROM dashboard
LEFT OUTER JOIN dashboard_tag ON dashboard_tag.dashboard_id = dashboard.id
`)
if sb.isStarred {
sb.sql.WriteString(" INNER JOIN star on star.dashboard_id = dashboard.id")
}
sb.sql.WriteString(` WHERE dashboard_tag.term IN (?` + strings.Repeat(",?", len(sb.tags)-1) + `) AND `)
for _, tag := range sb.tags {
sb.params = append(sb.params, tag)
}
sb.buildSearchWhereClause()
// this ends the inner select (tag filtered part)
sb.sql.WriteString(`
GROUP BY dashboard.id HAVING COUNT(dashboard.id) >= ?
ORDER BY dashboard.id` + dialect.Limit(int64(sb.limit)) + `) as ids
INNER JOIN dashboard on ids.id = dashboard.id
`)
sb.params = append(sb.params, len(sb.tags))
}
func (sb *SearchBuilder) buildMainQuery() {
sb.sql.WriteString(`( SELECT dashboard.id FROM dashboard `)
if sb.isStarred {
sb.sql.WriteString(" INNER JOIN star on star.dashboard_id = dashboard.id")
}
sb.sql.WriteString(` WHERE `)
sb.buildSearchWhereClause()
sb.sql.WriteString(` ORDER BY dashboard.title` + dialect.Limit(int64(sb.limit)) + `) as ids INNER JOIN dashboard on ids.id = dashboard.id `)
}
func (sb *SearchBuilder) buildSearchWhereClause() {
sb.sql.WriteString(` dashboard.org_id=?`)
sb.params = append(sb.params, sb.signedInUser.OrgId)
if sb.isStarred {
sb.sql.WriteString(` AND star.user_id=?`)
sb.params = append(sb.params, sb.signedInUser.UserId)
}
if len(sb.whereDashboardIdsIn) > 0 {
sb.sql.WriteString(` AND dashboard.id IN (?` + strings.Repeat(",?", len(sb.whereDashboardIdsIn)-1) + `)`)
for _, dashboardId := range sb.whereDashboardIdsIn {
sb.params = append(sb.params, dashboardId)
}
}
sb.writeDashboardPermissionFilter(sb.signedInUser, sb.permission)
if len(sb.whereTitle) > 0 {
sb.sql.WriteString(" AND dashboard.title " + dialect.LikeStr() + " ?")
sb.params = append(sb.params, "%"+sb.whereTitle+"%")
}
if sb.whereTypeFolder {
sb.sql.WriteString(" AND dashboard.is_folder = " + dialect.BooleanStr(true))
}
if sb.whereTypeDash {
sb.sql.WriteString(" AND dashboard.is_folder = " + dialect.BooleanStr(false))
}
if len(sb.whereFolderIds) > 0 {
sb.sql.WriteString(` AND dashboard.folder_id IN (?` + strings.Repeat(",?", len(sb.whereFolderIds)-1) + `) `)
for _, id := range sb.whereFolderIds {
sb.params = append(sb.params, id)
}
}
}