Skip to content

fix: handle empty column list in postgresql select statements #1843

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 1 commit 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
15 changes: 14 additions & 1 deletion internal/codegen/golang/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/kyleconroy/sqlc/internal/codegen/sdk"
"github.com/kyleconroy/sqlc/internal/inflection"
"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/plugin"
)

Expand Down Expand Up @@ -200,7 +201,7 @@ func buildQueries(req *plugin.CodeGenRequest, structs []Struct) ([]Query, error)
Typ: goType(req, c),
SQLPackage: sqlpkg,
}
} else if len(query.Columns) > 1 {
} else if putOutColumns(query) {
var gs *Struct
var emit bool

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

func putOutColumns(query *plugin.Query) bool {
if len(query.Columns) > 0 {
return true
}
for _, allowed := range []string{metadata.CmdMany, metadata.CmdOne, metadata.CmdBatchMany} {
if query.Cmd == allowed {
return true
}
}
return false
}

// It's possible that this method will generate duplicate JSON tag values
//
// Columns: count, count, count_2
Expand Down
78 changes: 78 additions & 0 deletions internal/codegen/golang/result_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package golang

import (
"testing"

"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/plugin"
)

func TestPutOutColumns_ForZeroColumns(t *testing.T) {
tests := []struct {
cmd string
want bool
}{
{
cmd: metadata.CmdExec,
want: false,
},
{
cmd: metadata.CmdExecResult,
want: false,
},
{
cmd: metadata.CmdExecRows,
want: false,
},
{
cmd: metadata.CmdExecLastId,
want: false,
},
{
cmd: metadata.CmdMany,
want: true,
},
{
cmd: metadata.CmdOne,
want: true,
},
{
cmd: metadata.CmdCopyFrom,
want: false,
},
{
cmd: metadata.CmdBatchExec,
want: false,
},
{
cmd: metadata.CmdBatchMany,
want: true,
},
{
cmd: metadata.CmdBatchOne,
want: false,
},
}
for _, tc := range tests {
t.Run(tc.cmd, func(t *testing.T) {
query := &plugin.Query{
Cmd: tc.cmd,
Columns: []*plugin.Column{},
}
got := putOutColumns(query)
if got != tc.want {
t.Errorf("putOutColumns failed. want %v, got %v", tc.want, got)
}
})
}
}

func TestPutOutColumns_AlwaysTrueWhenQueryHasColumns(t *testing.T) {
query := &plugin.Query{
Cmd: metadata.CmdMany,
Columns: []*plugin.Column{{}},
}
if putOutColumns(query) != true {
t.Error("should be true when we have columns")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE bar (name text);

-- name: GetBars :many
SELECT FROM bar;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "mysql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# package querytest
query.sql:4:12: syntax error near "FROM bar;" "

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE bar (id serial not null, name text);

-- name: GetBars :many
SELECT FROM bar LIMIT 5;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"sql_package": "pgx/v4",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE bar (id serial not null, name text);

-- name: GetBars :many
SELECT FROM bar LIMIT 5;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"path": "go",
"engine": "postgresql",
"name": "querytest",
"schema": "query.sql",
"queries": "query.sql"
}
]
}