Repository navigation
Expand file tree
/
Copy pathconvert.go
More file actions
1185 lines (1076 loc) · 29.5 KB
/
Copy pathconvert.go
File metadata and controls
1185 lines (1076 loc) · 29.5 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 sqlite
import (
"fmt"
"log"
"strconv"
"strings"
meyer "github.com/sqlc-dev/meyer/ast"
"github.com/sqlc-dev/sqlc/internal/debug"
"github.com/sqlc-dev/sqlc/internal/sql/ast"
)
// cc converts a meyer syntax tree into sqlc's engine-independent AST. One
// converter is used per statement; paramCount numbers the anonymous "?"
// markers within it, and src is the source the statement was parsed from.
type cc struct {
paramCount int
src string
}
func todo(funcname string, n meyer.Node) *ast.TODO {
if debug.Active {
log.Printf("sqlite.%s: Unknown node type %T\n", funcname, n)
}
return &ast.TODO{}
}
func (c *cc) convert(node meyer.Node) ast.Node {
switch n := node.(type) {
// Statements
case *meyer.AlterTableStmt:
return c.convertAlterTableStmt(n)
case *meyer.AttachStmt:
return c.convertAttachStmt(n)
case *meyer.CreateTableStmt:
return c.convertCreateTableStmt(n)
case *meyer.CreateViewStmt:
return c.convertCreateViewStmt(n)
case *meyer.CreateVirtualTableStmt:
return c.convertCreateVirtualTableStmt(n)
case *meyer.DeleteStmt:
return c.convertDeleteStmt(n)
case *meyer.DropTableStmt:
return c.convertDropStmt(n.Name, n.IfExists)
case *meyer.DropViewStmt:
return c.convertDropStmt(n.Name, n.IfExists)
case *meyer.ExplainStmt:
return c.convert(n.Stmt)
case *meyer.InsertStmt:
return c.convertInsertStmt(n)
case *meyer.SelectStmt:
return c.convertSelectStmt(n)
case *meyer.UpdateStmt:
return c.convertUpdateStmt(n)
// Expressions
case *meyer.BetweenExpr:
return c.convertBetweenExpr(n)
case *meyer.BinaryExpr:
return c.convertBinaryExpr(n)
case *meyer.BindParam:
return c.convertBindParam(n)
case *meyer.CaseExpr:
return c.convertCaseExpr(n)
case *meyer.CastExpr:
return c.convertCastExpr(n)
case *meyer.CollateExpr:
return c.convertCollateExpr(n)
case *meyer.ExistsExpr:
return &ast.SubLink{
SubLinkType: ast.EXISTS_SUBLINK,
Subselect: c.convert(n.Select),
Location: n.Pos(),
}
case *meyer.FuncCall:
return c.convertFuncCall(n)
case *meyer.Ident:
return c.columnRef(n.Pos(), n)
case *meyer.InExpr:
return c.convertInExpr(n)
case *meyer.IsExpr:
return c.convertIsExpr(n)
case *meyer.LikeExpr:
return c.convertLikeExpr(n)
case *meyer.Literal:
return c.convertLiteral(n)
case *meyer.NullCheckExpr:
return c.convertNullCheckExpr(n)
case *meyer.ParenExpr:
return c.convert(n.X)
case *meyer.QualifiedRef:
return c.columnRef(n.Pos(), n.Parts...)
case *meyer.Star:
return c.convertStar(n)
case *meyer.SubqueryExpr:
return &ast.SubLink{
SubLinkType: ast.EXPR_SUBLINK,
Subselect: c.convert(n.Select),
Location: n.Pos(),
}
case *meyer.UnaryExpr:
return c.convertUnaryExpr(n)
case *meyer.VectorExpr:
return c.convertExprList(n.List)
default:
return todo("convert(case=default)", node)
}
}
// convertExpr converts an optional expression, mapping an absent one to a
// nil node rather than to a TODO.
func (c *cc) convertExpr(e meyer.Expr) ast.Node {
if e == nil {
return nil
}
return c.convert(e)
}
func (c *cc) convertExprList(exprs []meyer.Expr) *ast.List {
list := &ast.List{Items: []ast.Node{}}
for _, e := range exprs {
list.Items = append(list.Items, c.convert(e))
}
return list
}
// columnRef builds the dotted reference sqlc uses for a column: a one, two
// or three part name of the form [[schema.]table.]column.
func (c *cc) columnRef(loc int, parts ...*meyer.Ident) *ast.ColumnRef {
items := make([]ast.Node, 0, len(parts))
for _, p := range parts {
items = append(items, NewIdentifier(p))
}
return &ast.ColumnRef{
Fields: &ast.List{Items: items},
Location: loc,
}
}
func (c *cc) convertStar(n *meyer.Star) *ast.ColumnRef {
items := []ast.Node{}
if n.Table != nil {
items = append(items, NewIdentifier(n.Table))
}
items = append(items, &ast.A_Star{})
return &ast.ColumnRef{
Fields: &ast.List{Items: items},
Location: n.Pos(),
}
}
//
// Statements
//
func (c *cc) convertAlterTableStmt(n *meyer.AlterTableStmt) ast.Node {
switch n.Action {
case meyer.AlterRenameTable:
name := identifier(n.NewName)
return &ast.RenameTableStmt{
Table: parseTableName(n.Table),
NewName: &name,
}
case meyer.AlterRenameColumn:
name := identifier(n.NewName)
return &ast.RenameColumnStmt{
Table: parseTableName(n.Table),
Col: &ast.ColumnRef{
Name: identifier(n.Column),
},
NewName: &name,
}
case meyer.AlterAddColumn:
def := n.ColumnDef
if def == nil {
break
}
name := identifier(def.Name)
return &ast.AlterTableStmt{
Table: parseTableName(n.Table),
Incomplete: !representableColumn(def),
Cmds: &ast.List{Items: []ast.Node{
&ast.AlterTableCmd{
Name: &name,
Subtype: ast.AT_AddColumn,
Def: &ast.ColumnDef{
Colname: name,
TypeName: &ast.TypeName{
Name: columnTypeName(def.Type),
Spelling: typeSpelling(def.Type),
},
Typeless: def.Type == nil,
IsNotNull: hasNotNullConstraint(def.Constraints),
PrimaryKey: hasPrimaryKeyConstraint(def.Constraints),
},
},
}},
}
case meyer.AlterDropColumn:
name := identifier(n.Column)
return &ast.AlterTableStmt{
Table: parseTableName(n.Table),
Cmds: &ast.List{Items: []ast.Node{
&ast.AlterTableCmd{
Name: &name,
Subtype: ast.AT_DropColumn,
},
}},
}
}
return todo("convertAlterTableStmt", n)
}
// convertAttachStmt records an attached database as a schema. Only the name
// matters to sqlc; the file expression is a runtime concern.
func (c *cc) convertAttachStmt(n *meyer.AttachStmt) ast.Node {
name := schemaName(n.Name)
return &ast.CreateSchemaStmt{
Name: &name,
}
}
// schemaName reads the schema out of the "AS <name>" operand of ATTACH,
// which the grammar accepts as an arbitrary expression.
func schemaName(e meyer.Expr) string {
switch n := e.(type) {
case *meyer.Ident:
return identifier(n)
case *meyer.Literal:
return n.Value
default:
return ""
}
}
// columnTypeName returns the declared type of a column, defaulting to
// SQLite's untyped "any" when the declaration omits one.
func columnTypeName(t *meyer.TypeName) string {
if t == nil {
return "any"
}
return typeName(t)
}
func (c *cc) convertCreateTableStmt(n *meyer.CreateTableStmt) ast.Node {
stmt := &ast.CreateTableStmt{
Name: parseTableName(n.Name),
IfNotExists: n.IfNotExists,
// The node models none of these, so a statement carrying them has
// no faithful rendering and the formatter keeps it as written.
Incomplete: n.Temp || n.Select != nil || len(n.Constraints) > 0 || len(n.Options) > 0,
}
for _, def := range n.Columns {
if !representableColumn(def) {
stmt.Incomplete = true
}
stmt.Cols = append(stmt.Cols, &ast.ColumnDef{
Colname: identifier(def.Name),
IsNotNull: hasNotNullConstraint(def.Constraints),
PrimaryKey: hasPrimaryKeyConstraint(def.Constraints),
TypeName: &ast.TypeName{
Name: columnTypeName(def.Type),
Spelling: typeSpelling(def.Type),
},
Typeless: def.Type == nil,
Location: def.Pos(),
})
}
return stmt
}
func (c *cc) convertCreateVirtualTableStmt(n *meyer.CreateVirtualTableStmt) ast.Node {
switch moduleName := identifier(n.Module); moduleName {
case "fts5":
// https://www.sqlite.org/fts5.html
return c.convertCreateVirtualTableFTS5(n)
default:
return todo(
fmt.Sprintf("create_virtual_table. unsupported module name: %q", moduleName),
n,
)
}
}
func (c *cc) convertCreateVirtualTableFTS5(n *meyer.CreateVirtualTableStmt) ast.Node {
stmt := &ast.CreateTableStmt{
Name: parseTableName(n.Name),
IfNotExists: n.IfNotExists,
Using: identifier(n.Module),
}
if n.HasArgs {
stmt.ModuleArgs = n.Args
if stmt.ModuleArgs == nil {
stmt.ModuleArgs = []string{}
}
// The arguments carry no spans of their own, so the author's exact
// breaks cannot be kept; a declaration written across lines keeps
// the canonical broken form instead.
if n.Pos() >= 0 && n.End() <= len(c.src) {
stmt.ModuleArgsMultiline = strings.Contains(c.src[n.Pos():n.End()], "\n")
}
}
// The module arguments of a virtual table are an arbitrary token
// sequence, so meyer hands them back as source text. For fts5 an
// argument is either a column ("b", or "c UNINDEXED") or one of the
// module's own options, which are always written as "key = value".
for _, arg := range n.Args {
name := fts5ColumnName(arg)
if name == "" {
continue
}
stmt.Cols = append(stmt.Cols, &ast.ColumnDef{
Colname: name,
// fts5 accepts no column constraints, so we supply them here
IsNotNull: true,
TypeName: &ast.TypeName{Name: "text"},
})
}
// An fts5 table also offers hidden columns: one named after the table,
// matched against the whole row (`tbl MATCH ?`) and passed to the
// auxiliary functions (`bm25(tbl)`), `rank`, the current match's score,
// and `rowid`, which an external-content table maps to its content
// table. Hidden keeps them out of star expansions and the table's model.
stmt.Cols = append(stmt.Cols,
&ast.ColumnDef{
Colname: stmt.Name.Name,
IsNotNull: true,
IsHidden: true,
TypeName: &ast.TypeName{Name: "text"},
},
&ast.ColumnDef{
Colname: "rank",
IsHidden: true,
TypeName: &ast.TypeName{Name: "real"},
},
&ast.ColumnDef{
Colname: "rowid",
IsNotNull: true,
IsHidden: true,
TypeName: &ast.TypeName{Name: "integer"},
},
)
return stmt
}
// fts5ColumnName returns the column an fts5 module argument declares, or the
// empty string when the argument configures the module instead.
func fts5ColumnName(arg string) string {
if strings.ContainsRune(arg, '=') {
return ""
}
name, _, _ := strings.Cut(strings.TrimSpace(arg), " ")
return unquoteIdent(name)
}
// unquoteIdent folds a name that has not been through the lexer, as happens
// for the raw module arguments of a virtual table.
func unquoteIdent(s string) string {
if len(s) >= 2 {
switch {
case s[0] == '"' && s[len(s)-1] == '"':
return strings.ReplaceAll(s[1:len(s)-1], `""`, `"`)
case s[0] == '`' && s[len(s)-1] == '`':
return strings.ReplaceAll(s[1:len(s)-1], "``", "`")
case s[0] == '[' && s[len(s)-1] == ']':
return s[1 : len(s)-1]
case s[0] == '\'' && s[len(s)-1] == '\'':
return strings.ReplaceAll(s[1:len(s)-1], `''`, `'`)
}
}
return strings.ToLower(s)
}
func (c *cc) convertCreateViewStmt(n *meyer.CreateViewStmt) ast.Node {
return &ast.ViewStmt{
View: parseRangeVar(n.Name, nil),
Aliases: &ast.List{},
Query: c.convert(n.Select),
Replace: false,
Options: &ast.List{},
WithCheckOption: ast.ViewCheckOption(0),
}
}
func (c *cc) convertDropStmt(name *meyer.QualifiedName, ifExists bool) ast.Node {
return &ast.DropTableStmt{
IfExists: ifExists,
Tables: []*ast.TableName{parseTableName(name)},
}
}
func (c *cc) convertDeleteStmt(n *meyer.DeleteStmt) ast.Node {
return &ast.DeleteStmt{
Relations: &ast.List{Items: []ast.Node{parseRangeVar(n.Table, n.Alias)}},
WhereClause: c.convertExpr(n.Where),
LimitCount: c.limitCount(n.Limit),
ReturningList: c.convertReturning(n.Returning),
WithClause: c.convertWith(n.With),
}
}
// limitCount converts the LIMIT of an UPDATE or DELETE. sqlc has no place to
// put the OFFSET or the ORDER BY that can accompany it, so they are dropped.
func (c *cc) limitCount(n *meyer.Limit) ast.Node {
if n == nil {
return nil
}
return c.convertExpr(n.Count)
}
func (c *cc) convertInsertStmt(n *meyer.InsertStmt) ast.Node {
insert := &ast.InsertStmt{
Relation: parseRangeVar(n.Table, n.Alias),
Cols: c.convertColumnNames(n.Columns),
ReturningList: c.convertReturning(n.Returning),
WithClause: c.convertWith(n.With),
DefaultValues: n.DefaultValues,
}
if n.Select != nil {
if sel, ok := c.convertSelectStmt(n.Select).(*ast.SelectStmt); ok {
insert.SelectStmt = sel
}
}
if len(n.Upserts) > 0 {
insert.OnConflictClause = c.convertUpsert(n.Upserts[0])
}
return insert
}
func (c *cc) convertUpsert(n *meyer.Upsert) *ast.OnConflictClause {
clause := &ast.OnConflictClause{
Action: ast.OnConflictActionNothing,
Location: n.Pos(),
}
if len(n.Target) > 0 || n.TargetWhere != nil {
clause.Infer = &ast.InferClause{
IndexElems: &ast.List{Items: c.convertOrderBy(n.Target)},
WhereClause: c.convertExpr(n.TargetWhere),
}
}
if n.DoNothing {
return clause
}
clause.Action = ast.OnConflictActionUpdate
clause.TargetList = c.convertSetPairs(n.Set)
clause.WhereClause = c.convertExpr(n.Where)
return clause
}
func (c *cc) convertColumnNames(cols []*meyer.Ident) *ast.List {
list := &ast.List{Items: []ast.Node{}}
for _, col := range cols {
name := identifier(col)
list.Items = append(list.Items, &ast.ResTarget{
Name: &name,
Location: col.Pos(),
})
}
return list
}
// convertSetPairs converts the assignment list of an UPDATE or of an upsert's
// DO UPDATE. SQLite allows "(a, b) = expr"; sqlc has no node for a
// multi-column assignment, so each name is given the same value expression.
func (c *cc) convertSetPairs(pairs []*meyer.SetPair) *ast.List {
list := &ast.List{}
for _, pair := range pairs {
val := c.convertExpr(pair.Value)
for _, col := range pair.Columns {
name := identifier(col)
list.Items = append(list.Items, &ast.ResTarget{
Name: &name,
Val: val,
Location: col.Pos(),
})
}
}
return list
}
func (c *cc) convertUpdateStmt(n *meyer.UpdateStmt) ast.Node {
return &ast.UpdateStmt{
Relations: &ast.List{Items: []ast.Node{parseRangeVar(n.Table, n.Alias)}},
TargetList: c.convertSetPairs(n.Set),
FromClause: &ast.List{Items: c.convertFrom(n.From)},
WhereClause: c.convertExpr(n.Where),
LimitCount: c.limitCount(n.Limit),
ReturningList: c.convertReturning(n.Returning),
WithClause: c.convertWith(n.With),
}
}
func (c *cc) convertReturning(cols []*meyer.ResultColumn) *ast.List {
list := &ast.List{Items: []ast.Node{}}
for _, col := range cols {
list.Items = append(list.Items, &ast.ResTarget{
Indirection: &ast.List{},
Val: c.convertExpr(col.Expr),
Location: col.Pos(),
})
}
return list
}
//
// SELECT
//
func (c *cc) convertSelectStmt(n *meyer.SelectStmt) ast.Node {
var stmt *ast.SelectStmt
for i, core := range n.Cores {
sel := c.convertSelectCore(core)
if stmt == nil {
stmt = sel
continue
}
op, all := ast.None, false
if i-1 < len(n.Ops) {
switch n.Ops[i-1].Op {
case "UNION":
op = ast.Union
all = n.Ops[i-1].All
case "INTERSECT":
op = ast.Intersect
case "EXCEPT":
op = ast.Except
}
}
stmt = &ast.SelectStmt{
TargetList: &ast.List{},
FromClause: &ast.List{},
Op: op,
All: all,
Larg: stmt,
Rarg: sel,
}
// ORDER BY and LIMIT are written on the last core of a compound
// select but apply to the compound as a whole.
hoistTail(stmt, stmt.Rarg)
}
if stmt == nil {
return todo("convertSelectStmt", n)
}
stmt.WithClause = c.convertWith(n.With)
return stmt
}
// hoistTail moves the sort and limit clauses of a compound select's final
// core up onto the compound itself.
func hoistTail(outer, inner *ast.SelectStmt) {
outer.SortClause, inner.SortClause = inner.SortClause, nil
outer.LimitCount, inner.LimitCount = inner.LimitCount, nil
outer.LimitOffset, inner.LimitOffset = inner.LimitOffset, nil
}
func (c *cc) convertSelectCore(core meyer.SelectCore) *ast.SelectStmt {
switch n := core.(type) {
case *meyer.SelectQuery:
stmt := &ast.SelectStmt{
TargetList: &ast.List{Items: c.convertResultColumns(n.Columns)},
FromClause: &ast.List{Items: c.convertFrom(n.From)},
WhereClause: c.convertExpr(n.Where),
GroupClause: &ast.List{},
HavingClause: c.convertExpr(n.Having),
WindowClause: &ast.List{},
ValuesLists: &ast.List{},
}
for _, g := range n.GroupBy {
stmt.GroupClause.Items = append(stmt.GroupClause.Items, c.convert(g))
}
if n.Distinct == meyer.DistinctDistinct {
// SQLite has no DISTINCT ON, so the clause carries no columns.
stmt.DistinctClause = &ast.List{Items: []ast.Node{&ast.TODO{}}}
}
for _, w := range n.Windows {
stmt.WindowClause.Items = append(stmt.WindowClause.Items, c.convertWindowDef(w))
}
if len(n.OrderBy) > 0 {
stmt.SortClause = &ast.List{Items: c.convertOrderBy(n.OrderBy)}
}
if n.Limit != nil {
stmt.LimitCount = c.convertExpr(n.Limit.Count)
stmt.LimitOffset = c.convertExpr(n.Limit.Offset)
}
return stmt
case *meyer.ValuesClause:
values := &ast.List{}
for _, row := range n.Rows {
values.Items = append(values.Items, c.convertExprList(row))
}
return &ast.SelectStmt{
TargetList: &ast.List{},
FromClause: &ast.List{},
ValuesLists: values,
}
default:
return &ast.SelectStmt{
TargetList: &ast.List{},
FromClause: &ast.List{},
ValuesLists: &ast.List{},
}
}
}
func (c *cc) convertResultColumns(cols []*meyer.ResultColumn) []ast.Node {
var out []ast.Node
for _, col := range cols {
val := c.convertExpr(col.Expr)
if val == nil {
continue
}
target := &ast.ResTarget{
Val: val,
Location: col.Pos(),
}
if col.Alias != nil {
name := identifier(col.Alias)
target.Name = &name
}
out = append(out, target)
}
return out
}
func (c *cc) convertOrderBy(terms []*meyer.OrderingTerm) []ast.Node {
var out []ast.Node
for _, term := range terms {
sortBy := &ast.SortBy{
Node: c.convertExpr(term.Expr),
UseOp: &ast.List{},
Location: term.Pos(),
}
switch term.Order {
case meyer.SortAsc:
sortBy.SortbyDir = ast.SortByDirAsc
case meyer.SortDesc:
sortBy.SortbyDir = ast.SortByDirDesc
default:
sortBy.SortbyDir = ast.SortByDirDefault
}
switch term.Nulls {
case meyer.NullsFirst:
sortBy.SortbyNulls = ast.SortByNullsFirst
case meyer.NullsLast:
sortBy.SortbyNulls = ast.SortByNullsLast
default:
sortBy.SortbyNulls = ast.SortByNullsDefault
}
out = append(out, sortBy)
}
return out
}
func (c *cc) convertWindowDef(n *meyer.WindowDef) ast.Node {
def := &ast.WindowDef{
PartitionClause: c.convertExprList(n.Partition),
OrderClause: &ast.List{Items: c.convertOrderBy(n.OrderBy)},
Location: n.Pos(),
}
if n.Name != nil {
name := identifier(n.Name)
def.Name = &name
}
if n.Base != nil {
base := identifier(n.Base)
def.Refname = &base
}
return def
}
func (c *cc) convertWith(n *meyer.With) *ast.WithClause {
if n == nil || len(n.CTEs) == 0 {
return nil
}
ctes := &ast.List{}
for _, cte := range n.CTEs {
name := identifier(cte.Name)
cols := &ast.List{}
for _, col := range cte.Columns {
cols.Items = append(cols.Items, NewIdentifier(col))
}
ctes.Items = append(ctes.Items, &ast.CommonTableExpr{
Ctename: &name,
Ctequery: c.convert(cte.Select),
Location: cte.Pos(),
Cterecursive: n.Recursive,
Ctecolnames: cols,
})
}
return &ast.WithClause{
Ctes: ctes,
Recursive: n.Recursive,
Location: n.Pos(),
}
}
// convertFrom converts a SQLite source list. The list is flat, with each
// item recording how it attaches to the one before it, so a chain of joins
// is rebuilt here as a left-leaning tree. A list joined only by commas has
// no join conditions to carry and stays flat.
func (c *cc) convertFrom(refs []*meyer.TableRef) []ast.Node {
if len(refs) == 0 {
return nil
}
if commaOnly(refs) {
out := make([]ast.Node, 0, len(refs))
for _, ref := range refs {
out = append(out, c.convertTableRef(ref))
}
return out
}
table := c.convertTableRef(refs[0])
for _, ref := range refs[1:] {
join := &ast.JoinExpr{
Larg: table,
Rarg: c.convertTableRef(ref),
}
if op := ref.Join; op != nil {
join.IsNatural = op.Type&meyer.JoinNatural != 0
left := op.Type&meyer.JoinLeft != 0
right := op.Type&meyer.JoinRight != 0
switch {
case left && right:
join.Jointype = ast.JoinTypeFull
case left:
join.Jointype = ast.JoinTypeLeft
case right:
join.Jointype = ast.JoinTypeRight
case op.Type&meyer.JoinComma != 0:
join.Jointype = ast.JoinTypeComma
case op.Type&meyer.JoinCross != 0:
join.Jointype = ast.JoinTypeCross
default:
join.Jointype = ast.JoinTypeInner
}
}
if ref.On != nil {
join.Quals = c.convert(ref.On)
}
if len(ref.Using) > 0 {
using := &ast.List{}
for _, col := range ref.Using {
using.Items = append(using.Items, NewIdentifier(col))
}
join.UsingClause = using
}
table = join
}
return []ast.Node{table}
}
func commaOnly(refs []*meyer.TableRef) bool {
for _, ref := range refs[1:] {
if ref.Join == nil || ref.Join.Type&meyer.JoinComma == 0 {
return false
}
}
return true
}
func (c *cc) convertTableRef(n *meyer.TableRef) ast.Node {
switch {
case n.Name != nil && n.HasArgs:
// A table-valued function, such as generate_series(1, 10).
fn := &ast.FuncCall{
Func: &ast.FuncName{Name: identifier(n.Name.Name)},
Funcname: &ast.List{Items: []ast.Node{NewIdentifier(n.Name.Name)}},
Args: c.convertExprList(n.Args),
Location: n.Pos(),
}
if n.Name.Schema != nil {
fn.Func.Schema = identifier(n.Name.Schema)
}
rf := &ast.RangeFunction{
Functions: &ast.List{Items: []ast.Node{fn}},
}
if n.Alias != nil {
alias := identifier(n.Alias)
rf.Alias = &ast.Alias{Aliasname: &alias}
}
return rf
case n.Name != nil:
rv := parseRangeVar(n.Name, n.Alias)
rv.Location = n.Pos()
return rv
case n.Select != nil:
rs := &ast.RangeSubselect{
Subquery: c.convert(n.Select),
}
if n.Alias != nil {
alias := identifier(n.Alias)
rs.Alias = &ast.Alias{Aliasname: &alias}
}
return rs
case len(n.List) > 0:
// A parenthesized source list, as in "FROM (a JOIN b ON ...)".
nested := c.convertFrom(n.List)
if len(nested) == 1 {
return nested[0]
}
return &ast.List{Items: nested}
}
return todo("convertTableRef", n)
}
//
// Expressions
//
func (c *cc) convertLiteral(n *meyer.Literal) ast.Node {
switch n.Kind {
case meyer.LitInteger:
if i, ok := parseInteger(n.Value); ok {
return &ast.A_Const{
Val: &ast.Integer{Ival: i},
Location: n.Pos(),
}
}
// Too wide for an int64, or written with the digit separators
// SQLite accepts. Either way the value is still numeric.
return &ast.A_Const{
Val: &ast.Float{Str: n.Value},
Location: n.Pos(),
}
case meyer.LitFloat:
return &ast.A_Const{
Val: &ast.Float{Str: n.Value},
Location: n.Pos(),
}
case meyer.LitString, meyer.LitBlob:
return &ast.A_Const{
Val: &ast.String{Str: n.Value},
Location: n.Pos(),
}
case meyer.LitNull:
return &ast.A_Const{
Val: &ast.Null{},
Location: n.Pos(),
}
}
// CURRENT_DATE, CURRENT_TIME and CURRENT_TIMESTAMP are keywords rather
// than function calls, but behave like niladic functions.
name := strings.ToLower(n.Raw)
return &ast.FuncCall{
Func: &ast.FuncName{Name: name},
Funcname: &ast.List{Items: []ast.Node{&ast.String{Str: name}}},
Args: &ast.List{},
AggOrder: &ast.List{},
Location: n.Pos(),
}
}
// parseInteger reads an integer literal in either of the two forms SQLite
// writes: decimal, where a leading zero is not an octal prefix, and
// hexadecimal.
func parseInteger(s string) (int64, bool) {
if len(s) > 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X') {
i, err := strconv.ParseInt(s[2:], 16, 64)
return i, err == nil
}
i, err := strconv.ParseInt(s, 10, 64)
return i, err == nil
}
func (c *cc) convertBindParam(n *meyer.BindParam) ast.Node {
switch n.Kind {
case meyer.ParamAnon, meyer.ParamNumber:
// Parameter numbers start at one.
c.paramCount += 1
number := c.paramCount
if n.Kind == meyer.ParamNumber {
number = n.Number
}
return &ast.ParamRef{
Number: number,
Location: n.Pos(),
Dollar: n.Kind == meyer.ParamNumber,
}
default:
// A named parameter. sqlc carries the name through as the operand of
// a pseudo-operator and resolves it later. The operator is the
// parameter's own sigil, so the formatter can print the spelling the
// author chose — SQLite accepts :name, @name and $name, but sqlc
// gives them different meanings, so they must never be rewritten
// into one another.
sigil := "@"
switch n.Kind {
case meyer.ParamColon:
sigil = ":"
case meyer.ParamDollar:
sigil = "$"
}
return &ast.A_Expr{
Name: &ast.List{Items: []ast.Node{&ast.String{Str: sigil}}},
Rexpr: &ast.String{Str: n.Name},
Location: n.Pos(),
}
}
}
func (c *cc) convertBinaryExpr(n *meyer.BinaryExpr) ast.Node {
switch n.Op {
case meyer.OpAnd, meyer.OpOr:
op := ast.BoolExprTypeAnd
if n.Op == meyer.OpOr {
op = ast.BoolExprTypeOr
}
return &ast.BoolExpr{
Boolop: op,
Args: &ast.List{Items: []ast.Node{
c.convert(n.X),
c.convert(n.Y),
}},
Location: n.Pos(),
}
}
// SQLite spells some operators two ways (<> and !=, = and ==) and the
// tree keeps only the kind, so read the author's choice back out of the
// source between the operands; the compiler recognizes every spelling.
op := n.Op.String()
switch n.Op {
case meyer.OpNe:
if strings.Contains(c.operatorText(n.X, n.Y), "!=") {
op = "!="
}
case meyer.OpEq:
if strings.Contains(c.operatorText(n.X, n.Y), "==") {
op = "=="
}
}
return &ast.A_Expr{
Name: &ast.List{Items: []ast.Node{&ast.String{Str: op}}},
Lexpr: c.convert(n.X),
Rexpr: c.convert(n.Y),
Location: n.Pos(),
}
}
// operatorText returns the source text separating two adjacent operands,
// where the operator token sits.
func (c *cc) operatorText(x, y meyer.Expr) string {
start, end := x.End(), y.Pos()
if start < 0 || end > len(c.src) || start > end {
return ""
}
return c.src[start:end]
}
func (c *cc) convertUnaryExpr(n *meyer.UnaryExpr) ast.Node {
expr := c.convert(n.X)