-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqb_where.go
451 lines (390 loc) · 13.5 KB
/
qb_where.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
package qb
import "fmt"
// WhereExists constructs one builder from another to implement WHERE EXISTS sql/dml clause
func (q *QbDB) WhereExists(db *QbDB) *QbDB {
q.Builder.whereExists = " WHERE EXISTS(" + db.Builder.buildSelect() + ")"
return q
}
// WhereNotExists constructs one builder from another to implement WHERE NOT EXISTS sql/dml clause
func (q *QbDB) WhereNotExists(db *QbDB) *QbDB {
q.Builder.whereExists = " WHERE NOT EXISTS(" + db.Builder.buildSelect() + ")"
return q
}
// Where accepts left operand-operator-right operand to apply them to where clause
func (q *QbDB) Where(operand, operator string, value any) *QbDB {
return q.buildWhere("", operand, operator, value)
}
// WhereEqIf accepts left operand-operator-right operand to apply them to where clause
func (q *QbDB) WhereEqIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.Where(operand, "=", value)
}
// WhereEqThanIf accepts left operand-operator-right operand to apply them to where clause
func (q *QbDB) WhereEqThanIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.Where(operand, ">=", value)
}
// WhereLessIf accepts left operand-operator-right operand to apply them to where clause
func (q *QbDB) WhereLessIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.Where(operand, "<", value)
}
// WhereLessThanIf accepts left operand-operator-right operand to apply them to where clause
func (q *QbDB) WhereLessThanIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.Where(operand, "<=", value)
}
// AndWhere accepts left operand-operator-right operand to apply them to where clause
// with AND logical operator
func (q *QbDB) AndWhere(operand, operator string, value any) *QbDB {
return q.buildWhere("AND", operand, operator, value)
}
// AndWhereEqIf accepts left operand-operator-right operand to apply them to where clause
// with AND logical operator
func (q *QbDB) AndWhereEqIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.AndWhere(operand, "=", value)
}
// AndWhereEqThanIf accepts left operand-operator-right operand to apply them to where clause
// with AND logical operator
func (q *QbDB) AndWhereEqThanIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.AndWhere(operand, ">=", value)
}
// AndWhereLessIf accepts left operand-operator-right operand to apply them to where clause
// with AND logical operator
func (q *QbDB) AndWhereLessIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.AndWhere(operand, "<", value)
}
// AndWhereLessThanIf accepts left operand-operator-right operand to apply them to where clause
// with AND logical operator
func (q *QbDB) AndWhereLessThanIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.AndWhere(operand, "<=", value)
}
// OrWhere accepts left operand-operator-right operand to apply them to where clause
// with OR logical operator
func (q *QbDB) OrWhere(operand, operator string, value any) *QbDB {
return q.buildWhere("OR", operand, operator, value)
}
// OrWhereEqIf accepts left operand-operator-right operand to apply them to where clause
// with OR logical operator
func (q *QbDB) OrWhereEqIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.OrWhere(operand, "=", value)
}
// OrWhereEqThanIf accepts left operand-operator-right operand to apply them to where clause
// with OR logical operator
func (q *QbDB) OrWhereEqThanIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.OrWhere(operand, ">=", value)
}
// OrWhereLessIf accepts left operand-operator-right operand to apply them to where clause
// with OR logical operator
func (q *QbDB) OrWhereLessIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.OrWhere(operand, "<", value)
}
// OrWhereLessThanIf accepts left operand-operator-right operand to apply them to where clause
// with OR logical operator
func (q *QbDB) OrWhereLessThanIf(fnc func() bool, operand string, value any) *QbDB {
if !fnc() {
return q
}
return q.OrWhere(operand, "<=", value)
}
// WhereBetween sets the clause BETWEEN 2 values
func (q *QbDB) WhereBetween(column string, value1, value2 any) *QbDB {
return q.buildWhere("", column, SqlOperatorBetween, transform2String(value1)+And+transform2String(value2))
}
// WhereBetweenIf sets the clause BETWEEN 2 values
func (q *QbDB) WhereBetweenIf(fnc func() bool, column string, value1, value2 any) *QbDB {
if !fnc() {
return q
}
return q.WhereBetween(column, value1, value2)
}
// OrWhereBetween sets the clause OR BETWEEN 2 values
func (q *QbDB) OrWhereBetween(column string, value1, value2 any) *QbDB {
return q.buildWhere(SqlOperatorOr, column, SqlOperatorBetween, transform2String(value1)+And+transform2String(value2))
}
// OrWhereBetweenIf sets the clause OR BETWEEN 2 values
func (q *QbDB) OrWhereBetweenIf(fnc func() bool, column string, value1, value2 any) *QbDB {
if !fnc() {
return q
}
return q.OrWhereBetween(column, value1, value2)
}
// AndWhereBetween sets the clause AND BETWEEN 2 values
func (q *QbDB) AndWhereBetween(column string, value1, value2 any) *QbDB {
return q.buildWhere(SqlOperatorAnd, column, SqlOperatorBetween, transform2String(value1)+And+transform2String(value2))
}
// AndWhereBetweenIf sets the clause AND BETWEEN 2 values
func (q *QbDB) AndWhereBetweenIf(fnc func() bool, column string, value1, value2 any) *QbDB {
if !fnc() {
return q
}
return q.AndWhereBetween(column, value1, value2)
}
// WhereNotBetween sets the clause NOT BETWEEN 2 values
func (q *QbDB) WhereNotBetween(column string, value1, value2 any) *QbDB {
return q.buildWhere("", column, SqlOperatorNotBetween, transform2String(value1)+And+transform2String(value2))
}
// WhereNotBetweenIf sets the clause NOT BETWEEN 2 values
func (q *QbDB) WhereNotBetweenIf(fnc func() bool, column string, value1, value2 any) *QbDB {
if !fnc() {
return q
}
return q.WhereNotBetween(column, value1, value2)
}
// OrWhereNotBetween sets the clause OR BETWEEN 2 values
func (q *QbDB) OrWhereNotBetween(column string, value1, value2 any) *QbDB {
return q.buildWhere(SqlOperatorOr, column, SqlOperatorNotBetween, transform2String(value1)+And+transform2String(value2))
}
// OrWhereNotBetweenIf sets the clause OR BETWEEN 2 values
func (q *QbDB) OrWhereNotBetweenIf(fnc func() bool, column string, value1, value2 any) *QbDB {
if !fnc() {
return q
}
return q.OrWhereNotBetween(column, value1, value2)
}
// AndWhereNotBetween sets the clause AND BETWEEN 2 values
func (q *QbDB) AndWhereNotBetween(column string, value1, value2 any) *QbDB {
return q.buildWhere(SqlOperatorAnd, column, SqlOperatorNotBetween, transform2String(value1)+And+transform2String(value2))
}
// AndWhereNotBetweenIf sets the clause AND BETWEEN 2 values
func (q *QbDB) AndWhereNotBetweenIf(fnc func() bool, column string, value1, value2 any) *QbDB {
if !fnc() {
return q
}
return q.AndWhereNotBetween(column, value1, value2)
}
// WhereRaw accepts custom string to apply it to where clause
func (q *QbDB) WhereRaw(raw string) *QbDB {
q.Builder.where = Where + raw
return q
}
// WhereRawIf accepts custom string to apply it to where clause
func (q *QbDB) WhereRawIf(fnc func() bool, raw string) *QbDB {
if !fnc() {
return q
}
return q.WhereRaw(raw)
}
// OrWhereRaw accepts custom string to apply it to where clause with logical OR
func (q *QbDB) OrWhereRaw(raw string) *QbDB {
q.Builder.where += Or + raw
return q
}
// OrWhereRawIf accepts custom string to apply it to where clause with logical OR
func (q *QbDB) OrWhereRawIf(fnc func() bool, raw string) *QbDB {
if !fnc() {
return q
}
return q.OrWhereRaw(raw)
}
// AndWhereRaw accepts custom string to apply it to where clause with logical OR
func (q *QbDB) AndWhereRaw(raw string) *QbDB {
q.Builder.where += And + raw
return q
}
// AndWhereRawIf accepts custom string to apply it to where clause with logical OR
func (q *QbDB) AndWhereRawIf(fnc func() bool, raw string) *QbDB {
if !fnc() {
return q
}
return q.AndWhereRaw(raw)
}
// WhereIn appends IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) WhereIn(field string, values any) *QbDB {
ins, err := Interface2Slice(values)
if err != nil {
return nil
}
q.buildWhere("", field, "IN", ins)
return q
}
// WhereInIf appends IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) WhereInIf(fnc func() bool, field string, values any) *QbDB {
if !fnc() {
return q
}
return q.WhereIn(field, values)
}
// WhereNotIn appends NOT IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) WhereNotIn(field string, values any) *QbDB {
ins, err := Interface2Slice(values)
if err != nil {
return nil
}
q.buildWhere("", field, "NOT IN", ins)
return q
}
// WhereNotInIf appends NOT IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) WhereNotInIf(fnc func() bool, field string, values any) *QbDB {
if !fnc() {
return q
}
return q.WhereNotIn(field, values)
}
// OrWhereIn appends OR IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) OrWhereIn(field string, values any) *QbDB {
ins, err := Interface2Slice(values)
if err != nil {
return nil
}
q.buildWhere("OR", field, "IN", ins)
return q
}
// OrWhereInIf appends OR IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) OrWhereInIf(fnc func() bool, field string, values any) *QbDB {
if !fnc() {
return q
}
return q.OrWhereIn(field, values)
}
// OrWhereNotIn appends OR NOT IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) OrWhereNotIn(field string, values any) *QbDB {
ins, err := Interface2Slice(values)
if err != nil {
return nil
}
q.buildWhere("OR", field, "NOT IN", ins)
return q
}
// OrWhereNotInIf appends OR NOT IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) OrWhereNotInIf(fnc func() bool, field string, values any) *QbDB {
if !fnc() {
return q
}
return q.OrWhereNotIn(field, values)
}
// AndWhereIn appends OR IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) AndWhereIn(field string, values any) *QbDB {
ins, err := Interface2Slice(values)
if err != nil {
return nil
}
q.buildWhere("AND", field, "IN", ins)
// r.buildWhere("AND", field, "IN", prepareSlice(ins))
return q
}
// AndWhereInIf appends OR IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) AndWhereInIf(fnc func() bool, field string, values any) *QbDB {
if !fnc() {
return q
}
return q.AndWhereIn(field, values)
}
// AndWhereNotIn appends OR NOT IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) AndWhereNotIn(field string, values any) *QbDB {
ins, err := Interface2Slice(values)
if err != nil {
return nil
}
q.buildWhere("AND", field, "NOT IN", ins)
return q
}
// AndWhereNotInIf appends OR NOT IN (val1, val2, val3...) stmt to WHERE clause
func (q *QbDB) AndWhereNotInIf(fnc func() bool, field string, values any) *QbDB {
if !fnc() {
return q
}
return q.AndWhereNotIn(field, values)
}
// WhereNull appends fieldName IS NULL stmt to WHERE clause
func (q *QbDB) WhereNull(field string) *QbDB {
return q.buildWhere("", field, SqlOperatorIs, SqlSpecificValueNull)
}
// WhereNullIf appends fieldName IS NULL stmt to WHERE clause
func (q *QbDB) WhereNullIf(fnc func() bool, field string) *QbDB {
if !fnc() {
return q
}
return q.WhereNull(field)
}
// WhereNotNull appends fieldName IS NOT NULL stmt to WHERE clause
func (q *QbDB) WhereNotNull(field string) *QbDB {
return q.buildWhere("", field, SqlOperatorIs, SqlSpecificValueNotNull)
}
// WhereNotNullIf appends fieldName IS NOT NULL stmt to WHERE clause
func (q *QbDB) WhereNotNullIf(fnc func() bool, field string) *QbDB {
if !fnc() {
return q
}
return q.WhereNotNull(field)
}
// OrWhereNull appends fieldName IS NULL stmt to WHERE clause
func (q *QbDB) OrWhereNull(field string) *QbDB {
return q.buildWhere(SqlOperatorOr, field, SqlOperatorIs, SqlSpecificValueNull)
}
// OrWhereNullIf appends fieldName IS NULL stmt to WHERE clause
func (q *QbDB) OrWhereNullIf(fnc func() bool, field string) *QbDB {
if !fnc() {
return q
}
return q.OrWhereNull(field)
}
// OrWhereNotNull appends fieldName IS NOT NULL stmt to WHERE clause
func (q *QbDB) OrWhereNotNull(field string) *QbDB {
return q.buildWhere(SqlOperatorOr, field, SqlOperatorIs, SqlSpecificValueNotNull)
}
// OrWhereNotNullIf appends fieldName IS NOT NULL stmt to WHERE clause
func (q *QbDB) OrWhereNotNullIf(fnc func() bool, field string) *QbDB {
if !fnc() {
return q
}
return q.OrWhereNotNull(field)
}
// AndWhereNull appends fieldName IS NULL stmt to WHERE clause
func (q *QbDB) AndWhereNull(field string) *QbDB {
return q.buildWhere(SqlOperatorAnd, field, SqlOperatorIs, SqlSpecificValueNull)
}
// AndWhereNullIf appends fieldName IS NULL stmt to WHERE clause
func (q *QbDB) AndWhereNullIf(fnc func() bool, field string) *QbDB {
if !fnc() {
return q
}
return q.AndWhereNull(field)
}
// AndWhereNotNull appends fieldName IS NOT NULL stmt to WHERE clause
func (q *QbDB) AndWhereNotNull(field string) *QbDB {
return q.buildWhere(SqlOperatorAnd, field, SqlOperatorIs, SqlSpecificValueNotNull)
}
// AndWhereNotNullIf appends fieldName IS NOT NULL stmt to WHERE clause
func (q *QbDB) AndWhereNotNullIf(fnc func() bool, field string) *QbDB {
if !fnc() {
return q
}
return q.AndWhereNotNull(field)
}
func (q *QbDB) buildWhere(prefix, operand, operator string, value any) *QbDB {
if IsStringNotEmpty(prefix) {
prefix = fmt.Sprintf("%s%s%s", " ", prefix, " ")
}
q.Builder.whereBindings = append(q.Builder.whereBindings, map[string]any{prefix + operand + " " + operator: value})
return q
}