-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example for querying with function out parameters
The main trick is to SELECT FROM the function which converts the record type into proper postgres columns. Fixes #25.
- Loading branch information
Showing
8 changed files
with
500 additions
and
5 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
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,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) | ||
} |
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,2 @@ | ||
-- name: OutParams :many | ||
SELECT * FROM out_params(); |
Oops, something went wrong.