forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ast.go
1097 lines (943 loc) · 23.9 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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package sqlparser
import (
"errors"
"fmt"
"strconv"
"strings"
"github.com/youtube/vitess/go/sqltypes"
)
// Instructions for creating new types: If a type
// needs to satisfy an interface, declare that function
// along with that interface. This will help users
// identify the list of types to which they can assert
// those interfaces.
// If the member of a type has a string with a predefined
// list of values, declare those values as const following
// the type.
// For interfaces that define dummy functions to consolidate
// a set of types, define the function as ITypeName.
// This will help avoid name collisions.
// Parse parses the sql and returns a Statement, which
// is the AST representation of the query.
func Parse(sql string) (Statement, error) {
tokenizer := NewStringTokenizer(sql)
if yyParse(tokenizer) != 0 {
return nil, errors.New(tokenizer.LastError)
}
return tokenizer.ParseTree, nil
}
// SQLNode defines the interface for all nodes
// generated by the parser.
type SQLNode interface {
Format(buf *TrackedBuffer)
}
// String returns a string representation of an SQLNode.
func String(node SQLNode) string {
buf := NewTrackedBuffer(nil)
buf.Myprintf("%v", node)
return buf.String()
}
// GenerateParsedQuery returns a ParsedQuery of the ast.
func GenerateParsedQuery(node SQLNode) *ParsedQuery {
buf := NewTrackedBuffer(nil)
buf.Myprintf("%v", node)
return buf.ParsedQuery()
}
// Statement represents a statement.
type Statement interface {
iStatement()
SQLNode
}
func (*Union) iStatement() {}
func (*Select) iStatement() {}
func (*Insert) iStatement() {}
func (*Update) iStatement() {}
func (*Delete) iStatement() {}
func (*Set) iStatement() {}
func (*DDL) iStatement() {}
func (*Other) iStatement() {}
// SelectStatement any SELECT statement.
type SelectStatement interface {
iSelectStatement()
iStatement()
iInsertRows()
SQLNode
}
func (*Select) iSelectStatement() {}
func (*Union) iSelectStatement() {}
// Select represents a SELECT statement.
type Select struct {
Comments Comments
Distinct string
SelectExprs SelectExprs
From TableExprs
Where *Where
GroupBy GroupBy
Having *Where
OrderBy OrderBy
Limit *Limit
Lock string
}
// Select.Distinct
const (
DistinctStr = "distinct "
)
// Select.Lock
const (
ForUpdateStr = " for update"
ShareModeStr = " lock in share mode"
)
// Format formats the node.
func (node *Select) Format(buf *TrackedBuffer) {
buf.Myprintf("select %v%s%v from %v%v%v%v%v%v%s",
node.Comments, node.Distinct, node.SelectExprs,
node.From, node.Where,
node.GroupBy, node.Having, node.OrderBy,
node.Limit, node.Lock)
}
// Union represents a UNION statement.
type Union struct {
Type string
Left, Right SelectStatement
}
// Union.Type
const (
UnionStr = "union"
UnionAllStr = "union all"
SetMinusStr = "minus"
ExceptStr = "except"
IntersectStr = "intersect"
)
// Format formats the node.
func (node *Union) Format(buf *TrackedBuffer) {
buf.Myprintf("%v %s %v", node.Left, node.Type, node.Right)
}
// Insert represents an INSERT statement.
type Insert struct {
Comments Comments
Ignore string
Table *TableName
Columns Columns
Rows InsertRows
OnDup OnDup
}
// Format formats the node.
func (node *Insert) Format(buf *TrackedBuffer) {
buf.Myprintf("insert %v%sinto %v%v %v%v",
node.Comments, node.Ignore,
node.Table, node.Columns, node.Rows, node.OnDup)
}
// InsertRows represents the rows for an INSERT statement.
type InsertRows interface {
iInsertRows()
SQLNode
}
func (*Select) iInsertRows() {}
func (*Union) iInsertRows() {}
func (Values) iInsertRows() {}
// Update represents an UPDATE statement.
type Update struct {
Comments Comments
Table *TableName
Exprs UpdateExprs
Where *Where
OrderBy OrderBy
Limit *Limit
}
// Format formats the node.
func (node *Update) Format(buf *TrackedBuffer) {
buf.Myprintf("update %v%v set %v%v%v%v",
node.Comments, node.Table,
node.Exprs, node.Where, node.OrderBy, node.Limit)
}
// Delete represents a DELETE statement.
type Delete struct {
Comments Comments
Table *TableName
Where *Where
OrderBy OrderBy
Limit *Limit
}
// Format formats the node.
func (node *Delete) Format(buf *TrackedBuffer) {
buf.Myprintf("delete %vfrom %v%v%v%v",
node.Comments,
node.Table, node.Where, node.OrderBy, node.Limit)
}
// Set represents a SET statement.
type Set struct {
Comments Comments
Exprs UpdateExprs
}
// Format formats the node.
func (node *Set) Format(buf *TrackedBuffer) {
buf.Myprintf("set %v%v", node.Comments, node.Exprs)
}
// DDL represents a CREATE, ALTER, DROP or RENAME statement.
// Table is set for AlterStr, DropStr, RenameStr.
// NewName is set for AlterStr, CreateStr, RenameStr.
type DDL struct {
Action string
Table SQLName
NewName SQLName
}
// DDL strings.
const (
CreateStr = "create"
AlterStr = "alter"
DropStr = "drop"
RenameStr = "rename"
)
// Format formats the node.
func (node *DDL) Format(buf *TrackedBuffer) {
switch node.Action {
case CreateStr:
buf.Myprintf("%s table %v", node.Action, node.NewName)
case RenameStr:
buf.Myprintf("%s table %v %v", node.Action, node.Table, node.NewName)
default:
buf.Myprintf("%s table %v", node.Action, node.Table)
}
}
// Other represents a SHOW, DESCRIBE, or EXPLAIN statement.
// It should be used only as an indicator. It does not contain
// the full AST for the statement.
type Other struct{}
// Format formats the node.
func (node *Other) Format(buf *TrackedBuffer) {
buf.WriteString("other")
}
// Comments represents a list of comments.
type Comments [][]byte
// Format formats the node.
func (node Comments) Format(buf *TrackedBuffer) {
for _, c := range node {
buf.Myprintf("%s ", c)
}
}
// SelectExprs represents SELECT expressions.
type SelectExprs []SelectExpr
// Format formats the node.
func (node SelectExprs) Format(buf *TrackedBuffer) {
var prefix string
for _, n := range node {
buf.Myprintf("%s%v", prefix, n)
prefix = ", "
}
}
// SelectExpr represents a SELECT expression.
type SelectExpr interface {
iSelectExpr()
SQLNode
}
func (*StarExpr) iSelectExpr() {}
func (*NonStarExpr) iSelectExpr() {}
// StarExpr defines a '*' or 'table.*' expression.
type StarExpr struct {
TableName SQLName
}
// Format formats the node.
func (node *StarExpr) Format(buf *TrackedBuffer) {
if node.TableName != "" {
buf.Myprintf("%v.", node.TableName)
}
buf.Myprintf("*")
}
// NonStarExpr defines a non-'*' select expr.
type NonStarExpr struct {
Expr Expr
As SQLName
}
// Format formats the node.
func (node *NonStarExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v", node.Expr)
if node.As != "" {
buf.Myprintf(" as %v", node.As)
}
}
// Columns represents an insert column list.
// The syntax for Columns is a subset of SelectExprs.
// So, it's castable to a SelectExprs and can be analyzed
// as such.
type Columns []SelectExpr
// Format formats the node.
func (node Columns) Format(buf *TrackedBuffer) {
if node == nil {
return
}
buf.Myprintf("(%v)", SelectExprs(node))
}
// TableExprs represents a list of table expressions.
type TableExprs []TableExpr
// Format formats the node.
func (node TableExprs) Format(buf *TrackedBuffer) {
var prefix string
for _, n := range node {
buf.Myprintf("%s%v", prefix, n)
prefix = ", "
}
}
// TableExpr represents a table expression.
type TableExpr interface {
iTableExpr()
SQLNode
}
func (*AliasedTableExpr) iTableExpr() {}
func (*ParenTableExpr) iTableExpr() {}
func (*JoinTableExpr) iTableExpr() {}
// AliasedTableExpr represents a table expression
// coupled with an optional alias or index hint.
// If As is empty, no alias was used.
type AliasedTableExpr struct {
Expr SimpleTableExpr
As SQLName
Hints *IndexHints
}
// Format formats the node.
func (node *AliasedTableExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v", node.Expr)
if node.As != "" {
buf.Myprintf(" as %v", node.As)
}
if node.Hints != nil {
// Hint node provides the space padding.
buf.Myprintf("%v", node.Hints)
}
}
// SimpleTableExpr represents a simple table expression.
type SimpleTableExpr interface {
iSimpleTableExpr()
SQLNode
}
func (*TableName) iSimpleTableExpr() {}
func (*Subquery) iSimpleTableExpr() {}
// TableName represents a table name.
// Qualifier, if specified, represents a database.
// It's generally not supported because vitess has its own
// rules about which database to send a query to.
type TableName struct {
Name, Qualifier SQLName
}
// Format formats the node.
func (node *TableName) Format(buf *TrackedBuffer) {
if node.Qualifier != "" {
buf.Myprintf("%v.", node.Qualifier)
}
buf.Myprintf("%v", node.Name)
}
// ParenTableExpr represents a parenthesized list of TableExpr.
type ParenTableExpr struct {
Exprs TableExprs
}
// Format formats the node.
func (node *ParenTableExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("(%v)", node.Exprs)
}
// JoinTableExpr represents a TableExpr that's a JOIN operation.
type JoinTableExpr struct {
LeftExpr TableExpr
Join string
RightExpr TableExpr
On BoolExpr
}
// JoinTableExpr.Join
const (
JoinStr = "join"
StraightJoinStr = "straight_join"
LeftJoinStr = "left join"
RightJoinStr = "right join"
NaturalJoinStr = "natural join"
NaturalLeftJoinStr = "natural left join"
NaturalRightJoinStr = "natural right join"
)
// Format formats the node.
func (node *JoinTableExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v %s %v", node.LeftExpr, node.Join, node.RightExpr)
if node.On != nil {
buf.Myprintf(" on %v", node.On)
}
}
// IndexHints represents a list of index hints.
type IndexHints struct {
Type string
Indexes []SQLName
}
// Index hints.
const (
UseStr = "use "
IgnoreStr = "ignore "
ForceStr = "force "
)
// Format formats the node.
func (node *IndexHints) Format(buf *TrackedBuffer) {
buf.Myprintf(" %sindex ", node.Type)
prefix := "("
for _, n := range node.Indexes {
buf.Myprintf("%s%v", prefix, n)
prefix = ", "
}
buf.Myprintf(")")
}
// Where represents a WHERE or HAVING clause.
type Where struct {
Type string
Expr BoolExpr
}
// Where.Type
const (
WhereStr = "where"
HavingStr = "having"
)
// NewWhere creates a WHERE or HAVING clause out
// of a BoolExpr. If the expression is nil, it returns nil.
func NewWhere(typ string, expr BoolExpr) *Where {
if expr == nil {
return nil
}
return &Where{Type: typ, Expr: expr}
}
// Format formats the node.
func (node *Where) Format(buf *TrackedBuffer) {
if node == nil || node.Expr == nil {
return
}
buf.Myprintf(" %s %v", node.Type, node.Expr)
}
// Expr represents an expression.
type Expr interface {
iExpr()
SQLNode
}
func (*AndExpr) iExpr() {}
func (*OrExpr) iExpr() {}
func (*NotExpr) iExpr() {}
func (*ParenBoolExpr) iExpr() {}
func (*ComparisonExpr) iExpr() {}
func (*RangeCond) iExpr() {}
func (*IsExpr) iExpr() {}
func (*ExistsExpr) iExpr() {}
func (*KeyrangeExpr) iExpr() {}
func (StrVal) iExpr() {}
func (NumVal) iExpr() {}
func (ValArg) iExpr() {}
func (*NullVal) iExpr() {}
func (BoolVal) iExpr() {}
func (*ColName) iExpr() {}
func (ValTuple) iExpr() {}
func (*Subquery) iExpr() {}
func (ListArg) iExpr() {}
func (*BinaryExpr) iExpr() {}
func (*UnaryExpr) iExpr() {}
func (*FuncExpr) iExpr() {}
func (*CaseExpr) iExpr() {}
// AllExprs must contain one variable for each
// AST type that satisfies Expr. This allows
// for external packages to verify
// that they're not missing on any types.
var AllExprs = []Expr{
&AndExpr{},
&OrExpr{},
&NotExpr{},
&ParenBoolExpr{},
&ComparisonExpr{},
&RangeCond{},
&IsExpr{},
&ExistsExpr{},
&KeyrangeExpr{},
StrVal(""),
NumVal(""),
ValArg(""),
&NullVal{},
BoolVal(false),
&ColName{},
ValTuple{},
&Subquery{},
ListArg(""),
&BinaryExpr{},
&UnaryExpr{},
&FuncExpr{},
&CaseExpr{},
}
// BoolExpr represents a boolean expression.
type BoolExpr interface {
iBoolExpr()
Expr
}
func (BoolVal) iBoolExpr() {}
func (*AndExpr) iBoolExpr() {}
func (*OrExpr) iBoolExpr() {}
func (*NotExpr) iBoolExpr() {}
func (*ParenBoolExpr) iBoolExpr() {}
func (*ComparisonExpr) iBoolExpr() {}
func (*RangeCond) iBoolExpr() {}
func (*IsExpr) iBoolExpr() {}
func (*ExistsExpr) iBoolExpr() {}
func (*KeyrangeExpr) iBoolExpr() {}
// AndExpr represents an AND expression.
type AndExpr struct {
Left, Right BoolExpr
}
// Format formats the node.
func (node *AndExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v and %v", node.Left, node.Right)
}
// OrExpr represents an OR expression.
type OrExpr struct {
Left, Right BoolExpr
}
// Format formats the node.
func (node *OrExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v or %v", node.Left, node.Right)
}
// NotExpr represents a NOT expression.
type NotExpr struct {
Expr BoolExpr
}
// Format formats the node.
func (node *NotExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("not %v", node.Expr)
}
// ParenBoolExpr represents a parenthesized boolean expression.
type ParenBoolExpr struct {
Expr BoolExpr
}
// Format formats the node.
func (node *ParenBoolExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("(%v)", node.Expr)
}
// ComparisonExpr represents a two-value comparison expression.
type ComparisonExpr struct {
Operator string
Left, Right ValExpr
}
// ComparisonExpr.Operator
const (
EqualStr = "="
LessThanStr = "<"
GreaterThanStr = ">"
LessEqualStr = "<="
GreaterEqualStr = ">="
NotEqualStr = "!="
NullSafeEqualStr = "<=>"
InStr = "in"
NotInStr = "not in"
LikeStr = "like"
NotLikeStr = "not like"
RegexpStr = "regexp"
NotRegexpStr = "not regexp"
)
// Format formats the node.
func (node *ComparisonExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v %s %v", node.Left, node.Operator, node.Right)
}
// RangeCond represents a BETWEEN or a NOT BETWEEN expression.
type RangeCond struct {
Operator string
Left ValExpr
From, To ValExpr
}
// RangeCond.Operator
const (
BetweenStr = "between"
NotBetweenStr = "not between"
)
// Format formats the node.
func (node *RangeCond) Format(buf *TrackedBuffer) {
buf.Myprintf("%v %s %v and %v", node.Left, node.Operator, node.From, node.To)
}
// IsExpr represents an IS ... or an IS NOT ... expression.
type IsExpr struct {
Operator string
Expr Expr
}
// IsExpr.Operator
const (
IsNullStr = "is null"
IsNotNullStr = "is not null"
IsTrueStr = "is true"
IsNotTrueStr = "is not true"
IsFalseStr = "is false"
IsNotFalseStr = "is not false"
)
// Format formats the node.
func (node *IsExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v %s", node.Expr, node.Operator)
}
// ExistsExpr represents an EXISTS expression.
type ExistsExpr struct {
Subquery *Subquery
}
// Format formats the node.
func (node *ExistsExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("exists %v", node.Subquery)
}
// KeyrangeExpr represents a KEYRANGE expression.
type KeyrangeExpr struct {
Start, End ValExpr
}
// Format formats the node.
func (node *KeyrangeExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("keyrange(%v, %v)", node.Start, node.End)
}
// ValExpr represents a value expression.
type ValExpr interface {
iValExpr()
Expr
}
func (StrVal) iValExpr() {}
func (NumVal) iValExpr() {}
func (ValArg) iValExpr() {}
func (*NullVal) iValExpr() {}
func (*ColName) iValExpr() {}
func (ValTuple) iValExpr() {}
func (*Subquery) iValExpr() {}
func (ListArg) iValExpr() {}
func (*BinaryExpr) iValExpr() {}
func (*UnaryExpr) iValExpr() {}
func (*FuncExpr) iValExpr() {}
func (*CaseExpr) iValExpr() {}
// StrVal represents a string value.
type StrVal []byte
// Format formats the node.
func (node StrVal) Format(buf *TrackedBuffer) {
s := sqltypes.MakeString([]byte(node))
s.EncodeSQL(buf)
}
// NumVal represents a number.
type NumVal []byte
// Format formats the node.
func (node NumVal) Format(buf *TrackedBuffer) {
buf.Myprintf("%s", []byte(node))
}
// ValArg represents a named bind var argument.
type ValArg []byte
// Format formats the node.
func (node ValArg) Format(buf *TrackedBuffer) {
buf.WriteArg(string(node))
}
// NullVal represents a NULL value.
type NullVal struct{}
// Format formats the node.
func (node *NullVal) Format(buf *TrackedBuffer) {
buf.Myprintf("null")
}
// BoolVal is true or false.
type BoolVal bool
// Format formats the node.
func (node BoolVal) Format(buf *TrackedBuffer) {
if node {
buf.Myprintf("true")
} else {
buf.Myprintf("false")
}
}
// ColName represents a column name.
type ColName struct {
Name SQLName
Qualifier SQLName
}
// Format formats the node.
func (node *ColName) Format(buf *TrackedBuffer) {
if node.Qualifier != "" {
buf.Myprintf("%v.", node.Qualifier)
}
buf.Myprintf("%v", node.Name)
}
// ColTuple represents a list of column values.
// It can be ValTuple, Subquery, ListArg.
type ColTuple interface {
iColTuple()
ValExpr
}
func (ValTuple) iColTuple() {}
func (*Subquery) iColTuple() {}
func (ListArg) iColTuple() {}
// ValTuple represents a tuple of actual values.
type ValTuple ValExprs
// Format formats the node.
func (node ValTuple) Format(buf *TrackedBuffer) {
buf.Myprintf("(%v)", ValExprs(node))
}
// ValExprs represents a list of value expressions.
// It's not a valid expression because it's not parenthesized.
type ValExprs []ValExpr
// Format formats the node.
func (node ValExprs) Format(buf *TrackedBuffer) {
var prefix string
for _, n := range node {
buf.Myprintf("%s%v", prefix, n)
prefix = ", "
}
}
// Subquery represents a subquery.
type Subquery struct {
Select SelectStatement
}
// Format formats the node.
func (node *Subquery) Format(buf *TrackedBuffer) {
buf.Myprintf("(%v)", node.Select)
}
// ListArg represents a named list argument.
type ListArg []byte
// Format formats the node.
func (node ListArg) Format(buf *TrackedBuffer) {
buf.WriteArg(string(node))
}
// BinaryExpr represents a binary value expression.
type BinaryExpr struct {
Operator string
Left, Right Expr
}
// BinaryExpr.Operator
const (
BitAndStr = "&"
BitOrStr = "|"
BitXorStr = "^"
PlusStr = "+"
MinusStr = "-"
MultStr = "*"
DivStr = "/"
ModStr = "%"
ShiftLeftStr = "<<"
ShiftRightStr = ">>"
)
// Format formats the node.
func (node *BinaryExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("%v %s %v", node.Left, node.Operator, node.Right)
}
// UnaryExpr represents a unary value expression.
type UnaryExpr struct {
Operator byte
Expr Expr
}
// UnaryExpr.Operator
const (
UPlusStr = '+'
UMinusStr = '-'
TildaStr = '~'
)
// Format formats the node.
func (node *UnaryExpr) Format(buf *TrackedBuffer) {
if _, unary := node.Expr.(*UnaryExpr); unary {
buf.Myprintf("%c %v", node.Operator, node.Expr)
return
}
buf.Myprintf("%c%v", node.Operator, node.Expr)
}
// FuncExpr represents a function call.
type FuncExpr struct {
Name string
Distinct bool
Exprs SelectExprs
}
// Format formats the node.
func (node *FuncExpr) Format(buf *TrackedBuffer) {
var distinct string
if node.Distinct {
distinct = "distinct "
}
buf.Myprintf("%s(%s%v)", node.Name, distinct, node.Exprs)
}
// Aggregates is a map of all aggregate functions.
var Aggregates = map[string]bool{
"avg": true,
"bit_and": true,
"bit_or": true,
"bit_xor": true,
"count": true,
"group_concat": true,
"max": true,
"min": true,
"std": true,
"stddev_pop": true,
"stddev_samp": true,
"stddev": true,
"sum": true,
"var_pop": true,
"var_samp": true,
"variance": true,
}
// IsAggregate returns true if the function is an aggregate.
func (node *FuncExpr) IsAggregate() bool {
return Aggregates[string(node.Name)]
}
// CaseExpr represents a CASE expression.
type CaseExpr struct {
Expr ValExpr
Whens []*When
Else ValExpr
}
// Format formats the node.
func (node *CaseExpr) Format(buf *TrackedBuffer) {
buf.Myprintf("case ")
if node.Expr != nil {
buf.Myprintf("%v ", node.Expr)
}
for _, when := range node.Whens {
buf.Myprintf("%v ", when)
}
if node.Else != nil {
buf.Myprintf("else %v ", node.Else)
}
buf.Myprintf("end")
}
// When represents a WHEN sub-expression.
type When struct {
Cond BoolExpr
Val ValExpr
}
// Format formats the node.
func (node *When) Format(buf *TrackedBuffer) {
buf.Myprintf("when %v then %v", node.Cond, node.Val)
}
// GroupBy represents a GROUP BY clause.
type GroupBy []ValExpr
// Format formats the node.
func (node GroupBy) Format(buf *TrackedBuffer) {
prefix := " group by "
for _, n := range node {
buf.Myprintf("%s%v", prefix, n)
prefix = ", "
}
}
// OrderBy represents an ORDER By clause.
type OrderBy []*Order
// Format formats the node.
func (node OrderBy) Format(buf *TrackedBuffer) {
prefix := " order by "
for _, n := range node {
buf.Myprintf("%s%v", prefix, n)
prefix = ", "
}
}
// Order represents an ordering expression.
type Order struct {
Expr ValExpr
Direction string
}
// Order.Direction
const (
AscScr = "asc"
DescScr = "desc"
)
// Format formats the node.
func (node *Order) Format(buf *TrackedBuffer) {
buf.Myprintf("%v %s", node.Expr, node.Direction)
}
// Limit represents a LIMIT clause.
type Limit struct {
Offset, Rowcount ValExpr
}
// Format formats the node.
func (node *Limit) Format(buf *TrackedBuffer) {
if node == nil {
return
}
buf.Myprintf(" limit ")
if node.Offset != nil {
buf.Myprintf("%v, ", node.Offset)
}
buf.Myprintf("%v", node.Rowcount)
}
// Limits returns the values of the LIMIT clause as interfaces.
// The returned values can be nil for absent field, string for
// bind variable names, or int64 for an actual number.
// Otherwise, it's an error.
func (node *Limit) Limits() (offset, rowcount interface{}, err error) {
if node == nil {
return nil, nil, nil
}
switch v := node.Offset.(type) {
case NumVal:
o, err := strconv.ParseInt(string(v), 0, 64)
if err != nil {
return nil, nil, err
}