diff --git a/internal/codegen/golang/templates/pgx/queryCode.tmpl b/internal/codegen/golang/templates/pgx/queryCode.tmpl index 3f56f694ec..1736fa11f7 100644 --- a/internal/codegen/golang/templates/pgx/queryCode.tmpl +++ b/internal/codegen/golang/templates/pgx/queryCode.tmpl @@ -33,7 +33,9 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, db DBTX, {{.Arg.Pair}}) ( func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.DefineType}}, error) { row := q.db.QueryRow(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/postgresql/pgx/v4/go/db.go b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/go/db.go new file mode 100644 index 0000000000..439db75e69 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/go/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.17.2 + +package querytest + +import ( + "context" + + "github.com/jackc/pgconn" + "github.com/jackc/pgx/v4" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.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/pgx/v4/go/models.go similarity index 100% rename from internal/endtoend/testdata/single_param_conflict/postgresql/go/models.go rename to internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/go/models.go diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/go/query.sql.go b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/go/query.sql.go new file mode 100644 index 0000000000..6e2e09b776 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/go/query.sql.go @@ -0,0 +1,67 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.17.2 +// 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.QueryRow(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.QueryRow(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.QueryRow(ctx, getUser, sub) + 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.QueryRow(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/pgx/v4/query.sql similarity index 100% rename from internal/endtoend/testdata/single_param_conflict/postgresql/query.sql rename to internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/query.sql diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/sqlc.json b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/sqlc.json new file mode 100644 index 0000000000..aa9521eab8 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v4/sqlc.json @@ -0,0 +1,13 @@ +{ + "version": "1", + "packages": [ + { + "engine": "postgresql", + "sql_package": "pgx/v4", + "path": "go", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/db.go b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/db.go new file mode 100644 index 0000000000..a84825e0e1 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.17.2 + +package querytest + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/models.go b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/models.go new file mode 100644 index 0000000000..f9d0e17505 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/models.go @@ -0,0 +1,19 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.17.2 + +package querytest + +import ( + "github.com/jackc/pgx/v5/pgtype" +) + +type Author struct { + ID int64 + Name string + Bio pgtype.Text +} + +type User struct { + Sub pgtype.UUID +} diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/query.sql.go b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/query.sql.go new file mode 100644 index 0000000000..33a15028c2 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/go/query.sql.go @@ -0,0 +1,67 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.17.2 +// source: query.sql + +package querytest + +import ( + "context" + + "github.com/jackc/pgx/v5/pgtype" +) + +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.QueryRow(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.QueryRow(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 pgtype.UUID) (pgtype.UUID, error) { + row := q.db.QueryRow(ctx, getUser, sub) + 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.QueryRow(ctx, setDefaultName, id) + err := row.Scan(&id) + return id, err +} diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/query.sql b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/query.sql new file mode 100644 index 0000000000..45c059a3aa --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/query.sql @@ -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; diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/sqlc.json b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/sqlc.json new file mode 100644 index 0000000000..94b58b2000 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/pgx/v5/sqlc.json @@ -0,0 +1,13 @@ +{ + "version": "1", + "packages": [ + { + "engine": "postgresql", + "sql_package": "pgx/v5", + "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/stdlib/go/db.go similarity index 100% rename from internal/endtoend/testdata/single_param_conflict/postgresql/go/db.go rename to internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/go/db.go diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/go/models.go new file mode 100644 index 0000000000..5b045823a9 --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/go/models.go @@ -0,0 +1,21 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.17.2 + +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/stdlib/go/query.sql.go similarity index 100% rename from internal/endtoend/testdata/single_param_conflict/postgresql/go/query.sql.go rename to internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/go/query.sql.go diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/query.sql b/internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/query.sql new file mode 100644 index 0000000000..45c059a3aa --- /dev/null +++ b/internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/query.sql @@ -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; diff --git a/internal/endtoend/testdata/single_param_conflict/postgresql/sqlc.json b/internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/sqlc.json similarity index 100% rename from internal/endtoend/testdata/single_param_conflict/postgresql/sqlc.json rename to internal/endtoend/testdata/single_param_conflict/postgresql/stdlib/sqlc.json