-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfields.go
1533 lines (1252 loc) · 48.8 KB
/
fields.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
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package sq
import (
"bytes"
"context"
"database/sql"
"database/sql/driver"
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
// Identifier represents an SQL identifier. If necessary, it will be quoted
// according to the dialect.
type Identifier string
var _ Field = (*Identifier)(nil)
// WriteSQL implements the SQLWriter interface.
func (id Identifier) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
buf.WriteString(QuoteIdentifier(dialect, string(id)))
return nil
}
// IsField implements the Field interface.
func (id Identifier) IsField() {}
// AnyField is a catch-all field type that satisfies the Any interface.
type AnyField struct {
table TableStruct
name string
alias string
desc sql.NullBool
nullsfirst sql.NullBool
}
var _ interface {
Field
Any
WithPrefix(string) Field
} = (*AnyField)(nil)
// NewAnyField returns a new AnyField.
func NewAnyField(name string, tbl TableStruct) AnyField {
return AnyField{table: tbl, name: name}
}
// WriteSQL implements the SQLWriter interface.
func (field AnyField) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
writeFieldIdentifier(ctx, dialect, buf, args, params, field.table, field.name)
writeFieldOrder(ctx, dialect, buf, args, params, field.desc, field.nullsfirst)
return nil
}
// As returns a new AnyField with the given alias.
func (field AnyField) As(alias string) AnyField {
field.alias = alias
return field
}
// Asc returns a new AnyField indicating that it should be ordered in ascending
// order i.e. 'ORDER BY field ASC'.
func (field AnyField) Asc() AnyField {
field.desc.Valid = true
field.desc.Bool = false
return field
}
// Desc returns a new AnyField indicating that it should be ordered in descending
// order i.e. 'ORDER BY field DESC'.
func (field AnyField) Desc() AnyField {
field.desc.Valid = true
field.desc.Bool = true
return field
}
// NullsLast returns a new NumberField indicating that it should be ordered
// with nulls last i.e. 'ORDER BY field NULLS LAST'.
func (field AnyField) NullsLast() AnyField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = false
return field
}
// NullsFirst returns a new NumberField indicating that it should be ordered
// with nulls first i.e. 'ORDER BY field NULLS FIRST'.
func (field AnyField) NullsFirst() AnyField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = true
return field
}
// WithPrefix returns a new Field that with the given prefix.
func (field AnyField) WithPrefix(prefix string) Field {
field.table.alias = ""
field.table.name = prefix
return field
}
// IsNull returns a 'field IS NULL' Predicate.
func (field AnyField) IsNull() Predicate { return Expr("{} IS NULL", field) }
// IsNotNull returns a 'field IS NOT NULL' Predicate.
func (field AnyField) IsNotNull() Predicate { return Expr("{} IS NOT NULL", field) }
// In returns a 'field IN (value)' Predicate. The value can be a slice, which
// corresponds to the expression 'field IN (x, y, z)'.
func (field AnyField) In(value any) Predicate { return In(field, value) }
// In returns a 'field NOT IN (value)' Predicate. The value can be a slice, which
// corresponds to the expression 'field NOT IN (x, y, z)'.
func (field AnyField) NotIn(value any) Predicate { return NotIn(field, value) }
// Eq returns a 'field = value' Predicate.
func (field AnyField) Eq(value any) Predicate { return Eq(field, value) }
// Ne returns a 'field <> value' Predicate.
func (field AnyField) Ne(value any) Predicate { return Ne(field, value) }
// Lt returns a 'field < value' Predicate.
func (field AnyField) Lt(value any) Predicate { return Lt(field, value) }
// Le returns a 'field <= value' Predicate.
func (field AnyField) Le(value any) Predicate { return Le(field, value) }
// Gt returns a 'field > value' Predicate.
func (field AnyField) Gt(value any) Predicate { return Gt(field, value) }
// Ge returns a 'field >= value' Predicate.
func (field AnyField) Ge(value any) Predicate { return Ge(field, value) }
// Expr returns an expression where the field is prepended to the front of the
// expression.
func (field AnyField) Expr(format string, values ...any) Expression {
values = append(values, field)
ordinal := len(values)
return Expr("{"+strconv.Itoa(ordinal)+"} "+format, values...)
}
// Set returns an Assignment assigning the value to the field.
func (field AnyField) Set(value any) Assignment {
return Set(field, value)
}
// Setf returns an Assignment assigning an expression to the field.
func (field AnyField) Setf(format string, values ...any) Assignment {
return Setf(field, format, values...)
}
// GetAlias returns the alias of the AnyField.
func (field AnyField) GetAlias() string { return field.alias }
// IsField implements the Field interface.
func (field AnyField) IsField() {}
// IsArray implements the Array interface.
func (field AnyField) IsArray() {}
// IsBinary implements the Binary interface.
func (field AnyField) IsBinary() {}
// IsBoolean implements the Boolean interface.
func (field AnyField) IsBoolean() {}
// IsEnum implements the Enum interface.
func (field AnyField) IsEnum() {}
// IsJSON implements the JSONValue interface.
func (field AnyField) IsJSON() {}
// IsNumber implements the Number interface.
func (field AnyField) IsNumber() {}
// IsString implements the String interface.
func (field AnyField) IsString() {}
// IsTime implements the Time interface.
func (field AnyField) IsTime() {}
// IsUUIDType implements the UUID interface.
func (field AnyField) IsUUID() {}
// ArrayField represents an SQL array field.
type ArrayField struct {
table TableStruct
name string
alias string
}
var _ interface {
Field
Array
WithPrefix(string) Field
} = (*ArrayField)(nil)
// NewArrayField returns a new ArrayField.
func NewArrayField(fieldName string, tableName TableStruct) ArrayField {
return ArrayField{table: tableName, name: fieldName}
}
// WriteSQL implements the SQLWriter interface.
func (field ArrayField) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
writeFieldIdentifier(ctx, dialect, buf, args, params, field.table, field.name)
return nil
}
// As returns a new ArrayField with the given alias.
func (field ArrayField) As(alias string) ArrayField {
field.alias = alias
return field
}
// WithPrefix returns a new Field that with the given prefix.
func (field ArrayField) WithPrefix(prefix string) Field {
field.table.alias = ""
field.table.name = prefix
return field
}
// IsNull returns a 'field IS NULL' Predicate.
func (field ArrayField) IsNull() Predicate { return Expr("{} IS NULL", field) }
// IsNull returns a 'field IS NOT NULL' Predicate.
func (field ArrayField) IsNotNull() Predicate { return Expr("{} IS NOT NULL", field) }
// Set returns an Assignment assigning the value to the field.
func (field ArrayField) Set(value any) Assignment {
switch value.(type) {
case SQLWriter:
return Set(field, value)
case []string, []int, []int64, []int32, []float64, []float32, []bool:
return Set(field, ArrayValue(value))
}
return Set(field, value)
}
// SetArray returns an Assignment assigning the value to the field. It wraps
// the value with ArrayValue().
func (field ArrayField) SetArray(value any) Assignment {
return Set(field, ArrayValue(value))
}
// Setf returns an Assignment assigning an expression to the field.
func (field ArrayField) Setf(format string, values ...any) Assignment {
return Set(field, Expr(format, values...))
}
// GetAlias returns the alias of the ArrayField.
func (field ArrayField) GetAlias() string { return field.alias }
// IsField implements the Field interface.
func (field ArrayField) IsField() {}
// IsArray implements the Array interface.
func (field ArrayField) IsArray() {}
// BinaryField represents an SQL binary field.
type BinaryField struct {
table TableStruct
name string
alias string
desc sql.NullBool
nullsfirst sql.NullBool
}
var _ interface {
Field
Binary
WithPrefix(string) Field
} = (*BinaryField)(nil)
// NewBinaryField returns a new BinaryField.
func NewBinaryField(fieldName string, tableName TableStruct) BinaryField {
return BinaryField{table: tableName, name: fieldName}
}
// WriteSQL implements the SQLWriter interface.
func (field BinaryField) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
writeFieldIdentifier(ctx, dialect, buf, args, params, field.table, field.name)
writeFieldOrder(ctx, dialect, buf, args, params, field.desc, field.nullsfirst)
return nil
}
// As returns a new BinaryField with the given alias.
func (field BinaryField) As(alias string) BinaryField {
field.alias = alias
return field
}
// Asc returns a new BinaryField indicating that it should be ordered in ascending
// order i.e. 'ORDER BY field ASC'.
func (field BinaryField) Asc() BinaryField {
field.desc.Valid = true
field.desc.Bool = false
return field
}
// Desc returns a new BinaryField indicating that it should be ordered in ascending
// order i.e. 'ORDER BY field DESC'.
func (field BinaryField) Desc() BinaryField {
field.desc.Valid = true
field.desc.Bool = true
return field
}
// NullsLast returns a new BinaryField indicating that it should be ordered
// with nulls last i.e. 'ORDER BY field NULLS LAST'.
func (field BinaryField) NullsLast() BinaryField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = false
return field
}
// NullsFirst returns a new BinaryField indicating that it should be ordered
// with nulls first i.e. 'ORDER BY field NULLS FIRST'.
func (field BinaryField) NullsFirst() BinaryField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = true
return field
}
// WithPrefix returns a new Field that with the given prefix.
func (field BinaryField) WithPrefix(prefix string) Field {
field.table.alias = ""
field.table.name = prefix
return field
}
// IsNull returns a 'field IS NULL' Predicate.
func (field BinaryField) IsNull() Predicate { return Expr("{} IS NULL", field) }
// IsNotNull returns a 'field IS NOT NULL' Predicate.
func (field BinaryField) IsNotNull() Predicate { return Expr("{} IS NOT NULL", field) }
// Eq returns a 'field = value' Predicate.
func (field BinaryField) Eq(value Binary) Predicate { return Eq(field, value) }
// Ne returns a 'field <> value' Predicate.
func (field BinaryField) Ne(value Binary) Predicate { return Ne(field, value) }
// EqBytes returns a 'field = b' Predicate.
func (field BinaryField) EqBytes(b []byte) Predicate { return Eq(field, b) }
// NeBytes returns a 'field <> b' Predicate.
func (field BinaryField) NeBytes(b []byte) Predicate { return Ne(field, b) }
// Set returns an Assignment assigning the value to the field.
func (field BinaryField) Set(value any) Assignment {
return Set(field, value)
}
// Setf returns an Assignment assigning an expression to the field.
func (field BinaryField) Setf(format string, values ...any) Assignment {
return Setf(field, format, values...)
}
// SetBytes returns an Assignment assigning a []byte to the field.
func (field BinaryField) SetBytes(b []byte) Assignment { return Set(field, b) }
// GetAlias returns the alias of the BinaryField.
func (field BinaryField) GetAlias() string { return field.alias }
// IsField implements the Field interface.
func (field BinaryField) IsField() {}
// IsBinary implements the Binary interface.
func (field BinaryField) IsBinary() {}
// BooleanField represents an SQL boolean field.
type BooleanField struct {
table TableStruct
name string
alias string
desc sql.NullBool
nullsfirst sql.NullBool
}
var _ interface {
Field
Boolean
Predicate
WithPrefix(string) Field
} = (*BooleanField)(nil)
// NewBooleanField returns a new BooleanField.
func NewBooleanField(fieldName string, tableName TableStruct) BooleanField {
return BooleanField{table: tableName, name: fieldName}
}
// WriteSQL implements the SQLWriter interface.
func (field BooleanField) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
writeFieldIdentifier(ctx, dialect, buf, args, params, field.table, field.name)
writeFieldOrder(ctx, dialect, buf, args, params, field.desc, field.nullsfirst)
return nil
}
// As returns a new BooleanField with the given alias.
func (field BooleanField) As(alias string) BooleanField {
field.alias = alias
return field
}
// Asc returns a new BooleanField indicating that it should be ordered in ascending
// order i.e. 'ORDER BY field ASC'.
func (field BooleanField) Asc() BooleanField {
field.desc.Valid = true
field.desc.Bool = false
return field
}
// Desc returns a new BooleanField indicating that it should be ordered in
// descending order i.e. 'ORDER BY field DESC'.
func (f BooleanField) Desc() BooleanField {
f.desc.Valid = true
f.desc.Bool = true
return f
}
// NullsLast returns a new BooleanField indicating that it should be ordered
// with nulls last i.e. 'ORDER BY field NULLS LAST'.
func (field BooleanField) NullsLast() BooleanField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = false
return field
}
// NullsFirst returns a new BooleanField indicating that it should be ordered
// with nulls first i.e. 'ORDER BY field NULLS FIRST'.
func (field BooleanField) NullsFirst() BooleanField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = true
return field
}
// WithPrefix returns a new Field that with the given prefix.
func (field BooleanField) WithPrefix(prefix string) Field {
field.table.alias = ""
field.table.name = prefix
return field
}
// IsNull returns a 'field IS NULL' Predicate.
func (field BooleanField) IsNull() Predicate { return Expr("{} IS NULL", field) }
// IsNotNull returns a 'field IS NOT NULL' Predicate.
func (field BooleanField) IsNotNull() Predicate { return Expr("{} IS NOT NULL", field) }
// Eq returns a 'field = value' Predicate.
func (field BooleanField) Eq(value Boolean) Predicate { return Eq(field, value) }
// Ne returns a 'field <> value' Predicate.
func (field BooleanField) Ne(value Boolean) Predicate { return Ne(field, value) }
// EqBool returns a 'field = b' Predicate.
func (field BooleanField) EqBool(b bool) Predicate { return Eq(field, b) }
// NeBool returns a 'field <> b' Predicate.
func (field BooleanField) NeBool(b bool) Predicate { return Ne(field, b) }
// Set returns an Assignment assigning the value to the field.
func (field BooleanField) Set(value any) Assignment {
return Set(field, value)
}
// Setf returns an Assignment assigning an expression to the field.
func (field BooleanField) Setf(format string, values ...any) Assignment {
return Setf(field, format, values...)
}
// SetBool returns an Assignment assigning a bool to the field i.e. 'field =
// b'.
func (field BooleanField) SetBool(b bool) Assignment { return Set(field, b) }
// GetAlias returns the alias of the BooleanField.
func (field BooleanField) GetAlias() string { return field.alias }
// IsField implements the Field interface.
func (field BooleanField) IsField() {}
// IsBoolean implements the Boolean interface.
func (field BooleanField) IsBoolean() {}
// EnumField represents an SQL enum field.
type EnumField struct {
table TableStruct
name string
alias string
}
var _ interface {
Field
Enum
WithPrefix(string) Field
} = (*EnumField)(nil)
// NewEnumField returns a new EnumField.
func NewEnumField(name string, tbl TableStruct) EnumField {
return EnumField{table: tbl, name: name}
}
// WriteSQL implements the SQLWriter interface.
func (field EnumField) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
writeFieldIdentifier(ctx, dialect, buf, args, params, field.table, field.name)
return nil
}
// As returns a new EnumField with the given alias.
func (field EnumField) As(alias string) EnumField {
field.alias = alias
return field
}
// WithPrefix returns a new Field that with the given prefix.
func (field EnumField) WithPrefix(prefix string) Field {
field.table.alias = ""
field.table.name = prefix
return field
}
// IsNull returns a 'field IS NULL' Predicate.
func (field EnumField) IsNull() Predicate { return Expr("{} IS NULL", field) }
// IsNotNull returns a 'field IS NOT NULL' Predicate.
func (field EnumField) IsNotNull() Predicate { return Expr("{} IS NOT NULL", field) }
// In returns a 'field IN (value)' Predicate. The value can be a slice, which
// corresponds to the expression 'field IN (x, y, z)'.
func (field EnumField) In(value any) Predicate { return In(field, value) }
// NotIn returns a 'field NOT IN (value)' Predicate. The value can be a slice, which
// corresponds to the expression 'field NOT IN (x, y, z)'.
func (field EnumField) NotIn(value any) Predicate { return NotIn(field, value) }
// Eq returns a 'field = value' Predicate.
func (field EnumField) Eq(value any) Predicate { return Eq(field, value) }
// Ne returns a 'field <> value' Predicate.
func (field EnumField) Ne(value any) Predicate { return Ne(field, value) }
// EqEnum returns a 'field = value' Predicate. It wraps the value with
// EnumValue().
func (field EnumField) EqEnum(value Enumeration) Predicate { return Eq(field, EnumValue(value)) }
// NeEnum returns a 'field <> value' Predicate. it wraps the value with
// EnumValue().
func (field EnumField) NeEnum(value Enumeration) Predicate { return Ne(field, EnumValue(value)) }
// Set returns an Assignment assigning the value to the field.
func (field EnumField) Set(value any) Assignment {
return Set(field, value)
}
// SetEnum returns an Assignment assigning the value to the field. It wraps the
// value with EnumValue().
func (field EnumField) SetEnum(value Enumeration) Assignment {
return Set(field, EnumValue(value))
}
// Setf returns an Assignment assigning an expression to the field.
func (field EnumField) Setf(format string, values ...any) Assignment {
return Setf(field, format, values...)
}
// GetAlias returns the alias of the EnumField.
func (field EnumField) GetAlias() string { return field.alias }
// IsField implements the Field interface.
func (field EnumField) IsField() {}
// IsEnum implements the Enum interface.
func (field EnumField) IsEnum() {}
// JSONField represents an SQL JSON field.
type JSONField struct {
table TableStruct
name string
alias string
}
var _ interface {
Field
Binary
JSON
String
WithPrefix(string) Field
} = (*JSONField)(nil)
// NewJSONField returns a new JSONField.
func NewJSONField(name string, tbl TableStruct) JSONField {
return JSONField{table: tbl, name: name}
}
// WriteSQL implements the SQLWriter interface.
func (field JSONField) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
writeFieldIdentifier(ctx, dialect, buf, args, params, field.table, field.name)
return nil
}
// As returns a new JSONField with the given alias.
func (field JSONField) As(alias string) JSONField {
field.alias = alias
return field
}
// WithPrefix returns a new Field that with the given prefix.
func (field JSONField) WithPrefix(prefix string) Field {
field.table.alias = ""
field.table.name = prefix
return field
}
// IsNull returns a 'field IS NULL' Predicate.
func (field JSONField) IsNull() Predicate { return Expr("{} IS NULL", field) }
// IsNotNull returns a 'field IS NOT NULL' Predicate.
func (field JSONField) IsNotNull() Predicate { return Expr("{} IS NOT NULL", field) }
// Set returns an Assignment assigning the value to the field.
func (field JSONField) Set(value any) Assignment {
switch value.(type) {
case []byte, driver.Valuer, SQLWriter:
return Set(field, value)
}
switch reflect.TypeOf(value).Kind() {
case reflect.Map, reflect.Struct, reflect.Slice, reflect.Array:
return Set(field, JSONValue(value))
}
return Set(field, value)
}
// SetJSON returns an Assignment assigning the value to the field. It wraps the
// value in JSONValue().
func (field JSONField) SetJSON(value any) Assignment {
return Set(field, JSONValue(value))
}
// Setf returns an Assignment assigning an expression to the field.
func (field JSONField) Setf(format string, values ...any) Assignment {
return Setf(field, format, values...)
}
// GetAlias returns the alias of the JSONField.
func (field JSONField) GetAlias() string { return field.alias }
// IsField implements the Field interface.
func (field JSONField) IsField() {}
// IsBinary implements the Binary interface.
func (field JSONField) IsBinary() {}
// IsJSON implements the JSON interface.
func (field JSONField) IsJSON() {}
// IsString implements the String interface.
func (field JSONField) IsString() {}
// NumberField represents an SQL number field.
type NumberField struct {
table TableStruct
name string
alias string
desc sql.NullBool
nullsfirst sql.NullBool
}
var _ interface {
Field
Number
WithPrefix(string) Field
} = (*NumberField)(nil)
// NewNumberField returns a new NumberField.
func NewNumberField(name string, tbl TableStruct) NumberField {
return NumberField{table: tbl, name: name}
}
// WriteSQL implements the SQLWriter interface.
func (field NumberField) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
writeFieldIdentifier(ctx, dialect, buf, args, params, field.table, field.name)
writeFieldOrder(ctx, dialect, buf, args, params, field.desc, field.nullsfirst)
return nil
}
// As returns a new NumberField with the given alias.
func (field NumberField) As(alias string) NumberField {
field.alias = alias
return field
}
// Asc returns a new NumberField indicating that it should be ordered in ascending
// order i.e. 'ORDER BY field ASC'.
func (field NumberField) Asc() NumberField {
field.desc.Valid = true
field.desc.Bool = false
return field
}
// Desc returns a new NumberField indicating that it should be ordered in ascending
// order i.e. 'ORDER BY field DESC'.
func (field NumberField) Desc() NumberField {
field.desc.Valid = true
field.desc.Bool = true
return field
}
// NullsLast returns a new NumberField indicating that it should be ordered
// with nulls last i.e. 'ORDER BY field NULLS LAST'.
func (field NumberField) NullsLast() NumberField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = false
return field
}
// NullsFirst returns a new NumberField indicating that it should be ordered
// with nulls first i.e. 'ORDER BY field NULLS FIRST'.
func (field NumberField) NullsFirst() NumberField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = true
return field
}
// WithPrefix returns a new Field that with the given prefix.
func (field NumberField) WithPrefix(prefix string) Field {
field.table.alias = ""
field.table.name = prefix
return field
}
// IsNull returns a 'field IS NULL' Predicate.
func (field NumberField) IsNull() Predicate { return Expr("{} IS NULL", field) }
// IsNotNull returns a 'field IS NOT NULL' Predicate.
func (field NumberField) IsNotNull() Predicate { return Expr("{} IS NOT NULL", field) }
// In returns a 'field IN (value)' Predicate. The value can be a slice, which
// corresponds to the expression 'field IN (x, y, z)'.
func (field NumberField) In(value any) Predicate { return In(field, value) }
// NotIn returns a 'field NOT IN (value)' Predicate. The value can be a slice,
// which corresponds to the expression 'field IN (x, y, z)'.
func (field NumberField) NotIn(value any) Predicate { return NotIn(field, value) }
// Eq returns a 'field = value' Predicate.
func (field NumberField) Eq(value Number) Predicate { return Eq(field, value) }
// Ne returns a 'field <> value' Predicate.
func (field NumberField) Ne(value Number) Predicate { return Ne(field, value) }
// Lt returns a 'field < value' Predicate.
func (field NumberField) Lt(value Number) Predicate { return Lt(field, value) }
// Le returns a 'field <= value' Predicate.
func (field NumberField) Le(value Number) Predicate { return Le(field, value) }
// Gt returns a 'field > value' Predicate.
func (field NumberField) Gt(value Number) Predicate { return Gt(field, value) }
// Ge returns a 'field >= value' Predicate.
func (field NumberField) Ge(value Number) Predicate { return Ge(field, value) }
// EqInt returns a 'field = num' Predicate.
func (field NumberField) EqInt(num int) Predicate { return Eq(field, num) }
// NeInt returns a 'field <> num' Predicate.
func (field NumberField) NeInt(num int) Predicate { return Ne(field, num) }
// LtInt returns a 'field < num' Predicate.
func (field NumberField) LtInt(num int) Predicate { return Lt(field, num) }
// LeInt returns a 'field <= num' Predicate.
func (field NumberField) LeInt(num int) Predicate { return Le(field, num) }
// GtInt returns a 'field > num' Predicate.
func (field NumberField) GtInt(num int) Predicate { return Gt(field, num) }
// GeInt returns a 'field >= num' Predicate.
func (field NumberField) GeInt(num int) Predicate { return Ge(field, num) }
// EqInt64 returns a 'field = num' Predicate.
func (field NumberField) EqInt64(num int64) Predicate { return Eq(field, num) }
// NeInt64 returns a 'field <> num' Predicate.
func (field NumberField) NeInt64(num int64) Predicate { return Ne(field, num) }
// LtInt64 returns a 'field < num' Predicate.
func (field NumberField) LtInt64(num int64) Predicate { return Lt(field, num) }
// LeInt64 returns a 'field <= num' Predicate.
func (field NumberField) LeInt64(num int64) Predicate { return Le(field, num) }
// GtInt64 returns a 'field > num' Predicate.
func (field NumberField) GtInt64(num int64) Predicate { return Gt(field, num) }
// GeInt64 returns a 'field >= num' Predicate.
func (field NumberField) GeInt64(num int64) Predicate { return Ge(field, num) }
// EqFloat64 returns a 'field = num' Predicate.
func (field NumberField) EqFloat64(num float64) Predicate { return Eq(field, num) }
// NeFloat64 returns a 'field <> num' Predicate.
func (field NumberField) NeFloat64(num float64) Predicate { return Ne(field, num) }
// LtFloat64 returns a 'field < num' Predicate.
func (field NumberField) LtFloat64(num float64) Predicate { return Lt(field, num) }
// LeFloat64 returns a 'field <= num' Predicate.
func (field NumberField) LeFloat64(num float64) Predicate { return Le(field, num) }
// GtFloat64 returns a 'field > num' Predicate.
func (field NumberField) GtFloat64(num float64) Predicate { return Gt(field, num) }
// GeFloat64 returns a 'field >= num' Predicate.
func (field NumberField) GeFloat64(num float64) Predicate { return Ge(field, num) }
// Set returns an Assignment assigning the value to the field.
func (field NumberField) Set(value any) Assignment {
return Set(field, value)
}
// Setf returns an Assignment assigning an expression to the field.
func (field NumberField) Setf(format string, values ...any) Assignment {
return Setf(field, format, values...)
}
// SetBytes returns an Assignment assigning an int to the field.
func (field NumberField) SetInt(num int) Assignment { return Set(field, num) }
// SetBytes returns an Assignment assigning an int64 to the field.
func (field NumberField) SetInt64(num int64) Assignment { return Set(field, num) }
// SetBytes returns an Assignment assigning an float64 to the field.
func (field NumberField) SetFloat64(num float64) Assignment { return Set(field, num) }
// GetAlias returns the alias of the NumberField.
func (field NumberField) GetAlias() string { return field.alias }
// IsField implements the Field interface.
func (field NumberField) IsField() {}
// IsNumber implements the Number interface.
func (field NumberField) IsNumber() {}
// StringField represents an SQL string field.
type StringField struct {
table TableStruct
name string
alias string
collation string
desc sql.NullBool
nullsfirst sql.NullBool
}
var _ interface {
Field
String
WithPrefix(string) Field
} = (*StringField)(nil)
// NewStringField returns a new StringField.
func NewStringField(name string, tbl TableStruct) StringField {
return StringField{table: tbl, name: name}
}
// WriteSQL implements the SQLWriter interface.
func (field StringField) WriteSQL(ctx context.Context, dialect string, buf *bytes.Buffer, args *[]any, params map[string][]int) error {
writeFieldIdentifier(ctx, dialect, buf, args, params, field.table, field.name)
if field.collation != "" {
buf.WriteString(" COLLATE ")
if dialect == DialectPostgres {
buf.WriteString(`"` + EscapeQuote(field.collation, '"') + `"`)
} else {
buf.WriteString(QuoteIdentifier(dialect, field.collation))
}
}
writeFieldOrder(ctx, dialect, buf, args, params, field.desc, field.nullsfirst)
return nil
}
// As returns a new StringField with the given alias.
func (field StringField) As(alias string) StringField {
field.alias = alias
return field
}
// Collate returns a new StringField using the given collation.
func (field StringField) Collate(collation string) StringField {
field.collation = collation
return field
}
// Asc returns a new StringField indicating that it should be ordered in
// ascending order i.e. 'ORDER BY field ASC'.
func (field StringField) Asc() StringField {
field.desc.Valid = true
field.desc.Bool = false
return field
}
// Desc returns a new StringField indicating that it should be ordered in
// descending order i.e. 'ORDER BY field DESC'.
func (field StringField) Desc() StringField {
field.desc.Valid = true
field.desc.Bool = true
return field
}
// NullsLast returns a new StringField indicating that it should be ordered
// with nulls last i.e. 'ORDER BY field NULLS LAST'.
func (field StringField) NullsLast() StringField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = false
return field
}
// NullsFirst returns a new StringField indicating that it should be ordered
// with nulls first i.e. 'ORDER BY field NULLS FIRST'.
func (field StringField) NullsFirst() StringField {
field.nullsfirst.Valid = true
field.nullsfirst.Bool = true
return field
}
// WithPrefix returns a new Field that with the given prefix.
func (field StringField) WithPrefix(prefix string) Field {
field.table.alias = ""
field.table.name = prefix
return field
}
// IsNull returns a 'field IS NULL' Predicate.
func (field StringField) IsNull() Predicate { return Expr("{} IS NULL", field) }
// IsNotNull returns a 'field IS NOT NULL' Predicate.
func (field StringField) IsNotNull() Predicate { return Expr("{} IS NOT NULL", field) }
// In returns a 'field IN (value)' Predicate. The value can be a slice, which
// corresponds to the expression 'field IN (x, y, z)'.
func (field StringField) In(value any) Predicate { return In(field, value) }
// In returns a 'field NOT IN (value)' Predicate. The value can be a slice,
// which corresponds to the expression 'field NOT IN (x, y, z)'.
func (field StringField) NotIn(value any) Predicate { return NotIn(field, value) }
// Eq returns a 'field = value' Predicate.
func (field StringField) Eq(value String) Predicate { return Eq(field, value) }
// Ne returns a 'field <> value' Predicate.
func (field StringField) Ne(value String) Predicate { return Ne(field, value) }
// Lt returns a 'field < value' Predicate.
func (field StringField) Lt(value String) Predicate { return Lt(field, value) }
// Le returns a 'field <= value' Predicate.
func (field StringField) Le(value String) Predicate { return Le(field, value) }
// Gt returns a 'field > value' Predicate.
func (field StringField) Gt(value String) Predicate { return Gt(field, value) }
// Ge returns a 'field >= value' Predicate.
func (field StringField) Ge(value String) Predicate { return Ge(field, value) }
// EqString returns a 'field = str' Predicate.
func (field StringField) EqString(str string) Predicate { return Eq(field, str) }
// NeString returns a 'field <> str' Predicate.
func (field StringField) NeString(str string) Predicate { return Ne(field, str) }
// LtString returns a 'field < str' Predicate.
func (field StringField) LtString(str string) Predicate { return Lt(field, str) }
// LeString returns a 'field <= str' Predicate.
func (field StringField) LeString(str string) Predicate { return Le(field, str) }
// GtString returns a 'field > str' Predicate.
func (field StringField) GtString(str string) Predicate { return Gt(field, str) }
// GeString returns a 'field >= str' Predicate.
func (field StringField) GeString(str string) Predicate { return Ge(field, str) }
// LikeString returns a 'field LIKE str' Predicate.
func (field StringField) LikeString(str string) Predicate {
return Expr("{} LIKE {}", field, str)
}
// NotLikeString returns a 'field NOT LIKE str' Predicate.
func (field StringField) NotLikeString(str string) Predicate {
return Expr("{} NOT LIKE {}", field, str)
}
// ILikeString returns a 'field ILIKE str' Predicate.
func (field StringField) ILikeString(str string) Predicate {
return Expr("{} ILIKE {}", field, str)
}
// NotILikeString returns a 'field NOT ILIKE str' Predicate.
func (field StringField) NotILikeString(str string) Predicate {
return Expr("{} NOT ILIKE {}", field, str)
}