-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlb_expr.go
1907 lines (1542 loc) · 57.3 KB
/
sqlb_expr.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 sqlb
import (
"fmt"
r "reflect"
"strconv"
"strings"
)
/*
Shortcut for interpolating strings into queries. Because this implements `Expr`,
when used as an argument in another expression, this will be directly
interpolated into the resulting query string. See the examples.
*/
type Str string
// Implement the `Expr` interface, making this a sub-expression.
func (self Str) AppendExpr(text []byte, args []any) ([]byte, []any) {
return self.AppendTo(text), args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Str) AppendTo(text []byte) []byte {
return appendMaybeSpaced(text, string(self))
}
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Str) String() string { return string(self) }
// Represents an SQL identifier, always quoted.
type Ident string
// Implement the `Expr` interface, making this a sub-expression.
func (self Ident) AppendExpr(text []byte, args []any) ([]byte, []any) {
return self.AppendTo(text), args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Ident) AppendTo(text []byte) []byte {
validateIdent(string(self))
text = maybeAppendSpace(text)
text = append(text, quoteDouble)
text = append(text, self...)
text = append(text, quoteDouble)
return text
}
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Ident) String() string { return AppenderString(&self) }
// Shortcut for internal use.
func (self Ident) BuiAppend(bui *Bui) {
bui.Text = self.AppendTo(bui.Text)
}
/*
Represents a nested SQL identifier where all elements are quoted but not
parenthesized. Useful for schema-qualified paths. For nested paths that don't
begin with a schema, use `Path` instead.
*/
type Identifier []string
// Implement the `Expr` interface, making this a sub-expression.
func (self Identifier) AppendExpr(text []byte, args []any) ([]byte, []any) {
return self.AppendTo(text), args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Identifier) AppendTo(text []byte) []byte {
if len(self) <= 0 {
return text
}
for ind, val := range self {
if ind > 0 {
text = append(text, `.`...)
}
text = Ident(val).AppendTo(text)
}
return text
}
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Identifier) String() string { return AppenderString(&self) }
// Normalizes the expression, returning nil or a single `Ident` if the length
// allows this. Otherwise returns self as-is.
func (self Identifier) Norm() Expr {
switch len(self) {
case 0:
return Identifier(nil)
case 1:
return Ident(self[0])
default:
return self
}
}
/*
Represents a nested SQL identifier where the first outer element is
parenthesized, and every element is quoted. Useful for nested paths that begin
with a table or view name. For schema-qualified paths, use `Identifier`
instead.
*/
type Path []string
// Implement the `Expr` interface, making this a sub-expression.
func (self Path) AppendExpr(text []byte, args []any) ([]byte, []any) {
return self.AppendTo(text), args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Path) AppendTo(text []byte) []byte {
if len(self) <= 0 {
return text
}
if len(self) == 1 {
return Ident(self[0]).AppendTo(text)
}
text = appendMaybeSpaced(text, `(`)
text = Ident(self[0]).AppendTo(text)
text = append(text, `)`...)
for _, val := range self[1:] {
text = append(text, `.`...)
text = Ident(val).AppendTo(text)
}
return text
}
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Path) String() string { return AppenderString(&self) }
// Normalizes the expression, returning nil or a single `Ident` if the length
// allows this. Otherwise returns self as-is.
func (self Path) Norm() Expr {
switch len(self) {
case 0:
return Path(nil)
case 1:
return Ident(self[0])
default:
return self
}
}
/*
Represents an arbitrarily-nested SQL path that gets encoded as a SINGLE quoted
identifier, where elements are dot-separated. This is a common convention for
nested structs, supported by SQL-scanning libraries such as
https://github.com/mitranim/gos.
*/
type PseudoPath []string
// Implement the `Expr` interface, making this a sub-expression.
func (self PseudoPath) AppendExpr(text []byte, args []any) ([]byte, []any) {
return self.AppendTo(text), args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self PseudoPath) AppendTo(text []byte) []byte {
if len(self) <= 0 {
return text
}
text = maybeAppendSpace(text)
text = append(text, quoteDouble)
for ind, val := range self {
validateIdent(val)
if ind > 0 {
text = append(text, `.`...)
}
text = append(text, val...)
}
text = append(text, quoteDouble)
return text
}
// Implement the `fmt.Stringer` interface for debug purposes.
func (self PseudoPath) String() string { return AppenderString(&self) }
// Normalizes the expression, returning nil or a single `Ident` if the length
// allows this. Otherwise returns self as-is.
func (self PseudoPath) Norm() Expr {
switch len(self) {
case 0:
return PseudoPath(nil)
case 1:
return Ident(self[0])
default:
return self
}
}
/*
Represents an arbitrarily-nested SQL path that gets encoded as `Path` followed
by `PseudoPath` alias. Useful for building "select" clauses. Used internally by
`ColsDeep`.
*/
type AliasedPath []string
// Implement the `Expr` interface, making this a sub-expression.
func (self AliasedPath) AppendExpr(text []byte, args []any) ([]byte, []any) {
return self.AppendTo(text), args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self AliasedPath) AppendTo(text []byte) []byte {
if len(self) <= 0 {
return text
}
if len(self) == 1 {
return Ident(self[0]).AppendTo(text)
}
text = Path(self).AppendTo(text)
text = append(text, ` as `...)
text = PseudoPath(self).AppendTo(text)
return text
}
// Implement the `fmt.Stringer` interface for debug purposes.
func (self AliasedPath) String() string { return AppenderString(&self) }
// Normalizes the expression, returning nil or a single `Ident` if the length
// allows this. Otherwise returns self as-is.
func (self AliasedPath) Norm() Expr {
switch len(self) {
case 0:
return AliasedPath(nil)
case 1:
return Ident(self[0])
default:
return self
}
}
/*
Same as `Identifier`, but preceded by the word "table". The SQL clause
"table some_name" is equivalent to "select * from some_name".
*/
type Table Identifier
// Implement the `Expr` interface, making this a sub-expression.
func (self Table) AppendExpr(text []byte, args []any) ([]byte, []any) {
return self.AppendTo(text), args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Table) AppendTo(text []byte) []byte {
if len(self) <= 0 {
return text
}
text = appendMaybeSpaced(text, `table`)
text = Identifier(self).AppendTo(text)
return text
}
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Table) String() string { return AppenderString(&self) }
/*
Variable-sized sequence of expressions. When encoding, expressions will be
space-separated if necessary.
*/
type Exprs []Expr
// Implement the `Expr` interface, making this a sub-expression.
func (self Exprs) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
for _, val := range self {
bui.Expr(val)
}
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Exprs) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Exprs) String() string { return exprString(self) }
/*
Represents an SQL "any()" expression. The inner value may be an instance of
`Expr`, or an arbitrary argument.
*/
type Any [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self Any) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
bui.Str(`any (`)
bui.Any(self[0])
bui.Str(`)`)
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Any) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Any) String() string { return exprString(self) }
/*
Represents an SQL assignment such as `"some_col" = arbitrary_expression`. The
LHS must be a column name, while the RHS can be an `Expr` instance or an
arbitrary argument.
*/
type Assign struct {
Lhs Ident
Rhs any
}
// Implement the `Expr` interface, making this a sub-expression.
func (self Assign) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
bui.Any(self.Lhs)
bui.Str(`=`)
bui.SubAny(self.Rhs)
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Assign) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Assign) String() string { return exprString(self) }
/*
Short for "equal". Represents SQL equality such as `A = B` or `A is null`.
Counterpart to `Neq`.
*/
type Eq [2]any
// Implement the `Expr` interface, making this a sub-expression.
func (self Eq) AppendExpr(text []byte, args []any) ([]byte, []any) {
text, args = self.AppendLhs(text, args)
text, args = self.AppendRhs(text, args)
return text, args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Eq) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Eq) String() string { return exprString(self) }
/*
Note: LHS and RHS are encoded differently because some SQL equality expressions
are asymmetric. For example, `any` allows an array only on the RHS, and there's
no way to invert it (AFAIK).
*/
func (self Eq) AppendLhs(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
bui.SubAny(self[0])
return bui.Get()
}
func (self Eq) AppendRhs(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
val := norm(self[1])
if val == nil {
bui.Str(`is null`)
return bui.Get()
}
bui.Str(`=`)
bui.SubAny(val)
return bui.Get()
}
/*
Short for "not equal". Represents SQL non-equality such as `A <> B` or
`A is not null`. Counterpart to `Eq`.
*/
type Neq [2]any
// Implement the `Expr` interface, making this a sub-expression.
func (self Neq) AppendExpr(text []byte, args []any) ([]byte, []any) {
text, args = self.AppendLhs(text, args)
text, args = self.AppendRhs(text, args)
return text, args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Neq) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Neq) String() string { return exprString(self) }
// See the comment on `Eq.AppendLhs`.
func (self Neq) AppendLhs(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
bui.SubAny(self[0])
return bui.Get()
}
func (self Neq) AppendRhs(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
val := norm(self[1])
if val == nil {
bui.Str(`is not null`)
return bui.Get()
}
bui.Str(`<>`)
bui.SubAny(val)
return bui.Get()
}
// Represents an SQL expression `A = any(B)`. Counterpart to `NeqAny`.
type EqAny [2]any
// Implement the `Expr` interface, making this a sub-expression.
func (self EqAny) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
bui.SubAny(self[0])
bui.Str(`=`)
bui.Set(Any{self[1]}.AppendExpr(bui.Get()))
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self EqAny) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self EqAny) String() string { return exprString(self) }
// Represents an SQL expression `A <> any(B)`. Counterpart to `EqAny`.
type NeqAny [2]any
// Implement the `Expr` interface, making this a sub-expression.
func (self NeqAny) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
bui.SubAny(self[0])
bui.Str(`<>`)
bui.Set(Any{self[1]}.AppendExpr(bui.Get()))
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self NeqAny) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self NeqAny) String() string { return exprString(self) }
// Represents SQL logical negation such as `not A`. The inner value can be an
// instance of `Expr` or an arbitrary argument.
type Not [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self Not) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
bui.Str(`not`)
bui.SubAny(self[0])
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Not) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Not) String() string { return exprString(self) }
/*
Represents a sequence of arbitrary sub-expressions or arguments, joined with a
customizable delimiter, with a customizable fallback in case of empty list.
This is mostly an internal tool for building other sequences, such as `And` and
`Or`. The inner value may be nil or a single `Expr`, otherwise it must be a
slice.
*/
type Seq struct {
Empty string
Delim string
Val any
}
// Implement the `Expr` interface, making this a sub-expression.
func (self Seq) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
val := self.Val
impl, _ := val.(Expr)
if impl != nil {
bui.Expr(impl)
} else {
self.any(&bui, val)
}
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Seq) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Seq) String() string { return exprString(self) }
func (self *Seq) any(bui *Bui, val any) {
switch kindOf(val) {
case r.Invalid:
self.appendEmpty(bui)
case r.Slice:
self.appendSlice(bui, val)
default:
panic(errExpectedSlice(`building SQL expression`, val))
}
}
func (self *Seq) appendEmpty(bui *Bui) {
bui.Str(self.Empty)
}
func (self Seq) appendSlice(bui *Bui, src any) {
val := valueOf(src)
if val.Len() <= 0 {
self.appendEmpty(bui)
return
}
if val.Len() == 1 {
bui.Any(val.Index(0).Interface())
return
}
for ind := range counter(val.Len()) {
if ind > 0 {
bui.Str(self.Delim)
}
bui.SubAny(val.Index(ind).Interface())
}
}
/*
Represents a comma-separated list of arbitrary sub-expressions. The inner value
may be nil or a single `Expr`, otherwise it must be a slice.
*/
type Comma [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self Comma) AppendExpr(text []byte, args []any) ([]byte, []any) {
// May revise in the future. Some SQL expressions, such as composite literals
// expressed as strings, are sensitive to whitespace around commas.
return Seq{``, `, `, self[0]}.AppendExpr(text, args)
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Comma) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Comma) String() string { return exprString(self) }
/*
Represents a sequence of arbitrary sub-expressions or arguments joined by the
SQL `and` operator. Rules for the inner value:
* nil or empty -> fallback to `true`
* single `Expr` -> render it as-is
* non-empty slice -> render its individual elements joined by `and`
* non-empty struct -> render column equality conditions joined by `and`
*/
type And [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self And) AppendExpr(text []byte, args []any) ([]byte, []any) {
return Cond{`true`, `and`, self[0]}.AppendExpr(text, args)
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self And) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self And) String() string { return exprString(self) }
/*
Represents a sequence of arbitrary sub-expressions or arguments joined by the
SQL `or` operator. Rules for the inner value:
* nil or empty -> fallback to `false`
* single `Expr` -> render it as-is
* non-empty slice -> render its individual elements joined by `or`
* non-empty struct -> render column equality conditions joined by `or`
*/
type Or [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self Or) AppendExpr(text []byte, args []any) ([]byte, []any) {
return Cond{`false`, `or`, self[0]}.AppendExpr(text, args)
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Or) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Or) String() string { return exprString(self) }
// Syntactic shortcut, same as `And` with a slice of sub-expressions or arguments.
type Ands []any
// Implement the `Expr` interface, making this a sub-expression.
func (self Ands) AppendExpr(text []byte, args []any) ([]byte, []any) {
if len(self) <= 0 {
return And{}.AppendExpr(text, args)
}
return And{[]any(self)}.AppendExpr(text, args)
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Ands) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Ands) String() string { return exprString(self) }
// Syntactic shortcut, same as `Or` with a slice of sub-expressions or arguments.
type Ors []any
// Implement the `Expr` interface, making this a sub-expression.
func (self Ors) AppendExpr(text []byte, args []any) ([]byte, []any) {
if len(self) <= 0 {
return Or{}.AppendExpr(text, args)
}
return Or{[]any(self)}.AppendExpr(text, args)
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Ors) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Ors) String() string { return exprString(self) }
/*
Superset of `Seq` with additional support for structs. When the inner value is
a struct, this generates a sequence of equality expressions, comparing the
struct's column names against the corresponding field values. Field values may
be arbitrary sub-expressions or arguments.
This is mostly an internal tool for building other expression types. Used
internally by `And` and `Or`.
*/
type Cond Seq
// Implement the `Expr` interface, making this a sub-expression.
func (self Cond) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
val := self.Val
impl, _ := val.(Expr)
if impl != nil {
bui.Expr(impl)
} else {
self.any(&bui, val)
}
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Cond) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Cond) String() string { return exprString(self) }
func (self *Cond) any(bui *Bui, val any) {
switch kindOf(val) {
case r.Invalid:
self.appendEmpty(bui)
case r.Struct:
self.appendStruct(bui, val)
case r.Slice:
self.appendSlice(bui, val)
default:
bui.Any(val)
}
}
func (self *Cond) appendEmpty(bui *Bui) {
(*Seq)(self).appendEmpty(bui)
}
// TODO consider if we should support nested non-embedded structs.
func (self *Cond) appendStruct(bui *Bui, src any) {
iter := makeIter(src)
for iter.next() {
if !iter.first() {
bui.Str(self.Delim)
}
lhs := Ident(FieldDbName(iter.field))
rhs := Eq{nil, iter.value.Interface()}
// Equivalent to using `Eq` for the full expression, but avoids an
// allocation caused by converting `Ident` to `Expr`. As a bonus, this also
// avoids unnecessary parens around the ident.
bui.Set(lhs.AppendExpr(bui.Get()))
bui.Set(rhs.AppendRhs(bui.Get()))
}
if iter.empty() {
self.appendEmpty(bui)
}
}
func (self *Cond) appendSlice(bui *Bui, val any) {
(*Seq)(self).appendSlice(bui, val)
}
/*
Represents a column list for a "select" expression. The inner value may be of
any type, and is used as a type carrier; its actual value is ignored. If the
inner value is a struct or struct slice, the resulting expression is a list of
column names corresponding to its fields, using a "db" tag. Otherwise the
expression is `*`.
Unlike many other struct-scanning expressions, this doesn't support filtering
via `Sparse`. It operates at the level of a struct type, not an individual
struct value.
TODO actually support `Sparse` because it's used for insert.
*/
type Cols [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self Cols) AppendExpr(text []byte, args []any) ([]byte, []any) {
return self.AppendTo(text), args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self Cols) AppendTo(text []byte) []byte {
return appendMaybeSpaced(text, self.String())
}
// Implement the `fmt.Stringer` interface for debug purposes.
func (self Cols) String() string {
return TypeCols(r.TypeOf(self[0]))
}
/*
Represents a column list for a "select" expression. The inner value may be of
any type, and is used as a type carrier; its actual value is ignored. If the
inner value is a struct or struct slice, the resulting expression is a list of
column names corresponding to its fields, using a "db" tag. Otherwise the
expression is `*`.
Unlike `Cols`, this has special support for nested structs and nested column
paths. See the examples.
Unlike many other struct-scanning expressions, this doesn't support filtering
via `Sparse`. It operates at the level of a struct type, not an individual
struct value.
*/
type ColsDeep [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self ColsDeep) AppendExpr(text []byte, args []any) ([]byte, []any) {
return self.AppendTo(text), args
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self ColsDeep) AppendTo(text []byte) []byte {
return appendMaybeSpaced(text, self.String())
}
// Implement the `fmt.Stringer` interface for debug purposes.
func (self ColsDeep) String() string {
return TypeColsDeep(r.TypeOf(self[0]))
}
/*
Represents comma-separated values from the "db"-tagged fields of an arbitrary
struct. Field/column names are ignored. Values may be arbitrary sub-expressions
or arguments. The value passed to `StructValues` may be nil, which is
equivalent to an empty struct. It may also be an arbitrarily-nested struct
pointer, which is automatically dereferenced.
Supports filtering. If the inner value implements `Sparse`, then not all fields
are considered to be "present", which is useful for PATCH semantics. See the
docs on `Sparse` and `Part`.
*/
type StructValues [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self StructValues) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
iter := makeIter(self[0])
// TODO consider panicking when empty.
iterAppendVals(&bui, iter, false)
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self StructValues) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self StructValues) String() string { return exprString(self) }
/*
Represents a names-and-values clause suitable for insertion. The inner value
must be nil or a struct. Nil or empty struct generates a "default values"
clause. Otherwise the resulting expression has SQL column names and values
generated by scanning the input struct. See the examples.
Supports filtering. If the inner value implements `Sparse`, then not all fields
are considered to be "present", which is useful for PATCH semantics. See the
docs on `Sparse` and `Part`.
*/
type StructInsert [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self StructInsert) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
iter := makeIter(self[0])
/**
The condition is slightly suboptimal: if the source is `Sparse`, `iter.has`
may iterate the fields and invoke a filter for multiple fields which happen
to be "missing", until it finds one that is "present". Then we iterate to
append cols, and to append values, for a total of three loops. It would be
more optimal to iterate only for cols and values, not for the condition.
*/
if iter.has() {
bui.Str(`(`)
iterAppendCols(&bui, iter, false)
bui.Str(`)`)
bui.Str(`values (`)
iterAppendVals(&bui, iter, false)
bui.Str(`)`)
} else {
bui.Str(`default values`)
}
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self StructInsert) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self StructInsert) String() string { return exprString(self) }
/*
Shortcut for creating `StructsInsert` from the given values.
Workaround for lack of type inference in type literals.
*/
func StructsInsertOf[A any](val ...A) StructsInsert[A] { return val }
/*
Variant of `StructInsert` that supports multiple structs. Generates a
names-and-values clause suitable for bulk insertion. The inner type must be a
struct. An empty slice generates an empty expression. See the examples.
*/
type StructsInsert[A any] []A
// Implement the `Expr` interface, making this a sub-expression.
func (self StructsInsert[A]) AppendExpr(text []byte, args []any) ([]byte, []any) {
if len(self) <= 0 {
return text, args
}
bui := Bui{text, args}
bui.Str(`(`)
bui.Str(TypeCols(typeOf((*A)(nil))))
bui.Str(`) values`)
for ind, val := range self {
if ind > 0 {
bui.Str(`, `)
}
bui.Str(`(`)
bui.Set(StructValues{val}.AppendExpr(bui.Get()))
bui.Str(`)`)
}
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self StructsInsert[_]) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self StructsInsert[_]) String() string { return exprString(self) }
/*
Represents an SQL assignment clause suitable for "update set" operations. The
inner value must be a struct. The resulting expression consists of
comma-separated assignments with column names and values derived from the
provided struct. See the example.
Supports filtering. If the inner value implements `Sparse`, then not all fields
are considered to be "present", which is useful for PATCH semantics. See the
docs on `Sparse` and `Part`. If there are NO fields, panics with
`ErrEmptyAssign`, which can be detected by user code via `errors.Is`.
*/
type StructAssign [1]any
// Implement the `Expr` interface, making this a sub-expression.
func (self StructAssign) AppendExpr(text []byte, args []any) ([]byte, []any) {
bui := Bui{text, args}
iter := makeIter(self[0])
for iter.next() {
if !iter.first() {
bui.Str(`,`)
}
bui.Set(Assign{
Ident(FieldDbName(iter.field)),
iter.value.Interface(),
}.AppendExpr(bui.Get()))
}
if iter.empty() {
panic(ErrEmptyAssign)
}
return bui.Get()
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self StructAssign) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self StructAssign) String() string { return exprString(self) }
/*
Wraps an arbitrary sub-expression, using `Cols{.Type}` to select specific
columns from it. If `.Type` doesn't specify a set of columns, for example
because it's not a struct type, then this uses the sub-expression as-is without
wrapping. Counterpart to `SelectColsDeep`.
*/
type SelectCols struct {
From Expr
Type any
}
// Implement the `Expr` interface, making this a sub-expression.
func (self SelectCols) AppendExpr(text []byte, args []any) ([]byte, []any) {
// Type-to-string is nearly free due to caching.
return SelectString{self.From, Cols{self.Type}.String()}.AppendExpr(text, args)
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text
// encoding.
func (self SelectCols) AppendTo(text []byte) []byte { return exprAppend(self, text) }
// Implement the `fmt.Stringer` interface for debug purposes.
func (self SelectCols) String() string { return exprString(self) }
/*
Wraps an arbitrary sub-expression, using `ColsDeep{.Type}` to select specific
columns from it. If `.Type` doesn't specify a set of columns, for example
because it's not a struct type, then this uses the sub-expression as-is without
wrapping. Counterpart to `SelectCols`.
*/
type SelectColsDeep struct {
From Expr
Type any
}
// Implement the `Expr` interface, making this a sub-expression.
func (self SelectColsDeep) AppendExpr(text []byte, args []any) ([]byte, []any) {
// Type-to-string is nearly free due to caching.
return SelectString{self.From, ColsDeep{self.Type}.String()}.AppendExpr(text, args)
}
// Implement the `AppenderTo` interface, sometimes allowing more efficient text