Skip to content

Commit cf1d9ab

Browse files
authored
astutils: Apply changes to the ValuesList slice (#372)
1 parent 463dc8a commit cf1d9ab

File tree

3 files changed

+64
-7
lines changed

3 files changed

+64
-7
lines changed

internal/endtoend/testdata/named_param/go/query.sql.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/named_param/query.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ SELECT name FROM foo WHERE name = sqlc.arg('slug') AND sqlc.arg(filter)::bool;
66
-- name: AtParams :many
77
SELECT name FROM foo WHERE name = @slug AND @filter::bool;
88

9+
-- name: InsertFuncParams :one
10+
INSERT INTO foo(name, bio) values (sqlc.arg('name'), sqlc.arg('bio')) returning name;
11+
12+
-- name: InsertAtParams :one
13+
INSERT INTO foo(name, bio) values (@name, @bio) returning name;
14+
15+
916
-- name: Update :one
1017
UPDATE foo
1118
SET

internal/postgresql/ast/astutil.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,17 @@ type application struct {
133133
iter iterator
134134
}
135135

136+
type dummyNode struct {
137+
Node nodes.Node
138+
}
139+
140+
func (node dummyNode) Deparse() string {
141+
panic("Not Implemented")
142+
}
143+
144+
func (node dummyNode) Fingerprint(ctx nodes.FingerprintContext, parentNode nodes.Node, parentFieldName string) {
145+
}
146+
136147
func (a *application) apply(parent nodes.Node, name string, iter *iterator, node nodes.Node) {
137148
// convert typed nil into untyped nil
138149
if v := reflect.ValueOf(node); v.Kind() == reflect.Ptr && v.IsNil() {
@@ -1291,13 +1302,20 @@ func (a *application) apply(parent nodes.Node, name string, iter *iterator, node
12911302
a.apply(&n, "GroupClause", nil, n.GroupClause)
12921303
a.apply(&n, "HavingClause", nil, n.HavingClause)
12931304
a.apply(&n, "WindowClause", nil, n.WindowClause)
1294-
// TODO: Not sure how to handle a slice of a slice
1295-
//
1296-
// for _, vs := range n.ValuesLists {
1297-
// for _, v := range vs {
1298-
// a.apply(&n, "", nil, v)
1299-
// }
1300-
// }
1305+
1306+
// ValuesLists is a slice of Node slices, which does not implement the
1307+
// Node interface For each Node in the list, pass a dummy node to apply
1308+
// and then updated the ValuesList if it was set.
1309+
for i := range n.ValuesLists {
1310+
for j := range n.ValuesLists[i] {
1311+
dn := dummyNode{}
1312+
a.apply(&dn, "Node", nil, n.ValuesLists[i][j])
1313+
if dn.Node != nil {
1314+
n.ValuesLists[i][j] = dn.Node
1315+
}
1316+
}
1317+
}
1318+
13011319
a.apply(&n, "SortClause", nil, n.SortClause)
13021320
a.apply(&n, "LimitOffset", nil, n.LimitOffset)
13031321
a.apply(&n, "LimitCount", nil, n.LimitCount)

0 commit comments

Comments
 (0)