Skip to content

fix(codegen): Prevent variable redeclaration in single param conflict #1298

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
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
2 changes: 2 additions & 0 deletions internal/codegen/golang/templates/stdlib/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.De
{{- else -}}
row := q.db.QueryRowContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end}}
{{- if ne .Arg.Pair .Ret.Pair }}
var {{.Ret.Name}} {{.Ret.Type}}
{{- end}}
err := row.Scan({{.Ret.Scan}})
return {{.Ret.ReturnName}}, err
}
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/single_param_conflict/mysql/go/db.go

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

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

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

29 changes: 29 additions & 0 deletions internal/endtoend/testdata/single_param_conflict/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Example queries for sqlc
CREATE TABLE authors (
id BIGINT PRIMARY KEY,
name TEXT NOT NULL,
bio text
);

-- name: GetAuthorIDByID :one
SELECT id
FROM authors
WHERE id = ?
LIMIT 1;

-- name: GetAuthorByID :one
SELECT id, name, bio
FROM authors
WHERE id = ?
LIMIT 1;

-- https://github.com/kyleconroy/sqlc/issues/1290
CREATE TABLE users (
sub TEXT PRIMARY KEY
);

-- name: GetUser :one
SELECT sub
FROM users
WHERE sub = ?
LIMIT 1;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/single_param_conflict/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "mysql",
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
-- Example queries for sqlc
CREATE TABLE authors (
id BIGSERIAL PRIMARY KEY,
name TEXT NOT NULL,
bio text
);

-- name: GetAuthorIDByID :one
SELECT id
FROM authors
WHERE id = $1
LIMIT 1;

-- name: GetAuthorByID :one
SELECT id, name, bio
FROM authors
WHERE id = $1
LIMIT 1;

-- https://github.com/kyleconroy/sqlc/issues/1290
CREATE TABLE users (
sub UUID PRIMARY KEY
);

-- name: GetUser :one
SELECT sub
FROM users
WHERE sub = $1
LIMIT 1;

-- https://github.com/kyleconroy/sqlc/issues/1235

-- name: SetDefaultName :one
UPDATE authors
SET name = "Default Name"
WHERE id = $1
RETURNING id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "postgresql",
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}