This repository has been archived by the owner on Jan 5, 2023. It is now read-only.
/
ast.go
1929 lines (1681 loc) · 43.6 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 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 (
"strings"
"github.com/xelabs/go-mysqlstack/sqlparser/depends/sqltypes"
)
// SQLNode defines the interface for all nodes
// generated by the parser.
type SQLNode interface {
Format(buf *TrackedBuffer)
}
// Statements.
type (
// Statement represents a statement.
Statement interface {
iStatement()
SQLNode
}
// SelectStatement any SELECT statement.
SelectStatement interface {
iSelectStatement()
iStatement()
iInsertRows()
AddOrder(*Order)
SetLimit(*Limit)
SQLNode
}
// Select represents a SELECT statement.
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
ForBackup string
}
// Union represents a UNION statement.
Union struct {
Type string
Left, Right SelectStatement
OrderBy OrderBy
Limit *Limit
Lock string
}
// ParenSelect is a parenthesized SELECT statement.
ParenSelect struct {
Select SelectStatement
}
// 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.
Insert struct {
Action string
Comments Comments
Ignore string
Table TableName
Columns Columns
Rows InsertRows
OnDup OnDup
}
// Update represents an UPDATE statement.
Update struct {
Comments Comments
Table TableName
Exprs UpdateExprs
Where *Where
OrderBy OrderBy
Limit *Limit
}
// Delete represents a DELETE statement.
Delete struct {
Comments Comments
Table TableName
Where *Where
OrderBy OrderBy
Limit *Limit
}
// Set represents a SET statement.
Set struct {
Comments Comments
Exprs SetExprs
}
// DDL represents a CREATE, ALTER, DROP or RENAME statement.
// Table is set for AlterStr, DropStr, RenameStr.
// NewName is set for AlterStr, CreateStr, RenameStr.
DDL struct {
Action string
Engine string
Charset string
IndexName string
PartitionName string
BackendName string
IfExists bool
IfNotExists bool
Table TableName
NewName TableName
Database TableIdent
DatabaseOptions DatabaseOptionListOpt
TableSpec *TableSpec
// [UNIQUE | FULLTEXT | SPATIAL] index.
IndexType string
IndexOpts *IndexOptions
// Tables is set if Action is DropStr.
Tables TableNames
// table column operation
DropColumnName string
ModifyColumnDef *ColumnDefinition
// Partition options.
PartitionOptions PartitionOptions
PartitionNum *SQLVal
}
// Show represents a show statement.
Show struct {
Type string
Full string
Table TableName
Database TableName
From string
Limit *Limit
Filter *ShowFilter
}
// Checksum represents a CHECKSUM statement.
Checksum struct {
Table TableName
}
// Use represents a use statement.
Use struct {
DBName TableIdent
}
// OtherRead represents a DESCRIBE, or EXPLAIN statement.
// It should be used only as an indicator. It does not contain
// the full AST for the statement.
OtherRead struct{}
// OtherAdmin represents a misc statement that relies on ADMIN privileges,
// such as REPAIR, OPTIMIZE, or TRUNCATE statement.
// It should be used only as an indicator. It does not contain
// the full AST for the statement.
OtherAdmin struct{}
// Radon represents the radon statement.
Radon struct {
Action string
Row ValTuple
Table TableName
NewName TableName
}
// Explain represents a explain statement.
Explain struct{}
// Kill represents a KILL statement.
Kill struct {
QueryID *NumVal
}
// Transaction represents the transaction tuple.
Transaction struct {
Action string
}
// Xa represents a XA statement.
Xa struct{}
)
// 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() {}
func (*Union) iStatement() {}
func (*Select) iStatement() {}
func (*Insert) iStatement() {}
func (*Update) iStatement() {}
func (*Delete) iStatement() {}
func (*Set) iStatement() {}
func (*DDL) iStatement() {}
func (*Show) iStatement() {}
func (*Checksum) iStatement() {}
func (*Use) iStatement() {}
func (*OtherRead) iStatement() {}
func (*OtherAdmin) iStatement() {}
func (*Radon) iStatement() {}
func (*Explain) iStatement() {}
func (*Kill) iStatement() {}
func (*Transaction) iStatement() {}
func (*Xa) iStatement() {}
func (*Select) iSelectStatement() {}
func (*Union) iSelectStatement() {}
func (*ParenSelect) iSelectStatement() {}
type (
// InsertRows represents the rows for an INSERT statement.
InsertRows interface {
iInsertRows()
SQLNode
}
// Values represents a VALUES clause.
Values []ValTuple
)
func (*Select) iInsertRows() {}
func (*Union) iInsertRows() {}
func (Values) iInsertRows() {}
func (*ParenSelect) iInsertRows() {}
type DatabaseOptionListOpt struct {
DBOptList []*DatabaseOption
}
// TableOptions is used by TableSpec
type TableOptions struct {
Comment string
Engine string
Charset string
Type string
AvgRowLength string
Checksum string
Collate string
Compression string
Connection string
DataDirectory string
IndexDirectory string
DelayKeyWrite string
Encryption string
InsertMethod string
KeyBlockSize string
MaxRows string
MinRows string
PackKeys string
Password string
RowFormat string
StatsAutoRecalc string
StatsPersistent string
StatsSamplePages string
TableSpace string
}
// IndexOptions is used by IndexOpts.
type IndexOptions struct {
Columns []*IndexColumn
Using string
Comment string
BlockSize *SQLVal
Parser string
Algorithm string
Lock string
}
// IndexDefinition describes an index in a CREATE TABLE statement
type IndexDefinition struct {
Type string
Name ColIdent
Opts *IndexOptions
Primary bool
Unique bool
}
// TableSpec describes the structure of a table from a CREATE TABLE statement
type TableSpec struct {
Columns []*ColumnDefinition
Indexes []*IndexDefinition
Options TableOptions
}
// ColumnDefinition describes a column in a CREATE TABLE statement
type ColumnDefinition struct {
Name ColIdent
Type ColumnType
}
// 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 string
Comment *SQLVal
Collate *SQLVal
ColumnFormat string
Storage string
// Numeric field options
Length *SQLVal
Unsigned BoolVal
Zerofill BoolVal
Scale *SQLVal
// Text field options
Charset string
// Enum values
EnumValues []string
// Key specification
PrimaryKeyOpt ColumnPrimaryKeyOption
UniqueKeyOpt ColumnUniqueKeyOption
}
// ShowFilter is show tables filter
type ShowFilter struct {
Like string
Filter Expr
}
// Comments represents a list of comments.
type Comments [][]byte
// SelectExprs represents SELECT expressions.
type SelectExprs []SelectExpr
type (
// SelectExpr represents a SELECT expression.
SelectExpr interface {
iSelectExpr()
SQLNode
// clone used to deep copy a new SelectExpr.
clone() SelectExpr
}
// StarExpr defines a '*' or 'table.*' expression.
StarExpr struct {
TableName TableName
}
// AliasedExpr defines an aliased SELECT expression.
AliasedExpr struct {
Expr Expr
As ColIdent
}
// Nextval defines the NEXT VALUE expression.
Nextval struct {
Expr Expr
}
)
func (*StarExpr) iSelectExpr() {}
func (*AliasedExpr) iSelectExpr() {}
func (Nextval) iSelectExpr() {}
// Columns represents an insert column list.
type Columns []ColIdent
// TableExprs represents a list of table expressions.
type TableExprs []TableExpr
type (
// TableExpr represents a table expression.
TableExpr interface {
iTableExpr()
SQLNode
}
// AliasedTableExpr represents a table expression
// coupled with an optional alias or index hint.
// If As is empty, no alias was used.
AliasedTableExpr struct {
Expr SimpleTableExpr
As TableIdent
Hints *IndexHints
}
// ParenTableExpr represents a parenthesized list of TableExpr.
ParenTableExpr struct {
Exprs TableExprs
}
// JoinTableExpr represents a TableExpr that's a JOIN operation.
JoinTableExpr struct {
LeftExpr TableExpr
Join string
RightExpr TableExpr
On Expr
}
)
func (*AliasedTableExpr) iTableExpr() {}
func (*ParenTableExpr) iTableExpr() {}
func (*JoinTableExpr) iTableExpr() {}
type (
// SimpleTableExpr represents a simple table expression.
SimpleTableExpr interface {
iSimpleTableExpr()
SQLNode
}
// TableName represents a table name.
// Qualifier, if specified, represents a database or keyspace.
// TableName is a value struct whose fields are case sensitive.
// This means two TableName vars can be compared for equality
// and a TableName can also be used as key in a map.
TableName struct {
Name, Qualifier TableIdent
}
// Subquery represents a subquery.
Subquery struct {
Select SelectStatement
}
)
func (TableName) iSimpleTableExpr() {}
func (*Subquery) iSimpleTableExpr() {}
// TableNames is a list of TableName.
type TableNames []TableName
// IndexHints represents a list of index hints.
type IndexHints struct {
Type string
Indexes []ColIdent
}
// Where represents a WHERE or HAVING clause.
type Where struct {
Type string
Expr Expr
}
// Exprs represents a list of value expressions.
// It's not a valid expression because it's not parenthesized.
type Exprs []Expr
// Expressions.
type (
// Expr represents an expression.
Expr interface {
iExpr()
SQLNode
// clone used to deep copy a new Expr.
clone() Expr
}
// AndExpr represents an AND expression.
AndExpr struct {
Left, Right Expr
}
// OrExpr represents an OR expression.
OrExpr struct {
Left, Right Expr
}
// NotExpr represents a NOT expression.
NotExpr struct {
Expr Expr
}
// ParenExpr represents a parenthesized boolean expression.
ParenExpr struct {
Expr Expr
}
// ComparisonExpr represents a two-value comparison expression.
ComparisonExpr struct {
Operator string
Left, Right Expr
Escape Expr
}
// RangeCond represents a BETWEEN or a NOT BETWEEN expression.
RangeCond struct {
Operator string
Left Expr
From, To Expr
}
// IsExpr represents an IS ... or an IS NOT ... expression.
IsExpr struct {
Operator string
Expr Expr
}
// ExistsExpr represents an EXISTS expression.
ExistsExpr struct {
Subquery *Subquery
}
// SQLVal represents a single value.
SQLVal struct {
Type ValType
Val []byte
}
// NullVal represents a NULL value.
NullVal struct{}
// BoolVal is true or false.
BoolVal bool
// ColName represents a column name.
ColName struct {
// Metadata is not populated by the parser.
// It's a placeholder for analyzers to store
// additional data, typically info about which
// table or column this node references.
Metadata interface{}
Name ColIdent
Qualifier TableName
}
// ColTuple represents a list of column values.
// It can be ValTuple, Subquery, ListArg.
ColTuple interface {
iColTuple()
Expr
}
// ValTuple represents a tuple of actual values.
ValTuple Exprs
// ListArg represents a named list argument.
ListArg []byte
// BinaryExpr represents a binary value expression.
BinaryExpr struct {
Operator string
Left, Right Expr
}
// UnaryExpr represents a unary value expression.
UnaryExpr struct {
Operator string
Expr Expr
}
// IntervalExpr represents a date-time INTERVAL expression.
IntervalExpr struct {
Expr Expr
Unit string
}
// CollateExpr represents dynamic collate operator.
CollateExpr struct {
Expr Expr
Charset string
}
// FuncExpr represents a function call.
FuncExpr struct {
Qualifier TableIdent
Name ColIdent
Distinct bool
Exprs SelectExprs
}
// GroupConcatExpr represents a call to GROUP_CONCAT
GroupConcatExpr struct {
Distinct string
Exprs SelectExprs
OrderBy OrderBy
Separator string
}
// ValuesFuncExpr represents a function call.
ValuesFuncExpr struct {
Name ColIdent
Resolved Expr
}
// ConvertExpr represents a call to CONVERT(expr, type)
// or it's equivalent CAST(expr AS type). Both are rewritten to the former.
ConvertExpr struct {
Expr Expr
Type *ConvertType
}
// ConvertUsingExpr represents a call to CONVERT(expr USING charset).
ConvertUsingExpr struct {
Expr Expr
Type string
}
// MatchExpr represents a call to the MATCH function
MatchExpr struct {
Columns SelectExprs
Expr Expr
Option string
}
// CaseExpr represents a CASE expression.
CaseExpr struct {
Expr Expr
Whens []*When
Else Expr
}
// Default represents a DEFAULT expression.
Default struct {
ColName string
}
)
func (*AndExpr) iExpr() {}
func (*OrExpr) iExpr() {}
func (*NotExpr) iExpr() {}
func (*ParenExpr) iExpr() {}
func (*ComparisonExpr) iExpr() {}
func (*RangeCond) iExpr() {}
func (*IsExpr) iExpr() {}
func (*ExistsExpr) iExpr() {}
func (*SQLVal) 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 (*IntervalExpr) iExpr() {}
func (*CollateExpr) iExpr() {}
func (*FuncExpr) iExpr() {}
func (*GroupConcatExpr) iExpr() {}
func (*CaseExpr) iExpr() {}
func (*ValuesFuncExpr) iExpr() {}
func (*ConvertExpr) iExpr() {}
func (*ConvertUsingExpr) iExpr() {}
func (*MatchExpr) iExpr() {}
func (*Default) iExpr() {}
func (ValTuple) iColTuple() {}
func (*Subquery) iColTuple() {}
func (ListArg) iColTuple() {}
// ConvertType represents the type in call to CONVERT(expr, type)
type ConvertType struct {
Type string
Length *SQLVal
Scale *SQLVal
Operator string
Charset string
}
// When represents a WHEN sub-expression.
type When struct {
Cond Expr
Val Expr
}
// GroupBy represents a GROUP BY clause.
type GroupBy Exprs
// OrderBy represents an ORDER By clause.
type OrderBy []*Order
// Order represents an ordering expression.
type Order struct {
Expr Expr
Direction string
}
// Limit represents a LIMIT clause.
type Limit struct {
Offset, Rowcount Expr
}
// UpdateExprs represents a list of update expressions.
type UpdateExprs []*UpdateExpr
// UpdateExpr represents an update expression.
type UpdateExpr struct {
Name *ColName
Expr Expr
}
// OnDup represents an ON DUPLICATE KEY clause.
type OnDup UpdateExprs
// SetExprs represents a list of set expressions.
type SetExprs []*SetExpr
// SetExpr represents a set expression.
type SetExpr struct {
// global|session
Scope string
Type ColIdent
Val SetVal
}
type (
// SetVal represents a set variable value.
SetVal interface {
SQLNode
iSetVal()
}
// OptVal represents the set variable value.
// See https://dev.mysql.com/doc/refman/5.7/en/set-variable.html
OptVal struct {
Value Expr
}
// TxnVal represents the set-transaction characteristic.
// See https://dev.mysql.com/doc/refman/5.7/en/set-transaction.html
TxnVal struct {
Level string
Mode string
}
)
func (*OptVal) iSetVal() {}
func (*TxnVal) iSetVal() {}
// ColIdent is a case insensitive SQL identifier. It will be escaped with
// backquotes if necessary.
type ColIdent struct {
// This artifact prevents this struct from being compared
// with itself. It consumes no space as long as it's not the
// last field in the struct.
_ [0]struct{ _ []byte }
val, lowered string
}
// TableIdent is a case sensitive SQL identifier. It will be escaped with
// backquotes if necessary.
type TableIdent struct {
v string
}
// 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)
}
// 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 *ParenSelect) Format(buf *TrackedBuffer) {
buf.Myprintf("(%v)", node.Select)
}
// 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 *Union) Format(buf *TrackedBuffer) {
buf.Myprintf("%v %s %v%v%v%s", node.Left, node.Type, node.Right,
node.OrderBy, node.Limit, node.Lock)
}
// 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 *Insert) Format(buf *TrackedBuffer) {
buf.Myprintf("%s %v%sinto %v%v %v%v",
node.Action,
node.Comments, node.Ignore,
node.Table, node.Columns, node.Rows, node.OnDup)
}
// 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)
}
// 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)
}
// Format formats the node.
func (node *Set) Format(buf *TrackedBuffer) {
buf.Myprintf("set %v%v", node.Comments, node.Exprs)
}
// Format formats the node.
func (node *Checksum) Format(buf *TrackedBuffer) {
buf.Myprintf("checksum table %v", node.Table)
}
// Format formats the node.
func (node *DDL) Format(buf *TrackedBuffer) {
switch node.Action {
case CreateDBStr:
ifnotexists := ""
if node.IfNotExists {
ifnotexists = " if not exists"
}
buf.Myprintf("%s%s %s", node.Action, ifnotexists, node.Database.String())
node.DatabaseOptions.Format(buf)
case DropDBStr:
exists := ""
if node.IfExists {
exists = " if exists"
}
buf.Myprintf("%s%s %s", node.Action, exists, node.Database.String())
case CreateTableStr:
ifnotexists := ""
if node.IfNotExists {
ifnotexists = " if not exists"
}
if node.TableSpec == nil {
buf.Myprintf("%s%s %v", node.Action, ifnotexists, node.NewName)
} else {
buf.Myprintf("%s%s %v %v", node.Action, ifnotexists, node.NewName, node.TableSpec)
}
case CreateIndexStr:
buf.Myprintf("create %s%s on %v%v", node.IndexType, node.IndexName, node.NewName, node.IndexOpts)
case DropTableStr:
exists := ""
if node.IfExists {
exists = " if exists"
}
buf.Myprintf("%s%s %v", node.Action, exists, node.Tables)
case DropIndexStr:
buf.Myprintf("%s %s on %v", node.Action, node.IndexName, node.Table)
case RenameStr:
buf.Myprintf("%s %v to %v", node.Action, node.Table, node.NewName)
case AlterStr:
buf.Myprintf("%s table %v", node.Action, node.NewName)
case AlterEngineStr:
buf.Myprintf("%s %v engine = %s", node.Action, node.NewName, node.Engine)
case AlterCharsetStr:
buf.Myprintf("alter table %v convert to character set %s", node.NewName, node.Charset)
case AlterAddColumnStr:
buf.Myprintf("alter table %v add column %v", node.NewName, node.TableSpec)
case AlterDropColumnStr:
buf.Myprintf("alter table %v drop column `%s`", node.NewName, node.DropColumnName)
case AlterModifyColumnStr:
buf.Myprintf("alter table %v modify column %v", node.NewName, node.ModifyColumnDef)
case TruncateTableStr:
buf.Myprintf("%s %v", node.Action, node.NewName)
}
}
// Format formats the node
func (optList DatabaseOptionListOpt) Format(buf *TrackedBuffer) {
for _, dbOpt := range optList.DBOptList {
buf.Myprintf(" %s %s", dbOpt.CharsetOrCollate, dbOpt.Value)
}
}
// Format formats the node.
func (opts TableOptions) Format(buf *TrackedBuffer) {
if opts.Comment != "" {
buf.Myprintf(" comment=%s", opts.Comment)
}
if opts.Engine != "" {
buf.Myprintf(" engine=%s", opts.Engine)
}
if opts.Charset != "" {
buf.Myprintf(" default charset=%s", opts.Charset)
}
if opts.AvgRowLength != "" {
buf.Myprintf(" avg_row_length=%s", opts.AvgRowLength)
}
if opts.Checksum != "" {
buf.Myprintf(" checksum=%s", opts.Checksum)
}
if opts.Collate != "" {
buf.Myprintf(" collate=%s", opts.Collate)
}
if opts.Compression != "" {
buf.Myprintf(" compression=%s", opts.Compression)
}
if opts.Connection != "" {
buf.Myprintf(" connection=%s", opts.Connection)
}
if opts.DataDirectory != "" {
buf.Myprintf(" data directory=%s", opts.DataDirectory)
}
if opts.IndexDirectory != "" {
buf.Myprintf(" index directory=%s", opts.IndexDirectory)
}
if opts.DelayKeyWrite != "" {
buf.Myprintf(" delay_key_write=%s", opts.DelayKeyWrite)
}
if opts.Encryption != "" {
buf.Myprintf(" encryption=%s", opts.Encryption)
}
if opts.InsertMethod != "" {
buf.Myprintf(" insert_method=%s", opts.InsertMethod)
}
if opts.InsertMethod != "" {
buf.Myprintf(" key_block_size=%s", opts.KeyBlockSize)
}
if opts.MaxRows != "" {
buf.Myprintf(" max_rows=%s", opts.MaxRows)
}
if opts.MinRows != "" {
buf.Myprintf(" min_rows=%s", opts.MinRows)
}
if opts.PackKeys != "" {
buf.Myprintf(" pack_keys=%s", opts.PackKeys)
}
if opts.Password != "" {
buf.Myprintf(" password=%s", opts.Password)
}
if opts.RowFormat != "" {
buf.Myprintf(" row_format=%s", opts.RowFormat)
}
if opts.StatsAutoRecalc != "" {
buf.Myprintf(" stats_auto_recalc=%s", opts.StatsAutoRecalc)
}
if opts.StatsPersistent != "" {
buf.Myprintf(" stats_persistent=%s", opts.StatsPersistent)
}
if opts.StatsSamplePages != "" {
buf.Myprintf(" stats_sample_pages=%s", opts.StatsSamplePages)
}
if opts.TableSpace != "" {
buf.Myprintf(" tablespace=%s", opts.TableSpace)
}
}
// Format formats the node.
func (opts *IndexOptions) Format(buf *TrackedBuffer) {
buf.Myprintf("(")
for i, col := range opts.Columns {
if i != 0 {
buf.Myprintf(", `%s`", col.Column.String())
} else {
buf.Myprintf("`%s`", col.Column.String())
}
if col.Length != nil {