diff --git a/internal/compiler/output_columns.go b/internal/compiler/output_columns.go index 8a450470bc..bec9292143 100644 --- a/internal/compiler/output_columns.go +++ b/internal/compiler/output_columns.go @@ -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: @@ -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 +} diff --git a/internal/endtoend/testdata/nullable_subselect/mysql/go/db.go b/internal/endtoend/testdata/nullable_subselect/mysql/go/db.go new file mode 100644 index 0000000000..8c5b31f933 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/mysql/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.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/nullable_subselect/mysql/go/models.go b/internal/endtoend/testdata/nullable_subselect/mysql/go/models.go new file mode 100644 index 0000000000..3b363b2288 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/mysql/go/models.go @@ -0,0 +1,14 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 + +package querytest + +import ( + "database/sql" +) + +type Foo struct { + A int32 + B sql.NullInt32 +} diff --git a/internal/endtoend/testdata/nullable_subselect/mysql/go/query.sql.go b/internal/endtoend/testdata/nullable_subselect/mysql/go/query.sql.go new file mode 100644 index 0000000000..a139cedbe4 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/mysql/go/query.sql.go @@ -0,0 +1,75 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 +// source: query.sql + +package querytest + +import ( + "context" + "database/sql" +) + +const subqueryWithHavingClause = `-- name: SubqueryWithHavingClause :many +SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) as "total" FROM foo +` + +type SubqueryWithHavingClauseRow struct { + A int32 + Total sql.NullInt64 +} + +func (q *Queries) SubqueryWithHavingClause(ctx context.Context) ([]SubqueryWithHavingClauseRow, error) { + rows, err := q.db.QueryContext(ctx, subqueryWithHavingClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithHavingClauseRow + for rows.Next() { + var i SubqueryWithHavingClauseRow + if err := rows.Scan(&i.A, &i.Total); 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 +} + +const subqueryWithWhereClause = `-- name: SubqueryWithWhereClause :many +SELECT a, (SELECT COUNT(a) FROM foo WHERE a > 10) as "total" FROM foo +` + +type SubqueryWithWhereClauseRow struct { + A int32 + Total sql.NullInt64 +} + +func (q *Queries) SubqueryWithWhereClause(ctx context.Context) ([]SubqueryWithWhereClauseRow, error) { + rows, err := q.db.QueryContext(ctx, subqueryWithWhereClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithWhereClauseRow + for rows.Next() { + var i SubqueryWithWhereClauseRow + if err := rows.Scan(&i.A, &i.Total); 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/nullable_subselect/mysql/query.sql b/internal/endtoend/testdata/nullable_subselect/mysql/query.sql new file mode 100644 index 0000000000..d68a4cb80d --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/mysql/query.sql @@ -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; diff --git a/internal/endtoend/testdata/nullable_subselect/mysql/sqlc.json b/internal/endtoend/testdata/nullable_subselect/mysql/sqlc.json new file mode 100644 index 0000000000..445bbd1589 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/mysql/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "mysql", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/go/db.go b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/go/db.go new file mode 100644 index 0000000000..5a2cde8cda --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/go/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 + +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/nullable_subselect/postgresql/pgx/v4/go/models.go b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/go/models.go new file mode 100644 index 0000000000..3b363b2288 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/go/models.go @@ -0,0 +1,14 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 + +package querytest + +import ( + "database/sql" +) + +type Foo struct { + A int32 + B sql.NullInt32 +} diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/go/query.sql.go b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/go/query.sql.go new file mode 100644 index 0000000000..3964210ab3 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/go/query.sql.go @@ -0,0 +1,68 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const subqueryWithHavingClause = `-- name: SubqueryWithHavingClause :many +SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) as "total" FROM foo +` + +type SubqueryWithHavingClauseRow struct { + A int32 + Total int64 +} + +func (q *Queries) SubqueryWithHavingClause(ctx context.Context) ([]SubqueryWithHavingClauseRow, error) { + rows, err := q.db.Query(ctx, subqueryWithHavingClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithHavingClauseRow + for rows.Next() { + var i SubqueryWithHavingClauseRow + if err := rows.Scan(&i.A, &i.Total); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const subqueryWithWhereClause = `-- name: SubqueryWithWhereClause :many +SELECT a, (SELECT COUNT(a) FROM foo WHERE a > 10) as "total" FROM foo +` + +type SubqueryWithWhereClauseRow struct { + A int32 + Total int64 +} + +func (q *Queries) SubqueryWithWhereClause(ctx context.Context) ([]SubqueryWithWhereClauseRow, error) { + rows, err := q.db.Query(ctx, subqueryWithWhereClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithWhereClauseRow + for rows.Next() { + var i SubqueryWithWhereClauseRow + if err := rows.Scan(&i.A, &i.Total); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/query.sql b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/query.sql new file mode 100644 index 0000000000..d68a4cb80d --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/query.sql @@ -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; diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/sqlc.json b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/sqlc.json new file mode 100644 index 0000000000..9403bd0279 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v4/sqlc.json @@ -0,0 +1,13 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "postgresql", + "sql_package": "pgx/v4", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/go/db.go b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/go/db.go new file mode 100644 index 0000000000..7bc02a1d7d --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/go/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 + +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/nullable_subselect/postgresql/pgx/v5/go/models.go b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/go/models.go new file mode 100644 index 0000000000..99fdb18bf8 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/go/models.go @@ -0,0 +1,14 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 + +package querytest + +import ( + "github.com/jackc/pgx/v5/pgtype" +) + +type Foo struct { + A int32 + B pgtype.Int4 +} diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/go/query.sql.go b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/go/query.sql.go new file mode 100644 index 0000000000..3964210ab3 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/go/query.sql.go @@ -0,0 +1,68 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const subqueryWithHavingClause = `-- name: SubqueryWithHavingClause :many +SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) as "total" FROM foo +` + +type SubqueryWithHavingClauseRow struct { + A int32 + Total int64 +} + +func (q *Queries) SubqueryWithHavingClause(ctx context.Context) ([]SubqueryWithHavingClauseRow, error) { + rows, err := q.db.Query(ctx, subqueryWithHavingClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithHavingClauseRow + for rows.Next() { + var i SubqueryWithHavingClauseRow + if err := rows.Scan(&i.A, &i.Total); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const subqueryWithWhereClause = `-- name: SubqueryWithWhereClause :many +SELECT a, (SELECT COUNT(a) FROM foo WHERE a > 10) as "total" FROM foo +` + +type SubqueryWithWhereClauseRow struct { + A int32 + Total int64 +} + +func (q *Queries) SubqueryWithWhereClause(ctx context.Context) ([]SubqueryWithWhereClauseRow, error) { + rows, err := q.db.Query(ctx, subqueryWithWhereClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithWhereClauseRow + for rows.Next() { + var i SubqueryWithWhereClauseRow + if err := rows.Scan(&i.A, &i.Total); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/query.sql b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/query.sql new file mode 100644 index 0000000000..d68a4cb80d --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/query.sql @@ -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; diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/sqlc.json b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/sqlc.json new file mode 100644 index 0000000000..6645ccbd1b --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/pgx/v5/sqlc.json @@ -0,0 +1,13 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "postgresql", + "sql_package": "pgx/v5", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/go/db.go b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/go/db.go new file mode 100644 index 0000000000..8c5b31f933 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.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/nullable_subselect/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/go/models.go new file mode 100644 index 0000000000..3b363b2288 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/go/models.go @@ -0,0 +1,14 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 + +package querytest + +import ( + "database/sql" +) + +type Foo struct { + A int32 + B sql.NullInt32 +} diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/go/query.sql.go b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/go/query.sql.go new file mode 100644 index 0000000000..05981f2002 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/go/query.sql.go @@ -0,0 +1,74 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 +// source: query.sql + +package querytest + +import ( + "context" +) + +const subqueryWithHavingClause = `-- name: SubqueryWithHavingClause :many +SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) as "total" FROM foo +` + +type SubqueryWithHavingClauseRow struct { + A int32 + Total int64 +} + +func (q *Queries) SubqueryWithHavingClause(ctx context.Context) ([]SubqueryWithHavingClauseRow, error) { + rows, err := q.db.QueryContext(ctx, subqueryWithHavingClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithHavingClauseRow + for rows.Next() { + var i SubqueryWithHavingClauseRow + if err := rows.Scan(&i.A, &i.Total); 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 +} + +const subqueryWithWhereClause = `-- name: SubqueryWithWhereClause :many +SELECT a, (SELECT COUNT(a) FROM foo WHERE a > 10) as "total" FROM foo +` + +type SubqueryWithWhereClauseRow struct { + A int32 + Total int64 +} + +func (q *Queries) SubqueryWithWhereClause(ctx context.Context) ([]SubqueryWithWhereClauseRow, error) { + rows, err := q.db.QueryContext(ctx, subqueryWithWhereClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithWhereClauseRow + for rows.Next() { + var i SubqueryWithWhereClauseRow + if err := rows.Scan(&i.A, &i.Total); 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/nullable_subselect/postgresql/stdlib/query.sql b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/query.sql new file mode 100644 index 0000000000..d68a4cb80d --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/query.sql @@ -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; diff --git a/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/sqlc.json b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/sqlc.json new file mode 100644 index 0000000000..c72b6132d5 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/postgresql/stdlib/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "postgresql", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} diff --git a/internal/endtoend/testdata/nullable_subselect/sqlite/go/db.go b/internal/endtoend/testdata/nullable_subselect/sqlite/go/db.go new file mode 100644 index 0000000000..8c5b31f933 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/sqlite/go/db.go @@ -0,0 +1,31 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.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/nullable_subselect/sqlite/go/models.go b/internal/endtoend/testdata/nullable_subselect/sqlite/go/models.go new file mode 100644 index 0000000000..c109517b0d --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/sqlite/go/models.go @@ -0,0 +1,14 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 + +package querytest + +import ( + "database/sql" +) + +type Foo struct { + A int64 + B sql.NullInt64 +} diff --git a/internal/endtoend/testdata/nullable_subselect/sqlite/go/query.sql.go b/internal/endtoend/testdata/nullable_subselect/sqlite/go/query.sql.go new file mode 100644 index 0000000000..159db0c937 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/sqlite/go/query.sql.go @@ -0,0 +1,75 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.18.0 +// source: query.sql + +package querytest + +import ( + "context" + "database/sql" +) + +const subqueryWithHavingClause = `-- name: SubqueryWithHavingClause :many +SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) FROM foo +` + +type SubqueryWithHavingClauseRow struct { + A int64 + Count int64 +} + +func (q *Queries) SubqueryWithHavingClause(ctx context.Context) ([]SubqueryWithHavingClauseRow, error) { + rows, err := q.db.QueryContext(ctx, subqueryWithHavingClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithHavingClauseRow + for rows.Next() { + var i SubqueryWithHavingClauseRow + if err := rows.Scan(&i.A, &i.Count); 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 +} + +const subqueryWithWhereClause = `-- name: SubqueryWithWhereClause :many +SELECT a, (SELECT COUNT(a) FROM foo WHERE a > 10) FROM foo +` + +type SubqueryWithWhereClauseRow struct { + A int64 + Count sql.NullInt64 +} + +func (q *Queries) SubqueryWithWhereClause(ctx context.Context) ([]SubqueryWithWhereClauseRow, error) { + rows, err := q.db.QueryContext(ctx, subqueryWithWhereClause) + if err != nil { + return nil, err + } + defer rows.Close() + var items []SubqueryWithWhereClauseRow + for rows.Next() { + var i SubqueryWithWhereClauseRow + if err := rows.Scan(&i.A, &i.Count); 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/nullable_subselect/sqlite/query.sql b/internal/endtoend/testdata/nullable_subselect/sqlite/query.sql new file mode 100644 index 0000000000..3c0edf6ab9 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/sqlite/query.sql @@ -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) FROM foo; + +-- name: SubqueryWithHavingClause :many +SELECT a, (SELECT COUNT(a) FROM foo GROUP BY b HAVING COUNT(a) > 10) FROM foo; diff --git a/internal/endtoend/testdata/nullable_subselect/sqlite/sqlc.json b/internal/endtoend/testdata/nullable_subselect/sqlite/sqlc.json new file mode 100644 index 0000000000..fcb288cb35 --- /dev/null +++ b/internal/endtoend/testdata/nullable_subselect/sqlite/sqlc.json @@ -0,0 +1,12 @@ +{ + "version": "1", + "packages": [ + { + "path": "go", + "engine": "sqlite", + "name": "querytest", + "schema": "query.sql", + "queries": "query.sql" + } + ] +} \ No newline at end of file