@@ -2,6 +2,7 @@ package query
2
2
3
3
import (
4
4
"fmt"
5
+ "reflect"
5
6
"strings"
6
7
)
7
8
@@ -16,20 +17,20 @@ type Condition interface {
16
17
OrWhereNull (field string ) Condition
17
18
WhereNotNull (field string ) Condition
18
19
WhereNull (field string ) Condition
19
- OrWhereRaw (raw string , items ... interface {} ) Condition
20
- WhereRaw (raw string , items ... interface {} ) Condition
21
- OrWhereNotIn (field string , items ... interface {} ) Condition
22
- OrWhereIn (field string , items ... interface {} ) Condition
23
- WhereNotIn (field string , items ... interface {} ) Condition
24
- WhereIn (field string , items ... interface {} ) Condition
20
+ OrWhereRaw (raw string , items ... any ) Condition
21
+ WhereRaw (raw string , items ... any ) Condition
22
+ OrWhereNotIn (field string , items ... any ) Condition
23
+ OrWhereIn (field string , items ... any ) Condition
24
+ WhereNotIn (field string , items ... any ) Condition
25
+ WhereIn (field string , items ... any ) Condition
25
26
WhereGroup (wc ConditionGroup ) Condition
26
27
OrWhereGroup (wc ConditionGroup ) Condition
27
- Where (field string , value ... interface {} ) Condition
28
- OrWhere (field string , value ... interface {} ) Condition
29
- WhereBetween (field string , min , max interface {} ) Condition
30
- WhereNotBetween (field string , min , max interface {} ) Condition
31
- OrWhereBetween (field string , min , max interface {} ) Condition
32
- OrWhereNotBetween (field string , min , max interface {} ) Condition
28
+ Where (field string , value ... any ) Condition
29
+ OrWhere (field string , value ... any ) Condition
30
+ WhereBetween (field string , min , max any ) Condition
31
+ WhereNotBetween (field string , min , max any ) Condition
32
+ OrWhereBetween (field string , min , max any ) Condition
33
+ OrWhereNotBetween (field string , min , max any ) Condition
33
34
34
35
WhereCondition (cond sqlCondition ) Condition
35
36
@@ -41,7 +42,7 @@ type Condition interface {
41
42
42
43
Clone () Condition
43
44
Empty () bool
44
- Resolve (tableAlias string ) (string , []interface {} )
45
+ Resolve (tableAlias string ) (string , []any )
45
46
}
46
47
47
48
type When func () bool
@@ -51,7 +52,7 @@ type conditionType int
51
52
52
53
const (
53
54
connectTypeAnd connectType = "AND"
54
- connectTypeOr = "OR"
55
+ connectTypeOr connectType = "OR"
55
56
)
56
57
57
58
const (
@@ -70,15 +71,15 @@ const (
70
71
)
71
72
72
73
type SubQuery interface {
73
- ResolveQuery () (string , []interface {} )
74
+ ResolveQuery () (string , []any )
74
75
}
75
76
76
77
type sqlCondition struct {
77
78
Connector connectType
78
79
Type conditionType
79
80
Field string
80
81
Operate string
81
- Values []interface {}
82
+ Values []any
82
83
Nested ConditionGroup
83
84
SubQuery SubQuery
84
85
When When
@@ -153,7 +154,7 @@ func (builder *conditionBuilder) WhereColumn(field, operator string, value strin
153
154
Type : condTypeColumn ,
154
155
Field : field ,
155
156
Operate : operator ,
156
- Values : []interface {} {value },
157
+ Values : []any {value },
157
158
})
158
159
}
159
160
@@ -163,7 +164,7 @@ func (builder *conditionBuilder) OrWhereColumn(field, operator string, value str
163
164
Type : condTypeColumn ,
164
165
Field : field ,
165
166
Operate : operator ,
166
- Values : []interface {} {value },
167
+ Values : []any {value },
167
168
})
168
169
}
169
170
@@ -235,25 +236,25 @@ func (builder *conditionBuilder) WhereNull(field string) Condition {
235
236
})
236
237
}
237
238
238
- func (builder * conditionBuilder ) OrWhereRaw (raw string , items ... interface {} ) Condition {
239
+ func (builder * conditionBuilder ) OrWhereRaw (raw string , items ... any ) Condition {
239
240
return builder .WhereCondition (sqlCondition {
240
241
Connector : connectTypeOr ,
241
242
Type : condTypeRaw ,
242
243
Field : raw ,
243
- Values : items ,
244
+ Values : arrayItems ( items ) ,
244
245
})
245
246
}
246
247
247
- func (builder * conditionBuilder ) WhereRaw (raw string , items ... interface {} ) Condition {
248
+ func (builder * conditionBuilder ) WhereRaw (raw string , items ... any ) Condition {
248
249
return builder .WhereCondition (sqlCondition {
249
250
Connector : connectTypeAnd ,
250
251
Type : condTypeRaw ,
251
252
Field : raw ,
252
- Values : items ,
253
+ Values : arrayItems ( items ) ,
253
254
})
254
255
}
255
256
256
- func (builder * conditionBuilder ) OrWhereNotIn (field string , items ... interface {} ) Condition {
257
+ func (builder * conditionBuilder ) OrWhereNotIn (field string , items ... any ) Condition {
257
258
return builder .WhereCondition (sqlCondition {
258
259
Connector : connectTypeOr ,
259
260
Type : condTypeNotIn ,
@@ -262,30 +263,50 @@ func (builder *conditionBuilder) OrWhereNotIn(field string, items ...interface{}
262
263
})
263
264
}
264
265
265
- func (builder * conditionBuilder ) OrWhereIn (field string , items ... interface {}) Condition {
266
+ func arrayItems (items []any ) []any {
267
+ if len (items ) == 1 {
268
+ s := reflect .ValueOf (items [0 ])
269
+ if s .Kind () == reflect .Slice {
270
+ if s .IsNil () {
271
+ return []any {}
272
+ }
273
+
274
+ r := make ([]any , s .Len ())
275
+ for i := 0 ; i < s .Len (); i ++ {
276
+ r [i ] = s .Index (i ).Interface ()
277
+ }
278
+
279
+ return r
280
+ }
281
+ }
282
+
283
+ return items
284
+ }
285
+
286
+ func (builder * conditionBuilder ) OrWhereIn (field string , items ... any ) Condition {
266
287
return builder .WhereCondition (sqlCondition {
267
288
Connector : connectTypeOr ,
268
289
Type : condTypeIn ,
269
290
Field : field ,
270
- Values : items ,
291
+ Values : arrayItems ( items ) ,
271
292
})
272
293
}
273
294
274
- func (builder * conditionBuilder ) WhereNotIn (field string , items ... interface {} ) Condition {
295
+ func (builder * conditionBuilder ) WhereNotIn (field string , items ... any ) Condition {
275
296
return builder .WhereCondition (sqlCondition {
276
297
Connector : connectTypeAnd ,
277
298
Type : condTypeNotIn ,
278
299
Field : field ,
279
- Values : items ,
300
+ Values : arrayItems ( items ) ,
280
301
})
281
302
}
282
303
283
- func (builder * conditionBuilder ) WhereIn (field string , items ... interface {} ) Condition {
304
+ func (builder * conditionBuilder ) WhereIn (field string , items ... any ) Condition {
284
305
return builder .WhereCondition (sqlCondition {
285
306
Connector : connectTypeAnd ,
286
307
Type : condTypeIn ,
287
308
Field : field ,
288
- Values : items ,
309
+ Values : arrayItems ( items ) ,
289
310
})
290
311
}
291
312
@@ -305,11 +326,11 @@ func (builder *conditionBuilder) OrWhereGroup(wc ConditionGroup) Condition {
305
326
})
306
327
}
307
328
308
- func (builder * conditionBuilder ) Where (field string , value ... interface {} ) Condition {
329
+ func (builder * conditionBuilder ) Where (field string , value ... any ) Condition {
309
330
argCount := len (value )
310
331
311
332
var operator string
312
- var values []interface {}
333
+ var values []any
313
334
314
335
if argCount == 1 {
315
336
operator = "="
@@ -335,11 +356,11 @@ func (builder *conditionBuilder) Where(field string, value ...interface{}) Condi
335
356
})
336
357
}
337
358
338
- func (builder * conditionBuilder ) OrWhere (field string , value ... interface {} ) Condition {
359
+ func (builder * conditionBuilder ) OrWhere (field string , value ... any ) Condition {
339
360
argCount := len (value )
340
361
341
362
var operator string
342
- var values []interface {}
363
+ var values []any
343
364
344
365
if argCount == 1 {
345
366
operator = "="
@@ -365,39 +386,39 @@ func (builder *conditionBuilder) OrWhere(field string, value ...interface{}) Con
365
386
})
366
387
}
367
388
368
- func (builder * conditionBuilder ) WhereBetween (field string , min , max interface {} ) Condition {
389
+ func (builder * conditionBuilder ) WhereBetween (field string , min , max any ) Condition {
369
390
return builder .WhereCondition (sqlCondition {
370
391
Connector : connectTypeAnd ,
371
392
Type : condTypeBetween ,
372
393
Field : field ,
373
- Values : []interface {} {min , max },
394
+ Values : []any {min , max },
374
395
})
375
396
}
376
397
377
- func (builder * conditionBuilder ) OrWhereBetween (field string , min , max interface {} ) Condition {
398
+ func (builder * conditionBuilder ) OrWhereBetween (field string , min , max any ) Condition {
378
399
return builder .WhereCondition (sqlCondition {
379
400
Connector : connectTypeOr ,
380
401
Type : condTypeBetween ,
381
402
Field : field ,
382
- Values : []interface {} {min , max },
403
+ Values : []any {min , max },
383
404
})
384
405
}
385
406
386
- func (builder * conditionBuilder ) WhereNotBetween (field string , min , max interface {} ) Condition {
407
+ func (builder * conditionBuilder ) WhereNotBetween (field string , min , max any ) Condition {
387
408
return builder .WhereCondition (sqlCondition {
388
409
Connector : connectTypeAnd ,
389
410
Type : condTypeNotBetween ,
390
411
Field : field ,
391
- Values : []interface {} {min , max },
412
+ Values : []any {min , max },
392
413
})
393
414
}
394
415
395
- func (builder * conditionBuilder ) OrWhereNotBetween (field string , min , max interface {} ) Condition {
416
+ func (builder * conditionBuilder ) OrWhereNotBetween (field string , min , max any ) Condition {
396
417
return builder .WhereCondition (sqlCondition {
397
418
Connector : connectTypeOr ,
398
419
Type : condTypeNotBetween ,
399
420
Field : field ,
400
- Values : []interface {} {min , max },
421
+ Values : []any {min , max },
401
422
})
402
423
}
403
424
@@ -412,9 +433,9 @@ func (builder *conditionBuilder) WhereCondition(cond sqlCondition) Condition {
412
433
return builder
413
434
}
414
435
415
- func (builder * conditionBuilder ) Resolve (tableAlias string ) (string , []interface {} ) {
436
+ func (builder * conditionBuilder ) Resolve (tableAlias string ) (string , []any ) {
416
437
var result = ""
417
- var params = make ([]interface {} , 0 )
438
+ var params = make ([]any , 0 )
418
439
for i , c := range builder .conditions {
419
440
if ! c .When () {
420
441
continue
@@ -434,9 +455,9 @@ func (builder *conditionBuilder) Resolve(tableAlias string) (string, []interface
434
455
return result , params
435
456
}
436
457
437
- func (builder * conditionBuilder ) resolveCondition (tableAlias string , connector connectType , c sqlCondition ) (string , []interface {} ) {
458
+ func (builder * conditionBuilder ) resolveCondition (tableAlias string , connector connectType , c sqlCondition ) (string , []any ) {
438
459
result := ""
439
- params := make ([]interface {} , 0 )
460
+ params := make ([]any , 0 )
440
461
441
462
switch c .Type {
442
463
case condTypeSimple :
0 commit comments