forked from schemalex/schemalex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.go
More file actions
1464 lines (1340 loc) · 36.4 KB
/
parser.go
File metadata and controls
1464 lines (1340 loc) · 36.4 KB
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 schemalex
import (
"context"
"io/ioutil"
"strings"
"github.com/schemalex/schemalex/internal/errors"
"github.com/schemalex/schemalex/model"
)
const (
coloptSize = 1 << iota
coloptDecimalSize
coloptDecimalOptionalSize
coloptUnsigned
coloptZerofill
coloptBinary
coloptCharacterSet
coloptCollate
coloptEnumValues
coloptSetValues
// Everything else, meaning after this position, you can put anything
// you want. e.g. these are allowed
// * INT(11) COMMENT 'foo' NOT NULL PRIMARY KEY AUTO_INCREMENT
// * INT(11) AUTO_INCREMENT NOT NULL DEFAULT 1
// But this needs to be an error
// * COMMENT 'foo' INT(11) NOT NULL
coloptEverythingElse
coloptNull = coloptEverythingElse
coloptDefault = coloptEverythingElse
coloptAutoIncrement = coloptEverythingElse
coloptKey = coloptEverythingElse
coloptComment = coloptEverythingElse
)
const (
coloptFlagNone = 0
coloptFlagDigit = coloptSize | coloptUnsigned | coloptZerofill
coloptFlagDecimal = coloptDecimalSize | coloptUnsigned | coloptZerofill
coloptFlagDecimalOptional = coloptDecimalOptionalSize | coloptUnsigned | coloptZerofill
coloptFlagTime = coloptSize
coloptFlagChar = coloptSize | coloptBinary | coloptCharacterSet | coloptCollate
coloptFlagBinary = coloptSize
coloptFlagEnum = coloptEnumValues
coloptFlagSet = coloptSetValues
)
// Parser is responsible to parse a set of SQL statements
type Parser struct{}
// New creates a new Parser
func New() *Parser {
return &Parser{}
}
type parseCtx struct {
context.Context
input []byte
lexsrc chan *Token
peekCount int
peekTokens [3]*Token
}
func newParseCtx(ctx context.Context) *parseCtx {
return &parseCtx{
Context: ctx,
peekCount: -1,
}
}
var eofToken = Token{Type: EOF}
// peek the next token. this operation fills the peekTokens
// buffer. `next()` is a combination of peek+advance.
//
// note: we do NOT check for peekCout > 2 for efficiency.
// if you do that, you're f*cked.
func (pctx *parseCtx) peek() *Token {
if pctx.peekCount < 0 {
select {
case <-pctx.Context.Done():
return &eofToken
case t, ok := <-pctx.lexsrc:
if !ok {
return &eofToken
}
pctx.peekCount++
pctx.peekTokens[pctx.peekCount] = t
}
}
return pctx.peekTokens[pctx.peekCount]
}
func (pctx *parseCtx) advance() {
if pctx.peekCount >= 0 {
pctx.peekCount--
}
}
func (pctx *parseCtx) rewind() {
if pctx.peekCount < 2 {
pctx.peekCount++
}
}
func (pctx *parseCtx) next() *Token {
t := pctx.peek()
pctx.advance()
return t
}
// ParseFile parses a file containing SQL statements and creates
// a mode.Stmts structure.
// See Parse for details.
func (p *Parser) ParseFile(fn string) (model.Stmts, error) {
src, err := ioutil.ReadFile(fn)
if err != nil {
return nil, errors.Wrapf(err, `failed to open file %s`, fn)
}
stmts, err := p.Parse(src)
if err != nil {
if pe, ok := err.(*parseError); ok {
pe.file = fn
}
return nil, err
}
return stmts, nil
}
// ParseString parses a string containing SQL statements and creates
// a mode.Stmts structure.
// See Parse for details.
func (p *Parser) ParseString(src string) (model.Stmts, error) {
return p.Parse([]byte(src))
}
// Parse parses the given set of SQL statements and creates a
// model.Stmts structure.
// If it encounters errors while parsing, the returned error will be a
// ParseError type.
func (p *Parser) Parse(src []byte) (model.Stmts, error) {
cctx, cancel := context.WithCancel(context.TODO())
defer cancel()
ctx := newParseCtx(cctx)
ctx.input = src
ctx.lexsrc = lex(cctx, src)
var stmts model.Stmts
LOOP:
for {
ctx.skipWhiteSpaces()
switch t := ctx.peek(); t.Type {
case CREATE:
stmt, err := p.parseCreate(ctx)
if err != nil {
if errors.IsIgnorable(err) {
// this is ignorable.
continue
}
if pe, ok := err.(ParseError); ok {
return nil, pe
}
return nil, errors.Wrap(err, `failed to parse create`)
}
stmts = append(stmts, stmt)
case COMMENT_IDENT:
ctx.advance()
case DROP, SET, USE:
// We don't do anything about these
S1:
for {
switch t := ctx.peek(); t.Type {
case SEMICOLON:
ctx.advance()
fallthrough
case EOF:
break S1
default:
ctx.advance()
}
}
case SEMICOLON:
// you could have statements where it's just empty, followed by a
// semicolon. These are just empty lines, so we just skip and go
// process the next statement
ctx.advance()
continue
case EOF:
ctx.advance()
break LOOP
default:
return nil, newParseError(ctx, t, "expected CREATE, COMMENT_IDENT, SEMICOLON or EOF")
}
}
return stmts, nil
}
func (p *Parser) parseCreate(ctx *parseCtx) (model.Stmt, error) {
if t := ctx.next(); t.Type != CREATE {
return nil, errors.New(`expected CREATE`)
}
ctx.skipWhiteSpaces()
switch t := ctx.peek(); t.Type {
case DATABASE:
if _, err := p.parseCreateDatabase(ctx); err != nil {
return nil, err
}
return nil, errors.Ignorable(nil)
case TABLE:
return p.parseCreateTable(ctx)
default:
return nil, newParseError(ctx, t, "expected DATABASE or TABLE")
}
}
// https://dev.mysql.com/doc/refman/5.5/en/create-database.html
// TODO: charset, collation
func (p *Parser) parseCreateDatabase(ctx *parseCtx) (model.Database, error) {
if t := ctx.next(); t.Type != DATABASE {
return nil, errors.New(`expected DATABASE`)
}
ctx.skipWhiteSpaces()
var notexists bool
if ctx.peek().Type == IF {
ctx.advance()
if _, err := p.parseIdents(ctx, NOT, EXISTS); err != nil {
return nil, err
}
notexists = true
}
ctx.skipWhiteSpaces()
var database model.Database
switch t := ctx.next(); t.Type {
case IDENT, BACKTICK_IDENT:
database = model.NewDatabase(t.Value)
default:
return nil, newParseError(ctx, t, "expected IDENT, BACKTICK_IDENT")
}
database.SetIfNotExists(notexists)
p.eol(ctx)
return database, nil
}
// http://dev.mysql.com/doc/refman/5.6/en/create-table.html
func (p *Parser) parseCreateTable(ctx *parseCtx) (model.Table, error) {
if t := ctx.next(); t.Type != TABLE {
return nil, errors.New(`expected TABLE`)
}
var table model.Table
ctx.skipWhiteSpaces()
var temporary bool
if t := ctx.peek(); t.Type == TEMPORARY {
ctx.advance()
ctx.skipWhiteSpaces()
temporary = true
}
// IF NOT EXISTS
var notexists bool
if ctx.peek().Type == IF {
ctx.advance()
if _, err := p.parseIdents(ctx, NOT, EXISTS); err != nil {
return nil, err
}
ctx.skipWhiteSpaces()
notexists = true
}
switch t := ctx.next(); t.Type {
case IDENT, BACKTICK_IDENT:
table = model.NewTable(t.Value)
default:
return nil, newParseError(ctx, t, "expected IDENT or BACKTICK_IDENT")
}
table.SetTemporary(temporary)
table.SetIfNotExists(notexists)
ctx.skipWhiteSpaces()
switch t := ctx.peek(); t.Type {
case LIKE:
// CREATE TABLE foo LIKE bar
ctx.advance()
ctx.skipWhiteSpaces()
switch t := ctx.next(); t.Type {
case IDENT, BACKTICK_IDENT:
table.SetLikeTable(t.Value)
default:
return nil, newParseError(ctx, t, "expected table name after LIKE")
}
ctx.skipWhiteSpaces()
switch t := ctx.peek(); t.Type {
case EOF, SEMICOLON:
ctx.advance()
}
return table, nil
case IF:
ctx.advance()
if _, err := p.parseIdents(ctx, NOT, EXISTS); err != nil {
return nil, newParseError(ctx, t, "should NOT EXISTS")
}
ctx.skipWhiteSpaces()
table.SetIfNotExists(true)
}
if t := ctx.next(); t.Type != LPAREN {
return nil, newParseError(ctx, t, "expected LPAREN")
}
if err := p.parseCreateTableFields(ctx, table); err != nil {
return nil, err
}
table, _ = table.Normalize()
return table, nil
}
// Start parsing after `CREATE TABLE *** (`
func (p *Parser) parseCreateTableFields(ctx *parseCtx, stmt model.Table) error {
for {
ctx.skipWhiteSpaces()
switch t := ctx.peek(); t.Type {
case CONSTRAINT:
if err := p.parseTableConstraint(ctx, stmt); err != nil {
return err
}
case PRIMARY:
if err := p.parseTablePrimaryKey(ctx, stmt); err != nil {
return err
}
case UNIQUE:
if err := p.parseTableUniqueKey(ctx, stmt); err != nil {
return err
}
case INDEX, KEY:
// TODO. separate to KEY and INDEX
if err := p.parseTableIndex(ctx, stmt); err != nil {
return err
}
case FULLTEXT:
if err := p.parseTableFulltextIndex(ctx, stmt); err != nil {
return err
}
case SPATIAL:
if err := p.parseTableSpatialIndex(ctx, stmt); err != nil {
return err
}
case FOREIGN:
if err := p.parseTableForeignKey(ctx, stmt); err != nil {
return err
}
case CHECK: // TODO
return newParseError(ctx, t, "unsupported field: CHECK")
case IDENT, BACKTICK_IDENT:
if err := p.parseTableColumn(ctx, stmt); err != nil {
return err
}
default:
return newParseError(ctx, t, "unexpected create table field token: %s", t.Type)
}
ctx.skipWhiteSpaces()
switch t := ctx.peek(); t.Type {
case RPAREN:
ctx.advance()
if err := p.parseCreateTableOptions(ctx, stmt); err != nil {
return err
}
// partition option
if !p.eol(ctx) {
return newParseError(ctx, t, "expected EOL")
}
return nil
case COMMA:
ctx.advance()
// Expecting another table field, keep looping
default:
return newParseError(ctx, t, "expected RPAREN or COMMA")
}
}
}
func (p *Parser) parseTableConstraint(ctx *parseCtx, table model.Table) error {
if t := ctx.next(); t.Type != CONSTRAINT {
return newParseError(ctx, t, "expected CONSTRAINT")
}
ctx.skipWhiteSpaces()
var sym string
switch t := ctx.peek(); t.Type {
case IDENT, BACKTICK_IDENT:
// TODO: should be smarter
// (lestrrat): I don't understand. How?
sym = t.Value
ctx.advance()
ctx.skipWhiteSpaces()
}
var index model.Index
switch t := ctx.peek(); t.Type {
case PRIMARY:
index = model.NewIndex(model.IndexKindPrimaryKey, table.ID())
if err := p.parseColumnIndexPrimaryKey(ctx, index); err != nil {
return err
}
case UNIQUE:
index = model.NewIndex(model.IndexKindUnique, table.ID())
if err := p.parseColumnIndexUniqueKey(ctx, index); err != nil {
return err
}
case FOREIGN:
index = model.NewIndex(model.IndexKindForeignKey, table.ID())
if err := p.parseColumnIndexForeignKey(ctx, index); err != nil {
return err
}
default:
return newParseError(ctx, t, "not supported")
}
if len(sym) > 0 {
index.SetSymbol(sym)
}
table.AddIndex(index)
return nil
}
func (p *Parser) parseTablePrimaryKey(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindPrimaryKey, table.ID())
if err := p.parseColumnIndexPrimaryKey(ctx, index); err != nil {
return err
}
table.AddIndex(index)
return nil
}
func (p *Parser) parseTableUniqueKey(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindUnique, table.ID())
if err := p.parseColumnIndexUniqueKey(ctx, index); err != nil {
return err
}
table.AddIndex(index)
return nil
}
func (p *Parser) parseTableIndex(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindNormal, table.ID())
if err := p.parseColumnIndexKey(ctx, index); err != nil {
return err
}
table.AddIndex(index)
return nil
}
func (p *Parser) parseTableFulltextIndex(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindFullText, table.ID())
if err := p.parseColumnIndexFullTextKey(ctx, index); err != nil {
return err
}
table.AddIndex(index)
return nil
}
func (p *Parser) parseTableSpatialIndex(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindSpatial, table.ID())
if err := p.parseColumnIndexSpatialKey(ctx, index); err != nil {
return err
}
table.AddIndex(index)
return nil
}
func (p *Parser) parseTableForeignKey(ctx *parseCtx, table model.Table) error {
index := model.NewIndex(model.IndexKindForeignKey, table.ID())
if err := p.parseColumnIndexForeignKey(ctx, index); err != nil {
return err
}
table.AddIndex(index)
return nil
}
func (p *Parser) parseTableColumn(ctx *parseCtx, table model.Table) error {
t := ctx.next()
switch t.Type {
case IDENT, BACKTICK_IDENT:
default:
return newParseError(ctx, t, "expcted IDENT or BACKTICK_IDENT")
}
col := model.NewTableColumn(t.Value)
if err := p.parseTableColumnSpec(ctx, col); err != nil {
return err
}
table.AddColumn(col)
return nil
}
func (p *Parser) parseTableColumnSpec(ctx *parseCtx, col model.TableColumn) error {
var coltyp model.ColumnType
var colopt int
ctx.skipWhiteSpaces()
switch t := ctx.next(); t.Type {
case BIT:
coltyp = model.ColumnTypeBit
colopt = coloptSize
case TINYINT:
coltyp = model.ColumnTypeTinyInt
colopt = coloptFlagDigit
case SMALLINT:
coltyp = model.ColumnTypeSmallInt
colopt = coloptFlagDigit
case MEDIUMINT:
coltyp = model.ColumnTypeMediumInt
colopt = coloptFlagDigit
case INT:
coltyp = model.ColumnTypeInt
colopt = coloptFlagDigit
case INTEGER:
coltyp = model.ColumnTypeInteger
colopt = coloptFlagDigit
case BIGINT:
coltyp = model.ColumnTypeBigInt
colopt = coloptFlagDigit
case REAL:
coltyp = model.ColumnTypeReal
colopt = coloptFlagDecimal
case DOUBLE:
coltyp = model.ColumnTypeDouble
colopt = coloptFlagDecimal
case FLOAT:
coltyp = model.ColumnTypeFloat
colopt = coloptFlagDecimal
case DECIMAL:
coltyp = model.ColumnTypeDecimal
colopt = coloptFlagDecimalOptional
case NUMERIC:
coltyp = model.ColumnTypeNumeric
colopt = coloptFlagDecimalOptional
case DATE:
coltyp = model.ColumnTypeDate
colopt = coloptFlagNone
case TIME:
coltyp = model.ColumnTypeTime
colopt = coloptFlagTime
case TIMESTAMP:
coltyp = model.ColumnTypeTimestamp
colopt = coloptFlagTime
case DATETIME:
coltyp = model.ColumnTypeDateTime
colopt = coloptFlagTime
case YEAR:
coltyp = model.ColumnTypeYear
colopt = coloptFlagNone
case CHAR:
coltyp = model.ColumnTypeChar
colopt = coloptFlagChar
case VARCHAR:
coltyp = model.ColumnTypeVarChar
colopt = coloptFlagChar
case BINARY:
coltyp = model.ColumnTypeBinary
colopt = coloptFlagBinary
case VARBINARY:
coltyp = model.ColumnTypeVarBinary
colopt = coloptFlagBinary
case TINYBLOB:
coltyp = model.ColumnTypeTinyBlob
colopt = coloptFlagNone
case BLOB:
coltyp = model.ColumnTypeBlob
colopt = coloptFlagNone
case MEDIUMBLOB:
coltyp = model.ColumnTypeMediumBlob
colopt = coloptFlagNone
case LONGBLOB:
coltyp = model.ColumnTypeLongBlob
colopt = coloptFlagNone
case TINYTEXT:
coltyp = model.ColumnTypeTinyText
colopt = coloptFlagChar
case TEXT:
coltyp = model.ColumnTypeText
colopt = coloptFlagChar
case MEDIUMTEXT:
coltyp = model.ColumnTypeMediumText
colopt = coloptFlagChar
case LONGTEXT:
coltyp = model.ColumnTypeLongText
colopt = coloptFlagChar
case ENUM:
coltyp = model.ColumnTypeEnum
colopt = coloptFlagEnum
case SET:
coltyp = model.ColumnTypeSet
colopt = coloptFlagSet
case BOOLEAN:
coltyp = model.ColumnTypeBoolean
colopt = coloptFlagNone
case BOOL:
coltyp = model.ColumnTypeBool
colopt = coloptFlagNone
case JSON:
coltyp = model.ColumnTypeJSON
colopt = coloptFlagNone
case GEOMETRY:
coltyp = model.ColumnTypeGEOMETRY
colopt = coloptFlagNone
default:
return newParseError(ctx, t, "unsupported type in column specification")
}
col.SetType(coltyp)
return p.parseColumnOption(ctx, col, colopt)
}
func (p *Parser) parseCreateTableOptionValue(ctx *parseCtx, table model.Table, name string, follow ...TokenType) error {
ctx.skipWhiteSpaces()
if t := ctx.peek(); t.Type == EQUAL {
ctx.advance()
ctx.skipWhiteSpaces()
}
t := ctx.next()
for _, typ := range follow {
if typ != t.Type {
continue
}
var quotes bool
switch t.Type {
case SINGLE_QUOTE_IDENT, DOUBLE_QUOTE_IDENT:
quotes = true
}
table.AddOption(model.NewTableOption(name, t.Value, quotes))
return nil
}
return newParseError(ctx, t, "expected %v", follow)
}
func (p *Parser) parseCreateTableOptions(ctx *parseCtx, table model.Table) error {
ctx.skipWhiteSpaces()
switch t := ctx.peek(); t.Type {
case EOF:
// no table options, end of input
ctx.advance()
return nil
case SEMICOLON:
// no table options, end of statement
return nil
}
for {
ctx.skipWhiteSpaces()
switch t := ctx.next(); t.Type {
case ENGINE:
if err := p.parseCreateTableOptionValue(ctx, table, "ENGINE", IDENT, BACKTICK_IDENT); err != nil {
return err
}
case AUTO_INCREMENT:
if err := p.parseCreateTableOptionValue(ctx, table, "AUTO_INCREMENT", NUMBER); err != nil {
return err
}
case AVG_ROW_LENGTH:
if err := p.parseCreateTableOptionValue(ctx, table, "AVG_ROW_LENGTH", NUMBER); err != nil {
return err
}
case DEFAULT:
var name string
ctx.skipWhiteSpaces()
switch t := ctx.next(); t.Type {
case CHARSET:
name = "DEFAULT CHARACTER SET"
case CHARACTER:
ctx.skipWhiteSpaces()
if t := ctx.next(); t.Type != SET {
return newParseError(ctx, t, "expected SET")
}
name = "DEFAULT CHARACTER SET"
case COLLATE:
name = "DEFAULT COLLATE"
default:
return newParseError(ctx, t, "expected CHARACTER or COLLATE")
}
if err := p.parseCreateTableOptionValue(ctx, table, name, IDENT, BACKTICK_IDENT); err != nil {
return err
}
case CHARACTER:
ctx.skipWhiteSpaces()
if t := ctx.next(); t.Type != SET {
return newParseError(ctx, t, "expected SET")
}
if err := p.parseCreateTableOptionValue(ctx, table, "DEFAULT CHARACTER SET", IDENT, BACKTICK_IDENT); err != nil {
return err
}
case COLLATE:
if err := p.parseCreateTableOptionValue(ctx, table, "DEFAULT COLLATE", IDENT, BACKTICK_IDENT); err != nil {
return err
}
case CHECKSUM:
if err := p.parseCreateTableOptionValue(ctx, table, "CHECKSUM", NUMBER); err != nil {
return err
}
case COMMENT:
if err := p.parseCreateTableOptionValue(ctx, table, "COMMENT", SINGLE_QUOTE_IDENT, DOUBLE_QUOTE_IDENT); err != nil {
return err
}
case CONNECTION:
if err := p.parseCreateTableOptionValue(ctx, table, "CONNECTION", SINGLE_QUOTE_IDENT, DOUBLE_QUOTE_IDENT); err != nil {
return err
}
case DATA:
ctx.skipWhiteSpaces()
if t := ctx.next(); t.Type != DIRECTORY {
return newParseError(ctx, t, "expected DIRECTORY")
}
if err := p.parseCreateTableOptionValue(ctx, table, "DATA DIRECTORY", SINGLE_QUOTE_IDENT, DOUBLE_QUOTE_IDENT); err != nil {
return err
}
case DELAY_KEY_WRITE:
if err := p.parseCreateTableOptionValue(ctx, table, "DATA_KEY_WRITE", NUMBER); err != nil {
return err
}
case INDEX:
ctx.skipWhiteSpaces()
if t := ctx.next(); t.Type != DIRECTORY {
return newParseError(ctx, t, "should DIRECTORY")
}
if err := p.parseCreateTableOptionValue(ctx, table, "INDEX DIRECTORY", SINGLE_QUOTE_IDENT, DOUBLE_QUOTE_IDENT); err != nil {
return err
}
case INSERT_METHOD:
if err := p.parseCreateTableOptionValue(ctx, table, "INSERT_METHOD", IDENT); err != nil {
return err
}
case KEY_BLOCK_SIZE:
if err := p.parseCreateTableOptionValue(ctx, table, "KEY_BLOCK_SIZE", NUMBER); err != nil {
return err
}
case MAX_ROWS:
if err := p.parseCreateTableOptionValue(ctx, table, "MAX_ROWS", NUMBER); err != nil {
return err
}
case MIN_ROWS:
if err := p.parseCreateTableOptionValue(ctx, table, "MIN_ROWS", NUMBER); err != nil {
return err
}
case PACK_KEYS:
if err := p.parseCreateTableOptionValue(ctx, table, "PACK_KEYS", NUMBER, IDENT); err != nil {
return err
}
case PASSWORD:
if err := p.parseCreateTableOptionValue(ctx, table, "PASSWORD", SINGLE_QUOTE_IDENT, DOUBLE_QUOTE_IDENT); err != nil {
return err
}
case ROW_FORMAT:
if err := p.parseCreateTableOptionValue(ctx, table, "ROW_FORMAT", DEFAULT, DYNAMIC, FIXED, COMPRESSED, REDUNDANT, COMPACT); err != nil {
return err
}
case STATS_AUTO_RECALC:
if err := p.parseCreateTableOptionValue(ctx, table, "STATS_AUTO_RECALC", NUMBER, DEFAULT); err != nil {
return err
}
case STATS_PERSISTENT:
if err := p.parseCreateTableOptionValue(ctx, table, "STATS_PERSISTENT", NUMBER, DEFAULT); err != nil {
return err
}
case STATS_SAMPLE_PAGES:
if err := p.parseCreateTableOptionValue(ctx, table, "STATS_SAMPLE_PAGES", NUMBER); err != nil {
return err
}
case TABLESPACE:
return newParseError(ctx, t, "unsupported option TABLESPACE")
case UNION:
return newParseError(ctx, t, "unsupported option UNION")
case COMMA:
// no op, continue to next option
continue
default:
return newParseError(ctx, t, "unexpected token in table options: "+t.Type.String())
}
ctx.skipWhiteSpaces()
// except for the case where we continue to the next option (COMMA)
// we should expect the end of this statement
switch t := ctx.peek(); t.Type {
case EOF:
// end of table options, end of input
ctx.advance()
return nil
case SEMICOLON:
// end of table options, end of statement
return nil
}
}
}
// parse column options
//
// Also see: https://github.com/schemalex/schemalex/pull/40
// Seems like MySQL doesn't really care about the order of some elements in the
// column options, although the docs (https://dev.mysql.com/doc/refman/5.7/en/create-table.html)
// seem to state otherwise.
//
func (p *Parser) parseColumnOption(ctx *parseCtx, col model.TableColumn, f int) error {
f = f | coloptNull | coloptDefault | coloptAutoIncrement | coloptKey | coloptComment
pos := 0
check := func(_f int) bool {
if pos > _f {
return false
}
if f|_f != f {
return false
}
pos = _f
return true
}
for {
ctx.skipWhiteSpaces()
switch t := ctx.next(); t.Type {
case LPAREN:
if check(coloptSize) {
ctx.skipWhiteSpaces()
t := ctx.next()
if t.Type != NUMBER {
return newParseError(ctx, t, "expected NUMBER (column size)")
}
tlen := t.Value
ctx.skipWhiteSpaces()
t = ctx.next()
if t.Type != RPAREN {
return newParseError(ctx, t, "expected RPAREN (column size)")
}
col.SetLength(model.NewLength(tlen))
} else if check(coloptDecimalSize) {
strs, err := p.parseIdents(ctx, NUMBER, COMMA, NUMBER, RPAREN)
if err != nil {
return err
}
l := model.NewLength(strs[0])
l.SetDecimal(strs[2])
col.SetLength(l)
} else if check(coloptDecimalOptionalSize) {
ctx.skipWhiteSpaces()
t := ctx.next()
if t.Type != NUMBER {
return newParseError(ctx, t, "expected NUMBER (decimal size `M`)")
}
tlen := t.Value
ctx.skipWhiteSpaces()
t = ctx.next()
if t.Type == RPAREN {
col.SetLength(model.NewLength(tlen))
continue
} else if t.Type != COMMA {
return newParseError(ctx, t, "expected COMMA (decimal size)")
}
ctx.skipWhiteSpaces()
t = ctx.next()
if t.Type != NUMBER {
return newParseError(ctx, t, "expected NUMBER (decimal size `D`)")
}
tscale := t.Value
ctx.skipWhiteSpaces()
if t := ctx.next(); t.Type != RPAREN {
return newParseError(ctx, t, "expected RPAREN (decimal size)")
}
l := model.NewLength(tlen)
l.SetDecimal(tscale)
col.SetLength(l)
} else if check(coloptEnumValues) {
ctx.parseSetOrEnum(col.SetEnumValues)
} else if check(coloptSetValues) {
ctx.parseSetOrEnum(col.SetSetValues)
} else {
return newParseError(ctx, t, "cannot apply coloptSize, coloptDecimalSize, coloptDecimalOptionalSize, coloptEnumValues, coloptSetValues")
}
case CHARACTER:
ctx.skipWhiteSpaces()
if t := ctx.next(); t.Type != SET {
return newParseError(ctx, t, "expected SET")
}
ctx.skipWhiteSpaces()
v := ctx.next()
col.SetCharacterSet(v.Value)
case COLLATE:
ctx.skipWhiteSpaces()
v := ctx.next()
col.SetCollation(v.Value)
case UNSIGNED:
if !check(coloptUnsigned) {
return newParseError(ctx, t, "cannot apply UNSIGNED")
}
col.SetUnsigned(true)
case ZEROFILL:
if !check(coloptZerofill) {
return newParseError(ctx, t, "cannot apply ZEROFILL")
}
col.SetZeroFill(true)
case BINARY:
if !check(coloptBinary) {
return newParseError(ctx, t, "cannot apply BINARY")
}
col.SetBinary(true)
case NOT:
if !check(coloptNull) {
return newParseError(ctx, t, "cannot apply NOT NULL")
}
ctx.skipWhiteSpaces()
switch t := ctx.next(); t.Type {
case NULL:
col.SetNullState(model.NullStateNotNull)
default:
return newParseError(ctx, t, "expected NULL")
}
case NULL:
if !check(coloptNull) {
return newParseError(ctx, t, "cannot apply NULL")
}
col.SetNullState(model.NullStateNull)
case ON:
// for now, only applicable to ON UPDATE ...
ctx.skipWhiteSpaces()
if t := ctx.next(); t.Type != UPDATE {
return newParseError(ctx, t, "expected ON UPDATE")
}
ctx.skipWhiteSpaces()
v := ctx.next()
col.SetAutoUpdate(v.Value)
case DEFAULT:
if !check(coloptDefault) {
return newParseError(ctx, t, "cannot apply DEFAULT")
}
ctx.skipWhiteSpaces()
switch t := ctx.next(); t.Type {
case IDENT, SINGLE_QUOTE_IDENT, DOUBLE_QUOTE_IDENT:
col.SetDefault(t.Value, true)
case NUMBER, CURRENT_TIMESTAMP, NULL, TRUE, FALSE:
col.SetDefault(strings.ToUpper(t.Value), false)
case NOW:
now := t.Value
if t := ctx.next(); t.Type != LPAREN {
return newParseError(ctx, t, "expected LPAREN")
}
if t := ctx.next(); t.Type != RPAREN {
return newParseError(ctx, t, "expected RPAREN")
}
col.SetDefault(strings.ToUpper(now)+"()", false)
default:
return newParseError(ctx, t, "expected IDENT, SINGLE_QUOTE_IDENT, DOUBLE_QUOTE_IDENT, NUMBER, CURRENT_TIMESTAMP, NULL")
}
case AUTO_INCREMENT:
if !check(coloptAutoIncrement) {
return newParseError(ctx, t, "cannot apply AUTO_INCREMENT")
}
col.SetAutoIncrement(true)
case UNIQUE:
if !check(coloptKey) {
return newParseError(ctx, t, "cannot apply UNIQUE KEY")
}
ctx.skipWhiteSpaces()
if t := ctx.peek(); t.Type == KEY {
ctx.advance()
}
col.SetUnique(true)
case KEY:
if !check(coloptKey) {
return newParseError(ctx, t, "cannot apply KEY")
}
col.SetKey(true)
case PRIMARY:
if !check(coloptKey) {
return newParseError(ctx, t, "cannot apply PRIMARY KEY")
}
ctx.skipWhiteSpaces()
if t := ctx.next(); t.Type != KEY {
return newParseError(ctx, t, "expected PRIMARY KEY")
}
col.SetPrimary(true)
case COMMENT:
if !check(coloptComment) {
return newParseError(ctx, t, "cannot apply COMMENT")
}