-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathast.go
More file actions
861 lines (753 loc) · 24.6 KB
/
ast.go
File metadata and controls
861 lines (753 loc) · 24.6 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
// Package ast provides Abstract Syntax Tree (AST) node definitions for SQL statements.
// It includes comprehensive support for DDL and DML operations, Common Table Expressions (CTEs),
// set operations, and window functions, with object pooling for performance optimization.
//
// Phase 2 Features (v1.2.0+):
// - WithClause and CommonTableExpr for CTE support
// - SetOperation for UNION, EXCEPT, INTERSECT operations
// - Recursive CTE support with proper AST representation
// - Integration with all statement types
//
// Phase 2.5 Features (v1.3.0+):
// - WindowSpec for window function specifications
// - WindowFrame and WindowFrameBound for frame clauses
// - Enhanced FunctionCall with Over field for window functions
// - Complete window function AST integration
package ast
import "fmt"
// Node represents any node in the AST
type Node interface {
TokenLiteral() string
Children() []Node
}
// Statement represents a SQL statement
type Statement interface {
Node
statementNode()
}
// Expression represents a SQL expression
type Expression interface {
Node
expressionNode()
}
// WithClause represents a WITH clause in a SQL statement.
// It supports both simple and recursive Common Table Expressions (CTEs).
// Phase 2 Complete: Full parser integration with all statement types.
type WithClause struct {
Recursive bool
CTEs []*CommonTableExpr
}
func (w *WithClause) statementNode() {}
func (w WithClause) TokenLiteral() string { return "WITH" }
func (w WithClause) Children() []Node {
children := make([]Node, len(w.CTEs))
for i, cte := range w.CTEs {
children[i] = cte
}
return children
}
// CommonTableExpr represents a single Common Table Expression in a WITH clause.
// It supports optional column specifications and any statement type as the CTE query.
// Phase 2 Complete: Full parser support with column specifications.
type CommonTableExpr struct {
Name string
Columns []string
Statement Statement
Materialized *bool // TODO: Add MATERIALIZED/NOT MATERIALIZED parsing support
}
func (c *CommonTableExpr) statementNode() {}
func (c CommonTableExpr) TokenLiteral() string { return c.Name }
func (c CommonTableExpr) Children() []Node {
return []Node{c.Statement}
}
// SetOperation represents set operations (UNION, EXCEPT, INTERSECT) between two statements.
// It supports the ALL modifier (e.g., UNION ALL) and proper left-associative parsing.
// Phase 2 Complete: Full parser support with left-associative precedence.
type SetOperation struct {
Left Statement
Operator string // UNION, EXCEPT, INTERSECT
Right Statement
All bool // UNION ALL vs UNION
}
func (s *SetOperation) statementNode() {}
func (s SetOperation) TokenLiteral() string { return s.Operator }
func (s SetOperation) Children() []Node {
return []Node{s.Left, s.Right}
}
// JoinClause represents a JOIN clause in SQL
type JoinClause struct {
Type string // INNER, LEFT, RIGHT, FULL
Left TableReference
Right TableReference
Condition Expression
}
func (j *JoinClause) expressionNode() {}
func (j JoinClause) TokenLiteral() string { return j.Type + " JOIN" }
func (j JoinClause) Children() []Node {
children := []Node{&j.Left, &j.Right}
if j.Condition != nil {
children = append(children, j.Condition)
}
return children
}
// TableReference represents a table in FROM clause
type TableReference struct {
Name string
Alias string
}
func (t *TableReference) statementNode() {}
func (t TableReference) TokenLiteral() string { return t.Name }
func (t TableReference) Children() []Node { return nil }
// OrderByExpression represents an ORDER BY clause element with direction and NULL ordering
type OrderByExpression struct {
Expression Expression // The expression to order by
Ascending bool // true for ASC (default), false for DESC
NullsFirst *bool // nil = default behavior, true = NULLS FIRST, false = NULLS LAST
}
func (*OrderByExpression) expressionNode() {}
func (o *OrderByExpression) TokenLiteral() string { return "ORDER BY" }
func (o *OrderByExpression) Children() []Node {
if o.Expression != nil {
return []Node{o.Expression}
}
return nil
}
// WindowSpec represents a window specification
type WindowSpec struct {
Name string
PartitionBy []Expression
OrderBy []OrderByExpression
FrameClause *WindowFrame
}
func (w *WindowSpec) statementNode() {}
func (w WindowSpec) TokenLiteral() string { return "WINDOW" }
func (w WindowSpec) Children() []Node {
children := make([]Node, 0)
children = append(children, nodifyExpressions(w.PartitionBy)...)
for _, orderBy := range w.OrderBy {
orderBy := orderBy // G601: Create local copy to avoid memory aliasing
children = append(children, &orderBy)
}
if w.FrameClause != nil {
children = append(children, w.FrameClause)
}
return children
}
// WindowFrame represents window frame clause
type WindowFrame struct {
Type string // ROWS, RANGE
Start WindowFrameBound
End *WindowFrameBound
}
func (w *WindowFrame) statementNode() {}
func (w WindowFrame) TokenLiteral() string { return w.Type }
func (w WindowFrame) Children() []Node { return nil }
// WindowFrameBound represents window frame bound
type WindowFrameBound struct {
Type string // CURRENT ROW, UNBOUNDED PRECEDING, etc.
Value Expression
}
// SelectStatement represents a SELECT SQL statement
type SelectStatement struct {
With *WithClause
Distinct bool
Columns []Expression
From []TableReference
TableName string // Added for pool operations
Joins []JoinClause
Where Expression
GroupBy []Expression
Having Expression
Windows []WindowSpec
OrderBy []OrderByExpression
Limit *int
Offset *int
}
func (s *SelectStatement) statementNode() {}
func (s SelectStatement) TokenLiteral() string { return "SELECT" }
func (s SelectStatement) Children() []Node {
children := make([]Node, 0)
if s.With != nil {
children = append(children, s.With)
}
children = append(children, nodifyExpressions(s.Columns)...)
for _, from := range s.From {
from := from // G601: Create local copy to avoid memory aliasing
children = append(children, &from)
}
for _, join := range s.Joins {
join := join // G601: Create local copy to avoid memory aliasing
children = append(children, &join)
}
if s.Where != nil {
children = append(children, s.Where)
}
children = append(children, nodifyExpressions(s.GroupBy)...)
if s.Having != nil {
children = append(children, s.Having)
}
for _, window := range s.Windows {
window := window // G601: Create local copy to avoid memory aliasing
children = append(children, &window)
}
for _, orderBy := range s.OrderBy {
orderBy := orderBy // G601: Create local copy to avoid memory aliasing
children = append(children, &orderBy)
}
return children
}
// Helper function to convert []Expression to []Node
func nodifyExpressions(exprs []Expression) []Node {
nodes := make([]Node, len(exprs))
for i, expr := range exprs {
nodes[i] = expr
}
return nodes
}
// Identifier represents a column or table name
type Identifier struct {
Name string
Table string // Optional table qualifier
}
func (i *Identifier) expressionNode() {}
func (i Identifier) TokenLiteral() string { return i.Name }
func (i Identifier) Children() []Node { return nil }
// FunctionCall represents a function call expression
type FunctionCall struct {
Name string
Arguments []Expression // Renamed from Args for consistency
Over *WindowSpec // For window functions
Distinct bool
Filter Expression // WHERE clause for aggregate functions
}
func (f *FunctionCall) expressionNode() {}
func (f FunctionCall) TokenLiteral() string { return f.Name }
func (f FunctionCall) Children() []Node {
children := nodifyExpressions(f.Arguments)
if f.Over != nil {
children = append(children, f.Over)
}
if f.Filter != nil {
children = append(children, f.Filter)
}
return children
}
// CaseExpression represents a CASE expression
type CaseExpression struct {
Value Expression // Optional CASE value
WhenClauses []WhenClause
ElseClause Expression
}
func (c *CaseExpression) expressionNode() {}
func (c CaseExpression) TokenLiteral() string { return "CASE" }
func (c CaseExpression) Children() []Node {
children := make([]Node, 0)
if c.Value != nil {
children = append(children, c.Value)
}
for _, when := range c.WhenClauses {
when := when // G601: Create local copy to avoid memory aliasing
children = append(children, &when)
}
if c.ElseClause != nil {
children = append(children, c.ElseClause)
}
return children
}
// WhenClause represents WHEN ... THEN ... in CASE expression
type WhenClause struct {
Condition Expression
Result Expression
}
func (w *WhenClause) expressionNode() {}
func (w WhenClause) TokenLiteral() string { return "WHEN" }
func (w WhenClause) Children() []Node {
return []Node{w.Condition, w.Result}
}
// ExistsExpression represents EXISTS (subquery)
type ExistsExpression struct {
Subquery Statement
}
func (e *ExistsExpression) expressionNode() {}
func (e ExistsExpression) TokenLiteral() string { return "EXISTS" }
func (e ExistsExpression) Children() []Node {
return []Node{e.Subquery}
}
// InExpression represents expr IN (values) or expr IN (subquery)
type InExpression struct {
Expr Expression
List []Expression // For value list: IN (1, 2, 3)
Subquery Statement // For subquery: IN (SELECT ...)
Not bool
}
func (i *InExpression) expressionNode() {}
func (i InExpression) TokenLiteral() string { return "IN" }
func (i InExpression) Children() []Node {
children := []Node{i.Expr}
if i.Subquery != nil {
children = append(children, i.Subquery)
} else {
children = append(children, nodifyExpressions(i.List)...)
}
return children
}
// SubqueryExpression represents a scalar subquery (SELECT ...)
type SubqueryExpression struct {
Subquery Statement
}
func (s *SubqueryExpression) expressionNode() {}
func (s SubqueryExpression) TokenLiteral() string { return "SUBQUERY" }
func (s SubqueryExpression) Children() []Node { return []Node{s.Subquery} }
// AnyExpression represents expr op ANY (subquery)
type AnyExpression struct {
Expr Expression
Operator string
Subquery Statement
}
func (a *AnyExpression) expressionNode() {}
func (a AnyExpression) TokenLiteral() string { return "ANY" }
func (a AnyExpression) Children() []Node { return []Node{a.Expr, a.Subquery} }
// AllExpression represents expr op ALL (subquery)
type AllExpression struct {
Expr Expression
Operator string
Subquery Statement
}
func (al *AllExpression) expressionNode() {}
func (al AllExpression) TokenLiteral() string { return "ALL" }
func (al AllExpression) Children() []Node { return []Node{al.Expr, al.Subquery} }
// BetweenExpression represents expr BETWEEN lower AND upper
type BetweenExpression struct {
Expr Expression
Lower Expression
Upper Expression
Not bool
}
func (b *BetweenExpression) expressionNode() {}
func (b BetweenExpression) TokenLiteral() string { return "BETWEEN" }
func (b BetweenExpression) Children() []Node {
return []Node{b.Expr, b.Lower, b.Upper}
}
// BinaryExpression represents operations like WHERE column = value
type BinaryExpression struct {
Left Expression
Operator string
Right Expression
Not bool // For NOT (expr)
CustomOp *CustomBinaryOperator // For PostgreSQL custom operators
}
func (b *BinaryExpression) expressionNode() {}
func (b *BinaryExpression) TokenLiteral() string {
if b.CustomOp != nil {
return b.CustomOp.String()
}
return b.Operator
}
func (b BinaryExpression) Children() []Node { return []Node{b.Left, b.Right} }
// LiteralValue represents a literal value in SQL
type LiteralValue struct {
Value interface{}
Type string // INTEGER, FLOAT, STRING, BOOLEAN, NULL, etc.
}
func (l *LiteralValue) expressionNode() {}
func (l LiteralValue) TokenLiteral() string { return fmt.Sprintf("%v", l.Value) }
func (l LiteralValue) Children() []Node { return nil }
// ListExpression represents a list of expressions (1, 2, 3)
type ListExpression struct {
Values []Expression
}
func (l *ListExpression) expressionNode() {}
func (l ListExpression) TokenLiteral() string { return "LIST" }
func (l ListExpression) Children() []Node { return nodifyExpressions(l.Values) }
// UnaryExpression represents operations like NOT expr
type UnaryExpression struct {
Operator UnaryOperator
Expr Expression
}
func (u *UnaryExpression) expressionNode() {}
func (u *UnaryExpression) TokenLiteral() string {
return u.Operator.String()
}
func (u UnaryExpression) Children() []Node { return []Node{u.Expr} }
// CastExpression represents CAST(expr AS type)
type CastExpression struct {
Expr Expression
Type string
}
func (c *CastExpression) expressionNode() {}
func (c CastExpression) TokenLiteral() string { return "CAST" }
func (c CastExpression) Children() []Node { return []Node{c.Expr} }
// ExtractExpression represents EXTRACT(field FROM source)
type ExtractExpression struct {
Field string
Source Expression
}
func (e *ExtractExpression) expressionNode() {}
func (e ExtractExpression) TokenLiteral() string { return "EXTRACT" }
func (e ExtractExpression) Children() []Node { return []Node{e.Source} }
// PositionExpression represents POSITION(substr IN str)
type PositionExpression struct {
Substr Expression
Str Expression
}
func (p *PositionExpression) expressionNode() {}
func (p PositionExpression) TokenLiteral() string { return "POSITION" }
func (p PositionExpression) Children() []Node { return []Node{p.Substr, p.Str} }
// SubstringExpression represents SUBSTRING(str FROM start [FOR length])
type SubstringExpression struct {
Str Expression
Start Expression
Length Expression
}
func (s *SubstringExpression) expressionNode() {}
func (s SubstringExpression) TokenLiteral() string { return "SUBSTRING" }
func (s SubstringExpression) Children() []Node {
children := []Node{s.Str, s.Start}
if s.Length != nil {
children = append(children, s.Length)
}
return children
}
// InsertStatement represents an INSERT SQL statement
type InsertStatement struct {
With *WithClause
TableName string
Columns []Expression
Values []Expression
Query *SelectStatement // For INSERT ... SELECT
Returning []Expression
OnConflict *OnConflict
}
func (i *InsertStatement) statementNode() {}
func (i InsertStatement) TokenLiteral() string { return "INSERT" }
func (i InsertStatement) Children() []Node {
children := make([]Node, 0)
if i.With != nil {
children = append(children, i.With)
}
children = append(children, nodifyExpressions(i.Columns)...)
children = append(children, nodifyExpressions(i.Values)...)
if i.Query != nil {
children = append(children, i.Query)
}
children = append(children, nodifyExpressions(i.Returning)...)
if i.OnConflict != nil {
children = append(children, i.OnConflict)
}
return children
}
// OnConflict represents ON CONFLICT DO UPDATE/NOTHING clause
type OnConflict struct {
Target []Expression // Target columns
Constraint string // Optional constraint name
Action OnConflictAction
}
func (o *OnConflict) expressionNode() {}
func (o OnConflict) TokenLiteral() string { return "ON CONFLICT" }
func (o OnConflict) Children() []Node {
children := nodifyExpressions(o.Target)
if o.Action.DoUpdate != nil {
for _, update := range o.Action.DoUpdate {
update := update // G601: Create local copy to avoid memory aliasing
children = append(children, &update)
}
}
return children
}
// OnConflictAction represents DO UPDATE/NOTHING in ON CONFLICT clause
type OnConflictAction struct {
DoNothing bool
DoUpdate []UpdateExpression
Where Expression
}
// UpsertClause represents INSERT ... ON DUPLICATE KEY UPDATE
type UpsertClause struct {
Updates []UpdateExpression
}
func (u *UpsertClause) expressionNode() {}
func (u UpsertClause) TokenLiteral() string { return "ON DUPLICATE KEY UPDATE" }
func (u UpsertClause) Children() []Node {
children := make([]Node, len(u.Updates))
for i, update := range u.Updates {
update := update // G601: Create local copy to avoid memory aliasing
children[i] = &update
}
return children
}
// Values represents VALUES clause
type Values struct {
Rows [][]Expression
}
func (v *Values) statementNode() {}
func (v Values) TokenLiteral() string { return "VALUES" }
func (v Values) Children() []Node {
children := make([]Node, 0)
for _, row := range v.Rows {
children = append(children, nodifyExpressions(row)...)
}
return children
}
// UpdateStatement represents an UPDATE SQL statement
type UpdateStatement struct {
With *WithClause
TableName string
Alias string
Updates []UpdateExpression // Keep for backward compatibility
Assignments []UpdateExpression // New field for consistency with span.go
From []TableReference
Where Expression
Returning []Expression
}
func (u *UpdateStatement) statementNode() {}
func (u UpdateStatement) TokenLiteral() string { return "UPDATE" }
func (u UpdateStatement) Children() []Node {
children := make([]Node, 0)
if u.With != nil {
children = append(children, u.With)
}
for _, update := range u.Updates {
update := update // G601: Create local copy to avoid memory aliasing
children = append(children, &update)
}
for _, assignment := range u.Assignments {
assignment := assignment // G601: Create local copy to avoid memory aliasing
children = append(children, &assignment)
}
for _, from := range u.From {
from := from // G601: Create local copy to avoid memory aliasing
children = append(children, &from)
}
if u.Where != nil {
children = append(children, u.Where)
}
children = append(children, nodifyExpressions(u.Returning)...)
return children
}
// CreateTableStatement represents a CREATE TABLE statement
type CreateTableStatement struct {
IfNotExists bool
Temporary bool
Name string
Columns []ColumnDef
Constraints []TableConstraint
Inherits []string
PartitionBy *PartitionBy
Options []TableOption
}
func (c *CreateTableStatement) statementNode() {}
func (c CreateTableStatement) TokenLiteral() string { return "CREATE TABLE" }
func (c CreateTableStatement) Children() []Node {
children := make([]Node, 0)
for _, col := range c.Columns {
col := col // G601: Create local copy to avoid memory aliasing
children = append(children, &col)
}
for _, constraint := range c.Constraints {
constraint := constraint // G601: Create local copy to avoid memory aliasing
children = append(children, &constraint)
}
if c.PartitionBy != nil {
children = append(children, c.PartitionBy)
}
return children
}
// ColumnDef represents a column definition in CREATE TABLE
type ColumnDef struct {
Name string
Type string
Constraints []ColumnConstraint
}
func (c *ColumnDef) expressionNode() {}
func (c ColumnDef) TokenLiteral() string { return c.Name }
func (c ColumnDef) Children() []Node {
children := make([]Node, len(c.Constraints))
for i, constraint := range c.Constraints {
constraint := constraint // G601: Create local copy to avoid memory aliasing
children[i] = &constraint
}
return children
}
// ColumnConstraint represents a column constraint
type ColumnConstraint struct {
Type string // NOT NULL, UNIQUE, PRIMARY KEY, etc.
Default Expression
References *ReferenceDefinition
Check Expression
AutoIncrement bool
}
func (c *ColumnConstraint) expressionNode() {}
func (c ColumnConstraint) TokenLiteral() string { return c.Type }
func (c ColumnConstraint) Children() []Node {
children := make([]Node, 0)
if c.Default != nil {
children = append(children, c.Default)
}
if c.References != nil {
children = append(children, c.References)
}
if c.Check != nil {
children = append(children, c.Check)
}
return children
}
// TableConstraint represents a table constraint
type TableConstraint struct {
Name string
Type string // PRIMARY KEY, UNIQUE, FOREIGN KEY, CHECK
Columns []string
References *ReferenceDefinition
Check Expression
}
func (t *TableConstraint) expressionNode() {}
func (t TableConstraint) TokenLiteral() string { return t.Type }
func (t TableConstraint) Children() []Node {
children := make([]Node, 0)
if t.References != nil {
children = append(children, t.References)
}
if t.Check != nil {
children = append(children, t.Check)
}
return children
}
// ReferenceDefinition represents a REFERENCES clause
type ReferenceDefinition struct {
Table string
Columns []string
OnDelete string
OnUpdate string
Match string
}
func (r *ReferenceDefinition) expressionNode() {}
func (r ReferenceDefinition) TokenLiteral() string { return "REFERENCES" }
func (r ReferenceDefinition) Children() []Node { return nil }
// PartitionBy represents a PARTITION BY clause
type PartitionBy struct {
Type string // RANGE, LIST, HASH
Columns []string
Boundary []Expression
}
func (p *PartitionBy) expressionNode() {}
func (p PartitionBy) TokenLiteral() string { return "PARTITION BY" }
func (p PartitionBy) Children() []Node { return nodifyExpressions(p.Boundary) }
// TableOption represents table options like ENGINE, CHARSET, etc.
type TableOption struct {
Name string
Value string
}
func (t *TableOption) expressionNode() {}
func (t TableOption) TokenLiteral() string { return t.Name }
func (t TableOption) Children() []Node { return nil }
// UpdateExpression represents a column=value expression in UPDATE
type UpdateExpression struct {
Column Expression
Value Expression
}
func (u *UpdateExpression) expressionNode() {}
func (u UpdateExpression) TokenLiteral() string { return "=" }
func (u UpdateExpression) Children() []Node { return []Node{u.Column, u.Value} }
// DeleteStatement represents a DELETE SQL statement
type DeleteStatement struct {
With *WithClause
TableName string
Alias string
Using []TableReference
Where Expression
Returning []Expression
}
func (d *DeleteStatement) statementNode() {}
func (d DeleteStatement) TokenLiteral() string { return "DELETE" }
func (d DeleteStatement) Children() []Node {
children := make([]Node, 0)
if d.With != nil {
children = append(children, d.With)
}
for _, using := range d.Using {
using := using // G601: Create local copy to avoid memory aliasing
children = append(children, &using)
}
if d.Where != nil {
children = append(children, d.Where)
}
children = append(children, nodifyExpressions(d.Returning)...)
return children
}
// AlterTableStatement represents an ALTER TABLE statement
type AlterTableStatement struct {
Table string
Actions []AlterTableAction
}
func (a *AlterTableStatement) statementNode() {}
func (a AlterTableStatement) TokenLiteral() string { return "ALTER TABLE" }
func (a AlterTableStatement) Children() []Node {
children := make([]Node, len(a.Actions))
for i, action := range a.Actions {
action := action // G601: Create local copy to avoid memory aliasing
children[i] = &action
}
return children
}
// AlterTableAction represents an action in ALTER TABLE
type AlterTableAction struct {
Type string // ADD COLUMN, DROP COLUMN, MODIFY COLUMN, etc.
ColumnName string
ColumnDef *ColumnDef
Constraint *TableConstraint
}
func (a *AlterTableAction) expressionNode() {}
func (a AlterTableAction) TokenLiteral() string { return a.Type }
func (a AlterTableAction) Children() []Node {
children := make([]Node, 0)
if a.ColumnDef != nil {
children = append(children, a.ColumnDef)
}
if a.Constraint != nil {
children = append(children, a.Constraint)
}
return children
}
// CreateIndexStatement represents a CREATE INDEX statement
type CreateIndexStatement struct {
Unique bool
IfNotExists bool
Name string
Table string
Columns []IndexColumn
Using string
Where Expression
}
func (c *CreateIndexStatement) statementNode() {}
func (c CreateIndexStatement) TokenLiteral() string { return "CREATE INDEX" }
func (c CreateIndexStatement) Children() []Node {
children := make([]Node, 0)
for _, col := range c.Columns {
col := col // G601: Create local copy to avoid memory aliasing
children = append(children, &col)
}
if c.Where != nil {
children = append(children, c.Where)
}
return children
}
// IndexColumn represents a column in an index definition
type IndexColumn struct {
Column string
Collate string
Direction string // ASC, DESC
NullsLast bool
}
func (i *IndexColumn) expressionNode() {}
func (i IndexColumn) TokenLiteral() string { return i.Column }
func (i IndexColumn) Children() []Node { return nil }
// AST represents the root of the Abstract Syntax Tree
type AST struct {
Statements []Statement
}
func (a AST) TokenLiteral() string { return "" }
func (a AST) Children() []Node {
children := make([]Node, len(a.Statements))
for i, stmt := range a.Statements {
children[i] = stmt
}
return children
}