From ac16369ae289217737476929bc3a6fd848147fbf Mon Sep 17 00:00:00 2001 From: Timothy Studd Date: Sun, 21 Nov 2021 07:08:57 -0800 Subject: [PATCH 1/2] fix(codegen): Prevent variable redeclaration in single param conflict --- .../golang/templates/stdlib/queryCode.tmpl | 2 + .../single_param_conflict/mysql/go/db.go | 29 +++++++++++ .../single_param_conflict/mysql/go/models.go | 17 +++++++ .../mysql/go/query.sql.go | 48 ++++++++++++++++++ .../single_param_conflict/mysql/query.sql | 29 +++++++++++ .../single_param_conflict/mysql/sqlc.json | 12 +++++ .../single_param_conflict/postgresql/go/db.go | 29 +++++++++++ .../postgresql/go/models.go | 19 +++++++ .../postgresql/go/query.sql.go | 50 +++++++++++++++++++ .../postgresql/query.sql | 29 +++++++++++ .../postgresql/sqlc.json | 12 +++++ 11 files changed, 276 insertions(+) create mode 100644 internal/endtoend/testdata/single_param_conflict/mysql/go/db.go create mode 100644 internal/endtoend/testdata/single_param_conflict/mysql/go/models.go create mode 100644 internal/endtoend/testdata/single_param_conflict/mysql/go/query.sql.go create mode 100644 internal/endtoend/testdata/single_param_conflict/mysql/query.sql create mode 100644 internal/endtoend/testdata/single_param_conflict/mysql/sqlc.json create mode 100644 internal/endtoend/testdata/single_param_conflict/postgresql/go/db.go create mode 100644 internal/endtoend/testdata/single_param_conflict/postgresql/go/models.go create mode 100644 internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go create mode 100644 internal/endtoend/testdata/single_param_conflict/postgresql/query.sql create mode 100644 internal/endtoend/testdata/single_param_conflict/postgresql/sqlc.json diff --git a/internal/codegen/golang/templates/stdlib/queryCode.tmpl b/internal/codegen/golang/templates/stdlib/queryCode.tmpl index 9a23ed3fba..508de43f97 100644 --- a/internal/codegen/golang/templates/stdlib/queryCode.tmpl +++ b/internal/codegen/golang/templates/stdlib/queryCode.tmpl @@ -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 } diff --git a/internal/endtoend/testdata/single_param_conflict/mysql/go/db.go b/internal/endtoend/testdata/single_param_conflict/mysql/go/db.go new file mode 100644 index 0000000000..6a99519302 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/mysql/go/db.go @@ -0,0 +1,29 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/single_param_conflict/mysql/go/models.go b/internal/endtoend/testdata/single_param_conflict/mysql/go/models.go new file mode 100644 index 0000000000..bacc1003a4 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/mysql/go/models.go @@ -0,0 +1,17 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import ( + "database/sql" +) + +type Author struct { + ID int64 + Name string + Bio sql.NullString +} + +type User struct { + Sub string +} diff --git a/internal/endtoend/testdata/single_param_conflict/mysql/go/query.sql.go b/internal/endtoend/testdata/single_param_conflict/mysql/go/query.sql.go new file mode 100644 index 0000000000..de08de63ea --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/mysql/go/query.sql.go @@ -0,0 +1,48 @@ +// Code generated by sqlc. DO NOT EDIT. +// source: query.sql + +package querytest + +import ( + "context" +) + +const getAuthorByID = `-- name: GetAuthorByID :one +SELECT id, name, bio +FROM authors +WHERE id = ? +LIMIT 1 +` + +func (q *Queries) GetAuthorByID(ctx context.Context, id int64) (Author, error) { + row := q.db.QueryRowContext(ctx, getAuthorByID, id) + var i Author + err := row.Scan(&i.ID, &i.Name, &i.Bio) + return i, err +} + +const getAuthorIDByID = `-- name: GetAuthorIDByID :one +SELECT id +FROM authors +WHERE id = ? +LIMIT 1 +` + +func (q *Queries) GetAuthorIDByID(ctx context.Context, id int64) (int64, error) { + row := q.db.QueryRowContext(ctx, getAuthorIDByID, id) + err := row.Scan(&id) + return id, err +} + +const getUser = `-- name: GetUser :one +SELECT sub +FROM users +WHERE sub = ? +LIMIT 1 +` + +func (q *Queries) GetUser(ctx context.Context, sub string) (string, error) { + row := q.db.QueryRowContext(ctx, getUser, sub) + err := row.Scan(&sub) + return sub, err +} diff --git a/internal/endtoend/testdata/single_param_conflict/mysql/query.sql b/internal/endtoend/testdata/single_param_conflict/mysql/query.sql new file mode 100644 index 0000000000..f5b577e27f --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/mysql/query.sql @@ -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; diff --git a/internal/endtoend/testdata/single_param_conflict/mysql/sqlc.json b/internal/endtoend/testdata/single_param_conflict/mysql/sqlc.json new file mode 100644 index 0000000000..ebd191983c --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/mysql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "engine": "mysql", + "path": "go", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/go/db.go b/internal/endtoend/testdata/single_param_conflict/postgresql/go/db.go new file mode 100644 index 0000000000..6a99519302 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/go/db.go @@ -0,0 +1,29 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import ( + "context" + "database/sql" +) + +type DBTX interface { + ExecContext(context.Context, string, ...interface{}) (sql.Result, error) + PrepareContext(context.Context, string) (*sql.Stmt, error) + QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) + QueryRowContext(context.Context, string, ...interface{}) *sql.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx *sql.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/go/models.go b/internal/endtoend/testdata/single_param_conflict/postgresql/go/models.go new file mode 100644 index 0000000000..e0b9e6091c --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/go/models.go @@ -0,0 +1,19 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import ( + "database/sql" + + "github.com/google/uuid" +) + +type Author struct { + ID int64 + Name string + Bio sql.NullString +} + +type User struct { + Sub uuid.UUID +} diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go b/internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go new file mode 100644 index 0000000000..482c7feb99 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go @@ -0,0 +1,50 @@ +// Code generated by sqlc. DO NOT EDIT. +// source: query.sql + +package querytest + +import ( + "context" + + "github.com/google/uuid" +) + +const getAuthorByID = `-- name: GetAuthorByID :one +SELECT id, name, bio +FROM authors +WHERE id = $1 +LIMIT 1 +` + +func (q *Queries) GetAuthorByID(ctx context.Context, id int64) (Author, error) { + row := q.db.QueryRowContext(ctx, getAuthorByID, id) + var i Author + err := row.Scan(&i.ID, &i.Name, &i.Bio) + return i, err +} + +const getAuthorIDByID = `-- name: GetAuthorIDByID :one +SELECT id +FROM authors +WHERE id = $1 +LIMIT 1 +` + +func (q *Queries) GetAuthorIDByID(ctx context.Context, id int64) (int64, error) { + row := q.db.QueryRowContext(ctx, getAuthorIDByID, id) + err := row.Scan(&id) + return id, err +} + +const getUser = `-- name: GetUser :one +SELECT sub +FROM users +WHERE sub = $1 +LIMIT 1 +` + +func (q *Queries) GetUser(ctx context.Context, sub uuid.UUID) (uuid.UUID, error) { + row := q.db.QueryRowContext(ctx, getUser, sub) + err := row.Scan(&sub) + return sub, err +} diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/query.sql b/internal/endtoend/testdata/single_param_conflict/postgresql/query.sql new file mode 100644 index 0000000000..3b8e665b15 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/query.sql @@ -0,0 +1,29 @@ +-- 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; diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/sqlc.json b/internal/endtoend/testdata/single_param_conflict/postgresql/sqlc.json new file mode 100644 index 0000000000..2e0c1b1ae1 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "engine": "postgresql", + "path": "go", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} From 5ce2d251ce406919e2277a771417fe20d1137ae2 Mon Sep 17 00:00:00 2001 From: Timothy Studd Date: Sun, 21 Nov 2021 07:15:40 -0800 Subject: [PATCH 2/2] Additional test --- .../postgresql/go/query.sql.go | 15 +++++++++++++++ .../single_param_conflict/postgresql/query.sql | 8 ++++++++ 2 files changed, 23 insertions(+) diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go b/internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go index 482c7feb99..72c1a4cb3f 100644 --- a/internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go @@ -48,3 +48,18 @@ func (q *Queries) GetUser(ctx context.Context, sub uuid.UUID) (uuid.UUID, error) err := row.Scan(&sub) return sub, err } + +const setDefaultName = `-- name: SetDefaultName :one + +UPDATE authors +SET name = "Default Name" +WHERE id = $1 +RETURNING id +` + +// https://github.com/kyleconroy/sqlc/issues/1235 +func (q *Queries) SetDefaultName(ctx context.Context, id int64) (int64, error) { + row := q.db.QueryRowContext(ctx, setDefaultName, id) + err := row.Scan(&id) + return id, err +} diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/query.sql b/internal/endtoend/testdata/single_param_conflict/postgresql/query.sql index 3b8e665b15..45c059a3aa 100644 --- a/internal/endtoend/testdata/single_param_conflict/postgresql/query.sql +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/query.sql @@ -27,3 +27,11 @@ 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;