forked from go-gorm/gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
do.go
746 lines (625 loc) · 19.9 KB
/
do.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
package gen
import (
"context"
"database/sql"
"fmt"
"reflect"
"strings"
"gorm.io/gorm"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
"gorm.io/gen/field"
)
var (
createClauses = []string{"INSERT", "VALUES", "ON CONFLICT"}
queryClauses = []string{"SELECT", "FROM", "WHERE", "GROUP BY", "ORDER BY", "LIMIT", "FOR"}
updateClauses = []string{"UPDATE", "SET", "WHERE"}
deleteClauses = []string{"DELETE", "FROM", "WHERE"}
)
// resultInfo query/execute info
type resultInfo struct {
RowsAffected int64
Error error
}
// DO (data object): implement basic query methods
// the structure embedded with a *gorm.DB, and has a element item "alias" will be used when used as a sub query
type DO struct {
db *gorm.DB
alias string // for subquery
modelType reflect.Type
schema *schema.Schema
}
func (d DO) getInstance(db *gorm.DB) *DO {
d.db = db
return &d
}
type doOptions func(*gorm.DB) *gorm.DB
var (
// Debug use DB in debug mode
Debug doOptions = func(db *gorm.DB) *gorm.DB { return db.Debug() }
)
// UseDB specify a db connection(*gorm.DB)
func (d *DO) UseDB(db *gorm.DB, opts ...doOptions) {
db = db.Session(&gorm.Session{Context: context.Background()})
for _, opt := range opts {
db = opt(db)
}
d.db = db
}
func (d *DO) ReplaceDB(db *gorm.DB) { d.db = db }
// UseModel specify a data model structure as a source for table name
func (d *DO) UseModel(model interface{}) {
mt := reflect.TypeOf(model)
if mt.Kind() == reflect.Ptr {
mt = mt.Elem()
}
d.modelType = mt
err := d.db.Statement.Parse(model)
if err != nil {
panic(fmt.Errorf("Cannot parse model: %+v", model))
}
d.schema = d.db.Statement.Schema
}
// UseTable specify table name
func (d *DO) UseTable(tableName string) { d.db = d.db.Table(tableName).Session(new(gorm.Session)) }
// TableName return table name
func (d DO) TableName() string {
if d.schema == nil {
return ""
}
return d.schema.Table
}
// Session replace db with new session
func (d *DO) Session(config *gorm.Session) Dao { return d.getInstance(d.db.Session(config)) }
// UnderlyingDB return the underlying database connection
func (d *DO) UnderlyingDB() *gorm.DB { return d.db }
// Quote return qutoed data
func (d *DO) Quote(raw string) string { return d.db.Statement.Quote(raw) }
// Build implement the interface of claues.Expression
// only call WHERE clause's Build
func (d *DO) Build(builder clause.Builder) {
for _, e := range d.buildCondition() {
e.Build(builder)
}
}
func (d *DO) buildCondition() []clause.Expression {
return d.db.Statement.BuildCondition(d.db)
}
// underlyingDO return self
func (d *DO) underlyingDO() *DO { return d }
// underlyingDB return self.db
func (d *DO) underlyingDB() *gorm.DB { return d.db }
func (d *DO) withError(err error) *DO {
if err == nil {
return d
}
newDB := d.db.Session(new(gorm.Session))
_ = newDB.AddError(err)
return d.getInstance(newDB)
}
// implements Condition
func (d *DO) BeCond() interface{} { return d.buildCondition() }
func (d *DO) CondError() error { return nil }
// Debug return a DO with db in debug mode
func (d *DO) Debug() Dao { return d.getInstance(d.db.Debug()) }
// WithContext return a DO with db with context
func (d *DO) WithContext(ctx context.Context) Dao { return d.getInstance(d.db.WithContext(ctx)) }
// Clauses specify Clauses
func (d *DO) Clauses(conds ...clause.Expression) Dao {
if err := checkConds(conds); err != nil {
newDB := d.db.Session(new(gorm.Session))
_ = newDB.AddError(err)
return d.getInstance(newDB)
}
return d.getInstance(d.db.Clauses(conds...))
}
// As alias cannot be heired, As must used on tail
func (d *DO) As(alias string) Dao { return &DO{db: d.db, alias: alias} }
// Columns return columns for Subquery
func (*DO) Columns(cols ...field.Expr) columns { return cols }
// ======================== chainable api ========================
func (d *DO) Not(conds ...Condition) Dao {
if len(conds) == 0 {
return d
}
exprs, err := condToExpression(conds)
if err != nil {
return d.withError(err)
}
return d.getInstance(d.db.Clauses(clause.Where{Exprs: []clause.Expression{clause.Not(exprs...)}}))
}
func (d *DO) Or(conds ...Condition) Dao {
if len(conds) == 0 {
return d
}
exprs, err := condToExpression(conds)
if err != nil {
return d.withError(err)
}
return d.getInstance(d.db.Clauses(clause.Where{Exprs: []clause.Expression{clause.Or(clause.And(exprs...))}}))
}
func (d *DO) Select(columns ...field.Expr) Dao {
if len(columns) == 0 {
return d.getInstance(d.db.Clauses(clause.Select{}))
}
query, args := buildExpr(d.db.Statement, columns...)
return d.getInstance(d.db.Select(query, args...))
}
func (d *DO) Where(conds ...Condition) Dao {
if len(conds) == 0 {
return d
}
exprs, err := condToExpression(conds)
if err != nil {
return d.withError(err)
}
return d.getInstance(d.db.Clauses(clause.Where{Exprs: exprs}))
}
func (d *DO) Order(columns ...field.Expr) Dao {
// lazy build Columns
// if c, ok := d.db.Statement.Clauses[clause.OrderBy{}.Name()]; ok {
// if order, ok := c.Expression.(clause.OrderBy); ok {
// if expr, ok := order.Expression.(clause.CommaExpression); ok {
// expr.Exprs = append(expr.Exprs, toExpression(columns)...)
// return d.newInstance(d.db.Clauses(clause.OrderBy{Expression: expr}))
// }
// }
// }
// return d.newInstance(d.db.Clauses(clause.OrderBy{Expression: clause.CommaExpression{Exprs: toExpression(columns)}}))
if len(columns) == 0 {
return d
}
return d.getInstance(d.db.Order(d.calcOrderValue(columns...)))
}
func (d *DO) calcOrderValue(columns ...field.Expr) string {
// eager build Columns
orderArray := make([]string, len(columns))
for i, c := range columns {
orderArray[i] = c.Build(d.db.Statement).String()
}
return strings.Join(orderArray, ",")
}
func (d *DO) Distinct(columns ...field.Expr) Dao {
return d.getInstance(d.db.Distinct(toInterfaceSlice(toColumnFullName(d.db.Statement, columns...))...))
}
func (d *DO) Omit(columns ...field.Expr) Dao {
if len(columns) == 0 {
return d
}
return d.getInstance(d.db.Omit(getColumnName(columns...)...))
}
func (d *DO) Group(columns ...field.Expr) Dao {
if len(columns) == 0 {
return d
}
name := columns[0].BuildColumn(d.db.Statement, field.WithTable).String()
for _, col := range columns[1:] {
name += "," + col.BuildColumn(d.db.Statement, field.WithTable).String()
}
return d.getInstance(d.db.Group(name))
}
func (d *DO) Having(conds ...Condition) Dao {
if len(conds) == 0 {
return d
}
exprs, err := condToExpression(conds)
if err != nil {
return d.withError(err)
}
return d.getInstance(d.db.Clauses(clause.GroupBy{Having: exprs}))
}
func (d *DO) Limit(limit int) Dao {
return d.getInstance(d.db.Limit(limit))
}
func (d *DO) Offset(offset int) Dao {
return d.getInstance(d.db.Offset(offset))
}
func (d *DO) Scopes(funcs ...func(Dao) Dao) Dao {
fcs := make([]func(*gorm.DB) *gorm.DB, len(funcs))
for i, f := range funcs {
sf := f
fcs[i] = func(tx *gorm.DB) *gorm.DB { return sf(d.getInstance(tx)).(*DO).db }
}
return d.getInstance(d.db.Scopes(fcs...))
}
func (d *DO) Unscoped() Dao {
return d.getInstance(d.db.Unscoped())
}
func (d *DO) Join(table schema.Tabler, conds ...field.Expr) Dao {
return d.join(table, clause.InnerJoin, conds)
}
func (d *DO) LeftJoin(table schema.Tabler, conds ...field.Expr) Dao {
return d.join(table, clause.LeftJoin, conds)
}
func (d *DO) RightJoin(table schema.Tabler, conds ...field.Expr) Dao {
return d.join(table, clause.RightJoin, conds)
}
func (d *DO) join(table schema.Tabler, joinType clause.JoinType, conds []field.Expr) Dao {
if len(conds) == 0 {
return d.withError(ErrEmptyCondition)
}
from := getFromClause(d.db)
from.Joins = append(from.Joins, clause.Join{
Type: joinType,
Table: clause.Table{Name: table.TableName()},
ON: clause.Where{Exprs: toExpression(conds...)},
})
return d.getInstance(d.db.Clauses(from))
}
func (d *DO) Attrs(attrs ...field.AssignExpr) Dao {
if len(attrs) == 0 {
return d
}
return d.getInstance(d.db.Attrs(d.attrsValue(attrs)...))
}
func (d *DO) Assign(attrs ...field.AssignExpr) Dao {
if len(attrs) == 0 {
return d
}
return d.getInstance(d.db.Assign(d.attrsValue(attrs)...))
}
func (d *DO) attrsValue(attrs []field.AssignExpr) []interface{} {
values := make([]interface{}, 0, len(attrs))
for _, attr := range attrs {
if expr, ok := attr.AssignExpr().(clause.Eq); ok {
values = append(values, expr)
}
}
return values
}
func (d *DO) Joins(field field.RelationField) Dao {
return d.getInstance(d.db.Joins(field.Path()))
}
// func (d *DO) Preload(column field.RelationPath, subQuery ...SubQuery) Dao {
// if len(subQuery) > 0 {
// return d.getInstance(d.db.Preload(string(column.Path()), subQuery[0].underlyingDB()))
// }
// return d.getInstance(d.db.Preload(string(column.Path())))
// }
func (d *DO) Preload(field field.RelationField) Dao {
var args []interface{}
if conds := field.GetConds(); len(conds) > 0 {
args = append(args, toExpressionInterface(conds...)...)
}
if columns := field.GetOrderCol(); len(columns) > 0 {
args = append(args, func(db *gorm.DB) *gorm.DB {
return db.Order(d.calcOrderValue(columns...))
})
}
if clauses := field.GetClauses(); len(clauses) > 0 {
args = append(args, func(db *gorm.DB) *gorm.DB {
return db.Clauses(clauses...)
})
}
return d.getInstance(d.db.Preload(field.Path(), args...))
}
func getFromClause(db *gorm.DB) *clause.From {
if db == nil || db.Statement == nil {
return &clause.From{}
}
c, ok := db.Statement.Clauses[clause.From{}.Name()]
if !ok || c.Expression == nil {
return &clause.From{}
}
from, ok := c.Expression.(clause.From)
if !ok {
return &clause.From{}
}
return &from
}
// ======================== finisher api ========================
func (d *DO) Create(value interface{}) error {
return d.db.Create(value).Error
}
func (d *DO) CreateInBatches(value interface{}, batchSize int) error {
return d.db.CreateInBatches(value, batchSize).Error
}
func (d *DO) Save(value interface{}) error {
return d.db.Clauses(clause.OnConflict{UpdateAll: true}).Create(value).Error
}
func (d *DO) First() (result interface{}, err error) {
return d.singleQuery(d.db.First)
}
func (d *DO) Take() (result interface{}, err error) {
return d.singleQuery(d.db.Take)
}
func (d *DO) Last() (result interface{}, err error) {
return d.singleQuery(d.db.Last)
}
func (d *DO) singleQuery(query func(dest interface{}, conds ...interface{}) *gorm.DB) (result interface{}, err error) {
if d.modelType == nil {
return d.singleScan()
}
result = d.newResultPointer()
if err := query(result).Error; err != nil {
return nil, err
}
return result, nil
}
func (d *DO) singleScan() (result interface{}, err error) {
result = map[string]interface{}{}
err = d.db.Scan(result).Error
return
}
func (d *DO) Find() (results interface{}, err error) {
return d.multiQuery(d.db.Find)
}
func (d *DO) multiQuery(query func(dest interface{}, conds ...interface{}) *gorm.DB) (results interface{}, err error) {
if d.modelType == nil {
return d.findToMap()
}
resultsPtr := d.newResultSlicePointer()
err = query(resultsPtr).Error
return reflect.Indirect(reflect.ValueOf(resultsPtr)).Interface(), err
}
func (d *DO) findToMap() (interface{}, error) {
var results []map[string]interface{}
err := d.db.Find(&results).Error
return results, err
}
func (d *DO) FindInBatch(batchSize int, fc func(tx Dao, batch int) error) (result interface{}, err error) {
resultsPtr := d.newResultSlicePointer()
err = d.db.FindInBatches(resultsPtr, batchSize, func(tx *gorm.DB, batch int) error { return fc(d.getInstance(tx), batch) }).Error
return reflect.Indirect(reflect.ValueOf(resultsPtr)).Interface(), err
}
func (d *DO) FindInBatches(dest interface{}, batchSize int, fc func(tx Dao, batch int) error) error {
return d.db.FindInBatches(dest, batchSize, func(tx *gorm.DB, batch int) error { return fc(d.getInstance(tx), batch) }).Error
}
func (d *DO) FirstOrInit() (result interface{}, err error) {
return d.singleQuery(d.db.FirstOrInit)
}
func (d *DO) FirstOrCreate() (result interface{}, err error) {
return d.singleQuery(d.db.FirstOrCreate)
}
func (d *DO) Update(column field.Expr, value interface{}) (info resultInfo, err error) {
tx := d.db.Model(d.newResultPointer())
columnStr := column.BuildColumn(d.db.Statement, field.WithoutQuote).String()
var result *gorm.DB
switch value := value.(type) {
case field.AssignExpr:
result = tx.Update(columnStr, value.AssignExpr())
case subQuery:
result = tx.Update(columnStr, value.underlyingDB())
default:
result = tx.Update(columnStr, value)
}
return resultInfo{RowsAffected: result.RowsAffected, Error: result.Error}, result.Error
}
func (d *DO) UpdateSimple(columns ...field.AssignExpr) (info resultInfo, err error) {
if len(columns) == 0 {
return
}
dest, err := assignMap(d.db.Statement, columns)
if err != nil {
return resultInfo{Error: err}, err
}
result := d.db.Model(d.newResultPointer()).Updates(dest)
return resultInfo{RowsAffected: result.RowsAffected, Error: result.Error}, result.Error
}
func (d *DO) Updates(value interface{}) (info resultInfo, err error) {
result := d.db.Model(d.newResultPointer()).Updates(value)
return resultInfo{RowsAffected: result.RowsAffected, Error: result.Error}, result.Error
}
func (d *DO) UpdateColumn(column field.Expr, value interface{}) (info resultInfo, err error) {
tx := d.db.Model(d.newResultPointer())
columnStr := column.BuildColumn(d.db.Statement, field.WithoutQuote).String()
var result *gorm.DB
switch value := value.(type) {
case field.Expr:
result = tx.UpdateColumn(columnStr, value.RawExpr())
case subQuery:
result = d.db.UpdateColumn(columnStr, value.underlyingDB())
default:
result = d.db.UpdateColumn(columnStr, value)
}
return resultInfo{RowsAffected: result.RowsAffected, Error: result.Error}, result.Error
}
func (d *DO) UpdateColumnSimple(columns ...field.AssignExpr) (info resultInfo, err error) {
if len(columns) == 0 {
return
}
dest, err := assignMap(d.db.Statement, columns)
if err != nil {
return resultInfo{Error: err}, err
}
result := d.db.Model(d.newResultPointer()).UpdateColumns(dest)
return resultInfo{RowsAffected: result.RowsAffected, Error: result.Error}, result.Error
}
func (d *DO) UpdateColumns(value interface{}) (info resultInfo, err error) {
result := d.db.Model(d.newResultPointer()).UpdateColumns(value)
return resultInfo{RowsAffected: result.RowsAffected, Error: result.Error}, result.Error
}
func (d *DO) Delete() (info resultInfo, err error) {
result := d.db.Model(d.newResultPointer()).Delete(reflect.New(d.modelType).Interface())
return resultInfo{RowsAffected: result.RowsAffected, Error: result.Error}, result.Error
}
func (d *DO) Count() (count int64, err error) {
return count, d.db.Session(&gorm.Session{}).Model(d.newResultPointer()).Count(&count).Error
}
func (d *DO) Row() *sql.Row {
return d.db.Model(d.newResultPointer()).Row()
}
func (d *DO) Rows() (*sql.Rows, error) {
return d.db.Model(d.newResultPointer()).Rows()
}
func (d *DO) Scan(dest interface{}) error {
return d.db.Model(d.newResultPointer()).Scan(dest).Error
}
func (d *DO) Pluck(column field.Expr, dest interface{}) error {
return d.db.Model(d.newResultPointer()).Pluck(column.ColumnName().String(), dest).Error
}
func (d *DO) ScanRows(rows *sql.Rows, dest interface{}) error {
return d.db.Model(d.newResultPointer()).ScanRows(rows, dest)
}
func (d *DO) newResultPointer() interface{} {
return reflect.New(d.modelType).Interface()
}
func (d *DO) newResultSlicePointer() interface{} {
return reflect.New(reflect.SliceOf(reflect.PtrTo(d.modelType))).Interface()
}
func toColumnFullName(stmt *gorm.Statement, columns ...field.Expr) []string {
return buildColumn(stmt, columns, field.WithAll)
}
func getColumnName(columns ...field.Expr) (result []string) {
for _, c := range columns {
result = append(result, c.ColumnName().String())
}
return result
}
func buildColumn(stmt *gorm.Statement, cols []field.Expr, opts ...field.BuildOpt) []string {
results := make([]string, len(cols))
for i, c := range cols {
results[i] = c.BuildColumn(stmt, opts...).String()
}
return results
}
func buildExpr(stmt *gorm.Statement, exprs ...field.Expr) (query string, args []interface{}) {
for _, e := range exprs {
sql, vars := e.BuildWithArgs(stmt)
if query == "" {
query = sql.String()
} else {
query += "," + sql.String()
}
args = append(args, vars...)
}
return query, args
}
func toExpression(exprs ...field.Expr) []clause.Expression {
result := make([]clause.Expression, len(exprs))
for i, e := range exprs {
result[i] = singleExpr(e)
}
return result
}
func toExpressionInterface(exprs ...field.Expr) []interface{} {
result := make([]interface{}, len(exprs))
for i, e := range exprs {
result[i] = singleExpr(e)
}
return result
}
func singleExpr(e field.Expr) clause.Expression {
switch v := e.RawExpr().(type) {
case clause.Expression:
return v
case clause.Column:
return clause.NamedExpr{SQL: "?", Vars: []interface{}{v}}
default:
return clause.Expr{}
}
}
func toInterfaceSlice(value interface{}) []interface{} {
switch v := value.(type) {
case string:
return []interface{}{v}
case []string:
res := make([]interface{}, len(v))
for i, item := range v {
res[i] = item
}
return res
case []clause.Column:
res := make([]interface{}, len(v))
for i, item := range v {
res[i] = item
}
return res
default:
return nil
}
}
func assignMap(stmt *gorm.Statement, exprs []field.AssignExpr) (map[string]interface{}, error) {
dest := make(map[string]interface{}, len(exprs))
for _, expr := range exprs {
target := expr.BuildColumn(stmt, field.WithoutQuote).String()
switch e := expr.AssignExpr().(type) {
case clause.Expr:
dest[target] = e
case clause.Eq:
dest[target] = e.Value
}
}
return dest, nil
}
// ======================== New Table ========================
// Table return a new table produced by subquery,
// the return value has to be used as root node
//
// Table(u.Select(u.ID, u.Name).Where(u.Age.Gt(18))).Select()
// the above usage is equivalent to SQL statement:
// SELECT * FROM (SELECT `id`, `name` FROM `users_info` WHERE `age` > ?)"
func Table(subQueries ...subQuery) Dao {
if len(subQueries) == 0 {
return &DO{}
}
tablePlaceholder := make([]string, len(subQueries))
tableExprs := make([]interface{}, len(subQueries))
for i, query := range subQueries {
tablePlaceholder[i] = "(?)"
do := query.underlyingDO()
tableExprs[i] = do.db
if do.alias != "" {
tablePlaceholder[i] += " AS " + do.Quote(do.alias)
}
}
return &DO{
db: subQueries[0].underlyingDO().db.Session(&gorm.Session{NewDB: true}).
Table(strings.Join(tablePlaceholder, ", "), tableExprs...),
}
}
// ======================== sub query method ========================
type columns []field.Expr
// In accept query or value
func (cs columns) In(queryOrValue Condition) field.Expr {
if len(cs) == 0 {
return field.EmptyExpr()
}
switch query := queryOrValue.(type) {
case field.Value:
return field.ContainsValue(cs, query)
case subQuery:
return field.ContainsSubQuery(cs, query.underlyingDB())
default:
return field.EmptyExpr()
}
}
func (cs columns) NotIn(queryOrValue Condition) field.Expr {
return field.Not(cs.In(queryOrValue))
}
func (cs columns) Eq(query subQuery) field.Expr {
if len(cs) == 0 {
return field.EmptyExpr()
}
return field.CompareSubQuery(field.EqOp, cs[0], query.underlyingDB())
}
func (cs columns) Neq(query subQuery) field.Expr {
if len(cs) == 0 {
return field.EmptyExpr()
}
return field.CompareSubQuery(field.NeqOp, cs[0], query.underlyingDB())
}
func (cs columns) Gt(query subQuery) field.Expr {
if len(cs) == 0 {
return field.EmptyExpr()
}
return field.CompareSubQuery(field.GtOp, cs[0], query.underlyingDB())
}
func (cs columns) Gte(query subQuery) field.Expr {
if len(cs) == 0 {
return field.EmptyExpr()
}
return field.CompareSubQuery(field.GteOp, cs[0], query.underlyingDB())
}
func (cs columns) Lt(query subQuery) field.Expr {
if len(cs) == 0 {
return field.EmptyExpr()
}
return field.CompareSubQuery(field.LtOp, cs[0], query.underlyingDB())
}
func (cs columns) Lte(query subQuery) field.Expr {
if len(cs) == 0 {
return field.EmptyExpr()
}
return field.CompareSubQuery(field.LteOp, cs[0], query.underlyingDB())
}