Skip to content

Commit

Permalink
Add example for querying with function out parameters (#64)
Browse files Browse the repository at this point in the history
The main trick is to SELECT FROM the function which converts the record
type into proper postgres columns.

Fixes #25.
  • Loading branch information
jschaf authored May 30, 2022
1 parent 50da598 commit b7cb617
Show file tree
Hide file tree
Showing 8 changed files with 440 additions and 5 deletions.
8 changes: 8 additions & 0 deletions example/acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ func TestExamples(t *testing.T) {
"--go-type", "tenant_id=int",
},
},
{
name: "example/function",
args: []string{
"--schema-glob", "example/function/schema.sql",
"--query-glob", "example/function/query.sql",
"--go-type", "hstore=map[string]string",
},
},
{
name: "example/go_pointer_types",
args: []string{
Expand Down
4 changes: 2 additions & 2 deletions example/composite/query.sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ func TestNewQuerier_ArraysInput(t *testing.T) {
t.Run("ArraysInput", func(t *testing.T) {
want := Arrays{
Texts: []string{"foo", "bar"},
Int8s: []*int{ptrs.NewInt(1), ptrs.NewInt(2), ptrs.NewInt(3)},
Int8s: []*int{ptrs.Int(1), ptrs.Int(2), ptrs.Int(3)},
Bools: []bool{true, true, false},
Floats: []*float64{ptrs.NewFloat64(33.3), ptrs.NewFloat64(66.6)},
Floats: []*float64{ptrs.Float64(33.3), ptrs.Float64(66.6)},
}
got, err := q.ArraysInput(context.Background(), want)
require.NoError(t, err)
Expand Down
44 changes: 44 additions & 0 deletions example/function/codegen_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package function

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_Function(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: "function",
Language: pggen.LangGo,
})
if err != nil {
t.Fatalf("Generate() example/function: %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)
}
2 changes: 2 additions & 0 deletions example/function/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: OutParams :many
SELECT * FROM out_params();
Loading

0 comments on commit b7cb617

Please sign in to comment.