Skip to content

Commit 186862c

Browse files
authored
fix: Prevent variable redeclaration in single param conflict for pgx (#2058)
1 parent c8d493b commit 186862c

File tree

16 files changed

+340
-0
lines changed

16 files changed

+340
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) (
3333
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) {
3434
row := q.db.QueryRow(ctx, {{.ConstantName}}, {{.Arg.Params}})
3535
{{- end}}
36+
{{- if ne .Arg.Pair .Ret.Pair }}
3637
var {{.Ret.Name}} {{.Ret.Type}}
38+
{{- end}}
3739
err := row.Scan({{.Ret.Scan}})
3840
return {{.Ret.ReturnName}}, err
3941
}

internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/go/db.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/single_param_conflict/postgresql/pgx/v4/go/query.sql.go

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "postgresql",
6+
"sql_package": "pgx/v4",
7+
"path": "go",
8+
"name": "querytest",
9+
"schema": "query.sql",
10+
"queries": "query.sql"
11+
}
12+
]
13+
}

internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/db.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/single_param_conflict/postgresql/pgx/v5/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/pgx/v5/go/query.sql.go

Lines changed: 67 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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"engine": "postgresql",
6+
"sql_package": "pgx/v5",
7+
"path": "go",
8+
"name": "querytest",
9+
"schema": "query.sql",
10+
"queries": "query.sql"
11+
}
12+
]
13+
}

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

Lines changed: 21 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;

0 commit comments

Comments
 (0)