From f834122366e8980380c47f74804b6bc6bf9aa846 Mon Sep 17 00:00:00 2001 From: Daniel Simmons Date: Sun, 30 Jan 2022 22:54:19 +0100 Subject: [PATCH] Fix return type for subqueries in MySQL (#1383) --- internal/compiler/output_columns.go | 11 ++++ .../select_nested_count/mysql/go/db.go | 29 +++++++++++ .../select_nested_count/mysql/go/models.go | 19 +++++++ .../select_nested_count/mysql/go/query.sql.go | 52 +++++++++++++++++++ .../select_nested_count/mysql/query.sql | 19 +++++++ .../select_nested_count/mysql/sqlc.json | 12 +++++ 6 files changed, 142 insertions(+) create mode 100644 internal/endtoend/testdata/select_nested_count/mysql/go/db.go create mode 100644 internal/endtoend/testdata/select_nested_count/mysql/go/models.go create mode 100644 internal/endtoend/testdata/select_nested_count/mysql/go/query.sql.go create mode 100644 internal/endtoend/testdata/select_nested_count/mysql/query.sql create mode 100644 internal/endtoend/testdata/select_nested_count/mysql/sqlc.json diff --git a/internal/compiler/output_columns.go b/internal/compiler/output_columns.go index 0b0e770025..e66cb7a006 100644 --- a/internal/compiler/output_columns.go +++ b/internal/compiler/output_columns.go @@ -268,6 +268,17 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) { } cols = append(cols, col) + case *ast.SelectStmt: + subcols, err := outputColumns(qc, n) + if err != nil { + return nil, err + } + first := subcols[0] + if res.Name != nil { + first.Name = *res.Name + } + cols = append(cols, first) + default: name := "" if res.Name != nil { diff --git a/internal/endtoend/testdata/select_nested_count/mysql/go/db.go b/internal/endtoend/testdata/select_nested_count/mysql/go/db.go new file mode 100644 index 0000000000..6a99519302 --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/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/select_nested_count/mysql/go/models.go b/internal/endtoend/testdata/select_nested_count/mysql/go/models.go new file mode 100644 index 0000000000..c1f8631d13 --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/mysql/go/models.go @@ -0,0 +1,19 @@ +// Code generated by sqlc. DO NOT EDIT. + +package querytest + +import ( + "database/sql" +) + +type Author struct { + ID int64 + Name string + Bio sql.NullString +} + +type Book struct { + ID int64 + AuthorID int64 + Title string +} diff --git a/internal/endtoend/testdata/select_nested_count/mysql/go/query.sql.go b/internal/endtoend/testdata/select_nested_count/mysql/go/query.sql.go new file mode 100644 index 0000000000..4159cdd5ba --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/mysql/go/query.sql.go @@ -0,0 +1,52 @@ +// Code generated by sqlc. DO NOT EDIT. +// source: query.sql + +package querytest + +import ( + "context" + "database/sql" +) + +const getAuthorsWithBooksCount = `-- name: GetAuthorsWithBooksCount :many +SELECT id, name, bio, ( + SELECT COUNT(id) FROM books + WHERE books.author_id = id +) AS books_count +FROM authors +` + +type GetAuthorsWithBooksCountRow struct { + ID int64 + Name string + Bio sql.NullString + BooksCount int64 +} + +func (q *Queries) GetAuthorsWithBooksCount(ctx context.Context) ([]GetAuthorsWithBooksCountRow, error) { + rows, err := q.db.QueryContext(ctx, getAuthorsWithBooksCount) + if err != nil { + return nil, err + } + defer rows.Close() + var items []GetAuthorsWithBooksCountRow + for rows.Next() { + var i GetAuthorsWithBooksCountRow + if err := rows.Scan( + &i.ID, + &i.Name, + &i.Bio, + &i.BooksCount, + ); 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_nested_count/mysql/query.sql b/internal/endtoend/testdata/select_nested_count/mysql/query.sql new file mode 100644 index 0000000000..fb9d37dfa1 --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/mysql/query.sql @@ -0,0 +1,19 @@ +CREATE TABLE authors ( + id bigint PRIMARY KEY, + name text NOT NULL, + bio text +); + +CREATE TABLE books ( + id bigint PRIMARY KEY, + author_id bigint NOT NULL + REFERENCES authors(id), + title text NOT NULL +); + +-- name: GetAuthorsWithBooksCount :many +SELECT *, ( + SELECT COUNT(id) FROM books + WHERE books.author_id = id +) AS books_count +FROM authors; diff --git a/internal/endtoend/testdata/select_nested_count/mysql/sqlc.json b/internal/endtoend/testdata/select_nested_count/mysql/sqlc.json new file mode 100644 index 0000000000..cb8f8d5ee6 --- /dev/null +++ b/internal/endtoend/testdata/select_nested_count/mysql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql", + "engine": "mysql" + } + ] +}