Skip to content

Commit 95f5175

Browse files
committed
Allow for extra parameters to the queryType
Like `-- name: InsertMulti :exec multiple` and `-- name: Select :many key=group_id` issue sqlc-dev#2353
1 parent 3e0fca0 commit 95f5175

8 files changed

Lines changed: 628 additions & 142 deletions

File tree

‎internal/cmd/shim.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/kyleconroy/sqlc/internal/config"
88
"github.com/kyleconroy/sqlc/internal/config/convert"
99
"github.com/kyleconroy/sqlc/internal/info"
10+
"github.com/kyleconroy/sqlc/internal/metadata"
1011
"github.com/kyleconroy/sqlc/internal/plugin"
1112
"github.com/kyleconroy/sqlc/internal/sql/catalog"
1213
)
@@ -226,9 +227,17 @@ func pluginQueries(r *compiler.Result) []*plugin.Query {
226227
Name: q.InsertIntoTable.Name,
227228
}
228229
}
230+
var cmdParams *plugin.CmdParams
231+
if q.CmdParams != (metadata.CmdParams{}) {
232+
cmdParams = &plugin.CmdParams{
233+
ManyKey: q.CmdParams.ManyKey,
234+
InsertMultiple: q.CmdParams.InsertMultiple,
235+
}
236+
}
229237
out = append(out, &plugin.Query{
230238
Name: q.Name,
231239
Cmd: q.Cmd,
240+
CmdParams: cmdParams,
232241
Text: q.SQL,
233242
Comments: q.Comments,
234243
Columns: columns,

‎internal/compiler/parse.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
7070
if err := validate.In(c.catalog, raw); err != nil {
7171
return nil, err
7272
}
73-
name, cmd, err := metadata.Parse(strings.TrimSpace(rawSQL), c.parser.CommentSyntax())
73+
name, cmd, cmdParams, err := metadata.Parse(strings.TrimSpace(rawSQL), c.parser.CommentSyntax())
7474
if err != nil {
7575
return nil, err
7676
}
@@ -133,6 +133,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
133133
Columns: cols,
134134
SQL: trimmed,
135135
InsertIntoTable: table,
136+
CmdParams: cmdParams,
136137
}, nil
137138
}
138139

‎internal/compiler/query.go‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package compiler
22

33
import (
4+
"github.com/kyleconroy/sqlc/internal/metadata"
45
"github.com/kyleconroy/sqlc/internal/sql/ast"
56
)
67

@@ -39,12 +40,13 @@ type Column struct {
3940
}
4041

