Skip to content

Commit

Permalink
Fix primitive array types inside a composite type (#63)
Browse files Browse the repository at this point in the history
Previously, we would attempt to create a newArrayInit function, but instead we
can use the pgtype array variants, like pgtype.BoolArray.
  • Loading branch information
jschaf authored May 30, 2022
1 parent 8c321e6 commit 50da598
Show file tree
Hide file tree
Showing 12 changed files with 281 additions and 4 deletions.
4 changes: 4 additions & 0 deletions example/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func TestExamples(t *testing.T) {
args: []string{
"--schema-glob", "example/composite/schema.sql",
"--query-glob", "example/composite/query.sql",
"--go-type", "_bool=[]bool",
"--go-type", "bool=bool",
"--go-type", "int8=int",
"--go-type", "int4=int",
"--go-type", "text=string",
Expand All @@ -71,6 +73,8 @@ func TestExamples(t *testing.T) {
args: []string{
"--schema-glob", "example/slices/schema.sql",
"--query-glob", "example/slices/query.sql",
"--go-type", "_bool=[]bool",
"--go-type", "bool=bool",
"--go-type", "timestamp=*time.Time",
"--go-type", "_timestamp=[]*time.Time",
"--go-type", "timestamptz=*time.Time",
Expand Down
8 changes: 5 additions & 3 deletions example/composite/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ func TestGenerate_Go_Example_Composite(t *testing.T) {
GoPackage: "composite",
Language: pggen.LangGo,
TypeOverrides: map[string]string{
"int4": "int",
"int8": "int",
"text": "string",
"_bool": "[]bool",
"bool": "bool",
"int8": "int",
"int4": "int",
"text": "string",
},
})
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions example/composite/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ INSERT
INTO blocks (screenshot_id, body)
VALUES (pggen.arg('ScreenshotID'), pggen.arg('Body'))
RETURNING id, screenshot_id, body;


-- name: ArraysInput :one
SELECT pggen.arg('arrays')::arrays;
93 changes: 93 additions & 0 deletions example/composite/query.sql.go

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

21 changes: 21 additions & 0 deletions example/composite/query.sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package composite
import (
"context"
"github.com/jackc/pgx/v4"
"github.com/jschaf/pggen/internal/difftest"
"github.com/jschaf/pggen/internal/errs"
"github.com/jschaf/pggen/internal/pgtest"
"github.com/jschaf/pggen/internal/ptrs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"testing"
Expand Down Expand Up @@ -85,6 +87,25 @@ func TestNewQuerier_SearchScreenshots(t *testing.T) {
})
}

func TestNewQuerier_ArraysInput(t *testing.T) {
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()

q := NewQuerier(conn)

t.Run("ArraysInput", func(t *testing.T) {
want := Arrays{
Texts: []string{"foo", "bar"},
Int8s: []*int{ptrs.NewInt(1), ptrs.NewInt(2), ptrs.NewInt(3)},
Bools: []bool{true, true, false},
Floats: []*float64{ptrs.NewFloat64(33.3), ptrs.NewFloat64(66.6)},
}
got, err := q.ArraysInput(context.Background(), want)
require.NoError(t, err)
difftest.AssertSame(t, want, got)
})
}

func insertScreenshotBlock(t *testing.T, q *DBQuerier, screenID int, body string) InsertScreenshotBlocksRow {
t.Helper()
row, err := q.InsertScreenshotBlocks(context.Background(), screenID, body)
Expand Down
7 changes: 7 additions & 0 deletions example/composite/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,10 @@ CREATE TABLE blocks (
screenshot_id bigint NOT NULL REFERENCES screenshots (id),
body text NOT NULL
);

CREATE TYPE arrays AS (
texts text[],
int8s int8[],
bools boolean[],
floats float8[]
);
2 changes: 2 additions & 0 deletions example/slices/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ func TestGenerate_Go_Example_Slices(t *testing.T) {
GoPackage: "slices",
Language: pggen.LangGo,
TypeOverrides: map[string]string{
"_bool": "[]bool",
"bool": "bool",
"timestamp": "*time.Time",
"_timestamp": "[]*time.Time",
"timestamptz": "*time.Time",
Expand Down
3 changes: 3 additions & 0 deletions example/slices/query.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
-- name: GetBools :one
SELECT pggen.arg('data')::boolean[];

-- name: GetOneTimestamp :one
SELECT pggen.arg('data')::timestamp;

Expand Down
99 changes: 99 additions & 0 deletions example/slices/query.sql.go

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

26 changes: 26 additions & 0 deletions example/slices/query.sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,32 @@ import (
"time"
)

func TestNewQuerier_GetBools(t *testing.T) {
ctx := context.Background()
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
defer cleanup()

q := NewQuerier(conn)

t.Run("GetBools", func(t *testing.T) {
want := []bool{true, true, false}
got, err := q.GetBools(ctx, want)
require.NoError(t, err)
difftest.AssertSame(t, want, got)
})

t.Run("GetBoolsBatch", func(t *testing.T) {
batch := &pgx.Batch{}
want := []bool{true, true, false}
q.GetBoolsBatch(batch, want)
results := conn.SendBatch(ctx, batch)
defer errs.CaptureT(t, results.Close, "close batch results")
got, err := q.GetBoolsScan(results)
require.NoError(t, err)
difftest.AssertSame(t, want, got)
})
}

func TestNewQuerier_GetOneTimestamp(t *testing.T) {
ctx := context.Background()
conn, cleanup := pgtest.NewPostgresSchema(t, []string{"schema.sql"})
Expand Down
Loading

0 comments on commit 50da598

Please sign in to comment.