Skip to content

Validate sqlc function arguments #1633

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 3 commits into from
May 23, 2022
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
3 changes: 3 additions & 0 deletions internal/endtoend/testdata/sqlc_arg_invalid/mysql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ select id, first_name from users where id = sqlc.argh(target_id);
-- name: TooManyArgs :one
select id, first_name from users where id = sqlc.arg('foo', 'bar');

-- name: TooFewArgs :one
select id, first_name from users where id = sqlc.arg();

-- name: InvalidArgFunc :one
select id, first_name from users where id = sqlc.arg(sqlc.arg(target_id));

Expand Down
3 changes: 2 additions & 1 deletion internal/endtoend/testdata/sqlc_arg_invalid/mysql/stderr.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# package querytest
query.sql:7:1: function "sqlc.argh" does not exist
query.sql:10:45: expected 1 parameter to sqlc.arg; got 2
query.sql:13:54: Invalid argument to sqlc.arg()
query.sql:13:45: expected 1 parameter to sqlc.arg; got 0
query.sql:16:54: Invalid argument to sqlc.arg()
query.sql:19:54: Invalid argument to sqlc.arg()
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ select id, first_name from users where id = sqlc.argh(target_id);
-- name: TooManyArgs :one
select id, first_name from users where id = sqlc.arg('foo', 'bar');

-- name: TooFewArgs :one
select id, first_name from users where id = sqlc.arg();

-- name: InvalidArgFunc :one
select id, first_name from users where id = sqlc.arg(sqlc.arg(target_id));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# package querytest
query.sql:7:1: function "sqlc.argh" does not exist
query.sql:10:45: expected 1 parameter to sqlc.arg; got 2
query.sql:13:54: Invalid argument to sqlc.arg()
query.sql:13:45: expected 1 parameter to sqlc.arg; got 0
query.sql:16:54: Invalid argument to sqlc.arg()
query.sql:19:54: Invalid argument to sqlc.arg()
6 changes: 2 additions & 4 deletions internal/sql/validate/func_call.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,8 @@ func (v *funcCallVisitor) Visit(node ast.Node) astutils.Visitor {
v.err = sqlerr.FunctionNotFound("sqlc." + fn.Name)
return nil
}
if call.Args == nil || len(call.Args.Items) == 0 {
return v
}
if len(call.Args.Items) > 1 {

if len(call.Args.Items) != 1 {
v.err = &sqlerr.Error{
Message: fmt.Sprintf("expected 1 parameter to sqlc.arg; got %d", len(call.Args.Items)),
Location: call.Pos(),
Expand Down
11 changes: 8 additions & 3 deletions internal/sql/validate/param_style.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ import (
func ParamStyle(n ast.Node) error {
namedFunc := astutils.Search(n, named.IsParamFunc)
for _, f := range namedFunc.Items {
fc, ok := f.(*ast.FuncCall)
if ok {
switch val := fc.Args.Items[0].(type) {
if fc, ok := f.(*ast.FuncCall); ok {
args := fc.Args.Items

if len(args) == 0 {
continue
}

switch val := args[0].(type) {
case *ast.FuncCall:
return &sqlerr.Error{
Code: "", // TODO: Pick a new error code
Expand Down