Skip to content

Commit

Permalink
Generate nullable subselect statements
Browse files Browse the repository at this point in the history
  • Loading branch information
ryu-ichiroh committed May 9, 2023
1 parent c13b156 commit 03ea5bc
Show file tree
Hide file tree
Showing 26 changed files with 703 additions and 0 deletions.
19 changes: 19 additions & 0 deletions internal/compiler/output_columns.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,11 @@ func outputColumns(qc *QueryCatalog, node ast.Node) ([]*Column, error) {
if res.Name != nil {
first.Name = *res.Name
}

if hasWhereOrHavingClause(n) {
first.NotNull = false
}

cols = append(cols, first)

default:
Expand Down Expand Up @@ -633,3 +638,17 @@ func findColumnForRef(ref *ast.ColumnRef, tables []*Table, selectStatement *ast.

return nil
}

// hasWhereOrHavingClause returns true if the statement contains WHERE or HAVING clause
func hasWhereOrHavingClause(node ast.Node) bool {
stmt := node.(*ast.SelectStmt)

if _, isTODO := stmt.WhereClause.(*ast.TODO); stmt.WhereClause != nil && !isTODO {
return true
}
if _, isTODO := stmt.HavingClause.(*ast.TODO); stmt.HavingClause != nil && !isTODO {
return true
}

return false
}
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/nullable_subselect/mysql/go/db.go

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

14 changes: 14 additions & 0 deletions internal/endtoend/testdata/nullable_subselect/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.

7 changes: 7 additions & 0 deletions internal/endtoend/testdata/nullable_subselect/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE foo (a int not null, b int);

-- name: SubqueryWithWhereClause :many
SELECT a, (SELECT COUNT(a) FROM foo WHERE a > 10) as "total" FROM foo;

-- name: SubqueryWithHavingClause :many
SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) as "total" FROM foo;
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/nullable_subselect/mysql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "mysql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}

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.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CREATE TABLE foo (a int not null, b int);

-- name: SubqueryWithWhereClause :many
SELECT a, (SELECT COUNT(a) FROM foo WHERE a > 10) as "total" FROM foo;

-- name: SubqueryWithHavingClause :many
SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) as "total" FROM foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}

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.

Loading

0 comments on commit 03ea5bc

Please sign in to comment.