Skip to content

Commit 0c6052a

Browse files
committed
WhereIn 系列函数支持 []T 类型的参数
1 parent 667b673 commit 0c6052a

File tree

3 files changed

+88
-49
lines changed

3 files changed

+88
-49
lines changed

_examples/main.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ func modelOperationExample(db *sql.DB) {
7575
misc.AssertError(user.As(&userView))
7676

7777
log.Infof("UserView name=%s, email=%s", userView.Name, userView.Email)
78-
7978
}
8079

8180
// only specified fields
@@ -108,6 +107,15 @@ func modelOperationExample(db *sql.DB) {
108107

109108
log.Infof("After force deleted count=%d/%d", c1, c2)
110109

110+
_, err = userModel.Get(context.TODO(), query.Builder().WhereIn("id", []int{1, 2, 3}))
111+
misc.AssertError(err)
112+
113+
_, err = userModel.Get(context.TODO(), query.Builder().WhereIn("id", 1, 2, 3, 4))
114+
misc.AssertError(err)
115+
116+
_, err = userModel.Get(context.TODO(), query.Builder().WhereIn("id", query.ToAnys([]int{1, 2, 3, 4, 5})...))
117+
misc.AssertError(err)
118+
111119
return nil
112120
})
113121

@@ -154,8 +162,8 @@ func databaseOperationExample(db *sql.DB) {
154162
)
155163
misc.AssertError(err)
156164

