Skip to content

fix: batch imports filter queries, update cmds having ret type #1842

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/batch/postgresql/batch.go

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

7 changes: 5 additions & 2 deletions examples/batch/postgresql/models.go

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

4 changes: 4 additions & 0 deletions examples/batch/postgresql/querier.go

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

8 changes: 8 additions & 0 deletions examples/batch/postgresql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
SELECT * FROM authors
WHERE author_id = $1;

-- name: DeleteBookExecResult :execresult
DELETE FROM books
WHERE book_id = $1;

-- name: DeleteBook :batchexec
DELETE FROM books
WHERE book_id = $1;
Expand Down Expand Up @@ -38,3 +42,7 @@ RETURNING *;
UPDATE books
SET title = $1, tags = $2
WHERE book_id = $3;

-- name: GetBiography :batchone
SELECT biography FROM authors
WHERE author_id = $1;
19 changes: 15 additions & 4 deletions examples/batch/postgresql/query.sql.go

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

3 changes: 2 additions & 1 deletion examples/batch/postgresql/schema.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY,
name text NOT NULL DEFAULT ''
name text NOT NULL DEFAULT '',
biography JSONB
);

CREATE TYPE book_type AS ENUM (
Expand Down
16 changes: 11 additions & 5 deletions internal/codegen/golang/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ func (i *importer) interfaceImports() fileImports {
std, pkg := buildImports(i.Settings, i.Queries, func(name string) bool {
for _, q := range i.Queries {
if q.hasRetType() {
if usesBatch([]Query{q}) {
continue
}
if strings.HasPrefix(q.Ret.Type(), name) {
return true
}
Expand Down Expand Up @@ -407,11 +410,14 @@ func (i *importer) copyfromImports() fileImports {
}

func (i *importer) batchImports(filename string) fileImports {
std, pkg := buildImports(i.Settings, i.Queries, func(name string) bool {
for _, q := range i.Queries {
if !usesBatch([]Query{q}) {
continue
}
batchQueries := make([]Query, 0, len(i.Queries))
for _, q := range i.Queries {
if usesBatch([]Query{q}) {
batchQueries = append(batchQueries, q)
}
}
std, pkg := buildImports(i.Settings, batchQueries, func(name string) bool {
for _, q := range batchQueries {
if q.hasRetType() {
if q.Ret.EmitStruct() {
for _, f := range q.Ret.Struct.Fields {
Expand Down
3 changes: 2 additions & 1 deletion internal/codegen/golang/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ type Query struct {
}

func (q Query) hasRetType() bool {
scanned := q.Cmd == metadata.CmdOne || q.Cmd == metadata.CmdMany
scanned := q.Cmd == metadata.CmdOne || q.Cmd == metadata.CmdMany ||
q.Cmd == metadata.CmdBatchMany || q.Cmd == metadata.CmdBatchOne
return scanned && !q.Ret.isEmpty()
}

Expand Down