From 3ff79f2d447e442723ed3d0d37749b84678f1761 Mon Sep 17 00:00:00 2001 From: Andrew Benton Date: Mon, 16 Oct 2023 13:31:33 -0700 Subject: [PATCH 1/2] test: Add a test case for https://github.com/sqlc-dev/sqlc/issues/1739 --- .../endtoend/testdata/column_alias/issue.md | 1 + .../testdata/column_alias/stdlib/exec.json | 3 + .../testdata/column_alias/stdlib/go/db.go | 31 +++++++ .../testdata/column_alias/stdlib/go/models.go | 18 +++++ .../column_alias/stdlib/go/query.sql.go | 80 +++++++++++++++++++ .../testdata/column_alias/stdlib/query.sql | 21 +++++ .../testdata/column_alias/stdlib/schema.sql | 12 +++ .../testdata/column_alias/stdlib/sqlc.yaml | 10 +++ 8 files changed, 176 insertions(+) create mode 100644 internal/endtoend/testdata/column_alias/issue.md create mode 100644 internal/endtoend/testdata/column_alias/stdlib/exec.json create mode 100644 internal/endtoend/testdata/column_alias/stdlib/go/db.go create mode 100644 internal/endtoend/testdata/column_alias/stdlib/go/models.go create mode 100644 internal/endtoend/testdata/column_alias/stdlib/go/query.sql.go create mode 100644 internal/endtoend/testdata/column_alias/stdlib/query.sql create mode 100644 internal/endtoend/testdata/column_alias/stdlib/schema.sql create mode 100644 internal/endtoend/testdata/column_alias/stdlib/sqlc.yaml diff --git a/internal/endtoend/testdata/column_alias/issue.md b/internal/endtoend/testdata/column_alias/issue.md new file mode 100644 index 0000000000..fc16c2a58a --- /dev/null +++ b/internal/endtoend/testdata/column_alias/issue.md @@ -0,0 +1 @@ +https://github.com/sqlc-dev/sqlc/issues/1739 diff --git a/internal/endtoend/testdata/column_alias/stdlib/exec.json b/internal/endtoend/testdata/column_alias/stdlib/exec.json new file mode 100644 index 0000000000..e5dfda7818 --- /dev/null +++ b/internal/endtoend/testdata/column_alias/stdlib/exec.json @@ -0,0 +1,3 @@ +{ + "contexts": ["managed-db"] +} diff --git a/internal/endtoend/testdata/column_alias/stdlib/go/db.go b/internal/endtoend/testdata/column_alias/stdlib/go/db.go new file mode 100644 index 0000000000..a457fb76b2 --- /dev/null +++ b/internal/endtoend/testdata/column_alias/stdlib/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 + +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/column_alias/stdlib/go/models.go b/internal/endtoend/testdata/column_alias/stdlib/go/models.go new file mode 100644 index 0000000000..e8547ba6cc --- /dev/null +++ b/internal/endtoend/testdata/column_alias/stdlib/go/models.go @@ -0,0 +1,18 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 + +package querytest + +import ( + "time" +) + +type User struct { + ID int32 + Fname string + Lname string + Email string + EncPasswd string + CreatedAt time.Time +} diff --git a/internal/endtoend/testdata/column_alias/stdlib/go/query.sql.go b/internal/endtoend/testdata/column_alias/stdlib/go/query.sql.go new file mode 100644 index 0000000000..71fdbdbff0 --- /dev/null +++ b/internal/endtoend/testdata/column_alias/stdlib/go/query.sql.go @@ -0,0 +1,80 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 +// source: query.sql + +package querytest + +import ( + "context" + "database/sql" + "time" +) + +const getUsers = `-- name: GetUsers :many +SELECT + users.id, + users.fname, + users.lname, + users.email, + users.created_at, + rank_email, + rank_fname, + rank_lname, + similarity +FROM + users, + to_tsvector(users.email || users.fname || users.lname) document, + to_tsquery($1::TEXT) query, + NULLIF(ts_rank(to_tsvector(users.email), query), 0) rank_email, + NULLIF(ts_rank(to_tsvector(users.fname), query), 0) rank_fname, + NULLIF(ts_rank(to_tsvector(users.lname), query), 0) rank_lname, + SIMILARITY($1::TEXT, users.email || users.fname || users.lname) similarity +WHERE query @@ document OR similarity > 0 +ORDER BY rank_email, rank_lname, rank_fname, similarity DESC NULLS LAST +` + +type GetUsersRow struct { + ID int32 + Fname string + Lname string + Email string + CreatedAt time.Time + RankEmail sql.NullFloat64 + RankFname sql.NullFloat64 + RankLname sql.NullFloat64 + Similarity sql.NullFloat64 +} + +func (q *Queries) GetUsers(ctx context.Context, searchTerm string) ([]GetUsersRow, error) { + rows, err := q.db.QueryContext(ctx, getUsers, searchTerm) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetUsersRow + for rows.Next() { + var i GetUsersRow + if err := rows.Scan( + &i.ID, + &i.Fname, + &i.Lname, + &i.Email, + &i.CreatedAt, + &i.RankEmail, + &i.RankFname, + &i.RankLname, + &i.Similarity, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/column_alias/stdlib/query.sql b/internal/endtoend/testdata/column_alias/stdlib/query.sql new file mode 100644 index 0000000000..9877fadde6 --- /dev/null +++ b/internal/endtoend/testdata/column_alias/stdlib/query.sql @@ -0,0 +1,21 @@ +-- name: GetUsers :many +SELECT + users.id, + users.fname, + users.lname, + users.email, + users.created_at, + rank_email, + rank_fname, + rank_lname, + similarity +FROM + users, + to_tsvector(users.email || users.fname || users.lname) document, + to_tsquery(@search_term::TEXT) query, + NULLIF(ts_rank(to_tsvector(users.email), query), 0) rank_email, + NULLIF(ts_rank(to_tsvector(users.fname), query), 0) rank_fname, + NULLIF(ts_rank(to_tsvector(users.lname), query), 0) rank_lname, + SIMILARITY(@search_term::TEXT, users.email || users.fname || users.lname) similarity +WHERE query @@ document OR similarity > 0 +ORDER BY rank_email, rank_lname, rank_fname, similarity DESC NULLS LAST; diff --git a/internal/endtoend/testdata/column_alias/stdlib/schema.sql b/internal/endtoend/testdata/column_alias/stdlib/schema.sql new file mode 100644 index 0000000000..9205a149e3 --- /dev/null +++ b/internal/endtoend/testdata/column_alias/stdlib/schema.sql @@ -0,0 +1,12 @@ +CREATE EXTENSION pg_trgm; +CREATE EXTENSION pgcrypto; + +CREATE TABLE users( + id INT GENERATED ALWAYS AS IDENTITY NOT NULL, + fname VARCHAR(100) NOT NULL, + lname VARCHAR(100) NOT NULL, + email VARCHAR(100) NOT NULL UNIQUE, + enc_passwd TEXT NOT NULL, + created_at TIMESTAMP WITH TIME ZONE NOT NULL default (NOW() AT TIME ZONE 'utc'), + PRIMARY KEY(id) +); diff --git a/internal/endtoend/testdata/column_alias/stdlib/sqlc.yaml b/internal/endtoend/testdata/column_alias/stdlib/sqlc.yaml new file mode 100644 index 0000000000..8c68222b49 --- /dev/null +++ b/internal/endtoend/testdata/column_alias/stdlib/sqlc.yaml @@ -0,0 +1,10 @@ +version: "2" +sql: + - engine: "postgresql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" + sql_package: "database/sql" From 23fb2481c46ced6c862647dbc7530e001cd2b4cc Mon Sep 17 00:00:00 2001 From: Andrew Benton Date: Mon, 16 Oct 2023 22:19:30 -0700 Subject: [PATCH 2/2] test: Add a test case for https://github.com/sqlc-dev/sqlc/pull/2639 --- .../select_subquery/postgresql/issue.md | 1 + .../postgresql/stdlib/exec.json | 3 ++ .../postgresql/stdlib/go/db.go | 31 +++++++++++ .../postgresql/stdlib/go/models.go | 19 +++++++ .../postgresql/stdlib/go/query.sql.go | 53 +++++++++++++++++++ .../postgresql/stdlib/query.sql | 6 +++ .../postgresql/stdlib/schema.sql | 2 + .../postgresql/stdlib/sqlc.yaml | 10 ++++ 8 files changed, 125 insertions(+) create mode 100644 internal/endtoend/testdata/select_subquery/postgresql/issue.md create mode 100644 internal/endtoend/testdata/select_subquery/postgresql/stdlib/exec.json create mode 100644 internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/db.go create mode 100644 internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/models.go create mode 100644 internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/query.sql.go create mode 100644 internal/endtoend/testdata/select_subquery/postgresql/stdlib/query.sql create mode 100644 internal/endtoend/testdata/select_subquery/postgresql/stdlib/schema.sql create mode 100644 internal/endtoend/testdata/select_subquery/postgresql/stdlib/sqlc.yaml diff --git a/internal/endtoend/testdata/select_subquery/postgresql/issue.md b/internal/endtoend/testdata/select_subquery/postgresql/issue.md new file mode 100644 index 0000000000..fa59b3a57d --- /dev/null +++ b/internal/endtoend/testdata/select_subquery/postgresql/issue.md @@ -0,0 +1 @@ +https://github.com/sqlc-dev/sqlc/pull/2639 diff --git a/internal/endtoend/testdata/select_subquery/postgresql/stdlib/exec.json b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/exec.json new file mode 100644 index 0000000000..e5dfda7818 --- /dev/null +++ b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/exec.json @@ -0,0 +1,3 @@ +{ + "contexts": ["managed-db"] +} diff --git a/internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/db.go b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/db.go new file mode 100644 index 0000000000..a457fb76b2 --- /dev/null +++ b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 + +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/select_subquery/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/models.go new file mode 100644 index 0000000000..56e82be769 --- /dev/null +++ b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/models.go @@ -0,0 +1,19 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 + +package querytest + +import ( + "database/sql" +) + +type Bar struct { + A int32 + Alias sql.NullString +} + +type Foo struct { + A int32 + Name sql.NullString +} diff --git a/internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/query.sql.go b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/query.sql.go new file mode 100644 index 0000000000..a8347ffe3e --- /dev/null +++ b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/go/query.sql.go @@ -0,0 +1,53 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.22.0 +// source: query.sql + +package querytest + +import ( + "context" + "database/sql" +) + +const subquery = `-- name: Subquery :many +SELECT + a, + name, + (SELECT alias FROM bar WHERE bar.a=foo.a AND alias = $1 ORDER BY bar.a DESC limit 1) as alias +FROM FOO WHERE a = $2 +` + +type SubqueryParams struct { + Column1 sql.NullString + Column2 sql.NullInt32 +} + +type SubqueryRow struct { + A int32 + Name sql.NullString + Alias sql.NullString +} + +func (q *Queries) Subquery(ctx context.Context, arg SubqueryParams) ([]SubqueryRow, error) { + rows, err := q.db.QueryContext(ctx, subquery, arg.Column1, arg.Column2) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryRow + for rows.Next() { + var i SubqueryRow + if err := rows.Scan(&i.A, &i.Name, &i.Alias); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/select_subquery/postgresql/stdlib/query.sql b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/query.sql new file mode 100644 index 0000000000..3d9b058b05 --- /dev/null +++ b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/query.sql @@ -0,0 +1,6 @@ +-- name: Subquery :many +SELECT + a, + name, + (SELECT alias FROM bar WHERE bar.a=foo.a AND alias = $1 ORDER BY bar.a DESC limit 1) as alias +FROM FOO WHERE a = $2; diff --git a/internal/endtoend/testdata/select_subquery/postgresql/stdlib/schema.sql b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/schema.sql new file mode 100644 index 0000000000..b256bf60ae --- /dev/null +++ b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/schema.sql @@ -0,0 +1,2 @@ +CREATE TABLE foo (a int not null, name text); +CREATE TABLE bar (a int not null, alias text); diff --git a/internal/endtoend/testdata/select_subquery/postgresql/stdlib/sqlc.yaml b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/sqlc.yaml new file mode 100644 index 0000000000..8c68222b49 --- /dev/null +++ b/internal/endtoend/testdata/select_subquery/postgresql/stdlib/sqlc.yaml @@ -0,0 +1,10 @@ +version: "2" +sql: + - engine: "postgresql" + schema: "schema.sql" + queries: "query.sql" + gen: + go: + package: "querytest" + out: "go" + sql_package: "database/sql"