Skip to content

Fix return type for subqueries in MySQL (#1383) #1404

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions internal/endtoend/testdata/select_nested_count/mysql/go/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions internal/endtoend/testdata/select_nested_count/mysql/go/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions internal/endtoend/testdata/select_nested_count/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/select_nested_count/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql",
"engine": "mysql"
}
]
}