diff --git a/internal/endtoend/testdata/coalesce/postgresql/pgx/go/models.go b/internal/endtoend/testdata/coalesce/postgresql/pgx/go/models.go index 1ee813a39b..10f85c7550 100644 --- a/internal/endtoend/testdata/coalesce/postgresql/pgx/go/models.go +++ b/internal/endtoend/testdata/coalesce/postgresql/pgx/go/models.go @@ -9,4 +9,6 @@ import ( type Foo struct { Bar sql.NullString Bat string + Baz sql.NullInt64 + Qux int64 } diff --git a/internal/endtoend/testdata/coalesce/postgresql/pgx/go/query.sql.go b/internal/endtoend/testdata/coalesce/postgresql/pgx/go/query.sql.go index 0cca9e601b..7623f689e8 100644 --- a/internal/endtoend/testdata/coalesce/postgresql/pgx/go/query.sql.go +++ b/internal/endtoend/testdata/coalesce/postgresql/pgx/go/query.sql.go @@ -8,13 +8,99 @@ import ( "database/sql" ) -const coalesce = `-- name: Coalesce :many +const coalesceNumeric = `-- name: CoalesceNumeric :many +SELECT coalesce(baz, 0) as login +FROM foo +` + +func (q *Queries) CoalesceNumeric(ctx context.Context) ([]int64, error) { + rows, err := q.db.Query(ctx, coalesceNumeric) + if err != nil { + return nil, err + } + defer rows.Close() + var items []int64 + for rows.Next() { + var login int64 + if err := rows.Scan(&login); err != nil { + return nil, err + } + items = append(items, login) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const coalesceNumericColumns = `-- name: CoalesceNumericColumns :many +SELECT baz, qux, coalesce(baz, qux) +FROM foo +` + +type CoalesceNumericColumnsRow struct { + Baz sql.NullInt64 + Qux int64 + Baz_2 int64 +} + +func (q *Queries) CoalesceNumericColumns(ctx context.Context) ([]CoalesceNumericColumnsRow, error) { + rows, err := q.db.Query(ctx, coalesceNumericColumns) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CoalesceNumericColumnsRow + for rows.Next() { + var i CoalesceNumericColumnsRow + if err := rows.Scan(&i.Baz, &i.Qux, &i.Baz_2); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const coalesceNumericNull = `-- name: CoalesceNumericNull :many +SELECT baz, coalesce(baz) +FROM foo +` + +type CoalesceNumericNullRow struct { + Baz sql.NullInt64 + Baz_2 int64 +} + +func (q *Queries) CoalesceNumericNull(ctx context.Context) ([]CoalesceNumericNullRow, error) { + rows, err := q.db.Query(ctx, coalesceNumericNull) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CoalesceNumericNullRow + for rows.Next() { + var i CoalesceNumericNullRow + if err := rows.Scan(&i.Baz, &i.Baz_2); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const coalesceString = `-- name: CoalesceString :many SELECT coalesce(bar, '') as login FROM foo ` -func (q *Queries) Coalesce(ctx context.Context) ([]string, error) { - rows, err := q.db.Query(ctx, coalesce) +func (q *Queries) CoalesceString(ctx context.Context) ([]string, error) { + rows, err := q.db.Query(ctx, coalesceString) if err != nil { return nil, err } @@ -33,26 +119,26 @@ func (q *Queries) Coalesce(ctx context.Context) ([]string, error) { return items, nil } -const coalesceColumns = `-- name: CoalesceColumns :many +const coalesceStringColumns = `-- name: CoalesceStringColumns :many SELECT bar, bat, coalesce(bar, bat) FROM foo ` -type CoalesceColumnsRow struct { +type CoalesceStringColumnsRow struct { Bar sql.NullString Bat string Bar_2 string } -func (q *Queries) CoalesceColumns(ctx context.Context) ([]CoalesceColumnsRow, error) { - rows, err := q.db.Query(ctx, coalesceColumns) +func (q *Queries) CoalesceStringColumns(ctx context.Context) ([]CoalesceStringColumnsRow, error) { + rows, err := q.db.Query(ctx, coalesceStringColumns) if err != nil { return nil, err } defer rows.Close() - var items []CoalesceColumnsRow + var items []CoalesceStringColumnsRow for rows.Next() { - var i CoalesceColumnsRow + var i CoalesceStringColumnsRow if err := rows.Scan(&i.Bar, &i.Bat, &i.Bar_2); err != nil { return nil, err } @@ -63,3 +149,33 @@ func (q *Queries) CoalesceColumns(ctx context.Context) ([]CoalesceColumnsRow, er } return items, nil } + +const coalesceStringNull = `-- name: CoalesceStringNull :many +SELECT bar, coalesce(bar) +FROM foo +` + +type CoalesceStringNullRow struct { + Bar sql.NullString + Bar_2 string +} + +func (q *Queries) CoalesceStringNull(ctx context.Context) ([]CoalesceStringNullRow, error) { + rows, err := q.db.Query(ctx, coalesceStringNull) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CoalesceStringNullRow + for rows.Next() { + var i CoalesceStringNullRow + if err := rows.Scan(&i.Bar, &i.Bar_2); 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/coalesce/postgresql/pgx/query.sql b/internal/endtoend/testdata/coalesce/postgresql/pgx/query.sql index 022c1fb653..63cf1a6d10 100644 --- a/internal/endtoend/testdata/coalesce/postgresql/pgx/query.sql +++ b/internal/endtoend/testdata/coalesce/postgresql/pgx/query.sql @@ -1,9 +1,30 @@ -CREATE TABLE foo (bar text, bat text not null); +CREATE TABLE foo ( + bar text, + bat text not null, + baz bigint, + qux bigint not null +); --- name: Coalesce :many +-- name: CoalesceString :many SELECT coalesce(bar, '') as login FROM foo; --- name: CoalesceColumns :many +-- name: CoalesceNumeric :many +SELECT coalesce(baz, 0) as login +FROM foo; + +-- name: CoalesceStringColumns :many SELECT bar, bat, coalesce(bar, bat) FROM foo; + +-- name: CoalesceNumericColumns :many +SELECT baz, qux, coalesce(baz, qux) +FROM foo; + +-- name: CoalesceStringNull :many +SELECT bar, coalesce(bar) +FROM foo; + +-- name: CoalesceNumericNull :many +SELECT baz, coalesce(baz) +FROM foo; diff --git a/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/models.go b/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/models.go index 1ee813a39b..10f85c7550 100644 --- a/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/models.go +++ b/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/models.go @@ -9,4 +9,6 @@ import ( type Foo struct { Bar sql.NullString Bat string + Baz sql.NullInt64 + Qux int64 } diff --git a/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/query.sql.go b/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/query.sql.go index 3f69c6b692..7dc9aade4e 100644 --- a/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/query.sql.go +++ b/internal/endtoend/testdata/coalesce/postgresql/stdlib/go/query.sql.go @@ -8,13 +8,108 @@ import ( "database/sql" ) -const coalesce = `-- name: Coalesce :many +const coalesceNumeric = `-- name: CoalesceNumeric :many +SELECT coalesce(baz, 0) as login +FROM foo +` + +func (q *Queries) CoalesceNumeric(ctx context.Context) ([]int64, error) { + rows, err := q.db.QueryContext(ctx, coalesceNumeric) + if err != nil { + return nil, err + } + defer rows.Close() + var items []int64 + for rows.Next() { + var login int64 + if err := rows.Scan(&login); err != nil { + return nil, err + } + items = append(items, login) + } + if err := rows.Close(); err != nil { + return nil, err + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const coalesceNumericColumns = `-- name: CoalesceNumericColumns :many +SELECT baz, qux, coalesce(baz, qux) +FROM foo +` + +type CoalesceNumericColumnsRow struct { + Baz sql.NullInt64 + Qux int64 + Baz_2 int64 +} + +func (q *Queries) CoalesceNumericColumns(ctx context.Context) ([]CoalesceNumericColumnsRow, error) { + rows, err := q.db.QueryContext(ctx, coalesceNumericColumns) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CoalesceNumericColumnsRow + for rows.Next() { + var i CoalesceNumericColumnsRow + if err := rows.Scan(&i.Baz, &i.Qux, &i.Baz_2); 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 coalesceNumericNull = `-- name: CoalesceNumericNull :many +SELECT baz, coalesce(baz) +FROM foo +` + +type CoalesceNumericNullRow struct { + Baz sql.NullInt64 + Baz_2 int64 +} + +func (q *Queries) CoalesceNumericNull(ctx context.Context) ([]CoalesceNumericNullRow, error) { + rows, err := q.db.QueryContext(ctx, coalesceNumericNull) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CoalesceNumericNullRow + for rows.Next() { + var i CoalesceNumericNullRow + if err := rows.Scan(&i.Baz, &i.Baz_2); 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 coalesceString = `-- name: CoalesceString :many SELECT coalesce(bar, '') as login FROM foo ` -func (q *Queries) Coalesce(ctx context.Context) ([]string, error) { - rows, err := q.db.QueryContext(ctx, coalesce) +func (q *Queries) CoalesceString(ctx context.Context) ([]string, error) { + rows, err := q.db.QueryContext(ctx, coalesceString) if err != nil { return nil, err } @@ -36,26 +131,26 @@ func (q *Queries) Coalesce(ctx context.Context) ([]string, error) { return items, nil } -const coalesceColumns = `-- name: CoalesceColumns :many +const coalesceStringColumns = `-- name: CoalesceStringColumns :many SELECT bar, bat, coalesce(bar, bat) FROM foo ` -type CoalesceColumnsRow struct { +type CoalesceStringColumnsRow struct { Bar sql.NullString Bat string Bar_2 string } -func (q *Queries) CoalesceColumns(ctx context.Context) ([]CoalesceColumnsRow, error) { - rows, err := q.db.QueryContext(ctx, coalesceColumns) +func (q *Queries) CoalesceStringColumns(ctx context.Context) ([]CoalesceStringColumnsRow, error) { + rows, err := q.db.QueryContext(ctx, coalesceStringColumns) if err != nil { return nil, err } defer rows.Close() - var items []CoalesceColumnsRow + var items []CoalesceStringColumnsRow for rows.Next() { - var i CoalesceColumnsRow + var i CoalesceStringColumnsRow if err := rows.Scan(&i.Bar, &i.Bat, &i.Bar_2); err != nil { return nil, err } @@ -69,3 +164,36 @@ func (q *Queries) CoalesceColumns(ctx context.Context) ([]CoalesceColumnsRow, er } return items, nil } + +const coalesceStringNull = `-- name: CoalesceStringNull :many +SELECT bar, coalesce(bar) +FROM foo +` + +type CoalesceStringNullRow struct { + Bar sql.NullString + Bar_2 string +} + +func (q *Queries) CoalesceStringNull(ctx context.Context) ([]CoalesceStringNullRow, error) { + rows, err := q.db.QueryContext(ctx, coalesceStringNull) + if err != nil { + return nil, err + } + defer rows.Close() + var items []CoalesceStringNullRow + for rows.Next() { + var i CoalesceStringNullRow + if err := rows.Scan(&i.Bar, &i.Bar_2); 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/coalesce/postgresql/stdlib/query.sql b/internal/endtoend/testdata/coalesce/postgresql/stdlib/query.sql index 022c1fb653..63cf1a6d10 100644 --- a/internal/endtoend/testdata/coalesce/postgresql/stdlib/query.sql +++ b/internal/endtoend/testdata/coalesce/postgresql/stdlib/query.sql @@ -1,9 +1,30 @@ -CREATE TABLE foo (bar text, bat text not null); +CREATE TABLE foo ( + bar text, + bat text not null, + baz bigint, + qux bigint not null +); --- name: Coalesce :many +-- name: CoalesceString :many SELECT coalesce(bar, '') as login FROM foo; --- name: CoalesceColumns :many +-- name: CoalesceNumeric :many +SELECT coalesce(baz, 0) as login +FROM foo; + +-- name: CoalesceStringColumns :many SELECT bar, bat, coalesce(bar, bat) FROM foo; + +-- name: CoalesceNumericColumns :many +SELECT baz, qux, coalesce(baz, qux) +FROM foo; + +-- name: CoalesceStringNull :many +SELECT bar, coalesce(bar) +FROM foo; + +-- name: CoalesceNumericNull :many +SELECT baz, coalesce(baz) +FROM foo;