Skip to content

astutils: Apply changes to the ValuesList slice #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions internal/endtoend/testdata/named_param/go/query.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions internal/endtoend/testdata/named_param/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ SELECT name FROM foo WHERE name = sqlc.arg('slug') AND sqlc.arg(filter)::bool;
-- name: AtParams :many
SELECT name FROM foo WHERE name = @slug AND @filter::bool;

-- name: InsertFuncParams :one
INSERT INTO foo(name, bio) values (sqlc.arg('name'), sqlc.arg('bio')) returning name;

-- name: InsertAtParams :one
INSERT INTO foo(name, bio) values (@name, @bio) returning name;


-- name: Update :one
UPDATE foo
SET
Expand Down
32 changes: 25 additions & 7 deletions internal/postgresql/ast/astutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,17 @@ type application struct {
iter iterator
}

type dummyNode struct {
Node nodes.Node
}

func (node dummyNode) Deparse() string {
panic("Not Implemented")
}

func (node dummyNode) Fingerprint(ctx nodes.FingerprintContext, parentNode nodes.Node, parentFieldName string) {
}

func (a *application) apply(parent nodes.Node, name string, iter *iterator, node nodes.Node) {
// convert typed nil into untyped nil
if v := reflect.ValueOf(node); v.Kind() == reflect.Ptr && v.IsNil() {
Expand Down Expand Up @@ -1291,13 +1302,20 @@ func (a *application) apply(parent nodes.Node, name string, iter *iterator, node
a.apply(&n, "GroupClause", nil, n.GroupClause)
a.apply(&n, "HavingClause", nil, n.HavingClause)
a.apply(&n, "WindowClause", nil, n.WindowClause)
// TODO: Not sure how to handle a slice of a slice
//
// for _, vs := range n.ValuesLists {
// for _, v := range vs {
// a.apply(&n, "", nil, v)
// }
// }

// ValuesLists is a slice of Node slices, which does not implement the
// Node interface For each Node in the list, pass a dummy node to apply
// and then updated the ValuesList if it was set.
for i := range n.ValuesLists {
for j := range n.ValuesLists[i] {
dn := dummyNode{}
a.apply(&dn, "Node", nil, n.ValuesLists[i][j])
if dn.Node != nil {
n.ValuesLists[i][j] = dn.Node
}
}
}

a.apply(&n, "SortClause", nil, n.SortClause)
a.apply(&n, "LimitOffset", nil, n.LimitOffset)
a.apply(&n, "LimitCount", nil, n.LimitCount)
Expand Down