FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlparser/ast.go at master · dearcode/sqlparser · GitHub
sqlparser/ast.go at master · dearcode/sqlparser · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
Accelerator
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
dearcode
/
sqlparser
Public
Notifications
You must be signed in to change notification settings
Fork
4
Star
4
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlparser
/
ast.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
3346 lines (2938 loc) · 73.7 KB
Breadcrumbs
sqlparser
/
ast.go
Copy path
File metadata and controls
3346 lines (2938 loc) · 73.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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 2017 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
sqlparser
import
(
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"strconv"
"strings"
"unicode"
log
"github.com/golang/glog"
"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/vterrors"
querypb
"vitess.io/vitess/go/vt/proto/query"
vtrpcpb
"vitess.io/vitess/go/vt/proto/vtrpc"
)
// 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 in full and returns a Statement, which
// is the AST representation of the query. If a DDL statement
// is partially parsed but still contains a syntax error, the
// error is ignored and the DDL is returned anyway.
func
Parse
(
sql
string
) (
Statement
,
error
) {
tokenizer
:=
NewStringTokenizer
(
sql
)
if
yyParse
(
tokenizer
)
!=
0
{
if
tokenizer
.
partialDDL
!=
nil
{
log
.
Warningf
(
"ignoring error parsing DDL '%s': %v"
,
sql
,
tokenizer
.
LastError
)
tokenizer
.
ParseTree
=
tokenizer
.
partialDDL
return
tokenizer
.
ParseTree
,
nil
}
return
nil
,
vterrors
.
New
(
vtrpcpb
.
Code_INVALID_ARGUMENT
,
tokenizer
.
LastError
.
Error
())
}
return
tokenizer
.
ParseTree
,
nil
}
// ParseStrictDDL is the same as Parse except it errors on
// partially parsed DDL statements.
func
ParseStrictDDL
(
sql
string
) (
Statement
,
error
) {
tokenizer
:=
NewStringTokenizer
(
sql
)
if
yyParse
(
tokenizer
)
!=
0
{
return
nil
,
tokenizer
.
LastError
}
return
tokenizer
.
ParseTree
,
nil
}
// ParseNext parses a single SQL statement from the tokenizer
// returning a Statement which is the AST representation of the query.
// The tokenizer will always read up to the end of the statement, allowing for
// the next call to ParseNext to parse any subsequent SQL statements. When
// there are no more statements to parse, a error of io.EOF is returned.
func
ParseNext
(
tokenizer
*
Tokenizer
) (
Statement
,
error
) {
if
tokenizer
.
lastChar
==
';'
{
tokenizer
.
next
()
tokenizer
.
skipBlank
()
}
if
tokenizer
.
lastChar
==
eofChar
{
return
nil
,
io
.
EOF
}
tokenizer
.
reset
()
tokenizer
.
multi
=
true
if
yyParse
(
tokenizer
)
!=
0
{
if
tokenizer
.
partialDDL
!=
nil
{
tokenizer
.
ParseTree
=
tokenizer
.
partialDDL
return
tokenizer
.
ParseTree
,
nil
}
return
nil
,
tokenizer
.
LastError
}
return
tokenizer
.
ParseTree
,
nil
}
// Split 直接拆分成数组.
func
Split
(
blob
string
) (
stmts
[]
string
,
err
error
) {
tokenizer
:=
NewStringTokenizer
(
blob
)
var
idx
int
for
tkn
:=
1
;
tkn
!=
0
&&
tkn
!=
eofChar
; {
if
tkn
,
_
=
tokenizer
.
Scan
();
tkn
==
';'
{
stmts
=
append
(
stmts
,
blob
[
idx
:
tokenizer
.
Position
-
2
])
for
idx
=
tokenizer
.
Position
-
1
;
idx
<
len
(
blob
)
&&
unicode
.
IsSpace
(
rune
(
blob
[
idx
]));
idx
++
{
}
}
}
err
=
tokenizer
.
LastError
return
}
// SplitStatement returns the first sql statement up to either a ; or EOF
// and the remainder from the given buffer
func
SplitStatement
(
blob
string
) (
string
,
string
,
error
) {
tokenizer
:=
NewStringTokenizer
(
blob
)
tkn
:=
0
for
{
tkn
,
_
=
tokenizer
.
Scan
()
if
tkn
==
0
||
tkn
==
';'
||
tkn
==
eofChar
{
break
}
}
if
tokenizer
.
LastError
!=
nil
{
return
""
,
""
,
tokenizer
.
LastError
}
if
tkn
==
';'
{
return
blob
[:
tokenizer
.
Position
-
2
],
blob
[
tokenizer
.
Position
-
1
:],
nil
}
return
blob
,
""
,
nil
}
// SQLNode defines the interface for all nodes
// generated by the parser.
type
SQLNode
interface
{
Format
(
buf
*
TrackedBuffer
)
// WalkSubtree calls visit on all underlying nodes
// of the subtree, but not the current one. Walking
// must be interrupted if visit returns an error.
WalkSubtree
(
visit
Visit
)
error
}
// Visit defines the signature of a function that
// can be used to visit all nodes of a parse tree.
type
Visit
func
(
node
SQLNode
) (
kontinue
bool
,
err
error
)
// Walk calls visit on every node.
// If visit returns true, the underlying nodes
// are also visited. If it returns an error, walking
// is interrupted, and the error is returned.
func
Walk
(
visit
Visit
,
nodes
...
SQLNode
)
error
{
for
_
,
node
:=
range
nodes
{
if
node
==
nil
{
continue
}
kontinue
,
err
:=
visit
(
node
)
if
err
!=
nil
{
return
err
}
if
kontinue
{
err
=
node
.
WalkSubtree
(
visit
)
if
err
!=
nil
{
return
err
}
}
}
return
nil
}
// String returns a string representation of an SQLNode.
func
String
(
node
SQLNode
)
string
{
if
node
==
nil
{
return
"<nil>"
}
buf
:=
NewTrackedBuffer
(
nil
)
buf
.
Myprintf
(
"%v"
,
node
)
return
buf
.
String
()
}
// Append appends the SQLNode to the buffer.
func
Append
(
buf
*
bytes.
Buffer
,
node
SQLNode
) {
tbuf
:=
&
TrackedBuffer
{
Buffer
:
buf
,
}
node
.
Format
(
tbuf
)
}
// Statement represents a statement.
type
Statement
interface
{
iStatement
()
SQLNode
}
func
(
*
Union
)
iStatement
() {}
func
(
*
Select
)
iStatement
() {}
func
(
*
Stream
)
iStatement
() {}
func
(
*
Insert
)
iStatement
() {}
func
(
*
Update
)
iStatement
() {}
func
(
*
Delete
)
iStatement
() {}
func
(
*
Set
)
iStatement
() {}
func
(
*
DDL
)
iStatement
() {}
func
(
*
Show
)
iStatement
() {}
func
(
*
Use
)
iStatement
() {}
func
(
*
Begin
)
iStatement
() {}
func
(
*
Commit
)
iStatement
() {}
func
(
*
Rollback
)
iStatement
() {}
func
(
*
OtherRead
)
iStatement
() {}
func
(
*
OtherAdmin
)
iStatement
() {}
// ParenSelect can actually not be a top level statement,
// but we have to allow it because it's a requirement
// of SelectStatement.
func
(
*
ParenSelect
)
iStatement
() {}
// SelectStatement any SELECT statement.
type
SelectStatement
interface
{
iSelectStatement
()
iStatement
()
iInsertRows
()
AddOrder
(
*
Order
)
SetLimit
(
*
Limit
)
SQLNode
}
func
(
*
Select
)
iSelectStatement
() {}
func
(
*
Union
)
iSelectStatement
() {}
func
(
*
ParenSelect
)
iSelectStatement
() {}
// Select represents a SELECT statement.
type
Select
struct
{
Cache
string
Comments
Comments
Distinct
string
Hints
string
SelectExprs
SelectExprs
From
TableExprs
Where
*
Where
GroupBy
GroupBy
Having
*
Where
OrderBy
OrderBy
Limit
*
Limit
Lock
string
}
// Select.Distinct
const
(
DistinctStr
=
"distinct "
StraightJoinHint
=
"straight_join "
)
// Select.Lock
const
(
ForUpdateStr
=
" for update"
ShareModeStr
=
" lock in share mode"
)
// Select.Cache
const
(
SQLCacheStr
=
"sql_cache "
SQLNoCacheStr
=
"sql_no_cache "
)
// AddOrder adds an order by element
func
(
node
*
Select
)
AddOrder
(
order
*
Order
) {
node
.
OrderBy
=
append
(
node
.
OrderBy
,
order
)
}
// SetLimit sets the limit clause
func
(
node
*
Select
)
SetLimit
(
limit
*
Limit
) {
node
.
Limit
=
limit
}
// Format formats the node.
func
(
node
*
Select
)
Format
(
buf
*
TrackedBuffer
) {
buf
.
Myprintf
(
"select %v%s%s%s%v from %v%v%v%v%v%v%s"
,
node
.
Comments
,
node
.
Cache
,
node
.
Distinct
,
node
.
Hints
,
node
.
SelectExprs
,
node
.
From
,
node
.
Where
,
node
.
GroupBy
,
node
.
Having
,
node
.
OrderBy
,
node
.
Limit
,
node
.
Lock
)
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
Select
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Comments
,
node
.
SelectExprs
,
node
.
From
,
node
.
Where
,
node
.
GroupBy
,
node
.
Having
,
node
.
OrderBy
,
node
.
Limit
,
)
}
// AddWhere adds the boolean expression to the
// WHERE clause as an AND condition. If the expression
// is an OR clause, it parenthesizes it. Currently,
// the OR operator is the only one that's lower precedence
// than AND.
func
(
node
*
Select
)
AddWhere
(
expr
Expr
) {
if
_
,
ok
:=
expr
.(
*
OrExpr
);
ok
{
expr
=
&
ParenExpr
{
Expr
:
expr
}
}
if
node
.
Where
==
nil
{
node
.
Where
=
&
Where
{
Type
:
WhereStr
,
Expr
:
expr
,
}
return
}
node
.
Where
.
Expr
=
&
AndExpr
{
Left
:
node
.
Where
.
Expr
,
Right
:
expr
,
}
return
}
// AddHaving adds the boolean expression to the
// HAVING clause as an AND condition. If the expression
// is an OR clause, it parenthesizes it. Currently,
// the OR operator is the only one that's lower precedence
// than AND.
func
(
node
*
Select
)
AddHaving
(
expr
Expr
) {
if
_
,
ok
:=
expr
.(
*
OrExpr
);
ok
{
expr
=
&
ParenExpr
{
Expr
:
expr
}
}
if
node
.
Having
==
nil
{
node
.
Having
=
&
Where
{
Type
:
HavingStr
,
Expr
:
expr
,
}
return
}
node
.
Having
.
Expr
=
&
AndExpr
{
Left
:
node
.
Having
.
Expr
,
Right
:
expr
,
}
return
}
// ParenSelect is a parenthesized SELECT statement.
type
ParenSelect
struct
{
Select
SelectStatement
}
// AddOrder adds an order by element
func
(
node
*
ParenSelect
)
AddOrder
(
order
*
Order
) {
panic
(
"unreachable"
)
}
// SetLimit sets the limit clause
func
(
node
*
ParenSelect
)
SetLimit
(
limit
*
Limit
) {
panic
(
"unreachable"
)
}
// Format formats the node.
func
(
node
*
ParenSelect
)
Format
(
buf
*
TrackedBuffer
) {
buf
.
Myprintf
(
"(%v)"
,
node
.
Select
)
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
ParenSelect
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Select
,
)
}
// Union represents a UNION statement.
type
Union
struct
{
Type
string
Left
,
Right
SelectStatement
OrderBy
OrderBy
Limit
*
Limit
Lock
string
}
// Union.Type
const
(
UnionStr
=
"union"
UnionAllStr
=
"union all"
UnionDistinctStr
=
"union distinct"
)
// AddOrder adds an order by element
func
(
node
*
Union
)
AddOrder
(
order
*
Order
) {
node
.
OrderBy
=
append
(
node
.
OrderBy
,
order
)
}
// SetLimit sets the limit clause
func
(
node
*
Union
)
SetLimit
(
limit
*
Limit
) {
node
.
Limit
=
limit
}
// Format formats the node.
func
(
node
*
Union
)
Format
(
buf
*
TrackedBuffer
) {
buf
.
Myprintf
(
"%v %s %v%v%v%s"
,
node
.
Left
,
node
.
Type
,
node
.
Right
,
node
.
OrderBy
,
node
.
Limit
,
node
.
Lock
)
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
Union
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Left
,
node
.
Right
,
)
}
// Stream represents a SELECT statement.
type
Stream
struct
{
Comments
Comments
SelectExpr
SelectExpr
Table
TableName
}
// Format formats the node.
func
(
node
*
Stream
)
Format
(
buf
*
TrackedBuffer
) {
buf
.
Myprintf
(
"stream %v%v from %v"
,
node
.
Comments
,
node
.
SelectExpr
,
node
.
Table
)
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
Stream
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Comments
,
node
.
SelectExpr
,
node
.
Table
,
)
}
// Insert represents an INSERT or REPLACE statement.
// Per the MySQL docs, http://dev.mysql.com/doc/refman/5.7/en/replace.html
// Replace is the counterpart to `INSERT IGNORE`, and works exactly like a
// normal INSERT except if the row exists. In that case it first deletes
// the row and re-inserts with new values. For that reason we keep it as an Insert struct.
// Replaces are currently disallowed in sharded schemas because
// of the implications the deletion part may have on vindexes.
// If you add fields here, consider adding them to calls to validateSubquerySamePlan.
type
Insert
struct
{
Action
string
Comments
Comments
Ignore
string
Table
TableName
Partitions
Partitions
Columns
Columns
Rows
InsertRows
OnDup
OnDup
}
// DDL strings.
const
(
InsertStr
=
"insert"
ReplaceStr
=
"replace"
)
// Format formats the node.
func
(
node
*
Insert
)
Format
(
buf
*
TrackedBuffer
) {
buf
.
Myprintf
(
"%s %v%sinto %v%v%v %v%v"
,
node
.
Action
,
node
.
Comments
,
node
.
Ignore
,
node
.
Table
,
node
.
Partitions
,
node
.
Columns
,
node
.
Rows
,
node
.
OnDup
)
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
Insert
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Comments
,
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
() {}
func
(
*
ParenSelect
)
iInsertRows
() {}
// Update represents an UPDATE statement.
// If you add fields here, consider adding them to calls to validateSubquerySamePlan.
type
Update
struct
{
Comments
Comments
TableExprs
TableExprs
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
.
TableExprs
,
node
.
Exprs
,
node
.
Where
,
node
.
OrderBy
,
node
.
Limit
)
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
Update
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Comments
,
node
.
TableExprs
,
node
.
Exprs
,
node
.
Where
,
node
.
OrderBy
,
node
.
Limit
,
)
}
// Delete represents a DELETE statement.
// If you add fields here, consider adding them to calls to validateSubquerySamePlan.
type
Delete
struct
{
Comments
Comments
Targets
TableNames
TableExprs
TableExprs
Partitions
Partitions
Where
*
Where
OrderBy
OrderBy
Limit
*
Limit
}
// Format formats the node.
func
(
node
*
Delete
)
Format
(
buf
*
TrackedBuffer
) {
buf
.
Myprintf
(
"delete %v"
,
node
.
Comments
)
if
node
.
Targets
!=
nil
{
buf
.
Myprintf
(
"%v "
,
node
.
Targets
)
}
buf
.
Myprintf
(
"from %v%v%v%v%v"
,
node
.
TableExprs
,
node
.
Partitions
,
node
.
Where
,
node
.
OrderBy
,
node
.
Limit
)
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
Delete
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Comments
,
node
.
Targets
,
node
.
TableExprs
,
node
.
Where
,
node
.
OrderBy
,
node
.
Limit
,
)
}
// Set represents a SET statement.
type
Set
struct
{
Comments
Comments
Exprs
SetExprs
Scope
string
}
// Set.Scope or Show.Scope
const
(
SessionStr
=
"session"
GlobalStr
=
"global"
)
// Format formats the node.
func
(
node
*
Set
)
Format
(
buf
*
TrackedBuffer
) {
if
node
.
Scope
==
""
{
buf
.
Myprintf
(
"set %v%v"
,
node
.
Comments
,
node
.
Exprs
)
}
else
{
buf
.
Myprintf
(
"set %v%s %v"
,
node
.
Comments
,
node
.
Scope
,
node
.
Exprs
)
}
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
Set
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Comments
,
node
.
Exprs
,
)
}
// DDL represents a CREATE, ALTER, DROP, RENAME or TRUNCATE statement.
// Table is set for AlterStr, DropStr, RenameStr, TruncateStr
// NewName is set for AlterStr, CreateStr, RenameStr.
// VindexSpec is set for CreateVindexStr, DropVindexStr, AddColVindexStr, DropColVindexStr
// VindexCols is set for AddColVindexStr
type
DDL
struct
{
Action
string
Table
TableName
NewName
TableName
IfExists
bool
Unique
bool
LikeTable
TableName
TableSpec
*
TableSpec
PartitionSpec
*
PartitionSpec
VindexSpec
*
VindexSpec
VindexCols
[]
ColIdent
Option
TableOption
}
// DDL strings.
const
(
CreateStr
=
"create"
CreateTableStr
=
"create table"
CreateDatabaseStr
=
"create database"
AlterStr
=
"alter"
AlterTableStr
=
"alter table"
DropTableStr
=
"drop table"
DropViewStr
=
"drop view"
DropDatabaseStr
=
"drop database"
RenameStr
=
"rename"
TruncateStr
=
"truncate"
CreateVindexStr
=
"create vindex"
AddColVindexStr
=
"add vindex"
DropColVindexStr
=
"drop vindex"
// Vindex DDL param to specify the owner of a vindex
VindexOwnerStr
=
"owner"
)
// Format formats the node.
func
(
node
*
DDL
)
Format
(
buf
*
TrackedBuffer
) {
switch
node
.
Action
{
case
CreateTableStr
:
if
node
.
TableSpec
==
nil
{
buf
.
Myprintf
(
"%s %v"
,
node
.
Action
,
node
.
NewName
)
}
else
{
buf
.
Myprintf
(
"%s %v %v"
,
node
.
Action
,
node
.
NewName
,
node
.
TableSpec
)
}
case
CreateStr
:
if
node
.
TableSpec
==
nil
{
buf
.
Myprintf
(
"%s table %v"
,
node
.
Action
,
node
.
NewName
)
}
else
{
buf
.
Myprintf
(
"%s table %v %v"
,
node
.
Action
,
node
.
NewName
,
node
.
TableSpec
)
}
case
CreateDatabaseStr
:
if
node
.
TableSpec
==
nil
{
buf
.
Myprintf
(
"%s %v"
,
node
.
Action
,
node
.
Table
.
Qualifier
)
}
else
{
buf
.
Myprintf
(
"%s %v %v"
,
node
.
Action
,
node
.
Table
.
Qualifier
,
node
.
TableSpec
.
Option
)
}
case
DropTableStr
,
DropViewStr
,
DropDatabaseStr
:
exists
:=
""
if
node
.
IfExists
{
exists
=
" if exists"
}
buf
.
Myprintf
(
"%s%s "
,
node
.
Action
,
exists
)
if
!
node
.
Table
.
Qualifier
.
IsEmpty
() {
buf
.
Myprintf
(
"%v."
,
node
.
Table
.
Qualifier
)
}
buf
.
Myprintf
(
"%v"
,
node
.
Table
.
Name
)
case
RenameStr
:
buf
.
Myprintf
(
"%s table %v to %v"
,
node
.
Action
,
node
.
Table
,
node
.
NewName
)
case
AlterStr
:
if
node
.
PartitionSpec
!=
nil
{
buf
.
Myprintf
(
"%s table %v %v"
,
node
.
Action
,
node
.
Table
,
node
.
PartitionSpec
)
}
else
{
buf
.
Myprintf
(
"%s table %v"
,
node
.
Action
,
node
.
Table
)
}
case
AlterTableStr
:
if
node
.
PartitionSpec
!=
nil
{
buf
.
Myprintf
(
"%s %v %v"
,
node
.
Action
,
node
.
Table
,
node
.
PartitionSpec
)
}
else
{
buf
.
Myprintf
(
"%s %v"
,
node
.
Action
,
node
.
Table
)
}
case
CreateVindexStr
:
buf
.
Myprintf
(
"%s %v %v"
,
node
.
Action
,
node
.
VindexSpec
.
Name
,
node
.
VindexSpec
)
case
AddColVindexStr
:
buf
.
Myprintf
(
"alter table %v %s %v ("
,
node
.
Table
,
node
.
Action
,
node
.
VindexSpec
.
Name
)
for
i
,
col
:=
range
node
.
VindexCols
{
if
i
!=
0
{
buf
.
Myprintf
(
", %v"
,
col
)
}
else
{
buf
.
Myprintf
(
"%v"
,
col
)
}
}
buf
.
Myprintf
(
")"
)
if
node
.
VindexSpec
.
Type
.
String
()
!=
""
{
buf
.
Myprintf
(
" %v"
,
node
.
VindexSpec
)
}
case
DropColVindexStr
:
buf
.
Myprintf
(
"alter table %v %s %v"
,
node
.
Table
,
node
.
Action
,
node
.
VindexSpec
.
Name
)
default
:
buf
.
Myprintf
(
"%s table %v"
,
node
.
Action
,
node
.
Table
)
}
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
DDL
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Table
,
node
.
NewName
,
)
}
// Partition strings
const
(
ReorganizeStr
=
"reorganize partition"
)
// PartitionSpec describe partition actions (for alter and create)
type
PartitionSpec
struct
{
Action
string
Name
ColIdent
Definitions
[]
*
PartitionDefinition
}
// Format formats the node.
func
(
node
*
PartitionSpec
)
Format
(
buf
*
TrackedBuffer
) {
switch
node
.
Action
{
case
ReorganizeStr
:
buf
.
Myprintf
(
"%s %v into ("
,
node
.
Action
,
node
.
Name
)
var
prefix
string
for
_
,
pd
:=
range
node
.
Definitions
{
buf
.
Myprintf
(
"%s%v"
,
prefix
,
pd
)
prefix
=
", "
}
buf
.
Myprintf
(
")"
)
default
:
panic
(
"unimplemented"
)
}
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
PartitionSpec
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
if
err
:=
Walk
(
visit
,
node
.
Name
);
err
!=
nil
{
return
err
}
for
_
,
def
:=
range
node
.
Definitions
{
if
err
:=
Walk
(
visit
,
def
);
err
!=
nil
{
return
err
}
}
return
nil
}
// PartitionDefinition describes a very minimal partition definition
type
PartitionDefinition
struct
{
Name
ColIdent
Limit
Expr
Maxvalue
bool
}
// Format formats the node
func
(
node
*
PartitionDefinition
)
Format
(
buf
*
TrackedBuffer
) {
if
!
node
.
Maxvalue
{
buf
.
Myprintf
(
"partition %v values less than (%v)"
,
node
.
Name
,
node
.
Limit
)
}
else
{
buf
.
Myprintf
(
"partition %v values less than (maxvalue)"
,
node
.
Name
)
}
}
// WalkSubtree walks the nodes of the subtree.
func
(
node
*
PartitionDefinition
)
WalkSubtree
(
visit
Visit
)
error
{
if
node
==
nil
{
return
nil
}
return
Walk
(
visit
,
node
.
Name
,
node
.
Limit
,
)
}
type
TableOption
struct
{
Engine
[]
byte
Charset
[]
byte
Comment
[]
byte
AutoIncrement
*
int64
Common
[]
byte
Collate
[]
byte
}
// TableSpec describes the structure of a table from a CREATE TABLE statement
type
TableSpec
struct
{
Columns
[]
*
ColumnDefinition
Indexes
[]
*
IndexDefinition
Option
TableOption
}
//IntegerPtr bytes to *int64.
func
IntegerPtr
(
b
[]
byte
)
*
int64
{
i
,
_
:=
strconv
.
ParseInt
(
string
(
b
),
10
,
len
(
b
)
*
8
)
return
&
i
}
//StringPtr bytes to *string.
func
StringPtr
(
b
[]
byte
)
*
string
{
s
:=
string
(
b
)
return
&
s
}
//Merge merge table options.
func
(
o
TableOption
)
Merge
(
n
TableOption
)
TableOption
{
o
.
Engine
=
append
(
o
.
Engine
,
n
.
Engine
...
)
o
.
Charset
=
append
(
o
.
Charset
,
n
.
Charset
...
)
o
.
Comment
=
append
(
o
.
Comment
,
n
.
Comment
...
)
o
.
Common
=
append
(
o
.
Common
,
' '
)
o
.
Common
=
append
(
o
.
Common
,
n
.
Common
...
)
o
.
Collate
=
append
(
o
.
Collate
,
n
.
Collate
...
)
if
n
.
AutoIncrement
!=
nil
{
o
.
AutoIncrement
=
n
.
AutoIncrement
}
return
o
}
func
(
o
TableOption
)
Format
(
buf
*
TrackedBuffer
) {
//ENGINE=InnoDB AUTO_INCREMENT=41 DEFAULT CHARSET=utf8;
if
len
(
o
.
Engine
)
>
0
{
buf
.
Myprintf
(
"ENGINE=%s "
,
o
.
Engine
)
}
if
o
.
AutoIncrement
!=
nil
{
fmt
.
Fprintf
(
buf
,
"AUTO_INCREMENT=%v "
,
*
o
.
AutoIncrement
)
}
if
len
(
o
.
Charset
)
>
0
{
buf
.
Myprintf
(
"DEFAULT CHARSET=%s "
,
o
.
Charset
)
}
if
len
(
o
.
Collate
)
>
0
{
buf
.
Myprintf
(
"DEFAULT COLLATE=%s "
,
o
.
Collate
)
}
if
len
(
o
.
Comment
)
>
0
{
buf
.
Myprintf
(
"Comment='%s' "
,
o
.
Comment
)
}
if
len
(
o
.
Common
)
>
0
{
buf
.
Myprintf
(
"%s "
,
o
.
Common
)
}
}
// Format formats the node.
func
(
ts
*
TableSpec
)
Format
(
buf
*
TrackedBuffer
) {
buf
.
Myprintf
(
"(
\n
"
)
for
i
,
col
:=
range
ts
.
Columns
{
if
i
==
0
{
buf
.
Myprintf
(
"
\t
%v"
,
col
)
}
else
{
buf
.
Myprintf
(
",
\n
\t
%v"
,
col
)
}
}
for
_
,
idx
:=
range
ts
.
Indexes
{
buf
.
Myprintf
(
",
\n
\t
%v"
,
idx
)
}
buf
.
Myprintf
(
"
\n
)"
)
ts
.
Option
.
Format
(
buf
)
}
// AddColumn appends the given column to the list in the spec
func
(
ts
*
TableSpec
)
AddColumn
(
cd
*
ColumnDefinition
) {
ts
.
Columns
=
append
(
ts
.
Columns
,
cd
)
}
// AddIndex appends the given index to the list in the spec
func
(
ts
*
TableSpec
)
AddIndex
(
id
*
IndexDefinition
) {
ts
.
Indexes
=
append
(
ts
.
Indexes
,
id
)
}
// WalkSubtree walks the nodes of the subtree.
func
(
ts
*
TableSpec
)
WalkSubtree
(
visit
Visit
)
error
{
if
ts
==
nil
{
return
nil
}
for
_
,
n
:=
range
ts
.
Columns
{
if
err
:=
Walk
(
visit
,
n
);
err
!=
nil
{
return
err
}
}
for
_
,
n
:=
range
ts
.
Indexes
{
if
err
:=
Walk
(
visit
,
n
);
err
!=
nil
{
return
err
}
}
return
nil
}
type
columnOption
struct
{
key
string
value
interface
{}
}
// ColumnDefinition describes a column in a CREATE TABLE statement
type
ColumnDefinition
struct
{
Name
ColIdent
Type
ColumnType
options
[]
columnOption
}
func
(
col
*
ColumnDefinition
)
mergeOptions
() {
for
_
,
opt
:=
range
col
.
options
{
switch
opt
.
key
{
case
"null_opt"
:
col
.
Type
.
NotNull
=
opt
.
value
.(
BoolVal
)
case
"column_default_opt"
:
col
.
Type
.
Default
=
opt
.
value
.(
*
SQLVal
)
case
"on_update_opt"
:
col
.
Type
.
OnUpdate
=
opt
.
value
.(
*
SQLVal
)
case
"auto_increment_opt"
:
col
.
Type
.
Autoincrement
=
opt
.
value
.(
BoolVal
)
case
"column_key_opt"
:
col
.
Type
.
KeyOpt
=
opt
.
value
.(
ColumnKeyOption
)
case
"column_comment_opt"
:
col
.
Type
.
Comment
=
opt
.
value
.(
*
SQLVal
)
case
"charset_opt"
:
col
.
Type
.
Charset
=
opt
.
value
.(
string
)
case
"collate_opt"
:
col
.
Type
.
Collate
=
opt
.
value
.(
string
)
}
}
}
// Format formats the node.
func
(
col
*
ColumnDefinition
)
Format
(
buf
*
TrackedBuffer
) {
buf
.
Myprintf
(
"%v %v"
,
col
.
Name
,
&
col
.
Type
)
}
// WalkSubtree walks the nodes of the subtree.
func
(
col
*
ColumnDefinition
)
WalkSubtree
(
visit
Visit
)
error
{
if
col
==
nil
{
return
nil
}
return
Walk
(
visit
,
col
.
Name
,
&
col
.
Type
,
)
}
// ColumnType represents a sql type in a CREATE TABLE statement
// All optional fields are nil if not specified
type
ColumnType
struct
{
// The base type string
Type
string
// Generic field options.
NotNull
BoolVal
Autoincrement
BoolVal
Default
*
SQLVal
OnUpdate
*
SQLVal
Comment
*
SQLVal
// Numeric field options
Length
*
SQLVal
Unsigned
BoolVal
View remainder of file in raw view
Footer
© 2026 GitHub, Inc.
Footer navigation
Terms
Privacy
Security
Status
Community
Docs
Contact
You can’t perform that action at this time.
Back
|
FazBrowse Home
|
New Git URL