-
Notifications
You must be signed in to change notification settings - Fork 23
/
ast.go
3629 lines (3205 loc) · 90.5 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
package sql
import (
"bytes"
"fmt"
"strings"
)
type Node interface {
node()
fmt.Stringer
}
func (*AlterTableStatement) node() {}
func (*AnalyzeStatement) node() {}
func (*Assignment) node() {}
func (*BeginStatement) node() {}
func (*BinaryExpr) node() {}
func (*BindExpr) node() {}
func (*BlobLit) node() {}
func (*BoolLit) node() {}
func (*Call) node() {}
func (*CaseBlock) node() {}
func (*CaseExpr) node() {}
func (*CastExpr) node() {}
func (*CheckConstraint) node() {}
func (*CollateConstraint) node() {}
func (*ColumnDefinition) node() {}
func (*CommitStatement) node() {}
func (*CreateIndexStatement) node() {}
func (*CreateTableStatement) node() {}
func (*CreateTriggerStatement) node() {}
func (*CreateViewStatement) node() {}
func (*DefaultConstraint) node() {}
func (*DeleteStatement) node() {}
func (*DropIndexStatement) node() {}
func (*DropTableStatement) node() {}
func (*DropTriggerStatement) node() {}
func (*DropViewStatement) node() {}
func (*Exists) node() {}
func (*ExplainStatement) node() {}
func (*ExprList) node() {}
func (*FilterClause) node() {}
func (*ForeignKeyArg) node() {}
func (*ForeignKeyConstraint) node() {}
func (*FrameSpec) node() {}
func (*GeneratedConstraint) node() {}
func (*Ident) node() {}
func (*IndexedColumn) node() {}
func (*InsertStatement) node() {}
func (*JoinClause) node() {}
func (*JoinOperator) node() {}
func (*NotNullConstraint) node() {}
func (*NullLit) node() {}
func (*NumberLit) node() {}
func (*OnConstraint) node() {}
func (*OrderingTerm) node() {}
func (*OverClause) node() {}
func (*ParenExpr) node() {}
func (*ParenSource) node() {}
func (*PrimaryKeyConstraint) node() {}
func (*QualifiedRef) node() {}
func (*QualifiedTableName) node() {}
func (*QualifiedTableFunctionName) node() {}
func (*Raise) node() {}
func (*Range) node() {}
func (*ReleaseStatement) node() {}
func (*ResultColumn) node() {}
func (*ReturningClause) node() {}
func (*RollbackStatement) node() {}
func (*SavepointStatement) node() {}
func (*SelectStatement) node() {}
func (*StringLit) node() {}
func (*TimestampLit) node() {}
func (*Type) node() {}
func (*UnaryExpr) node() {}
func (*UniqueConstraint) node() {}
func (*UpdateStatement) node() {}
func (*UpsertClause) node() {}
func (*UsingConstraint) node() {}
func (*Window) node() {}
func (*WindowDefinition) node() {}
func (*WithClause) node() {}
type Statement interface {
Node
stmt()
}
func (*AlterTableStatement) stmt() {}
func (*AnalyzeStatement) stmt() {}
func (*BeginStatement) stmt() {}
func (*CommitStatement) stmt() {}
func (*CreateIndexStatement) stmt() {}
func (*CreateTableStatement) stmt() {}
func (*CreateTriggerStatement) stmt() {}
func (*CreateViewStatement) stmt() {}
func (*DeleteStatement) stmt() {}
func (*DropIndexStatement) stmt() {}
func (*DropTableStatement) stmt() {}
func (*DropTriggerStatement) stmt() {}
func (*DropViewStatement) stmt() {}
func (*ExplainStatement) stmt() {}
func (*InsertStatement) stmt() {}
func (*ReleaseStatement) stmt() {}
func (*RollbackStatement) stmt() {}
func (*SavepointStatement) stmt() {}
func (*SelectStatement) stmt() {}
func (*UpdateStatement) stmt() {}
// CloneStatement returns a deep copy stmt.
func CloneStatement(stmt Statement) Statement {
if stmt == nil {
return nil
}
switch stmt := stmt.(type) {
case *AlterTableStatement:
return stmt.Clone()
case *AnalyzeStatement:
return stmt.Clone()
case *BeginStatement:
return stmt.Clone()
case *CommitStatement:
return stmt.Clone()
case *CreateIndexStatement:
return stmt.Clone()
case *CreateTableStatement:
return stmt.Clone()
case *CreateTriggerStatement:
return stmt.Clone()
case *CreateViewStatement:
return stmt.Clone()
case *DeleteStatement:
return stmt.Clone()
case *DropIndexStatement:
return stmt.Clone()
case *DropTableStatement:
return stmt.Clone()
case *DropTriggerStatement:
return stmt.Clone()
case *DropViewStatement:
return stmt.Clone()
case *ExplainStatement:
return stmt.Clone()
case *InsertStatement:
return stmt.Clone()
case *ReleaseStatement:
return stmt.Clone()
case *RollbackStatement:
return stmt.Clone()
case *SavepointStatement:
return stmt.Clone()
case *SelectStatement:
return stmt.Clone()
case *UpdateStatement:
return stmt.Clone()
default:
panic(fmt.Sprintf("invalid statement type: %T", stmt))
}
}
func cloneStatements(a []Statement) []Statement {
if a == nil {
return nil
}
other := make([]Statement, len(a))
for i := range a {
other[i] = CloneStatement(a[i])
}
return other
}
// StatementSource returns the root statement for a statement.
func StatementSource(stmt Statement) Source {
switch stmt := stmt.(type) {
case *SelectStatement:
return stmt.Source
case *UpdateStatement:
return stmt.Table
case *DeleteStatement:
return stmt.Table
default:
return nil
}
}
type Expr interface {
Node
expr()
}
func (*BinaryExpr) expr() {}
func (*BindExpr) expr() {}
func (*BlobLit) expr() {}
func (*BoolLit) expr() {}
func (*Call) expr() {}
func (*CaseExpr) expr() {}
func (*CastExpr) expr() {}
func (*Exists) expr() {}
func (*ExprList) expr() {}
func (*Ident) expr() {}
func (*NullLit) expr() {}
func (*NumberLit) expr() {}
func (*ParenExpr) expr() {}
func (*QualifiedRef) expr() {}
func (*Raise) expr() {}
func (*Range) expr() {}
func (*StringLit) expr() {}
func (*TimestampLit) expr() {}
func (*UnaryExpr) expr() {}
func (SelectExpr) expr() {}
// CloneExpr returns a deep copy expr.
func CloneExpr(expr Expr) Expr {
if expr == nil {
return nil
}
switch expr := expr.(type) {
case *BinaryExpr:
return expr.Clone()
case *BindExpr:
return expr.Clone()
case *BlobLit:
return expr.Clone()
case *BoolLit:
return expr.Clone()
case *Call:
return expr.Clone()
case *CaseExpr:
return expr.Clone()
case *CastExpr:
return expr.Clone()
case *Exists:
return expr.Clone()
case *ExprList:
return expr.Clone()
case *Ident:
return expr.Clone()
case *NullLit:
return expr.Clone()
case *NumberLit:
return expr.Clone()
case *ParenExpr:
return expr.Clone()
case *QualifiedRef:
return expr.Clone()
case *Raise:
return expr.Clone()
case *Range:
return expr.Clone()
case *StringLit:
return expr.Clone()
case *TimestampLit:
return expr.Clone()
case *UnaryExpr:
return expr.Clone()
default:
panic(fmt.Sprintf("invalid expr type: %T", expr))
}
}
func cloneExprs(a []Expr) []Expr {
if a == nil {
return nil
}
other := make([]Expr, len(a))
for i := range a {
other[i] = CloneExpr(a[i])
}
return other
}
// ExprString returns the string representation of expr.
// Returns a blank string if expr is nil.
func ExprString(expr Expr) string {
if expr == nil {
return ""
}
return expr.String()
}
// SplitExprTree splits apart expr so it is a list of all AND joined expressions.
// For example, the expression "A AND B AND (C OR (D AND E))" would be split into
// a list of "A", "B", "C OR (D AND E)".
func SplitExprTree(expr Expr) []Expr {
if expr == nil {
return nil
}
var a []Expr
splitExprTree(expr, &a)
return a
}
func splitExprTree(expr Expr, a *[]Expr) {
switch expr := expr.(type) {
case *BinaryExpr:
if expr.Op != AND {
*a = append(*a, expr)
return
}
splitExprTree(expr.X, a)
splitExprTree(expr.Y, a)
case *ParenExpr:
splitExprTree(expr.X, a)
default:
*a = append(*a, expr)
}
}
// Scope represents a context for name resolution.
// Names can be resolved at the current source or in parent scopes.
type Scope struct {
Parent *Scope
Source Source
}
// Source represents a table or subquery.
type Source interface {
Node
source()
}
func (*JoinClause) source() {}
func (*ParenSource) source() {}
func (*QualifiedTableName) source() {}
func (*QualifiedTableFunctionName) source() {}
func (*SelectStatement) source() {}
// CloneSource returns a deep copy src.
func CloneSource(src Source) Source {
if src == nil {
return nil
}
switch src := src.(type) {
case *JoinClause:
return src.Clone()
case *ParenSource:
return src.Clone()
case *QualifiedTableName:
return src.Clone()
case *SelectStatement:
return src.Clone()
default:
panic(fmt.Sprintf("invalid source type: %T", src))
}
}
// SourceName returns the name of the source.
// Only returns for QualifiedTableName & ParenSource.
func SourceName(src Source) string {
switch src := src.(type) {
case *JoinClause, *SelectStatement:
return ""
case *ParenSource:
return IdentName(src.Alias)
case *QualifiedTableName:
return src.TableName()
default:
return ""
}
}
// SourceList returns a list of scopes in the current scope.
func SourceList(src Source) []Source {
var a []Source
ForEachSource(src, func(s Source) bool {
a = append(a, s)
return true
})
return a
}
// ForEachSource calls fn for every source within the current scope.
// Stops iteration if fn returns false.
func ForEachSource(src Source, fn func(Source) bool) {
forEachSource(src, fn)
}
func forEachSource(src Source, fn func(Source) bool) bool {
if !fn(src) {
return false
}
switch src := src.(type) {
case *JoinClause:
if !forEachSource(src.X, fn) {
return false
} else if !forEachSource(src.Y, fn) {
return false
}
case *SelectStatement:
if !forEachSource(src.Source, fn) {
return false
}
}
return true
}
// ResolveSource returns a source with the given name.
// This can either be the table name or the alias for a source.
func ResolveSource(root Source, name string) Source {
var ret Source
ForEachSource(root, func(src Source) bool {
switch src := src.(type) {
case *ParenSource:
if IdentName(src.Alias) == name {
ret = src
}
case *QualifiedTableName:
if src.TableName() == name {
ret = src
}
}
return ret == nil // continue until we find the matching source
})
return ret
}
// JoinConstraint represents either an ON or USING join constraint.
type JoinConstraint interface {
Node
joinConstraint()
}
func (*OnConstraint) joinConstraint() {}
func (*UsingConstraint) joinConstraint() {}
// CloneJoinConstraint returns a deep copy cons.
func CloneJoinConstraint(cons JoinConstraint) JoinConstraint {
if cons == nil {
return nil
}
switch cons := cons.(type) {
case *OnConstraint:
return cons.Clone()
case *UsingConstraint:
return cons.Clone()
default:
panic(fmt.Sprintf("invalid join constraint type: %T", cons))
}
}
type ExplainStatement struct {
Explain Pos // position of EXPLAIN
Query Pos // position of QUERY (optional)
QueryPlan Pos // position of PLAN after QUERY (optional)
Stmt Statement // target statement
}
// Clone returns a deep copy of s.
func (s *ExplainStatement) Clone() *ExplainStatement {
if s == nil {
return nil
}
other := *s
other.Stmt = CloneStatement(s.Stmt)
return &other
}
// String returns the string representation of the statement.
func (s *ExplainStatement) String() string {
var buf bytes.Buffer
buf.WriteString("EXPLAIN")
if s.QueryPlan.IsValid() {
buf.WriteString(" QUERY PLAN")
}
fmt.Fprintf(&buf, " %s", s.Stmt.String())
return buf.String()
}
type BeginStatement struct {
Begin Pos // position of BEGIN
Deferred Pos // position of DEFERRED keyword
Immediate Pos // position of IMMEDIATE keyword
Exclusive Pos // position of EXCLUSIVE keyword
Transaction Pos // position of TRANSACTION keyword (optional)
}
// Clone returns a deep copy of s.
func (s *BeginStatement) Clone() *BeginStatement {
if s == nil {
return nil
}
other := *s
return &other
}
// String returns the string representation of the statement.
func (s *BeginStatement) String() string {
var buf bytes.Buffer
buf.WriteString("BEGIN")
if s.Deferred.IsValid() {
buf.WriteString(" DEFERRED")
} else if s.Immediate.IsValid() {
buf.WriteString(" IMMEDIATE")
} else if s.Exclusive.IsValid() {
buf.WriteString(" EXCLUSIVE")
}
if s.Transaction.IsValid() {
buf.WriteString(" TRANSACTION")
}
return buf.String()
}
type CommitStatement struct {
Commit Pos // position of COMMIT keyword
End Pos // position of END keyword
Transaction Pos // position of TRANSACTION keyword (optional)
}
// Clone returns a deep copy of s.
func (s *CommitStatement) Clone() *CommitStatement {
if s == nil {
return nil
}
other := *s
return &other
}
// String returns the string representation of the statement.
func (s *CommitStatement) String() string {
var buf bytes.Buffer
if s.End.IsValid() {
buf.WriteString("END")
} else {
buf.WriteString("COMMIT")
}
if s.Transaction.IsValid() {
buf.WriteString(" TRANSACTION")
}
return buf.String()
}
type RollbackStatement struct {
Rollback Pos // position of ROLLBACK keyword
Transaction Pos // position of TRANSACTION keyword (optional)
To Pos // position of TO keyword (optional)
Savepoint Pos // position of SAVEPOINT keyword (optional)
SavepointName *Ident // name of savepoint
}
// Clone returns a deep copy of s.
func (s *RollbackStatement) Clone() *RollbackStatement {
if s == nil {
return s
}
other := *s
other.SavepointName = s.SavepointName.Clone()
return &other
}
// String returns the string representation of the statement.
func (s *RollbackStatement) String() string {
var buf bytes.Buffer
buf.WriteString("ROLLBACK")
if s.Transaction.IsValid() {
buf.WriteString(" TRANSACTION")
}
if s.SavepointName != nil {
buf.WriteString(" TO")
if s.Savepoint.IsValid() {
buf.WriteString(" SAVEPOINT")
}
fmt.Fprintf(&buf, " %s", s.SavepointName.String())
}
return buf.String()
}
type SavepointStatement struct {
Savepoint Pos // position of SAVEPOINT keyword
Name *Ident // name of savepoint
}
// Clone returns a deep copy of s.
func (s *SavepointStatement) Clone() *SavepointStatement {
if s == nil {
return s
}
other := *s
other.Name = s.Name.Clone()
return &other
}
// String returns the string representation of the statement.
func (s *SavepointStatement) String() string {
return fmt.Sprintf("SAVEPOINT %s", s.Name.String())
}
type ReleaseStatement struct {
Release Pos // position of RELEASE keyword
Savepoint Pos // position of SAVEPOINT keyword (optional)
Name *Ident // name of savepoint
}
// Clone returns a deep copy of s.
func (s *ReleaseStatement) Clone() *ReleaseStatement {
if s == nil {
return s
}
other := *s
other.Name = s.Name.Clone()
return &other
}
// String returns the string representation of the statement.
func (s *ReleaseStatement) String() string {
var buf bytes.Buffer
buf.WriteString("RELEASE")
if s.Savepoint.IsValid() {
buf.WriteString(" SAVEPOINT")
}
fmt.Fprintf(&buf, " %s", s.Name.String())
return buf.String()
}
type CreateTableStatement struct {
Create Pos // position of CREATE keyword
Table Pos // position of CREATE keyword
If Pos // position of IF keyword (optional)
IfNot Pos // position of NOT keyword (optional)
IfNotExists Pos // position of EXISTS keyword (optional)
Name *Ident // table name
Lparen Pos // position of left paren of column list
Columns []*ColumnDefinition // column definitions
Constraints []Constraint // table constraints
Rparen Pos // position of right paren of column list
Without Pos // position of WITHOUT keyword (optional)
Rowid Pos // position of ROWID keyword (optional)
Strict Pos // position of STRICT keyword (optional)
As Pos // position of AS keyword (optional)
Select *SelectStatement // select stmt to build from
}
// Clone returns a deep copy of s.
func (s *CreateTableStatement) Clone() *CreateTableStatement {
if s == nil {
return s
}
other := *s
other.Name = s.Name.Clone()
other.Columns = cloneColumnDefinitions(s.Columns)
other.Constraints = cloneConstraints(s.Constraints)
other.Select = s.Select.Clone()
return &other
}
// String returns the string representation of the statement.
func (s *CreateTableStatement) String() string {
var buf bytes.Buffer
buf.WriteString("CREATE TABLE")
if s.IfNotExists.IsValid() {
buf.WriteString(" IF NOT EXISTS")
}
buf.WriteString(" ")
buf.WriteString(s.Name.String())
if s.Select != nil {
buf.WriteString(" AS ")
buf.WriteString(s.Select.String())
} else {
buf.WriteString(" (")
for i := range s.Columns {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(s.Columns[i].String())
}
for i := range s.Constraints {
buf.WriteString(", ")
buf.WriteString(s.Constraints[i].String())
}
buf.WriteString(")")
}
return buf.String()
}
type ColumnDefinition struct {
Name *Ident // column name
Type *Type // data type
Constraints []Constraint // column constraints
}
// Clone returns a deep copy of d.
func (d *ColumnDefinition) Clone() *ColumnDefinition {
if d == nil {
return d
}
other := *d
other.Name = d.Name.Clone()
other.Type = d.Type.Clone()
other.Constraints = cloneConstraints(d.Constraints)
return &other
}
func cloneColumnDefinitions(a []*ColumnDefinition) []*ColumnDefinition {
if a == nil {
return nil
}
other := make([]*ColumnDefinition, len(a))
for i := range a {
other[i] = a[i].Clone()
}
return other
}
// String returns the string representation of the statement.
func (c *ColumnDefinition) String() string {
var buf bytes.Buffer
buf.WriteString(c.Name.String())
if c.Type != nil {
buf.WriteString(" ")
buf.WriteString(c.Type.String())
}
for i := range c.Constraints {
buf.WriteString(" ")
buf.WriteString(c.Constraints[i].String())
}
return buf.String()
}
type Constraint interface {
Node
constraint()
}
func (*PrimaryKeyConstraint) constraint() {}
func (*NotNullConstraint) constraint() {}
func (*UniqueConstraint) constraint() {}
func (*CheckConstraint) constraint() {}
func (*DefaultConstraint) constraint() {}
func (*GeneratedConstraint) constraint() {}
func (*CollateConstraint) constraint() {}
func (*ForeignKeyConstraint) constraint() {}
// CloneConstraint returns a deep copy cons.
func CloneConstraint(cons Constraint) Constraint {
if cons == nil {
return nil
}
switch cons := cons.(type) {
case *PrimaryKeyConstraint:
return cons.Clone()
case *NotNullConstraint:
return cons.Clone()
case *UniqueConstraint:
return cons.Clone()
case *CheckConstraint:
return cons.Clone()
case *DefaultConstraint:
return cons.Clone()
case *GeneratedConstraint:
return cons.Clone()
case *CollateConstraint:
return cons.Clone()
case *ForeignKeyConstraint:
return cons.Clone()
default:
panic(fmt.Sprintf("invalid constraint type: %T", cons))
}
}
func cloneConstraints(a []Constraint) []Constraint {
if a == nil {
return nil
}
other := make([]Constraint, len(a))
for i := range a {
other[i] = CloneConstraint(a[i])
}
return other
}
type PrimaryKeyConstraint struct {
Constraint Pos // position of CONSTRAINT keyword
Name *Ident // constraint name
Primary Pos // position of PRIMARY keyword
Key Pos // position of KEY keyword
Lparen Pos // position of left paren (table only)
Columns []*Ident // indexed columns (table only)
Rparen Pos // position of right paren (table only)
Autoincrement Pos // position of AUTOINCREMENT keyword (column only)
}
// Clone returns a deep copy of c.
func (c *PrimaryKeyConstraint) Clone() *PrimaryKeyConstraint {
if c == nil {
return c
}
other := *c
other.Name = c.Name.Clone()
other.Columns = cloneIdents(c.Columns)
return &other
}
// String returns the string representation of the constraint.
func (c *PrimaryKeyConstraint) String() string {
var buf bytes.Buffer
if c.Name != nil {
buf.WriteString("CONSTRAINT ")
buf.WriteString(c.Name.String())
buf.WriteString(" ")
}
buf.WriteString("PRIMARY KEY")
if len(c.Columns) > 0 {
buf.WriteString(" (")
for i := range c.Columns {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(c.Columns[i].String())
}
buf.WriteString(")")
}
if c.Autoincrement.IsValid() {
buf.WriteString(" AUTOINCREMENT")
}
return buf.String()
}
type NotNullConstraint struct {
Constraint Pos // position of CONSTRAINT keyword
Name *Ident // constraint name
Not Pos // position of NOT keyword
Null Pos // position of NULL keyword
}
// Clone returns a deep copy of c.
func (c *NotNullConstraint) Clone() *NotNullConstraint {
if c == nil {
return c
}
other := *c
other.Name = c.Name.Clone()
return &other
}
// String returns the string representation of the constraint.
func (c *NotNullConstraint) String() string {
var buf bytes.Buffer
if c.Name != nil {
buf.WriteString("CONSTRAINT ")
buf.WriteString(c.Name.String())
buf.WriteString(" ")
}
buf.WriteString("NOT NULL")
return buf.String()
}
type UniqueConstraint struct {
Constraint Pos // position of CONSTRAINT keyword
Name *Ident // constraint name
Unique Pos // position of UNIQUE keyword
Lparen Pos // position of left paren (table only)
Columns []*IndexedColumn // indexed columns (table only)
Rparen Pos // position of right paren (table only)
}
// Clone returns a deep copy of c.
func (c *UniqueConstraint) Clone() *UniqueConstraint {
if c == nil {
return c
}
other := *c
other.Name = c.Name.Clone()
other.Columns = make([]*IndexedColumn, len(c.Columns))
for i := range c.Columns {
other.Columns[i] = c.Columns[i].Clone()
}
return &other
}
// String returns the string representation of the constraint.
func (c *UniqueConstraint) String() string {
var buf bytes.Buffer
if c.Name != nil {
buf.WriteString("CONSTRAINT ")
buf.WriteString(c.Name.String())
buf.WriteString(" ")
}
buf.WriteString("UNIQUE")
if len(c.Columns) > 0 {
buf.WriteString(" (")
for i := range c.Columns {
if i != 0 {
buf.WriteString(", ")
}
buf.WriteString(c.Columns[i].String())
}
buf.WriteString(")")
}
return buf.String()
}
type CheckConstraint struct {
Constraint Pos // position of CONSTRAINT keyword
Name *Ident // constraint name
Check Pos // position of UNIQUE keyword
Lparen Pos // position of left paren
Expr Expr // check expression
Rparen Pos // position of right paren
}
// Clone returns a deep copy of c.
func (c *CheckConstraint) Clone() *CheckConstraint {
if c == nil {
return c
}
other := *c
other.Name = c.Name.Clone()
other.Expr = CloneExpr(c.Expr)
return &other
}
// String returns the string representation of the constraint.
func (c *CheckConstraint) String() string {
var buf bytes.Buffer
if c.Name != nil {
buf.WriteString("CONSTRAINT ")
buf.WriteString(c.Name.String())
buf.WriteString(" ")
}
buf.WriteString("CHECK (")
buf.WriteString(c.Expr.String())
buf.WriteString(")")
return buf.String()
}
type DefaultConstraint struct {
Constraint Pos // position of CONSTRAINT keyword
Name *Ident // constraint name
Default Pos // position of DEFAULT keyword
Lparen Pos // position of left paren
Expr Expr // default expression
Rparen Pos // position of right paren
}
// Clone returns a deep copy of c.
func (c *DefaultConstraint) Clone() *DefaultConstraint {
if c == nil {
return c
}
other := *c
other.Name = c.Name.Clone()
other.Expr = CloneExpr(c.Expr)
return &other
}
// String returns the string representation of the constraint.
func (c *DefaultConstraint) String() string {
var buf bytes.Buffer
if c.Name != nil {
buf.WriteString("CONSTRAINT ")
buf.WriteString(c.Name.String())
buf.WriteString(" ")
}
buf.WriteString("DEFAULT ")
if c.Lparen.IsValid() {
buf.WriteString("(")
buf.WriteString(c.Expr.String())
buf.WriteString(")")
} else {
buf.WriteString(c.Expr.String())
}
return buf.String()
}
type GeneratedConstraint struct {
Constraint Pos // position of CONSTRAINT keyword
Name *Ident // constraint name
Generated Pos // position of GENERATED keyword
Always Pos // position of ALWAYS keyword
As Pos // position of AS keyword
Lparen Pos // position of left paren