-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathddl.go
More file actions
860 lines (761 loc) · 21.7 KB
/
Copy pathddl.go
File metadata and controls
860 lines (761 loc) · 21.7 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
// Package parser - ddl.go
// DDL statement parsing: CREATE, DROP, REFRESH for views, materialized views, tables, and indexes.
package parser
import (
"fmt"
"strings"
"github.com/ajitpratap0/GoSQLX/pkg/models"
"github.com/ajitpratap0/GoSQLX/pkg/sql/ast"
)
// isTokenMatch checks if the current token matches the given keyword
// This handles both keyword tokens and identifier tokens with matching literal values
// (needed because some keywords like DATA, NO may be tokenized as identifiers)
func (p *Parser) isTokenMatch(keyword string) bool {
upperKeyword := strings.ToUpper(keyword)
// Check if token type matches the keyword directly
if strings.ToUpper(string(p.currentToken.Type)) == upperKeyword {
return true
}
// Check if it's an identifier with matching literal (case-insensitive)
if p.currentToken.Type == "IDENT" && strings.ToUpper(p.currentToken.Literal) == upperKeyword {
return true
}
return false
}
// parseCreateStatement parses CREATE statements (TABLE, VIEW, MATERIALIZED VIEW, INDEX)
func (p *Parser) parseCreateStatement() (ast.Statement, error) {
// Check for modifiers: OR REPLACE, TEMPORARY, TEMP
orReplace := false
temporary := false
for {
if p.isType(models.TokenTypeOr) {
p.advance() // Consume OR
if !p.isType(models.TokenTypeReplace) {
return nil, p.expectedError("REPLACE after OR")
}
p.advance() // Consume REPLACE
orReplace = true
} else if p.isTokenMatch("TEMPORARY") || p.isTokenMatch("TEMP") {
p.advance() // Consume TEMPORARY/TEMP
temporary = true
} else {
break
}
}
// Determine object type
if p.isType(models.TokenTypeMaterialized) {
p.advance() // Consume MATERIALIZED
if !p.isType(models.TokenTypeView) {
return nil, p.expectedError("VIEW after MATERIALIZED")
}
p.advance() // Consume VIEW
return p.parseCreateMaterializedView()
} else if p.isType(models.TokenTypeView) {
p.advance() // Consume VIEW
return p.parseCreateView(orReplace, temporary)
} else if p.isType(models.TokenTypeTable) {
p.advance() // Consume TABLE
return p.parseCreateTable(temporary)
} else if p.isType(models.TokenTypeIndex) {
p.advance() // Consume INDEX
return p.parseCreateIndex(false) // Not unique
} else if p.isType(models.TokenTypeUnique) {
p.advance() // Consume UNIQUE
if !p.isType(models.TokenTypeIndex) {
return nil, p.expectedError("INDEX after UNIQUE")
}
p.advance() // Consume INDEX
return p.parseCreateIndex(true) // Unique
}
return nil, p.expectedError("TABLE, VIEW, MATERIALIZED VIEW, or INDEX after CREATE")
}
// parseCreateView parses CREATE [OR REPLACE] [TEMPORARY] VIEW statement
func (p *Parser) parseCreateView(orReplace, temporary bool) (*ast.CreateViewStatement, error) {
stmt := &ast.CreateViewStatement{
OrReplace: orReplace,
Temporary: temporary,
}
// Check for IF NOT EXISTS
if p.isType(models.TokenTypeIf) {
p.advance() // Consume IF
if !p.isType(models.TokenTypeNot) {
return nil, p.expectedError("NOT after IF")
}
p.advance() // Consume NOT
if !p.isType(models.TokenTypeExists) {
return nil, p.expectedError("EXISTS after NOT")
}
p.advance() // Consume EXISTS
stmt.IfNotExists = true
}
// Parse view name
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("view name")
}
stmt.Name = p.currentToken.Literal
p.advance()
// Parse optional column list
if p.isType(models.TokenTypeLParen) {
p.advance() // Consume (
for {
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("column name")
}
stmt.Columns = append(stmt.Columns, p.currentToken.Literal)
p.advance()
if p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
continue
}
break
}
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
}
// Expect AS
if !p.isType(models.TokenTypeAs) {
return nil, p.expectedError("AS")
}
p.advance() // Consume AS
// Parse the SELECT statement
if !p.isType(models.TokenTypeSelect) {
return nil, p.expectedError("SELECT")
}
p.advance() // Consume SELECT
query, err := p.parseSelectWithSetOperations()
if err != nil {
return nil, fmt.Errorf("error parsing view query: %v", err)
}
stmt.Query = query
// Parse optional WITH CHECK OPTION
if p.isType(models.TokenTypeWith) {
p.advance() // Consume WITH
if p.isType(models.TokenTypeCheck) {
p.advance() // Consume CHECK
if p.isTokenMatch("OPTION") {
p.advance() // Consume OPTION
stmt.WithOption = "CHECK OPTION"
}
} else if p.isTokenMatch("CASCADED") {
p.advance() // Consume CASCADED
if p.isType(models.TokenTypeCheck) {
p.advance() // Consume CHECK
if p.isTokenMatch("OPTION") {
p.advance() // Consume OPTION
stmt.WithOption = "CASCADED CHECK OPTION"
}
}
} else if p.isTokenMatch("LOCAL") {
p.advance() // Consume LOCAL
if p.isType(models.TokenTypeCheck) {
p.advance() // Consume CHECK
if p.isTokenMatch("OPTION") {
p.advance() // Consume OPTION
stmt.WithOption = "LOCAL CHECK OPTION"
}
}
}
}
return stmt, nil
}
// parseCreateMaterializedView parses CREATE MATERIALIZED VIEW statement
func (p *Parser) parseCreateMaterializedView() (*ast.CreateMaterializedViewStatement, error) {
stmt := &ast.CreateMaterializedViewStatement{}
// Check for IF NOT EXISTS
if p.isType(models.TokenTypeIf) {
p.advance() // Consume IF
if !p.isType(models.TokenTypeNot) {
return nil, p.expectedError("NOT after IF")
}
p.advance() // Consume NOT
if !p.isType(models.TokenTypeExists) {
return nil, p.expectedError("EXISTS after NOT")
}
p.advance() // Consume EXISTS
stmt.IfNotExists = true
}
// Parse view name
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("materialized view name")
}
stmt.Name = p.currentToken.Literal
p.advance()
// Parse optional column list
if p.isType(models.TokenTypeLParen) {
p.advance() // Consume (
for {
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("column name")
}
stmt.Columns = append(stmt.Columns, p.currentToken.Literal)
p.advance()
if p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
continue
}
break
}
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
}
// Parse optional TABLESPACE
if p.isTokenMatch("TABLESPACE") {
p.advance() // Consume TABLESPACE
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("tablespace name")
}
stmt.Tablespace = p.currentToken.Literal
p.advance()
}
// Expect AS
if !p.isType(models.TokenTypeAs) {
return nil, p.expectedError("AS")
}
p.advance() // Consume AS
// Parse the SELECT statement
if !p.isType(models.TokenTypeSelect) {
return nil, p.expectedError("SELECT")
}
p.advance() // Consume SELECT
query, err := p.parseSelectWithSetOperations()
if err != nil {
return nil, fmt.Errorf("error parsing materialized view query: %v", err)
}
stmt.Query = query
// Parse optional WITH [NO] DATA
// Note: DATA and NO may be tokenized as IDENT since they're common identifiers
if p.isType(models.TokenTypeWith) {
p.advance() // Consume WITH
if p.isTokenMatch("NO") {
p.advance() // Consume NO
if !p.isTokenMatch("DATA") {
return nil, p.expectedError("DATA after NO")
}
p.advance() // Consume DATA
withData := false
stmt.WithData = &withData
} else if p.isTokenMatch("DATA") {
p.advance() // Consume DATA
withData := true
stmt.WithData = &withData
}
}
return stmt, nil
}
// parseCreateTable parses CREATE TABLE statement with partitioning support
func (p *Parser) parseCreateTable(temporary bool) (*ast.CreateTableStatement, error) {
stmt := &ast.CreateTableStatement{
Temporary: temporary,
}
// Check for IF NOT EXISTS
if p.isType(models.TokenTypeIf) {
p.advance() // Consume IF
if !p.isType(models.TokenTypeNot) {
return nil, p.expectedError("NOT after IF")
}
p.advance() // Consume NOT
if !p.isType(models.TokenTypeExists) {
return nil, p.expectedError("EXISTS after NOT")
}
p.advance() // Consume EXISTS
stmt.IfNotExists = true
}
// Parse table name
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("table name")
}
stmt.Name = p.currentToken.Literal
p.advance()
// Expect opening parenthesis for column definitions
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("(")
}
p.advance() // Consume (
// Parse column definitions and constraints
for {
// Check for table-level constraints
if p.isAnyType(models.TokenTypePrimary, models.TokenTypeForeign,
models.TokenTypeUnique, models.TokenTypeCheck, models.TokenTypeConstraint) {
constraint, err := p.parseTableConstraint()
if err != nil {
return nil, err
}
stmt.Constraints = append(stmt.Constraints, *constraint)
} else {
// Parse column definition
colDef, err := p.parseColumnDef()
if err != nil {
return nil, err
}
stmt.Columns = append(stmt.Columns, *colDef)
}
// Check for more definitions
if p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
continue
}
break
}
// Expect closing parenthesis
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
// Parse optional PARTITION BY clause
if p.isType(models.TokenTypePartition) {
p.advance() // Consume PARTITION
if !p.isType(models.TokenTypeBy) {
return nil, p.expectedError("BY after PARTITION")
}
p.advance() // Consume BY
partitionBy, err := p.parsePartitionByClause()
if err != nil {
return nil, err
}
stmt.PartitionBy = partitionBy
// Parse partition definitions if present
if p.isType(models.TokenTypeLParen) {
p.advance() // Consume (
for {
partDef, err := p.parsePartitionDefinition()
if err != nil {
return nil, err
}
stmt.Partitions = append(stmt.Partitions, *partDef)
if p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
continue
}
break
}
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
}
}
// Parse optional table options
for p.isTokenMatch("ENGINE") || p.isTokenMatch("CHARSET") ||
p.isType(models.TokenTypeCollate) || p.isTokenMatch("COMMENT") {
opt := ast.TableOption{Name: p.currentToken.Literal}
p.advance()
if p.isType(models.TokenTypeEq) {
p.advance() // Consume =
}
if p.isType(models.TokenTypeIdentifier) || p.isType(models.TokenTypeString) {
opt.Value = p.currentToken.Literal
p.advance()
}
stmt.Options = append(stmt.Options, opt)
}
return stmt, nil
}
// parsePartitionByClause parses PARTITION BY RANGE/LIST/HASH (columns)
func (p *Parser) parsePartitionByClause() (*ast.PartitionBy, error) {
partitionBy := &ast.PartitionBy{}
// Parse partition type
if p.isType(models.TokenTypeRange) {
partitionBy.Type = "RANGE"
p.advance()
} else if p.isTokenMatch("LIST") {
partitionBy.Type = "LIST"
p.advance()
} else if p.isTokenMatch("HASH") {
partitionBy.Type = "HASH"
p.advance()
} else {
return nil, p.expectedError("RANGE, LIST, or HASH")
}
// Expect opening parenthesis
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("(")
}
p.advance() // Consume (
// Parse column list
for {
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("column name")
}
partitionBy.Columns = append(partitionBy.Columns, p.currentToken.Literal)
p.advance()
if p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
continue
}
break
}
// Expect closing parenthesis
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
return partitionBy, nil
}
// parsePartitionDefinition parses a single partition definition
func (p *Parser) parsePartitionDefinition() (*ast.PartitionDefinition, error) {
partDef := &ast.PartitionDefinition{}
// Expect PARTITION keyword
if !p.isType(models.TokenTypePartition) {
return nil, p.expectedError("PARTITION")
}
p.advance() // Consume PARTITION
// Parse partition name
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("partition name")
}
partDef.Name = p.currentToken.Literal
p.advance()
// Parse VALUES clause
if !p.isType(models.TokenTypeValues) {
return nil, p.expectedError("VALUES")
}
p.advance() // Consume VALUES
// Parse value specification
if p.isTokenMatch("LESS") {
p.advance() // Consume LESS
if !p.isTokenMatch("THAN") {
return nil, p.expectedError("THAN after LESS")
}
p.advance() // Consume THAN
partDef.Type = "LESS THAN"
// Parse value or MAXVALUE
if p.isType(models.TokenTypeLParen) {
p.advance() // Consume (
if p.isTokenMatch("MAXVALUE") {
partDef.LessThan = &ast.Identifier{Name: "MAXVALUE"}
p.advance()
} else {
expr, err := p.parseExpression()
if err != nil {
return nil, err
}
partDef.LessThan = expr
}
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
} else if p.isTokenMatch("MAXVALUE") {
partDef.LessThan = &ast.Identifier{Name: "MAXVALUE"}
p.advance()
}
} else if p.isType(models.TokenTypeIn) {
p.advance() // Consume IN
partDef.Type = "IN"
// Parse value list
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("(")
}
p.advance() // Consume (
for {
expr, err := p.parseExpression()
if err != nil {
return nil, err
}
partDef.InValues = append(partDef.InValues, expr)
if p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
continue
}
break
}
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
} else if p.isType(models.TokenTypeFrom) {
p.advance() // Consume FROM
partDef.Type = "FROM TO"
// Parse FROM value
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("(")
}
p.advance() // Consume (
fromExpr, err := p.parseExpression()
if err != nil {
return nil, err
}
partDef.From = fromExpr
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
// Expect TO
if !p.isType(models.TokenTypeTo) {
return nil, p.expectedError("TO")
}
p.advance() // Consume TO
// Parse TO value
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("(")
}
p.advance() // Consume (
toExpr, err := p.parseExpression()
if err != nil {
return nil, err
}
partDef.To = toExpr
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
}
// Parse optional TABLESPACE
if p.isTokenMatch("TABLESPACE") {
p.advance() // Consume TABLESPACE
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("tablespace name")
}
partDef.Tablespace = p.currentToken.Literal
p.advance()
}
return partDef, nil
}
// parseCreateIndex parses CREATE [UNIQUE] INDEX statement
func (p *Parser) parseCreateIndex(unique bool) (*ast.CreateIndexStatement, error) {
stmt := &ast.CreateIndexStatement{
Unique: unique,
}
// Check for IF NOT EXISTS
if p.isType(models.TokenTypeIf) {
p.advance() // Consume IF
if !p.isType(models.TokenTypeNot) {
return nil, p.expectedError("NOT after IF")
}
p.advance() // Consume NOT
if !p.isType(models.TokenTypeExists) {
return nil, p.expectedError("EXISTS after NOT")
}
p.advance() // Consume EXISTS
stmt.IfNotExists = true
}
// Parse index name
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("index name")
}
stmt.Name = p.currentToken.Literal
p.advance()
// Expect ON
if !p.isType(models.TokenTypeOn) {
return nil, p.expectedError("ON")
}
p.advance() // Consume ON
// Parse table name
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("table name")
}
stmt.Table = p.currentToken.Literal
p.advance()
// Parse optional USING
if p.isType(models.TokenTypeUsing) {
p.advance() // Consume USING
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("index method")
}
stmt.Using = p.currentToken.Literal
p.advance()
}
// Expect opening parenthesis
if !p.isType(models.TokenTypeLParen) {
return nil, p.expectedError("(")
}
p.advance() // Consume (
// Parse column list
for {
col := ast.IndexColumn{}
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("column name")
}
col.Column = p.currentToken.Literal
p.advance()
// Parse optional direction
if p.isType(models.TokenTypeAsc) {
col.Direction = "ASC"
p.advance()
} else if p.isType(models.TokenTypeDesc) {
col.Direction = "DESC"
p.advance()
}
// Parse optional NULLS LAST
if p.isType(models.TokenTypeNulls) {
p.advance() // Consume NULLS
if p.isType(models.TokenTypeLast) {
col.NullsLast = true
p.advance()
} else if p.isType(models.TokenTypeFirst) {
p.advance()
}
}
stmt.Columns = append(stmt.Columns, col)
if p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
continue
}
break
}
// Expect closing parenthesis
if !p.isType(models.TokenTypeRParen) {
return nil, p.expectedError(")")
}
p.advance() // Consume )
// Parse optional WHERE clause (partial index)
if p.isType(models.TokenTypeWhere) {
p.advance() // Consume WHERE
whereClause, err := p.parseExpression()
if err != nil {
return nil, err
}
stmt.Where = whereClause
}
return stmt, nil
}
// parseDropStatement parses DROP statements (TABLE, VIEW, MATERIALIZED VIEW, INDEX)
func (p *Parser) parseDropStatement() (*ast.DropStatement, error) {
stmt := &ast.DropStatement{}
// Determine object type
if p.isType(models.TokenTypeMaterialized) {
p.advance() // Consume MATERIALIZED
if !p.isType(models.TokenTypeView) {
return nil, p.expectedError("VIEW after MATERIALIZED")
}
p.advance() // Consume VIEW
stmt.ObjectType = "MATERIALIZED VIEW"
} else if p.isType(models.TokenTypeView) {
p.advance() // Consume VIEW
stmt.ObjectType = "VIEW"
} else if p.isType(models.TokenTypeTable) {
p.advance() // Consume TABLE
stmt.ObjectType = "TABLE"
} else if p.isType(models.TokenTypeIndex) {
p.advance() // Consume INDEX
stmt.ObjectType = "INDEX"
} else {
return nil, p.expectedError("TABLE, VIEW, MATERIALIZED VIEW, or INDEX after DROP")
}
// Check for IF EXISTS
if p.isType(models.TokenTypeIf) {
p.advance() // Consume IF
if !p.isType(models.TokenTypeExists) {
return nil, p.expectedError("EXISTS after IF")
}
p.advance() // Consume EXISTS
stmt.IfExists = true
}
// Parse object names (can be comma-separated)
for {
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("object name")
}
stmt.Names = append(stmt.Names, p.currentToken.Literal)
p.advance()
if p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
continue
}
break
}
// Parse optional CASCADE/RESTRICT
if p.isType(models.TokenTypeCascade) {
stmt.CascadeType = "CASCADE"
p.advance()
} else if p.isType(models.TokenTypeRestrict) {
stmt.CascadeType = "RESTRICT"
p.advance()
}
return stmt, nil
}
// parseRefreshStatement parses REFRESH MATERIALIZED VIEW statement
func (p *Parser) parseRefreshStatement() (*ast.RefreshMaterializedViewStatement, error) {
// Expect MATERIALIZED
if !p.isType(models.TokenTypeMaterialized) {
return nil, p.expectedError("MATERIALIZED after REFRESH")
}
p.advance() // Consume MATERIALIZED
// Expect VIEW
if !p.isType(models.TokenTypeView) {
return nil, p.expectedError("VIEW after MATERIALIZED")
}
p.advance() // Consume VIEW
stmt := &ast.RefreshMaterializedViewStatement{}
// Check for CONCURRENTLY
if p.isTokenMatch("CONCURRENTLY") {
stmt.Concurrently = true
p.advance()
}
// Parse view name
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("materialized view name")
}
stmt.Name = p.currentToken.Literal
p.advance()
// Parse optional WITH [NO] DATA
// Note: DATA and NO may be tokenized as IDENT since they're common identifiers
if p.isType(models.TokenTypeWith) {
p.advance() // Consume WITH
if p.isTokenMatch("NO") {
p.advance() // Consume NO
if !p.isTokenMatch("DATA") {
return nil, p.expectedError("DATA after NO")
}
p.advance() // Consume DATA
withData := false
stmt.WithData = &withData
} else if p.isTokenMatch("DATA") {
p.advance() // Consume DATA
withData := true
stmt.WithData = &withData
}
}
return stmt, nil
}
// parseTruncateStatement parses TRUNCATE TABLE statement
// Syntax: TRUNCATE [TABLE] table_name [, table_name ...] [RESTART IDENTITY | CONTINUE IDENTITY] [CASCADE | RESTRICT]
func (p *Parser) parseTruncateStatement() (*ast.TruncateStatement, error) {
stmt := &ast.TruncateStatement{}
// Optional TABLE keyword
if p.isType(models.TokenTypeTable) {
p.advance() // Consume TABLE
}
// Parse table names (can be comma-separated)
for {
if !p.isType(models.TokenTypeIdentifier) {
return nil, p.expectedError("table name")
}
stmt.Tables = append(stmt.Tables, p.currentToken.Literal)
p.advance()
if p.isType(models.TokenTypeComma) {
p.advance() // Consume comma
continue
}
break
}
// Parse optional RESTART IDENTITY / CONTINUE IDENTITY
if p.isTokenMatch("RESTART") {
p.advance() // Consume RESTART
if !p.isTokenMatch("IDENTITY") {
return nil, p.expectedError("IDENTITY after RESTART")
}
p.advance() // Consume IDENTITY
stmt.RestartIdentity = true
} else if p.isTokenMatch("CONTINUE") {
p.advance() // Consume CONTINUE
if !p.isTokenMatch("IDENTITY") {
return nil, p.expectedError("IDENTITY after CONTINUE")
}
p.advance() // Consume IDENTITY
stmt.ContinueIdentity = true
}
// Parse optional CASCADE/RESTRICT
if p.isType(models.TokenTypeCascade) {
stmt.CascadeType = "CASCADE"
p.advance()
} else if p.isType(models.TokenTypeRestrict) {
stmt.CascadeType = "RESTRICT"
p.advance()
}
return stmt, nil
}