Skip to content

Commit

Permalink
Add support for void columns
Browse files Browse the repository at this point in the history
Void types commonly occur with function calls in projected columns. Add the
necessary plumbing to propagate the void type using a separate type. When
emitting Go code, we emit nil for row scan parameters but ignore void
columns in return types and in structs.

Void types complicate how we handle the ergonomic return values when there's
only 1 output column. We might have a query that has two void columns and one
int column. In that case, we return an int, not a Row.

Fixes #11.
  • Loading branch information
jschaf committed Feb 13, 2021
1 parent 1564c8d commit a12d23e
Show file tree
Hide file tree
Showing 17 changed files with 522 additions and 27 deletions.
7 changes: 7 additions & 0 deletions example/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func TestExamples(t *testing.T) {
"--query-glob", "example/pgcrypto/query.sql",
},
},
{
name: "example/void",
args: []string{
"--schema-glob", "example/void/schema.sql",
"--query-glob", "example/void/query.sql",
},
},
}
if *update {
// update only disables the assertions. Running the tests causes pggen
Expand Down
43 changes: 43 additions & 0 deletions example/void/codegen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package void

import (
"github.com/jschaf/pggen"
"github.com/jschaf/pggen/internal/pgtest"
"github.com/stretchr/testify/assert"
"io/ioutil"
"path/filepath"
"testing"
)

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

tmpDir := t.TempDir()
err := pggen.Generate(
pggen.GenerateOptions{
ConnString: conn.Config().ConnString(),
QueryFiles: []string{"query.sql"},
OutputDir: tmpDir,
GoPackage: "void",
Language: pggen.LangGo,
})
if err != nil {
t.Fatalf("Generate() example/syntax: %s", err)
}

wantQueriesFile := "query.sql.go"
gotQueriesFile := filepath.Join(tmpDir, "query.sql.go")
assert.FileExists(t, gotQueriesFile, "Generate() should emit query.sql.go")
wantQueries, err := ioutil.ReadFile(wantQueriesFile)
if err != nil {
t.Fatalf("read wanted query.go.sql: %s", err)
}
gotQueries, err := ioutil.ReadFile(gotQueriesFile)
if err != nil {
t.Fatalf("read generated query.go.sql: %s", err)
}
assert.Equalf(t, string(wantQueries), string(gotQueries),
"Got file %s; does not match contents of %s",
gotQueriesFile, wantQueriesFile)
}
14 changes: 14 additions & 0 deletions example/void/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- name: VoidOnly :exec
SELECT void_fn();

-- name: VoidOnlyTwoParams :exec
SELECT void_fn_two_params(pggen.arg('id'), 'text');

-- name: VoidTwo :one
SELECT void_fn(), 'foo' as name;

-- name: VoidThree :one
SELECT void_fn(), 'foo' as foo, 'bar' as bar;

-- name: VoidThree2 :many
SELECT 'foo' as foo, void_fn(), void_fn();
252 changes: 252 additions & 0 deletions example/void/query.sql.go

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

48 changes: 48 additions & 0 deletions example/void/query.sql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package void

import (
"context"
"github.com/jschaf/pggen/internal/pgtest"
"github.com/stretchr/testify/assert"
"testing"
)

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

q := NewQuerier(conn)
ctx := context.Background()

if _, err := q.VoidOnly(ctx); err != nil {
t.Fatal(err)
}

if _, err := q.VoidOnlyTwoParams(ctx, 33); err != nil {
t.Fatal(err)
}

{
row, err := q.VoidTwo(ctx)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, "foo", row)
}

{
row, err := q.VoidThree(ctx)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, VoidThreeRow{Foo: "foo", Bar: "bar"}, row)
}

{
foos, err := q.VoidThree2(ctx)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, []string{"foo"}, foos)
}
}
12 changes: 12 additions & 0 deletions example/void/schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE FUNCTION void_fn() RETURNS void AS
$$
BEGIN
END;
$$ LANGUAGE plpgsql;

-- noinspection SqlUnused
CREATE FUNCTION void_fn_two_params(id int, comment text) RETURNS void AS
$$
BEGIN
END;
$$ LANGUAGE plpgsql;
Loading

0 comments on commit a12d23e

Please sign in to comment.