Skip to content

Commit 721eba0

Browse files
authored
fix(codegen): Never generate return structs for :exec (#2976)
* fix(codegen): Never generate return structs for :exec * fix(validate): Ensure batch commands have return values * Fix test output
1 parent fbb5ab8 commit 721eba0

File tree

15 files changed

+129
-19
lines changed

15 files changed

+129
-19
lines changed

internal/codegen/golang/result.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,16 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
312312
return qs, nil
313313
}
314314

315+
var cmdReturnsData = map[string]struct{}{
316+
metadata.CmdBatchMany: {},
317+
metadata.CmdBatchOne: {},
318+
metadata.CmdMany: {},
319+
metadata.CmdOne: {},
320+
}
321+
315322
func putOutColumns(query *plugin.Query) bool {
316-
if len(query.Columns) > 0 {
317-
return true
318-
}
319-
for _, allowed := range []string{metadata.CmdMany, metadata.CmdOne, metadata.CmdBatchMany} {
320-
if query.Cmd == allowed {
321-
return true
322-
}
323-
}
324-
return false
323+
_, found := cmdReturnsData[query.Cmd]
324+
return found
325325
}
326326

327327
// It's possible that this method will generate duplicate JSON tag values

internal/codegen/golang/result_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestPutOutColumns_ForZeroColumns(t *testing.T) {
5050
},
5151
{
5252
cmd: metadata.CmdBatchOne,
53-
want: false,
53+
want: true,
5454
},
5555
}
5656
for _, tc := range tests {

internal/endtoend/testdata/cte_left_join/postgresql/pgx/go/query.sql.go

Lines changed: 0 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/sqlc-dev/sqlc/issues/2970

internal/endtoend/testdata/exec_no_return_struct/postgresql/pgx/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/exec_no_return_struct/postgresql/pgx/go/models.go

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/exec_no_return_struct/postgresql/pgx/go/query.sql.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- name: ResetTaskRun :exec
2+
UPDATE job
3+
SET last_run = NOW()
4+
FROM (
5+
SELECT last_run,
6+
task_name
7+
FROM job AS o
8+
WHERE o.task_name = $1
9+
FOR UPDATE
10+
) AS old_values
11+
WHERE job.task_name = $1
12+
RETURNING job.last_run AS this_run,
13+
old_values.last_run AS last_run;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE IF NOT EXISTS job
2+
(
3+
task_name text NOT NULL,
4+
last_run timestamp with time zone DEFAULT now() NOT NULL
5+
);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: "2"
2+
sql:
3+
- engine: "postgresql"
4+
schema: "schema.sql"
5+
queries: "query.sql"
6+
gen:
7+
go:
8+
package: "querytest"
9+
out: "go"
10+
sql_package: "pgx/v5"

internal/endtoend/testdata/invalid_queries_foo/pgx/v4/query.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ UPDATE foo SET id = $2 WHERE id = $1;
1515

1616
-- name: InsertFoo :one
1717
INSERT INTO foo (id) VALUES ($1);
18+
19+
-- name: InsertFoo :batchone
20+
INSERT INTO foo (id) VALUES ($1);

internal/endtoend/testdata/invalid_queries_foo/pgx/v4/stderr.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ query.sql:5:1: invalid query comment: -- name: ListFoos :one :many
44
query.sql:8:1: invalid query type: :two
55
query.sql:11:1: query "DeleteFoo" specifies parameter ":one" without containing a RETURNING clause
66
query.sql:14:1: query "UpdateFoo" specifies parameter ":one" without containing a RETURNING clause
7-
query.sql:17:1: query "InsertFoo" specifies parameter ":one" without containing a RETURNING clause
7+
query.sql:17:1: query "InsertFoo" specifies parameter ":one" without containing a RETURNING clause
8+
query.sql:20:1: query "InsertFoo" specifies parameter ":batchone" without containing a RETURNING clause

internal/endtoend/testdata/invalid_queries_foo/pgx/v5/query.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ UPDATE foo SET id = $2 WHERE id = $1;
1515

1616
-- name: InsertFoo :one
1717
INSERT INTO foo (id) VALUES ($1);
18+
19+
-- name: InsertFoo :batchone
20+
INSERT INTO foo (id) VALUES ($1);

internal/endtoend/testdata/invalid_queries_foo/pgx/v5/stderr.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ query.sql:5:1: invalid query comment: -- name: ListFoos :one :many
44
query.sql:8:1: invalid query type: :two
55
query.sql:11:1: query "DeleteFoo" specifies parameter ":one" without containing a RETURNING clause
66
query.sql:14:1: query "UpdateFoo" specifies parameter ":one" without containing a RETURNING clause
7-
query.sql:17:1: query "InsertFoo" specifies parameter ":one" without containing a RETURNING clause
7+
query.sql:17:1: query "InsertFoo" specifies parameter ":one" without containing a RETURNING clause
8+
query.sql:20:1: query "InsertFoo" specifies parameter ":batchone" without containing a RETURNING clause

internal/sql/validate/cmd.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,11 @@ func Cmd(n ast.Node, name, cmd string) error {
6767
return validateCopyfrom(n)
6868
}
6969
if (cmd == metadata.CmdBatchExec || cmd == metadata.CmdBatchMany) || cmd == metadata.CmdBatchOne {
70-
return validateBatch(n)
70+
if err := validateBatch(n); err != nil {
71+
return err
72+
}
7173
}
72-
if !(cmd == metadata.CmdMany || cmd == metadata.CmdOne) {
74+
if !(cmd == metadata.CmdMany || cmd == metadata.CmdOne || cmd == metadata.CmdBatchMany || cmd == metadata.CmdBatchOne) {
7375
return nil
7476
}
7577
var list *ast.List

0 commit comments

Comments
 (0)