-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
17 changed files
with
522 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.