@@ -36,17 +36,18 @@ func ErrorsQuery(dom blip.Domain, baseQuery string, groupBy string, subDomain st
36
36
}, nil
37
37
}
38
38
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 ""
42
43
} else {
43
- * where = * where + " AND"
44
+ return " AND"
44
45
}
45
46
}
46
47
47
48
func setWhere (dom blip.Domain , subDomain string ) (string , []any , error ) {
48
49
// Initialize the where clause once if we need it
49
- var where string
50
+ var where strings. Builder
50
51
var params []any = make ([]any , 0 )
51
52
var errorNum []any = make ([]any , 0 )
52
53
var errorName []any = make ([]any , 0 )
@@ -64,7 +65,7 @@ func setWhere(dom blip.Domain, subDomain string) (string, []any, error) {
64
65
return "" , nil , fmt .Errorf ("invalid option %s" , dom .Options [OPT_ALL ])
65
66
}
66
67
67
- initWhere ( & where )
68
+ where . WriteString ( setAnd ( where ) )
68
69
69
70
for _ , metric := range dom .Metrics {
70
71
if i , err := strconv .Atoi (metric ); err == nil {
@@ -85,35 +86,46 @@ func setWhere(dom blip.Domain, subDomain string) (string, []any, error) {
85
86
params = append (params , errorName ... )
86
87
}
87
88
88
- where = where + fmt .Sprintf (" (%s)" , strings .Join (errorConditions , conj ))
89
+ where . WriteString ( fmt .Sprintf (" (%s)" , strings .Join (errorConditions , conj ) ))
89
90
} else {
90
91
// 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" )
93
94
}
94
95
95
96
// Handle include/exclude filters based on the collector type
97
+ var subWhere string
98
+ var subParams []any = make ([]any , 0 )
99
+
96
100
switch subDomain {
97
101
case SUB_DOMAIN_ACCOUNT :
98
- params = addAccountFilters ( & where , params , dom )
102
+ subWhere , subParams = getAccountFilters ( dom )
99
103
case SUB_DOMAIN_USER :
100
- params = addUserFilters ( & where , params , dom )
104
+ subWhere , subParams = getUserFilters ( dom )
101
105
case SUB_DOMAIN_HOST :
102
- params = addHostFilters ( & where , params , dom )
106
+ subWhere , subParams = getHostFilters ( dom )
103
107
case SUB_DOMAIN_THREAD :
104
- params = addThreadFilters ( & where , params , dom )
108
+ subWhere , subParams = getThreadFilters ( dom )
105
109
case SUB_DOMAIN_GLOBAL :
106
110
// No additional filters needed
107
111
}
108
112
113
+ if len (subWhere ) > 0 {
114
+ where .WriteString (setAnd (where ))
115
+ where .WriteString (subWhere )
116
+ params = append (params , subParams ... )
117
+ }
118
+
109
119
// 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" )
112
122
113
- return where , params , nil
123
+ return " WHERE" + where . String () , params , nil
114
124
}
115
125
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 {}
117
129
hasValidTokens := false
118
130
var conjunction , inOp string
119
131
onlyUser := []string {}
@@ -154,8 +166,8 @@ func addAccountFilters(where *string, params []any, dom blip.Domain) []any {
154
166
}
155
167
156
168
if hasValidTokens {
157
- initWhere ( where )
158
- * where = * where + " ("
169
+ where . WriteString ( setAnd ( where ) )
170
+ where . WriteString ( " (" )
159
171
parts := make ([]string , 0 , 3 )
160
172
161
173
if len (onlyAccount ) > 0 {
@@ -175,73 +187,84 @@ func addAccountFilters(where *string, params []any, dom blip.Domain) []any {
175
187
params = append (params , sqlutil .ToInterfaceArray (onlyHost )... )
176
188
}
177
189
178
- * where = * where + strings .Join (parts , fmt .Sprintf (" %s " , conjunction )) + ")"
190
+ where .WriteString (strings .Join (parts , fmt .Sprintf (" %s " , conjunction )))
191
+ where .WriteString (")" )
179
192
}
180
193
181
194
// 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" )
184
197
185
- return params
198
+ return where . String (), params
186
199
}
187
200
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
+
189
205
if include := dom .Options [OPT_INCLUDE ]; include != "" {
190
- initWhere ( where )
206
+ where . WriteString ( setAnd ( where ) )
191
207
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 ))))
193
210
params = append (params , sqlutil .ToInterfaceArray (users )... )
194
211
} else if exclude := dom .Options [OPT_EXCLUDE ]; exclude != "" {
195
- initWhere ( where )
212
+ where . WriteString ( setAnd ( where ) )
196
213
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 ) )))
198
215
params = append (params , sqlutil .ToInterfaceArray (users )... )
199
216
} else {
200
217
// 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" )
203
220
}
204
221
205
- return params
222
+ return where . String (), params
206
223
}
207
224
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
+
209
229
if include := dom .Options [OPT_INCLUDE ]; include != "" {
210
- initWhere ( where )
230
+ where . WriteString ( setAnd ( where ) )
211
231
hosts := strings .Split (include , "," )
212
232
213
- * where = * where + fmt .Sprintf (" HOST IN (%s)" , sqlutil .PlaceholderList (len (hosts )))
233
+ where . WriteString ( fmt .Sprintf (" HOST IN (%s)" , sqlutil .PlaceholderList (len (hosts ) )))
214
234
params = append (params , sqlutil .ToInterfaceArray (hosts )... )
215
235
} else if exclude := dom .Options [OPT_EXCLUDE ]; exclude != "" {
216
- initWhere ( where )
236
+ where . WriteString ( setAnd ( where ) )
217
237
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 ) )))
219
239
params = append (params , sqlutil .ToInterfaceArray (hosts )... )
220
240
} else {
221
241
// 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" )
224
244
}
225
245
226
- return params
246
+ return where . String (), params
227
247
}
228
248
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
+
230
253
if include := dom .Options [OPT_INCLUDE ]; include != "" {
231
- initWhere ( where )
254
+ where . WriteString ( setAnd ( where ) )
232
255
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 ) )))
234
257
params = append (params , sqlutil .ToInterfaceArray (threads )... )
235
258
} else if exclude := dom .Options [OPT_EXCLUDE ]; exclude != "" {
236
- initWhere ( where )
259
+ where . WriteString ( setAnd ( where ) )
237
260
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 ) )))
239
262
params = append (params , sqlutil .ToInterfaceArray (threads )... )
240
263
} else {
241
264
// 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" )
244
267
}
245
268
246
- return params
269
+ return where . String (), params
247
270
}
0 commit comments