Skip to content

Commit b6b5ae4

Browse files
authored
Compile the sql/ast and sql/ast/pg packages into one (sqlc-dev#673)
* sql/ast/pg: Move all files to the sql/ast package * /s/package pg/package ast * /s/*ast.List/*List * /s/ast.Node/Node/ * sql/ast: Remove circular package import * Rename collisions * resolve SelectStmt * rm crate_enum_stmt_pg * rm alter table pg * apply things * rm string * Remove things * remove composite type * column ref/def gone * No more PG types * remove all pg imports * fix imports and rewrite * more work * more work * getting closer * a ton of failing tests * it... works?
1 parent d2406c5 commit b6b5ae4

355 files changed

Lines changed: 2547 additions & 3162 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/compiler/compat.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import (
55
"strings"
66

77
"github.com/kyleconroy/sqlc/internal/sql/ast"
8-
"github.com/kyleconroy/sqlc/internal/sql/ast/pg"
98
"github.com/kyleconroy/sqlc/internal/sql/astutils"
109
)
1110

1211
// This is mainly copy-pasted from internal/postgresql/parse.go
1312
func stringSlice(list *ast.List) []string {
1413
items := []string{}
1514
for _, item := range list.Items {
16-
if n, ok := item.(*pg.String); ok {
15+
if n, ok := item.(*ast.String); ok {
1716
items = append(items, n.Str)
1817
continue
1918
}
@@ -56,7 +55,7 @@ func parseRelation(node ast.Node) (*Relation, error) {
5655
return nil, fmt.Errorf("invalid name: %s", astutils.Join(n, "."))
5756
}
5857

59-
case *pg.RangeVar:
58+
case *ast.RangeVar:
6059
name := Relation{}
6160
if n.Catalogname != nil {
6261
name.Catalog = *n.Catalogname
@@ -69,7 +68,7 @@ func parseRelation(node ast.Node) (*Relation, error) {
6968
}
7069
return &name, nil
7170

72-
case *pg.TypeName:
71+
case *ast.TypeName:
7372
return parseRelation(n.Names)
7473

7574
default:

internal/compiler/expand.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,17 @@ import (
77
"github.com/kyleconroy/sqlc/internal/config"
88
"github.com/kyleconroy/sqlc/internal/source"
99
"github.com/kyleconroy/sqlc/internal/sql/ast"
10-
"github.com/kyleconroy/sqlc/internal/sql/ast/pg"
1110
"github.com/kyleconroy/sqlc/internal/sql/astutils"
1211
"github.com/kyleconroy/sqlc/internal/sql/lang"
1312
)
1413

1514
func (c *Compiler) expand(qc *QueryCatalog, raw *ast.RawStmt) ([]source.Edit, error) {
1615
list := astutils.Search(raw, func(node ast.Node) bool {
1716
switch node.(type) {
18-
case *pg.DeleteStmt:
19-
case *pg.InsertStmt:
20-
case *pg.SelectStmt:
21-
case *pg.UpdateStmt:
17+
case *ast.DeleteStmt:
18+
case *ast.InsertStmt:
19+
case *ast.SelectStmt:
20+
case *ast.UpdateStmt:
2221
default:
2322
return false
2423
}
@@ -59,25 +58,25 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)
5958

6059
var targets *ast.List
6160
switch n := node.(type) {
62-
case *pg.DeleteStmt:
61+
case *ast.DeleteStmt:
6362
targets = n.ReturningList
64-
case *pg.InsertStmt:
63+
case *ast.InsertStmt:
6564
targets = n.ReturningList
66-
case *pg.SelectStmt:
65+
case *ast.SelectStmt:
6766
targets = n.TargetList
68-
case *pg.UpdateStmt:
67+
case *ast.UpdateStmt:
6968
targets = n.ReturningList
7069
default:
7170
return nil, fmt.Errorf("outputColumns: unsupported node type: %T", n)
7271
}
7372

7473
var edits []source.Edit
7574
for _, target := range targets.Items {
76-
res, ok := target.(*pg.ResTarget)
75+
res, ok := target.(*ast.ResTarget)
7776
if !ok {
7877
continue
7978
}
80-
ref, ok := res.Val.(*pg.ColumnRef)
79+
ref, ok := res.Val.(*ast.ColumnRef)
8180
if !ok {
8281
continue
8382
}
@@ -89,9 +88,7 @@ func (c *Compiler) expandStmt(qc *QueryCatalog, raw *ast.RawStmt, node ast.Node)
8988
switch field := f.(type) {
9089
case *ast.String:
9190
parts = append(parts, field.Str)
92-
case *pg.String:
93-
parts = append(parts, field.Str)
94-
case *pg.A_Star:
91+
case *ast.A_Star:
9592
parts = append(parts, "*")
9693
default:
9794
return nil, fmt.Errorf("unknown field in ColumnRef: %T", f)

internal/compiler/find_params.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package compiler
22

33
import (
44
"github.com/kyleconroy/sqlc/internal/sql/ast"
5-
"github.com/kyleconroy/sqlc/internal/sql/ast/pg"
65
"github.com/kyleconroy/sqlc/internal/sql/astutils"
76
)
87

@@ -15,14 +14,14 @@ func findParameters(root ast.Node) []paramRef {
1514

1615
type paramRef struct {
1716
parent ast.Node
18-
rv *pg.RangeVar
19-
ref *pg.ParamRef
17+
rv *ast.RangeVar
18+
ref *ast.ParamRef
2019
name string // Named parameter support
2120
}
2221

2322
type paramSearch struct {
2423
parent ast.Node
25-
rangeVar *pg.RangeVar
24+
rangeVar *ast.RangeVar
2625
refs *[]paramRef
2726
seen map[int]struct{}
2827

@@ -48,20 +47,20 @@ func (l *limitOffset) Pos() int {
4847
func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
4948
switch n := node.(type) {
5049

51-
case *pg.A_Expr:
50+
case *ast.A_Expr:
5251
p.parent = node
5352

5453
case *ast.FuncCall:
5554
p.parent = node
5655

57-
case *pg.InsertStmt:
58-
if s, ok := n.SelectStmt.(*pg.SelectStmt); ok {
56+
case *ast.InsertStmt:
57+
if s, ok := n.SelectStmt.(*ast.SelectStmt); ok {
5958
for i, item := range s.TargetList.Items {
60-
target, ok := item.(*pg.ResTarget)
59+
target, ok := item.(*ast.ResTarget)
6160
if !ok {
6261
continue
6362
}
64-
ref, ok := target.Val.(*pg.ParamRef)
63+
ref, ok := target.Val.(*ast.ParamRef)
6564
if !ok {
6665
continue
6766
}
@@ -75,7 +74,7 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
7574
continue
7675
}
7776
for i, v := range vl.Items {
78-
ref, ok := v.(*pg.ParamRef)
77+
ref, ok := v.(*ast.ParamRef)
7978
if !ok {
8079
continue
8180
}
@@ -86,33 +85,33 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
8685
}
8786
}
8887

89-
case *pg.RangeVar:
88+
case *ast.RangeVar:
9089
p.rangeVar = n
9190

92-
case *pg.ResTarget:
91+
case *ast.ResTarget:
9392
p.parent = node
9493

95-
case *pg.SelectStmt:
94+
case *ast.SelectStmt:
9695
if n.LimitCount != nil {
9796
p.limitCount = n.LimitCount
9897
}
9998
if n.LimitOffset != nil {
10099
p.limitOffset = n.LimitOffset
101100
}
102101

103-
case *pg.TypeCast:
102+
case *ast.TypeCast:
104103
p.parent = node
105104

106-
case *pg.ParamRef:
105+
case *ast.ParamRef:
107106
parent := p.parent
108107

109-
if count, ok := p.limitCount.(*pg.ParamRef); ok {
108+
if count, ok := p.limitCount.(*ast.ParamRef); ok {
110109
if n.Number == count.Number {
111110
parent = &limitCount{}
112111
}
113112
}
114113

115-
if offset, ok := p.limitOffset.(*pg.ParamRef); ok {
114+
if offset, ok := p.limitOffset.(*ast.ParamRef); ok {
116115
if n.Number == offset.Number {
117116
parent = &limitOffset{}
118117
}
@@ -121,14 +120,14 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
121120
break
122121
}
123122

124-
// Special, terrible case for *pg.MultiAssignRef
123+
// Special, terrible case for *ast.MultiAssignRef
125124
set := true
126-
if res, ok := parent.(*pg.ResTarget); ok {
127-
if multi, ok := res.Val.(*pg.MultiAssignRef); ok {
125+
if res, ok := parent.(*ast.ResTarget); ok {
126+
if multi, ok := res.Val.(*ast.MultiAssignRef); ok {
128127
set = false
129-
if row, ok := multi.Source.(*pg.RowExpr); ok {
128+
if row, ok := multi.Source.(*ast.RowExpr); ok {
130129
for i, arg := range row.Args.Items {
131-
if ref, ok := arg.(*pg.ParamRef); ok {
130+
if ref, ok := arg.(*ast.ParamRef); ok {
132131
if multi.Colno == i+1 && ref.Number == n.Number {
133132
set = true
134133
}

0 commit comments

Comments
 (0)