Skip to content

Commit 22b5a6c

Browse files
committed
Better idomatic go for query building
1 parent c869bf7 commit 22b5a6c

File tree

1 file changed

+70
-47
lines changed

1 file changed

+70
-47
lines changed

metrics/error/query.go

Lines changed: 70 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ func ErrorsQuery(dom blip.Domain, baseQuery string, groupBy string, subDomain st
3636
}, nil
3737
}
3838

39-
func initWhere(where *string) {
40-
if *where == "" {
41-
*where = " WHERE"
39+
// setAnd is a helper function to determine if the "AND" clause should be added to the WHERE clause.
40+
func setAnd(where strings.Builder) string {
41+
if where.Len() == 0 {
42+
return ""
4243
} else {
43-
*where = *where + " AND"
44+
return " AND"
4445
}
4546
}
4647

4748
func setWhere(dom blip.Domain, subDomain string) (string, []any, error) {
4849
// Initialize the where clause once if we need it
49-
var where string
50+
var where strings.Builder
5051
var params []any = make([]any, 0)
5152
var errorNum []any = make([]any, 0)
5253
var errorName []any = make([]any, 0)
@@ -64,7 +65,7 @@ func setWhere(dom blip.Domain, subDomain string) (string, []any, error) {
6465
return "", nil, fmt.Errorf("invalid option %s", dom.Options[OPT_ALL])
6566
}
6667

67-
initWhere(&where)
68+
where.WriteString(setAnd(where))
6869

6970
for _, metric := range dom.Metrics {
7071
if i, err := strconv.Atoi(metric); err == nil {
@@ -85,35 +86,46 @@ func setWhere(dom blip.Domain, subDomain string) (string, []any, error) {
8586
params = append(params, errorName...)
8687
}
8788

88-
where = where + fmt.Sprintf(" (%s)", strings.Join(errorConditions, conj))
89+
where.WriteString(fmt.Sprintf(" (%s)", strings.Join(errorConditions, conj)))
8990
} else {
9091
// Exclude NULL error numbers
91-
initWhere(&where)
92-
where = where + " ERROR_NUMBER IS NOT NULL"
92+
where.WriteString(setAnd(where))
93+
where.WriteString(" ERROR_NUMBER IS NOT NULL")
9394
}
9495

9596
// Handle include/exclude filters based on the collector type
97+
var subWhere string
98+
var subParams []any = make([]any, 0)
99+
96100
switch subDomain {
97101
case SUB_DOMAIN_ACCOUNT:
98-
params = addAccountFilters(&where, params, dom)
102+
subWhere, subParams = getAccountFilters(dom)
99103
case SUB_DOMAIN_USER:
100-
params = addUserFilters(&where, params, dom)
104+
subWhere, subParams = getUserFilters(dom)
101105
case SUB_DOMAIN_HOST:
102-
params = addHostFilters(&where, params, dom)
106+
subWhere, subParams = getHostFilters(dom)
103107
case SUB_DOMAIN_THREAD:
104-
params = addThreadFilters(&where, params, dom)
108+
subWhere, subParams = getThreadFilters(dom)
105109
case SUB_DOMAIN_GLOBAL:
106110
// No additional filters needed
107111
}
108112

113+
if len(subWhere) > 0 {
114+
where.WriteString(setAnd(where))
115+
where.WriteString(subWhere)
116+
params = append(params, subParams...)
117+
}
118+
109119
// Exclude errors without any errors raised
110-
initWhere(&where)
111-
where = where + " SUM_ERROR_RAISED > 0"
120+
where.WriteString(setAnd(where))
121+
where.WriteString(" SUM_ERROR_RAISED > 0")
112122

113-
return where, params, nil
123+
return " WHERE" + where.String(), params, nil
114124
}
115125

116-
func addAccountFilters(where *string, params []any, dom blip.Domain) []any {
126+
func getAccountFilters(dom blip.Domain) (string, []any) {
127+
var where strings.Builder
128+
var params []any = []any{}
117129
hasValidTokens := false
118130
var conjunction, inOp string
119131
onlyUser := []string{}
@@ -154,8 +166,8 @@ func addAccountFilters(where *string, params []any, dom blip.Domain) []any {
154166
}
155167

156168
if hasValidTokens {
157-
initWhere(where)
158-
*where = *where + " ("
169+
where.WriteString(setAnd(where))
170+
where.WriteString(" (")
159171
parts := make([]string, 0, 3)
160172

161173
if len(onlyAccount) > 0 {
@@ -175,73 +187,84 @@ func addAccountFilters(where *string, params []any, dom blip.Domain) []any {
175187
params = append(params, sqlutil.ToInterfaceArray(onlyHost)...)
176188
}
177189

178-
*where = *where + strings.Join(parts, fmt.Sprintf(" %s ", conjunction)) + ")"
190+
where.WriteString(strings.Join(parts, fmt.Sprintf(" %s ", conjunction)))
191+
where.WriteString(")")
179192
}
180193

181194
// Ensure user and host are not NULL
182-
initWhere(where)
183-
*where = *where + " USER IS NOT NULL AND HOST IS NOT NULL"
195+
where.WriteString(setAnd(where))
196+
where.WriteString(" USER IS NOT NULL AND HOST IS NOT NULL")
184197

185-
return params
198+
return where.String(), params
186199
}
187200

188-
func addUserFilters(where *string, params []any, dom blip.Domain) []any {
201+
func getUserFilters(dom blip.Domain) (string, []any) {
202+
var where strings.Builder
203+
var params []any = []any{}
204+
189205
if include := dom.Options[OPT_INCLUDE]; include != "" {
190-
initWhere(where)
206+
where.WriteString(setAnd(where))
191207
users := strings.Split(include, ",")
192-
*where = *where + fmt.Sprintf(" USER IN (%s)", sqlutil.PlaceholderList(len(users)))
208+
209+
where.WriteString(fmt.Sprintf(" USER IN (%s)", sqlutil.PlaceholderList(len(users))))
193210
params = append(params, sqlutil.ToInterfaceArray(users)...)
194211
} else if exclude := dom.Options[OPT_EXCLUDE]; exclude != "" {
195-
initWhere(where)
212+
where.WriteString(setAnd(where))
196213
users := strings.Split(exclude, ",")
197-
*where = *where + fmt.Sprintf(" USER NOT IN (%s)", sqlutil.PlaceholderList(len(users)))
214+
where.WriteString(fmt.Sprintf(" USER NOT IN (%s)", sqlutil.PlaceholderList(len(users))))
198215
params = append(params, sqlutil.ToInterfaceArray(users)...)
199216
} else {
200217
// Exclude NULL users
201-
initWhere(where)
202-
*where = *where + " USER IS NOT NULL"
218+
where.WriteString(setAnd(where))
219+
where.WriteString(" USER IS NOT NULL")
203220
}
204221

205-
return params
222+
return where.String(), params
206223
}
207224

208-
func addHostFilters(where *string, params []any, dom blip.Domain) []any {
225+
func getHostFilters(dom blip.Domain) (string, []any) {
226+
var where strings.Builder
227+
var params []any = []any{}
228+
209229
if include := dom.Options[OPT_INCLUDE]; include != "" {
210-
initWhere(where)
230+
where.WriteString(setAnd(where))
211231
hosts := strings.Split(include, ",")
212232

213-
*where = *where + fmt.Sprintf(" HOST IN (%s)", sqlutil.PlaceholderList(len(hosts)))
233+
where.WriteString(fmt.Sprintf(" HOST IN (%s)", sqlutil.PlaceholderList(len(hosts))))
214234
params = append(params, sqlutil.ToInterfaceArray(hosts)...)
215235
} else if exclude := dom.Options[OPT_EXCLUDE]; exclude != "" {
216-
initWhere(where)
236+
where.WriteString(setAnd(where))
217237
hosts := strings.Split(exclude, ",")
218-
*where = *where + fmt.Sprintf(" HOST NOT IN (%s)", sqlutil.PlaceholderList(len(hosts)))
238+
where.WriteString(fmt.Sprintf(" HOST NOT IN (%s)", sqlutil.PlaceholderList(len(hosts))))
219239
params = append(params, sqlutil.ToInterfaceArray(hosts)...)
220240
} else {
221241
// Exclude NULL hosts
222-
initWhere(where)
223-
*where = *where + " HOST IS NOT NULL"
242+
where.WriteString(setAnd(where))
243+
where.WriteString(" HOST IS NOT NULL")
224244
}
225245

226-
return params
246+
return where.String(), params
227247
}
228248

229-
func addThreadFilters(where *string, params []any, dom blip.Domain) []any {
249+
func getThreadFilters(dom blip.Domain) (string, []any) {
250+
var where strings.Builder
251+
var params []any = []any{}
252+
230253
if include := dom.Options[OPT_INCLUDE]; include != "" {
231-
initWhere(where)
254+
where.WriteString(setAnd(where))
232255
threads := strings.Split(include, ",")
233-
*where = *where + fmt.Sprintf(" THREAD_ID IN (%s)", sqlutil.PlaceholderList(len(threads)))
256+
where.WriteString(fmt.Sprintf(" THREAD_ID IN (%s)", sqlutil.PlaceholderList(len(threads))))
234257
params = append(params, sqlutil.ToInterfaceArray(threads)...)
235258
} else if exclude := dom.Options[OPT_EXCLUDE]; exclude != "" {
236-
initWhere(where)
259+
where.WriteString(setAnd(where))
237260
threads := strings.Split(exclude, ",")
238-
*where = *where + fmt.Sprintf(" THREAD_ID NOT IN (%s)", sqlutil.PlaceholderList(len(threads)))
261+
where.WriteString(fmt.Sprintf(" THREAD_ID NOT IN (%s)", sqlutil.PlaceholderList(len(threads))))
239262
params = append(params, sqlutil.ToInterfaceArray(threads)...)
240263
} else {
241264
// Exclude NULL thread IDs
242-
initWhere(where)
243-
*where = *where + " THREAD_ID IS NOT NULL"
265+
where.WriteString(setAnd(where))
266+
where.WriteString(" THREAD_ID IS NOT NULL")
244267
}
245268

246-
return params
269+
return where.String(), params
247270
}

0 commit comments

Comments
 (0)