Skip to content

Commit 4ca759f

Browse files
authored
fix: handle empty column list in postgresql (#1843)
1 parent f29e426 commit 4ca759f

File tree

15 files changed

+311
-1
lines changed

15 files changed

+311
-1
lines changed

internal/codegen/golang/result.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/kyleconroy/sqlc/internal/codegen/sdk"
99
"github.com/kyleconroy/sqlc/internal/inflection"
10+
"github.com/kyleconroy/sqlc/internal/metadata"
1011
"github.com/kyleconroy/sqlc/internal/plugin"
1112
)
1213

@@ -200,7 +201,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
200201
Typ: goType(req, c),
201202
SQLPackage: sqlpkg,
202203
}
203-
} else if len(query.Columns) > 1 {
204+
} else if putOutColumns(query) {
204205
var gs *Struct
205206
var emit bool
206207

@@ -254,6 +255,18 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
254255
return qs, nil
255256
}
256257

258+
func putOutColumns(query *plugin.Query) bool {
259+
if len(query.Columns) > 0 {
260+
return true
261+
}
262+
for _, allowed := range []string{metadata.CmdMany, metadata.CmdOne, metadata.CmdBatchMany} {
263+
if query.Cmd == allowed {
264+
return true
265+
}
266+
}
267+
return false
268+
}
269+
257270
// It's possible that this method will generate duplicate JSON tag values
258271
//
259272
// Columns: count, count, count_2
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package golang
2+
3+
import (
4+
"testing"
5+
6+
"github.com/kyleconroy/sqlc/internal/metadata"
7+
"github.com/kyleconroy/sqlc/internal/plugin"
8+
)
9+
10+
func TestPutOutColumns_ForZeroColumns(t *testing.T) {
11+
tests := []struct {
12+
cmd string
13+
want bool
14+
}{
15+
{
16+
cmd: metadata.CmdExec,
17+
want: false,
18+
},
19+
{
20+
cmd: metadata.CmdExecResult,
21+
want: false,
22+
},
23+
{
24+
cmd: metadata.CmdExecRows,
25+
want: false,
26+
},
27+
{
28+
cmd: metadata.CmdExecLastId,
29+
want: false,
30+
},
31+
{
32+
cmd: metadata.CmdMany,
33+
want: true,
34+
},
35+
{
36+
cmd: metadata.CmdOne,
37+
want: true,
38+
},
39+
{
40+
cmd: metadata.CmdCopyFrom,
41+
want: false,
42+
},
43+
{
44+
cmd: metadata.CmdBatchExec,
45+
want: false,
46+
},
47+
{
48+
cmd: metadata.CmdBatchMany,
49+
want: true,
50+
},
51+
{
52+
cmd: metadata.CmdBatchOne,
53+
want: false,
54+
},
55+
}
56+
for _, tc := range tests {
57+
t.Run(tc.cmd, func(t *testing.T) {
58+
query := &plugin.Query{
59+
Cmd: tc.cmd,
60+
Columns: []*plugin.Column{},
61+
}
62+
got := putOutColumns(query)
63+
if got != tc.want {
64+
t.Errorf("putOutColumns failed. want %v, got %v", tc.want, got)
65+
}
66+
})
67+
}
68+
}
69+
70+
func TestPutOutColumns_AlwaysTrueWhenQueryHasColumns(t *testing.T) {
71+
query := &plugin.Query{
72+
Cmd: metadata.CmdMany,
73+
Columns: []*plugin.Column{{}},
74+
}
75+
if putOutColumns(query) != true {
76+
t.Error("should be true when we have columns")
77+
}
78+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE bar (name text);
2+
3+
-- name: GetBars :many
4+
SELECT FROM bar;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "mysql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package querytest
2+
query.sql:4:12: syntax error near "FROM bar;" "

internal/endtoend/testdata/select_empty_column_list/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/select_empty_column_list/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/select_empty_column_list/postgresql/pgx/go/query.sql.go

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE bar (id serial not null, name text);
2+
3+
-- name: GetBars :many
4+
SELECT FROM bar LIMIT 5;
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"sql_package": "pgx/v4",
8+
"name": "querytest",
9+
"schema": "query.sql",
10+
"queries": "query.sql"
11+
}
12+
]
13+
}

internal/endtoend/testdata/select_empty_column_list/postgresql/stdlib/go/db.go

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

internal/endtoend/testdata/select_empty_column_list/postgresql/stdlib/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/select_empty_column_list/postgresql/stdlib/go/query.sql.go

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
CREATE TABLE bar (id serial not null, name text);
2+
3+
-- name: GetBars :many
4+
SELECT FROM bar LIMIT 5;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

0 commit comments

Comments
 (0)