157-
res.Each(func(user models.UserN) {
158-
log.Infof("user_id=%d, name=%s, email=%s", user.Id.ValueOrZero(), user.Name.ValueOrZero(), user.Email.ValueOrZero())
165+
res.Each(func(user models.User) {
166+
log.Infof("user_id=%d, name=%s, email=%s", user.Id, user.Name, user.Email)
159167
})
160168

161169
res, err = eloquent.DB(tx).Query(context.TODO(), eloquent.Raw("select count(*) from wz_user"), func(row eloquent.Scanner) (interface{}, error) {

query/condition.go

+66-45
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package query
22

33
import (
44
"fmt"
5+
"reflect"
56
"strings"
67
)
78

@@ -16,20 +17,20 @@ type Condition interface {
1617
OrWhereNull(field string) Condition
1718
WhereNotNull(field string) Condition
1819
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
2526
WhereGroup(wc ConditionGroup) Condition
2627
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
3334

3435
WhereCondition(cond sqlCondition) Condition
3536

@@ -41,7 +42,7 @@ type Condition interface {
4142

4243
Clone() Condition
4344
Empty() bool
44-
Resolve(tableAlias string) (string, []interface{})
45+
Resolve(tableAlias string) (string, []any)
4546
}
4647

4748
type When func() bool
@@ -51,7 +52,7 @@ type conditionType int
5152

5253
const (
5354
connectTypeAnd connectType = "AND"
54-
connectTypeOr = "OR"
55+
connectTypeOr connectType = "OR"
5556
)
5657

5758
const (
@@ -70,15 +71,15 @@ const (
7071
)
7172

7273
type SubQuery interface {
73-
ResolveQuery() (string, []interface{})
74+
ResolveQuery() (string, []any)
7475
}
7576

7677
type sqlCondition struct {
7778
Connector connectType
7879
Type conditionType
7980
Field string
8081
Operate string
81-
Values []interface{}
82+
Values []any
8283
Nested ConditionGroup
8384
SubQuery SubQuery
8485
When When
@@ -153,7 +154,7 @@ func (builder *conditionBuilder) WhereColumn(field, operator string, value strin
153154
Type: condTypeColumn,
154155
Field: field,
155156
Operate: operator,
156-
Values: []interface{}{value},
157+
Values: []any{value},
157158
})
158159
}
159160

@@ -163,7 +164,7 @@ func (builder *conditionBuilder) OrWhereColumn(field, operator string, value str
163164
Type: condTypeColumn,
164165
Field: field,
165166
Operate: operator,
166-
Values: []interface{}{value},
167+
Values: []any{value},
167168
})
168169
}
169170

@@ -235,25 +236,25 @@ func (builder *conditionBuilder) WhereNull(field string) Condition {
235236
})
236237
}
237238

238-
func (builder *conditionBuilder) OrWhereRaw(raw string, items ...interface{}) Condition {
239+
func (builder *conditionBuilder) OrWhereRaw(raw string, items ...any) Condition {
239240
return builder.WhereCondition(sqlCondition{
240241
Connector: connectTypeOr,
241242
Type: condTypeRaw,
242243
Field: raw,
243-
Values: items,
244+
Values: arrayItems(items),
244245
})
245246
}
246247

247-
func (builder *conditionBuilder) WhereRaw(raw string, items ...interface{}) Condition {
248+
func (builder *conditionBuilder) WhereRaw(raw string, items ...any) Condition {
248249
return builder.WhereCondition(sqlCondition{
249250
Connector: connectTypeAnd,
250251
Type: condTypeRaw,
251252
Field: raw,
252-
Values: items,
253+
Values: arrayItems(items),
253254
})
254255
}
255256

256-
func (builder *conditionBuilder) OrWhereNotIn(field string, items ...interface{}) Condition {
257+
func (builder *conditionBuilder) OrWhereNotIn(field string, items ...any) Condition {
257258
return builder.WhereCondition(sqlCondition{
258259
Connector: connectTypeOr,
259260
Type: condTypeNotIn,
@@ -262,30 +263,50 @@ func (builder *conditionBuilder) OrWhereNotIn(field string, items ...interface{}
262263
})
263264
}
264265

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 {
266287
return builder.WhereCondition(sqlCondition{
267288
Connector: connectTypeOr,
268289
Type: condTypeIn,
269290
Field: field,
270-
Values: items,
291+
Values: arrayItems(items),
271292
})
272293
}
273294

274-
func (builder *conditionBuilder) WhereNotIn(field string, items ...interface{}) Condition {
295+
func (builder *conditionBuilder) WhereNotIn(field string, items ...any) Condition {
275296
return builder.WhereCondition(sqlCondition{
276297
Connector: connectTypeAnd,
277298
Type: condTypeNotIn,
278299
Field: field,
279-
Values: items,
300+
Values: arrayItems(items),
280301
})
281302
}
282303

283-
func (builder *conditionBuilder) WhereIn(field string, items ...interface{}) Condition {
304+
func (builder *conditionBuilder) WhereIn(field string, items ...any) Condition {
284305
return builder.WhereCondition(sqlCondition{
285306
Connector: connectTypeAnd,
286307
Type: condTypeIn,
287308
Field: field,
288-
Values: items,
309+
Values: arrayItems(items),
289310
})
290311
}
291312

@@ -305,11 +326,11 @@ func (builder *conditionBuilder) OrWhereGroup(wc ConditionGroup) Condition {
305326
})
306327
}
307328

308-
func (builder *conditionBuilder) Where(field string, value ...interface{}) Condition {
329+
func (builder *conditionBuilder) Where(field string, value ...any) Condition {
309330
argCount := len(value)
310331

311332
var operator string
312-
var values []interface{}
333+
var values []any
313334

314335
if argCount == 1 {
315336
operator = "="
@@ -335,11 +356,11 @@ func (builder *conditionBuilder) Where(field string, value ...interface{}) Condi
335356
})
336357
}
337358

338-
func (builder *conditionBuilder) OrWhere(field string, value ...interface{}) Condition {
359+
func (builder *conditionBuilder) OrWhere(field string, value ...any) Condition {
339360
argCount := len(value)
340361

341362
var operator string
342-
var values []interface{}
363+
var values []any
343364

344365
if argCount == 1 {
345366
operator = "="
@@ -365,39 +386,39 @@ func (builder *conditionBuilder) OrWhere(field string, value ...interface{}) Con
365386
})
366387
}
367388

368-
func (builder *conditionBuilder) WhereBetween(field string, min, max interface{}) Condition {
389+
func (builder *conditionBuilder) WhereBetween(field string, min, max any) Condition {
369390
return builder.WhereCondition(sqlCondition{
370391
Connector: connectTypeAnd,
371392
Type: condTypeBetween,
372393
Field: field,
373-
Values: []interface{}{min, max},
394+
Values: []any{min, max},
374395
})
375396
}
376397

377-
func (builder *conditionBuilder) OrWhereBetween(field string, min, max interface{}) Condition {
398+
func (builder *conditionBuilder) OrWhereBetween(field string, min, max any) Condition {
378399
return builder.WhereCondition(sqlCondition{
379400
Connector: connectTypeOr,
380401
Type: condTypeBetween,
381402
Field: field,
382-
Values: []interface{}{min, max},
403+
Values: []any{min, max},
383404
})
384405
}
385406

386-
func (builder *conditionBuilder) WhereNotBetween(field string, min, max interface{}) Condition {
407+
func (builder *conditionBuilder) WhereNotBetween(field string, min, max any) Condition {
387408
return builder.WhereCondition(sqlCondition{
388409
Connector: connectTypeAnd,
389410
Type: condTypeNotBetween,
390411
Field: field,
391-
Values: []interface{}{min, max},
412+
Values: []any{min, max},
392413
})
393414
}
394415

395-
func (builder *conditionBuilder) OrWhereNotBetween(field string, min, max interface{}) Condition {
416+
func (builder *conditionBuilder) OrWhereNotBetween(field string, min, max any) Condition {
396417
return builder.WhereCondition(sqlCondition{
397418
Connector: connectTypeOr,
398419
Type: condTypeNotBetween,
399420
Field: field,
400-
Values: []interface{}{min, max},
421+
Values: []any{min, max},
401422
})
402423
}
403424

@@ -412,9 +433,9 @@ func (builder *conditionBuilder) WhereCondition(cond sqlCondition) Condition {
412433
return builder
413434
}
414435

415-
func (builder *conditionBuilder) Resolve(tableAlias string) (string, []interface{}) {
436+
func (builder *conditionBuilder) Resolve(tableAlias string) (string, []any) {
416437
var result = ""
417-
var params = make([]interface{}, 0)
438+
var params = make([]any, 0)
418439
for i, c := range builder.conditions {
419440
if !c.When() {
420441
continue
@@ -434,9 +455,9 @@ func (builder *conditionBuilder) Resolve(tableAlias string) (string, []interface
434455
return result, params
435456
}
436457

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) {
438459
result := ""
439-
params := make([]interface{}, 0)
460+
params := make([]any, 0)
440461

441462
switch c.Type {
442463
case condTypeSimple:

query/misc.go

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,17 @@ type PaginateMeta struct {
2929
LastPage int64 `json:"last_page"`
3030
}
3131

32-
func isSubQuery(values []interface{}) bool {
32+
// ToAnys convert []T to []any ([]any)
33+
func ToAnys[T any](items []T) []any {
34+
arr := make([]any, len(items))
35+
for i, item := range items {
36+
arr[i] = item
37+
}
38+
39+
return arr
40+
}
41+
42+
func isSubQuery(values []any) bool {
3343
if len(values) != 1 {
3444
return false
3545
}

0 commit comments

Comments
 (0)