Skip to content

Commit 39b7f27

Browse files
committed
Support a few more nodes
1 parent abfcad8 commit 39b7f27

5 files changed

Lines changed: 44 additions & 1 deletion

File tree

internal/engine/postgresql/print_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/google/go-cmp/cmp"
99

10+
"github.com/sqlc-dev/sqlc/internal/debug"
1011
"github.com/sqlc-dev/sqlc/internal/sql/ast"
1112
)
1213

@@ -18,8 +19,13 @@ func TestPrintAst(t *testing.T) {
1819
`SELECT * FROM foo,bar;`,
1920
`SELECT * FROM foo WHERE EXISTS (SELECT * FROM foo);`,
2021
`WITH bar AS (SELECT * FROM foo), bat AS (SELECT 1) SELECT * FROM foo;`,
22+
`SELECT t.* FROM foo t;`,
23+
`SELECT *,*,foo.* FROM foo;`,
2124
}
2225

26+
// Use astutils to look for select nodes
27+
// Search for the deepest select nodes
28+
2329
for i, q := range queries {
2430
q := q
2531
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
@@ -30,6 +36,7 @@ func TestPrintAst(t *testing.T) {
3036
for _, stmt := range stmts {
3137
out := ast.Format(stmt.Raw)
3238
if diff := cmp.Diff(q, out); diff != "" {
39+
debug.Dump(stmt)
3340
t.Errorf("- %s", q)
3441
t.Errorf("+ %s", out)
3542
}

internal/sql/ast/alias.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ type Alias struct {
88
func (n *Alias) Pos() int {
99
return 0
1010
}
11+
12+
func (n *Alias) Format(buf *TrackedBuffer) {
13+
if n == nil {
14+
return
15+
}
16+
if n.Aliasname != nil {
17+
buf.WriteString(*n.Aliasname)
18+
}
19+
}

internal/sql/ast/column_ref.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ast
22

3+
import "strings"
4+
35
type ColumnRef struct {
46
Name string
57

@@ -16,5 +18,17 @@ func (n *ColumnRef) Format(buf *TrackedBuffer) {
1618
if n == nil {
1719
return
1820
}
19-
buf.astFormat(n.Fields)
21+
22+
if n.Fields != nil {
23+
var items []string
24+
for _, item := range n.Fields.Items {
25+
switch nn := item.(type) {
26+
case *String:
27+
items = append(items, nn.Str)
28+
case *A_Star:
29+
items = append(items, "*")
30+
}
31+
}
32+
buf.WriteString(strings.Join(items, "."))
33+
}
2034
}

internal/sql/ast/range_var.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ func (n *RangeVar) Format(buf *TrackedBuffer) {
2121
if n.Relname != nil {
2222
buf.WriteString(*n.Relname)
2323
}
24+
if n.Alias != nil {
25+
buf.WriteString(" ")
26+
buf.astFormat(n.Alias)
27+
}
2428
}

internal/sql/ast/string.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
package ast
22

3+
import "fmt"
4+
35
type String struct {
46
Str string
57
}
68

79
func (n *String) Pos() int {
810
return 0
911
}
12+
13+
func (n *String) Format(buf *TrackedBuffer) {
14+
if n == nil {
15+
return
16+
}
17+
fmt.Fprintf(buf, "%q", n.Str)
18+
}

0 commit comments

Comments
 (0)