forked from rqlite/sql
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ast.go
1222 lines (1046 loc) · 26.7 KB
/
ast.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 sqlparser
import (
"bytes"
"fmt"
"strings"
)
type Node interface {
node()
fmt.Stringer
}
func (*Assignment) node() {}
func (*BinaryExpr) node() {}
func (*BindExpr) node() {}
func (*BlobLit) node() {}
func (*BoolLit) node() {}
func (*Call) node() {}
func (*CaseBlock) node() {}
func (*CaseExpr) node() {}
func (*DeleteStatement) node() {}
func (*Exists) node() {}
func (*Exprs) node() {}
func (*FilterClause) node() {}
func (*Hint) node() {}
func (*Ident) node() {}
func (*IndexedColumn) node() {}
func (*InsertStatement) node() {}
func (*JoinClause) node() {}
func (*JoinOperator) node() {}
func (*NullLit) node() {}
func (*NumberLit) node() {}
func (*OnConstraint) node() {}
func (*OrderingTerm) node() {}
func (*ParenExpr) node() {}
func (*ParenSource) node() {}
func (*QualifiedRef) node() {}
func (*Range) node() {}
func (*OutputNames) node() {}
func (*ResultColumn) node() {}
func (*SelectStatement) node() {}
func (*StringLit) node() {}
func (*TableName) node() {}
func (*Type) node() {}
func (*UnaryExpr) node() {}
func (*UpdateStatement) node() {}
func (*UpsertClause) node() {}
func (*UsingConstraint) node() {}
type Statement interface {
Node
stmt()
}
func (*DeleteStatement) stmt() {}
func (*InsertStatement) stmt() {}
func (*SelectStatement) stmt() {}
func (*UpdateStatement) stmt() {}
// StatementSource returns the root statement for a statement.
func StatementSource(stmt Statement) Source {
switch stmt := stmt.(type) {
case *SelectStatement:
return stmt.FromItems
case *UpdateStatement:
return stmt.TableName
case *DeleteStatement:
return stmt.TableName
default:
return nil
}
}
type Expr interface {
Node
expr()
}
func (*BinaryExpr) expr() {}
func (*BindExpr) expr() {}
func (*BlobLit) expr() {}
func (*BoolLit) expr() {}
func (*Call) expr() {}
func (*CaseExpr) expr() {}
func (*Exists) expr() {}
func (*Exprs) expr() {}
func (*Ident) expr() {}
func (*NullLit) expr() {}
func (*NumberLit) expr() {}
func (*ParenExpr) expr() {}
func (*QualifiedRef) expr() {}
func (*Range) expr() {}
func (*StringLit) expr() {}
func (*UnaryExpr) expr() {}
// ExprString returns the string representation of expr.
// Returns a blank string if expr is nil.
func ExprString(expr Expr) string {
if expr == nil {
return ""
}
return expr.String()
}
// SplitExprTree splits apart expr so it is a list of all AND joined expressions.
// For example, the expression "A AND B AND (C OR (D AND E))" would be split into
// a list of "A", "B", "C OR (D AND E)".
func SplitExprTree(expr Expr) []Expr {
if expr == nil {
return nil
}
var a []Expr
splitExprTree(expr, &a)
return a
}
func splitExprTree(expr Expr, a *[]Expr) {
switch expr := expr.(type) {
case *BinaryExpr:
if expr.Op != AND {
*a = append(*a, expr)
return
}
splitExprTree(expr.X, a)
splitExprTree(expr.Y, a)
case *ParenExpr:
splitExprTree(expr.X, a)
default:
*a = append(*a, expr)
}
}
// Scope represents a context for name resolution.
// Names can be resolved at the current source or in parent scopes.
type Scope struct {
Parent *Scope
Source Source
}
// Source represents a table or subquery.
type Source interface {
Node
source()
}
func (*JoinClause) source() {}
func (*ParenSource) source() {}
func (*TableName) source() {}
func (*SelectStatement) source() {}
// SourceName returns the name of the source.
// Only returns for TableName & ParenSource.
func SourceName(src Source) string {
switch src := src.(type) {
case *JoinClause, *SelectStatement:
return ""
case *ParenSource:
return IdentName(src.Alias)
case *TableName:
return src.TableName()
default:
return ""
}
}
// SourceList returns a list of scopes in the current scope.
func SourceList(src Source) []Source {
var a []Source
ForEachSource(src, func(s Source) bool {
a = append(a, s)
return true
})
return a
}
// ForEachSource calls fn for every source within the current scope.
// Stops iteration if fn returns false.
func ForEachSource(src Source, fn func(Source) bool) {
forEachSource(src, fn)
}
func forEachSource(src Source, fn func(Source) bool) bool {
if !fn(src) {
return false
}
switch src := src.(type) {
case *JoinClause:
if !forEachSource(src.X, fn) {
return false
} else if !forEachSource(src.Y, fn) {
return false
}
case *SelectStatement:
if !forEachSource(src.FromItems, fn) {
return false
}
}
return true
}
// ResolveSource returns a source with the given name.
// This can either be the table name or the alias for a source.
func ResolveSource(root Source, name string) Source {
var ret Source
ForEachSource(root, func(src Source) bool {
switch src := src.(type) {
case *ParenSource:
if IdentName(src.Alias) == name {
ret = src
}
case *TableName:
if src.TableName() == name {
ret = src
}
}
return ret == nil // continue until we find the matching source
})
return ret
}
// JoinConstraint represents either an ON or USING join constraint.
type JoinConstraint interface {
Node
joinConstraint()
}
func (*OnConstraint) joinConstraint() {}
func (*UsingConstraint) joinConstraint() {}
type Constraint interface {
Node
constraint()
}
type Ident struct {
Name string // identifier name
Quoted bool // true if double quoted
QuoteChar string // " for postgresql, ` for mysql, etc
}
// String returns the string representation of the expression.
func (i *Ident) String() string {
if i.Quoted {
if i.QuoteChar == `"` {
return i.QuoteChar + strings.ReplaceAll(i.Name, `"`, `""`) + i.QuoteChar
}
return i.QuoteChar + i.Name + i.QuoteChar
}
return i.Name
}
// IdentName returns the name of ident. Returns a blank string if ident is nil.
func IdentName(ident *Ident) string {
if ident == nil {
return ""
}
return ident.Name
}
type Type struct {
Name *Ident // type name
Lparen Pos // position of left paren (optional)
Precision *NumberLit // precision (optional)
Scale *NumberLit // scale (optional)
Rparen Pos // position of right paren (optional)
}
// String returns the string representation of the type.
func (t *Type) String() string {
if t.Precision != nil && t.Scale != nil {
return fmt.Sprintf("%s(%s,%s)", t.Name.Name, t.Precision.String(), t.Scale.String())
} else if t.Precision != nil {
return fmt.Sprintf("%s(%s)", t.Name.Name, t.Precision.String())
}
return t.Name.Name
}
type StringLit struct {
Value string // literal value (without quotes)
}
// String returns the string representation of the expression.
func (lit *StringLit) String() string {
return `'` + strings.Replace(lit.Value, `'`, `''`, -1) + `'`
}
type Hint struct {
Value string
}
// String returns the string representation of the expression.
func (h *Hint) String() string {
return `/* ` + h.Value + ` */`
}
type BlobLit struct {
Value string // literal value
}
// String returns the string representation of the expression.
func (lit *BlobLit) String() string {
return `x'` + lit.Value + `'`
}
type NumberLit struct {
Value string // literal value
}
// String returns the string representation of the expression.
func (lit *NumberLit) String() string {
return lit.Value
}
type NullLit struct {
}
// String returns the string representation of the expression.
func (lit *NullLit) String() string {
return "NULL"
}
type BoolLit struct {
Value bool // literal value
}
// String returns the string representation of the expression.
func (lit *BoolLit) String() string {
if lit.Value {
return "TRUE"
}
return "FALSE"
}
type BindExpr struct {
Name string // binding name
Pos int // binding position
}
// String returns the string representation of the expression.
func (expr *BindExpr) String() string {
// TODO(BBJ): Support all bind characters.
return expr.Name
}
type UnaryExpr struct {
Op Token // operation
X Expr // target expression
}
// String returns the string representation of the expression.
func (expr *UnaryExpr) String() string {
switch expr.Op {
case PLUS:
return "+" + expr.X.String()
case MINUS:
return "-" + expr.X.String()
default:
panic(fmt.Sprintf("sqlparser.UnaryExpr.String(): invalid op %s", expr.Op))
}
}
type BinaryExpr struct {
X Expr // lhs
Op Token // operator
Y Expr // rhs
}
// String returns the string representation of the expression.
func (expr *BinaryExpr) String() string {
switch expr.Op {
case PLUS:
return expr.X.String() + " + " + expr.Y.String()
case MINUS:
return expr.X.String() + " - " + expr.Y.String()
case STAR:
return expr.X.String() + " * " + expr.Y.String()
case SLASH:
return expr.X.String() + " / " + expr.Y.String()
case REM:
return expr.X.String() + " % " + expr.Y.String()
case CONCAT:
return expr.X.String() + " || " + expr.Y.String()
case BETWEEN:
return expr.X.String() + " BETWEEN " + expr.Y.String()
case NOTBETWEEN:
return expr.X.String() + " NOT BETWEEN " + expr.Y.String()
case LSHIFT:
return expr.X.String() + " << " + expr.Y.String()
case RSHIFT:
return expr.X.String() + " >> " + expr.Y.String()
case BITAND:
return expr.X.String() + " & " + expr.Y.String()
case BITOR:
return expr.X.String() + " | " + expr.Y.String()
case LT:
return expr.X.String() + " < " + expr.Y.String()
case LG:
return expr.X.String() + " <> " + expr.Y.String()
case LE:
return expr.X.String() + " <= " + expr.Y.String()
case GT:
return expr.X.String() + " > " + expr.Y.String()
case GE:
return expr.X.String() + " >= " + expr.Y.String()
case EQ:
return expr.X.String() + " = " + expr.Y.String()
case NE:
return expr.X.String() + " != " + expr.Y.String()
case IS:
return expr.X.String() + " IS " + expr.Y.String()
case ISNOT:
return expr.X.String() + " IS NOT " + expr.Y.String()
case IN:
return expr.X.String() + " IN " + expr.Y.String()
case NOTIN:
return expr.X.String() + " NOT IN " + expr.Y.String()
case LIKE:
return expr.X.String() + " LIKE " + expr.Y.String()
case NOTLIKE:
return expr.X.String() + " NOT LIKE " + expr.Y.String()
case GLOB:
return expr.X.String() + " GLOB " + expr.Y.String()
case NOTGLOB:
return expr.X.String() + " NOT GLOB " + expr.Y.String()
case MATCH:
return expr.X.String() + " MATCH " + expr.Y.String()
case NOTMATCH:
return expr.X.String() + " NOT MATCH " + expr.Y.String()
case REGEXP:
return expr.X.String() + " REGEXP " + expr.Y.String()
case NOTREGEXP:
return expr.X.String() + " NOT REGEXP " + expr.Y.String()
case AND:
return expr.X.String() + " AND " + expr.Y.String()
case OR:
return expr.X.String() + " OR " + expr.Y.String()
default:
panic(fmt.Sprintf("sqlparser.BinaryExpr.String(): invalid op %s", expr.Op))
}
}
type CaseExpr struct {
Operand Expr // optional condition after the CASE keyword
Blocks []*CaseBlock // list of WHEN/THEN pairs
ElseExpr Expr // expression used by default case
}
// String returns the string representation of the expression.
func (expr *CaseExpr) String() string {
var buf bytes.Buffer
buf.WriteString("CASE")
if expr.Operand != nil {
buf.WriteString(" ")
buf.WriteString(expr.Operand.String())
}
for _, blk := range expr.Blocks {
buf.WriteString(" ")
buf.WriteString(blk.String())
}
if expr.ElseExpr != nil {
buf.WriteString(" ELSE ")
buf.WriteString(expr.ElseExpr.String())
}
buf.WriteString(" END")
return buf.String()
}
type CaseBlock struct {
Condition Expr // block condition
Body Expr // result expression
}
// String returns the string representation of the block.
func (b *CaseBlock) String() string {
return fmt.Sprintf("WHEN %s THEN %s", b.Condition.String(), b.Body.String())
}
type Exists struct {
Not bool
Select *SelectStatement // select statement
}
// String returns the string representation of the expression.
func (expr *Exists) String() string {
if expr.Not {
return fmt.Sprintf("NOT EXISTS (%s)", expr.Select.String())
}
return fmt.Sprintf("EXISTS (%s)", expr.Select.String())
}
type Exprs struct {
Exprs []Expr // list of expressions
}
// String returns the string representation of the expression.
func (l *Exprs) String() string {
var buf bytes.Buffer
buf.WriteString("(")
for i, expr := range l.Exprs {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(expr.String())
}
buf.WriteString(")")
return buf.String()
}
type Range struct {
X Expr // lhs expression
Y Expr // rhs expression
}
// String returns the string representation of the expression.
func (r *Range) String() string {
return fmt.Sprintf("%s AND %s", r.X.String(), r.Y.String())
}
type QualifiedRef struct {
Table *Ident // table name
Star bool
Column *Ident // column name
}
// String returns the string representation of the expression.
func (r *QualifiedRef) String() string {
if r.Star {
return fmt.Sprintf("%s.*", r.Table.String())
}
return fmt.Sprintf("%s.%s", r.Table.String(), r.Column.String())
}
type Call struct {
Name *Ident // function name
Star bool
Distinct bool
Args []Expr // argument list
Filter *FilterClause // filter clause
}
// String returns the string representation of the expression.
func (c *Call) String() string {
var buf bytes.Buffer
buf.WriteString(c.Name.Name)
buf.WriteString("(")
if c.Star {
buf.WriteString("*")
} else {
if c.Distinct {
buf.WriteString("DISTINCT")
if len(c.Args) != 0 {
buf.WriteString(" ")
}
}
for i, arg := range c.Args {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(arg.String())
}
}
buf.WriteString(")")
if c.Filter != nil {
buf.WriteString(" ")
buf.WriteString(c.Filter.String())
}
return buf.String()
}
type FilterClause struct {
X Expr // filter expression
}
// String returns the string representation of the clause.
func (c *FilterClause) String() string {
return fmt.Sprintf("FILTER (WHERE %s)", c.X.String())
}
type OrderingTerm struct {
X Expr // ordering expression
Asc bool
Desc bool
NullsFirst bool
NullsLast bool
}
// String returns the string representation of the term.
func (t *OrderingTerm) String() string {
var buf bytes.Buffer
buf.WriteString(t.X.String())
if t.Asc {
buf.WriteString(" ASC")
} else if t.Desc {
buf.WriteString(" DESC")
}
if t.NullsFirst {
buf.WriteString(" NULLS FIRST")
} else if t.NullsLast {
buf.WriteString(" NULLS LAST")
}
return buf.String()
}
type LockStrength int
const (
Update LockStrength = iota
NoKeyUpdate
Share
KeyShare
)
type LockOption int
func (l LockOption) ToPtr() *LockOption {
return &l
}
const (
Nowait LockOption = iota
SkipLocked
)
type LockingClause struct {
Strength LockStrength
Option *LockOption
}
func (c *LockingClause) String() string {
var buf bytes.Buffer
buf.Grow(30)
buf.WriteString("FOR")
switch c.Strength {
case Update:
buf.WriteString(" UPDATE")
case NoKeyUpdate:
buf.WriteString(" NO KEY UPDATE")
case Share:
buf.WriteString(" SHARE")
case KeyShare:
buf.WriteString(" KEY SHARE")
}
if c.Option != nil {
switch *c.Option {
case Nowait:
buf.WriteString(" NOWAIT")
case SkipLocked:
buf.WriteString(" SKIP LOCKED")
}
}
return buf.String()
}
type ColumnArg interface {
Node
columnArg()
}
// InsertStatement see http://www.postgres.cn/docs/12/sql-insert.html
type InsertStatement struct {
TableName *TableName
ColumnNames []*Ident
Overriding string
DefaultValues bool
Expressions []*Exprs
Query *SelectStatement
UpsertClause *UpsertClause
OutputExpressions *OutputNames
}
// String returns the string representation of the statement.
func (s *InsertStatement) String() string {
var buf bytes.Buffer
buf.WriteString("INSERT")
fmt.Fprintf(&buf, " INTO %s", s.TableName.String())
if len(s.ColumnNames) != 0 {
buf.WriteString(" (")
for i, col := range s.ColumnNames {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(col.String())
}
buf.WriteString(")")
}
if s.DefaultValues {
buf.WriteString(" DEFAULT VALUES")
} else if s.Query != nil {
fmt.Fprintf(&buf, " %s", s.Query.String())
} else {
buf.WriteString(" VALUES")
for i := range s.Expressions {
if i != 0 {
buf.WriteString(",")
}
buf.WriteString(" (")
for j, expr := range s.Expressions[i].Exprs {
if j != 0 {
buf.WriteString(", ")
}
buf.WriteString(expr.String())
}
buf.WriteString(")")
}
}
if s.UpsertClause != nil {
fmt.Fprintf(&buf, " %s", s.UpsertClause.String())
}
if s.OutputExpressions != nil {
fmt.Fprintf(&buf, " RETURNING %s", s.OutputExpressions.String())
}
return buf.String()
}
type UpsertClause struct {
Columns []*IndexedColumn // optional indexed column list
WhereExpr Expr // optional conditional expression
DoNothing bool // position of NOTHING keyword after DO
DoUpdate bool // position of UPDATE keyword after DO
DuplicateKey bool // position of ON DUPLICATE KEY UPDATE keyword
Assignments []*Assignment // list of column assignments
UpdateWhereExpr Expr // optional conditional expression for DO UPDATE SET
}
// String returns the string representation of the clause.
func (c *UpsertClause) String() string {
var buf bytes.Buffer
if c.DuplicateKey {
buf.WriteString("ON DUPLICATE KEY UPDATE ")
for i := range c.Assignments {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(c.Assignments[i].String())
}
} else {
buf.WriteString("ON CONFLICT")
if len(c.Columns) != 0 {
buf.WriteString(" (")
for i, col := range c.Columns {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(col.String())
}
buf.WriteString(")")
if c.WhereExpr != nil {
fmt.Fprintf(&buf, " WHERE %s", c.WhereExpr.String())
}
}
buf.WriteString(" DO")
if c.DoNothing {
buf.WriteString(" NOTHING")
} else {
buf.WriteString(" UPDATE SET ")
for i := range c.Assignments {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(c.Assignments[i].String())
}
if c.UpdateWhereExpr != nil {
fmt.Fprintf(&buf, " WHERE %s", c.UpdateWhereExpr.String())
}
}
}
return buf.String()
}
// UpdateStatement see http://www.postgres.cn/docs/12/sql-update.html
type UpdateStatement struct {
Only bool
TableName *TableName
TableStar bool
Alias *Ident
Assignments []*Assignment
FromList []*TableName
Condition Expr
CursorName *Ident
OutputExpressions *OutputNames
}
// String returns the string representation of the clause.
func (s *UpdateStatement) String() string {
var buf bytes.Buffer
buf.WriteString("UPDATE ")
if s.Only {
buf.WriteString("ONLY ")
}
fmt.Fprintf(&buf, "%s", s.TableName.String())
if s.TableStar {
buf.WriteString(" *")
}
if s.Alias != nil {
fmt.Fprintf(&buf, " AS %s", s.Alias.String())
}
buf.WriteString(" SET ")
for i := range s.Assignments {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(s.Assignments[i].String())
}
if len(s.FromList) > 0 {
buf.WriteString(" From ")
for i, name := range s.FromList {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(name.String())
}
}
if s.Condition != nil {
fmt.Fprintf(&buf, " WHERE %s", s.Condition.String())
} else if s.CursorName != nil {
fmt.Fprintf(&buf, " WHERE CURRENT OF %s", s.CursorName.String())
}
if s.OutputExpressions != nil {
fmt.Fprintf(&buf, " RETURNING %s", s.OutputExpressions.String())
}
return buf.String()
}
// DeleteStatement see http://www.postgres.cn/docs/12/sql-delete.html
type DeleteStatement struct {
Only bool
TableName *TableName
TableStar bool
Alias *Ident
UsingList []*TableName
Condition Expr
CursorName *Ident
OutputExpressions *OutputNames
}
// String returns the string representation of the clause.
func (s *DeleteStatement) String() string {
var buf bytes.Buffer
buf.WriteString("DELETE FROM ")
if s.Only {
buf.WriteString("ONLY ")
}
fmt.Fprintf(&buf, "%s", s.TableName.String())
if s.TableStar {
buf.WriteString(" *")
}
if s.Alias != nil {
fmt.Fprintf(&buf, " AS %s", s.Alias.String())
}
if len(s.UsingList) > 0 {
buf.WriteString(" USING ")
for i, name := range s.UsingList {
if i > 0 {
buf.WriteString(", ")
}
buf.WriteString(name.String())
}
}
if s.Condition != nil {
fmt.Fprintf(&buf, " WHERE %s", s.Condition.String())
} else if s.CursorName != nil {
fmt.Fprintf(&buf, " WHERE CURRENT OF %s", s.CursorName.String())
}
if s.OutputExpressions != nil {
fmt.Fprintf(&buf, " RETURNING %s", s.OutputExpressions.String())
}
return buf.String()
}
// Assignment is used within the UPDATE statement & upsert clause.
// It is similiar to an expression except that it must be an equality.
type Assignment struct {
Columns []*Ident // column list
Expr Expr // assigned expression
}
// String returns the string representation of the clause.
func (a *Assignment) String() string {
var buf bytes.Buffer
if len(a.Columns) == 1 {
buf.WriteString(a.Columns[0].String())
} else if len(a.Columns) > 1 {
buf.WriteString("(")
for i, col := range a.Columns {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(col.String())
}
buf.WriteString(")")
}
fmt.Fprintf(&buf, " = %s", a.Expr.String())
return buf.String()
}
type IndexedColumn struct {
X Expr // column expression
Asc bool
Desc bool
}
// String returns the string representation of the column.
func (c *IndexedColumn) String() string {
if c.Asc {
return fmt.Sprintf("%s ASC", c.X.String())
} else if c.Desc {
return fmt.Sprintf("%s DESC", c.X.String())
}
return c.X.String()
}
// SelectStatement see http://www.postgres.cn/docs/12/sql-select.html
type SelectStatement struct {
All bool
Distinct bool
Columns *OutputNames // list of result columns in the SELECT clause
FromItems Source
Condition Expr
GroupingElements []Expr
HavingCondition Expr
Union bool
UnionAll bool
Intersect bool
Except bool
Compound *SelectStatement // compounded SELECT statement
OrderBy []*OrderingTerm // terms of ORDER BY clause
Limit Expr
Offset Expr // offset expression
Locking *LockingClause
Hint *Hint
}
// String returns the string representation of the statement.
func (s *SelectStatement) String() string {
var buf bytes.Buffer
buf.WriteString("SELECT ")
if s.Hint != nil {
fmt.Fprintf(&buf, "%s ", s.Hint.String())
}
if s.All {
buf.WriteString("ALL ")