Skip to content

Commit c34ad5e

Browse files
authored
fix(codegen): Prevent variable redeclaration in single param conflict (#1298)
1 parent 768ccb6 commit c34ad5e

File tree

11 files changed

+299
-0
lines changed

11 files changed

+299
-0
lines changed

internal/codegen/golang/templates/stdlib/queryCode.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.De
3434
{{- else -}}
3535
row := q.db.QueryRowContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
3636
{{- end}}
37+
{{- if ne .Arg.Pair .Ret.Pair }}
3738
var {{.Ret.Name}} {{.Ret.Type}}
39+
{{- end}}
3840
err := row.Scan({{.Ret.Scan}})
3941
return {{.Ret.ReturnName}}, err
4042
}

internal/endtoend/testdata/single_param_conflict/mysql/go/db.go

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

internal/endtoend/testdata/single_param_conflict/mysql/go/models.go

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

internal/endtoend/testdata/single_param_conflict/mysql/go/query.sql.go

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE authors (
3+
id BIGINT PRIMARY KEY,
4+
name TEXT NOT NULL,
5+
bio text
6+
);
7+
8+
-- name: GetAuthorIDByID :one
9+
SELECT id
10+
FROM authors
11+
WHERE id = ?
12+
LIMIT 1;
13+
14+
-- name: GetAuthorByID :one
15+
SELECT id, name, bio
16+
FROM authors
17+
WHERE id = ?
18+
LIMIT 1;
19+
20+
-- https://github.com/kyleconroy/sqlc/issues/1290
21+
CREATE TABLE users (
22+
sub TEXT PRIMARY KEY
23+
);
24+
25+
-- name: GetUser :one
26+
SELECT sub
27+
FROM users
28+
WHERE sub = ?
29+
LIMIT 1;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "mysql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/endtoend/testdata/single_param_conflict/postgresql/go/db.go

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

internal/endtoend/testdata/single_param_conflict/postgresql/go/models.go

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

internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
-- Example queries for sqlc
2+
CREATE TABLE authors (
3+
id BIGSERIAL PRIMARY KEY,
4+
name TEXT NOT NULL,
5+
bio text
6+
);
7+
8+
-- name: GetAuthorIDByID :one
9+
SELECT id
10+
FROM authors
11+
WHERE id = $1
12+
LIMIT 1;
13+
14+
-- name: GetAuthorByID :one
15+
SELECT id, name, bio
16+
FROM authors
17+
WHERE id = $1
18+
LIMIT 1;
19+
20+
-- https://github.com/kyleconroy/sqlc/issues/1290
21+
CREATE TABLE users (
22+
sub UUID PRIMARY KEY
23+
);
24+
25+
-- name: GetUser :one
26+
SELECT sub
27+
FROM users
28+
WHERE sub = $1
29+
LIMIT 1;
30+
31+
-- https://github.com/kyleconroy/sqlc/issues/1235
32+
33+
-- name: SetDefaultName :one
34+
UPDATE authors
35+
SET name = "Default Name"
36+
WHERE id = $1
37+
RETURNING id;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "postgresql",
6+
"path": "go",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)