4142
type Query struct {
42-
SQL string
43-
Name string
44-
Cmd string // TODO: Pick a better name. One of: one, many, exec, execrows, copyFrom
45-
Columns []*Column
46-
Params []Parameter
47-
Comments []string
43+
SQL string
44+
Name string
45+
Cmd string // TODO: Pick a better name. One of: one, many, exec, execrows, copyFrom
46+
CmdParams metadata.CmdParams
47+
Columns []*Column
48+
Params []Parameter
49+
Comments []string
4850

4951
// XXX: Hack
5052
Filename string

‎internal/metadata/meta.go‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ const (
2525
CmdBatchOne = ":batchone"
2626
)
2727

28+
type CmdParams struct {
29+
ManyKey string
30+
InsertMultiple bool
31+
}
32+
2833
// A query name must be a valid Go identifier
2934
//
3035
// https://golang.org/ref/spec#Identifiers
@@ -44,7 +49,7 @@ func validateQueryName(name string) error {
4449
return nil
4550
}
4651

47-
func Parse(t string, commentStyle CommentSyntax) (string, string, error) {
52+
func Parse(t string, commentStyle CommentSyntax) (string, string, CmdParams, error) {
4853
for _, line := range strings.Split(t, "\n") {
4954
var prefix string
5055
if strings.HasPrefix(line, "--") {
@@ -76,30 +81,51 @@ func Parse(t string, commentStyle CommentSyntax) (string, string, error) {
7681
continue
7782
}
7883
if !strings.HasPrefix(rest, " name: ") {
79-
return "", "", fmt.Errorf("invalid metadata: %s", line)
84+
return "", "", CmdParams{}, fmt.Errorf("invalid metadata: %s", line)
8085
}
8186

8287
part := strings.Split(strings.TrimSpace(line), " ")
8388
if prefix == "/*" {
8489
part = part[:len(part)-1] // removes the trailing "*/" element
8590
}
8691
if len(part) == 2 {
87-
return "", "", fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
88-
}
89-
if len(part) != 4 {
90-
return "", "", fmt.Errorf("invalid query comment: %s", line)
92+
return "", "", CmdParams{}, fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
9193
}
9294
queryName := part[2]
9395
queryType := strings.TrimSpace(part[3])
9496
switch queryType {
9597
case CmdOne, CmdMany, CmdExec, CmdExecResult, CmdExecRows, CmdExecLastId, CmdCopyFrom, CmdBatchExec, CmdBatchMany, CmdBatchOne:
9698
default:
97-
return "", "", fmt.Errorf("invalid query type: %s", queryType)
99+
return "", "", CmdParams{}, fmt.Errorf("invalid query type: %s", queryType)
98100
}
99101
if err := validateQueryName(queryName); err != nil {
100-
return "", "", err
102+
return "", "", CmdParams{}, err
103+
}
104+
cmdParams, err := parseCmdParams(part[4:], queryType)
105+
if err != nil {
106+
return "", "", CmdParams{}, err
107+
}
108+
return queryName, queryType, cmdParams, nil
109+
}
110+
return "", "", CmdParams{}, nil
111+
}
112+
113+
func parseCmdParams(part []string, queryType string) (CmdParams, error) {
114+
var ret CmdParams
115+
for _, p := range part {
116+
if p == "multiple" {
117+
if queryType != CmdExec && queryType != CmdExecResult && queryType != CmdExecRows && queryType != CmdExecLastId {
118+
return ret, fmt.Errorf("query command parameter multiple is invalid for query type %s", queryType)
119+
}
120+
ret.InsertMultiple = true
121+
} else if strings.HasPrefix(p, "key=") {
122+
if queryType != CmdMany {
123+
return ret, fmt.Errorf("query command parameter %s is invalid for query type %s", p, queryType)
124+
}
125+
ret.ManyKey = strings.TrimPrefix(p, "key=")
126+
} else {
127+
return ret, fmt.Errorf("invalid query command parameter %q", p)
101128
}
102-
return queryName, queryType, nil
103129
}
104-
return "", "", nil
130+
return ret, nil
105131
}

‎internal/metadata/meta_test.go‎

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package metadata
22

33
import "testing"
44

5-
func TestParseMetadata(t *testing.T) {
5+
func TestNonMetadata(t *testing.T) {
66

77
for _, query := range []string{
88
`-- name: CreateFoo, :one`,
@@ -17,7 +17,7 @@ func TestParseMetadata(t *testing.T) {
1717
"-- name:CreateFoo",
1818
`--name:CreateFoo :two`,
1919
} {
20-
if _, _, err := Parse(query, CommentSyntax{Dash: true}); err == nil {
20+
if _, _, _, err := Parse(query, CommentSyntax{Dash: true}); err == nil {
2121
t.Errorf("expected invalid metadata: %q", query)
2222
}
2323
}
@@ -27,21 +27,52 @@ func TestParseMetadata(t *testing.T) {
2727
`-- name comment`,
2828
`--name comment`,
2929
} {
30-
if _, _, err := Parse(query, CommentSyntax{Dash: true}); err != nil {
30+
if _, _, _, err := Parse(query, CommentSyntax{Dash: true}); err != nil {
3131
t.Errorf("expected valid comment: %q", query)
3232
}
3333
}
34+
}
3435

35-
query := `-- name: CreateFoo :one`
36-
queryName, queryType, err := Parse(query, CommentSyntax{Dash: true})
37-
if err != nil {
38-
t.Errorf("expected valid metadata: %q", query)
39-
}
40-
if queryName != "CreateFoo" {
41-
t.Errorf("incorrect queryName parsed: %q", query)
36+
func TestParse(t *testing.T) {
37+
tests := []struct {
38+
query string
39+
wantName string
40+
wantType string
41+
wantCmdParams CmdParams
42+
}{
43+
{
44+
query: "-- name: CreateFoo :one",
45+
wantName: "CreateFoo",
46+
wantType: CmdOne,
47+
},
48+
{
49+
query: "-- name: InsertMulti :exec multiple",
50+
wantName: "InsertMulti",
51+
wantType: CmdExec,
52+
wantCmdParams: CmdParams{InsertMultiple: true},
53+
},
54+
{
55+
query: "-- name: SelectKey :many key=group_id",
56+
wantName: "SelectKey",
57+
wantType: CmdMany,
58+
wantCmdParams: CmdParams{ManyKey: "group_id"},
59+
},
4260
}
43-
if queryType != CmdOne {
44-
t.Errorf("incorrect queryType parsed: %q", query)
61+
for _, tc := range tests {
62+
t.Run(tc.query, func(t *testing.T) {
63+
name, queryType, cmdParams, err := Parse(tc.query, CommentSyntax{Dash: true})
64+
if err != nil {
65+
t.Fatalf("Parse failed: %v", err)
66+
}
67+
if name != tc.wantName {
68+
t.Errorf("unexpected name: got %q; want %q", name, tc.wantName)
69+
}
70+
if queryType != tc.wantType {
71+
t.Errorf("unexpected queryType: got %q; want %q", queryType, tc.wantType)
72+
}
73+
if cmdParams != tc.wantCmdParams {
74+
t.Errorf("unexpected cmdParams: got %#v; want %#v", cmdParams, tc.wantCmdParams)
75+
}
76+
})
4577
}
46-
4778
}

0 commit comments

Comments
 (0)