Skip to content
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

Allow for extra parameters to the queryType #2375

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/kyleconroy/sqlc/internal/config"
"github.com/kyleconroy/sqlc/internal/config/convert"
"github.com/kyleconroy/sqlc/internal/info"
"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/plugin"
"github.com/kyleconroy/sqlc/internal/sql/catalog"
)
Expand Down Expand Up @@ -226,9 +227,18 @@ func pluginQueries(r *compiler.Result) []*plugin.Query {
Name: q.InsertIntoTable.Name,
}
}
var cmdParams *plugin.CmdParams
if q.CmdParams != (metadata.CmdParams{}) {
cmdParams = &plugin.CmdParams{
ManyKey: q.CmdParams.ManyKey,
InsertMultiple: q.CmdParams.InsertMultiple,
NoInference: q.CmdParams.NoInference,
}
}
out = append(out, &plugin.Query{
Name: q.Name,
Cmd: q.Cmd,
CmdParams: cmdParams,
Text: q.SQL,
Comments: q.Comments,
Columns: columns,
Expand Down
3 changes: 2 additions & 1 deletion internal/compiler/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
if err := validate.In(c.catalog, raw); err != nil {
return nil, err
}
name, cmd, err := metadata.Parse(strings.TrimSpace(rawSQL), c.parser.CommentSyntax())
name, cmd, cmdParams, err := metadata.Parse(strings.TrimSpace(rawSQL), c.parser.CommentSyntax())
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -133,6 +133,7 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
Columns: cols,
SQL: trimmed,
InsertIntoTable: table,
CmdParams: cmdParams,
}, nil
}

Expand Down
14 changes: 8 additions & 6 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package compiler

import (
"github.com/kyleconroy/sqlc/internal/metadata"
"github.com/kyleconroy/sqlc/internal/sql/ast"
)

Expand Down Expand Up @@ -39,12 +40,13 @@ type Column struct {
}

type Query struct {
SQL string
Name string
Cmd string // TODO: Pick a better name. One of: one, many, exec, execrows, copyFrom
Columns []*Column
Params []Parameter
Comments []string
SQL string
Name string
Cmd string // TODO: Pick a better name. One of: one, many, exec, execrows, copyFrom
CmdParams metadata.CmdParams
Columns []*Column
Params []Parameter
Comments []string

// XXX: Hack
Filename string
Expand Down
4 changes: 4 additions & 0 deletions internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -62583,6 +62583,7 @@
"text": "SELECT id, name, bio FROM authors\nWHERE id = $1 LIMIT 1",
"name": "GetAuthor",
"cmd": ":one",
"cmd_params": null,
"columns": [
{
"name": "id",
Expand Down Expand Up @@ -62698,6 +62699,7 @@
"text": "SELECT id, name, bio FROM authors\nORDER BY name",
"name": "ListAuthors",
"cmd": ":many",
"cmd_params": null,
"columns": [
{
"name": "id",
Expand Down Expand Up @@ -62784,6 +62786,7 @@
"text": "INSERT INTO authors (\n name, bio\n) VALUES (\n $1, $2\n)\nRETURNING id, name, bio",
"name": "CreateAuthor",
"cmd": ":one",
"cmd_params": null,
"columns": [
{
"name": "id",
Expand Down Expand Up @@ -62931,6 +62934,7 @@
"text": "DELETE FROM authors\nWHERE id = $1",
"name": "DeleteAuthor",
"cmd": ":exec",
"cmd_params": null,
"columns": [],
"params": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# package querytest
query.sql:4:1: invalid query comment: -- name: ListFoos
query.sql:7:1: invalid query comment: -- name: ListFoos :one :many
query.sql:4:1: missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: -- name: ListFoos
query.sql:7:1: invalid query command parameter ":many"
query.sql:10:1: invalid query type: :two
query.sql:13:1: query "DeleteFoo" specifies parameter ":one" without containing a RETURNING clause
query.sql:16:1: query "UpdateFoo" specifies parameter ":one" without containing a RETURNING clause
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# package querytest
query.sql:4:1: invalid query comment: -- name: ListFoos
query.sql:7:1: invalid query comment: -- name: ListFoos :one :many
query.sql:4:1: missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: -- name: ListFoos
query.sql:7:1: invalid query command parameter ":many"
query.sql:10:1: invalid query type: :two
query.sql:13:1: query "DeleteFoo" specifies parameter ":one" without containing a RETURNING clause
query.sql:16:1: query "UpdateFoo" specifies parameter ":one" without containing a RETURNING clause
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# package querytest
query.sql:4:1: invalid query comment: -- name: ListFoos
query.sql:7:1: invalid query comment: -- name: ListFoos :one :many
query.sql:4:1: missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: -- name: ListFoos
query.sql:7:1: invalid query command parameter ":many"
query.sql:10:1: invalid query type: :two
query.sql:13:1: query "DeleteFoo" specifies parameter ":one" without containing a RETURNING clause
query.sql:16:1: query "UpdateFoo" specifies parameter ":one" without containing a RETURNING clause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62583,6 +62583,7 @@
"text": "SELECT id, name, bio FROM authors\nWHERE id = $1 LIMIT 1",
"name": "GetAuthor",
"cmd": ":one",
"cmd_params": null,
"columns": [
{
"name": "id",
Expand Down Expand Up @@ -62698,6 +62699,7 @@
"text": "SELECT id, name, bio FROM authors\nORDER BY name",
"name": "ListAuthors",
"cmd": ":many",
"cmd_params": null,
"columns": [
{
"name": "id",
Expand Down Expand Up @@ -62784,6 +62786,7 @@
"text": "INSERT INTO authors (\n name, bio\n) VALUES (\n $1, $2\n)\nRETURNING id, name, bio",
"name": "CreateAuthor",
"cmd": ":one",
"cmd_params": null,
"columns": [
{
"name": "id",
Expand Down Expand Up @@ -62931,6 +62934,7 @@
"text": "DELETE FROM authors\nWHERE id = $1",
"name": "DeleteAuthor",
"cmd": ":exec",
"cmd_params": null,
"columns": [],
"params": [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62583,6 +62583,7 @@
"text": "SELECT id, name, bio FROM authors\nWHERE id = $1 LIMIT 1",
"name": "GetAuthor",
"cmd": ":one",
"cmd_params": null,
"columns": [
{
"name": "id",
Expand Down Expand Up @@ -62698,6 +62699,7 @@
"text": "SELECT id, name, bio FROM authors\nORDER BY name",
"name": "ListAuthors",
"cmd": ":many",
"cmd_params": null,
"columns": [
{
"name": "id",
Expand Down Expand Up @@ -62784,6 +62786,7 @@
"text": "INSERT INTO authors (\n name, bio\n) VALUES (\n $1, $2\n)\nRETURNING id, name, bio",
"name": "CreateAuthor",
"cmd": ":one",
"cmd_params": null,
"columns": [
{
"name": "id",
Expand Down Expand Up @@ -62931,6 +62934,7 @@
"text": "DELETE FROM authors\nWHERE id = $1",
"name": "DeleteAuthor",
"cmd": ":exec",
"cmd_params": null,
"columns": [],
"params": [
{
Expand Down
54 changes: 43 additions & 11 deletions internal/metadata/meta.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ const (
CmdBatchOne = ":batchone"
)

type CmdParams struct {
ManyKey string
InsertMultiple bool
NoInference bool
}

// A query name must be a valid Go identifier
//
// https://golang.org/ref/spec#Identifiers
Expand All @@ -44,7 +50,7 @@ func validateQueryName(name string) error {
return nil
}

func Parse(t string, commentStyle CommentSyntax) (string, string, error) {
func Parse(t string, commentStyle CommentSyntax) (string, string, CmdParams, error) {
for _, line := range strings.Split(t, "\n") {
var prefix string
if strings.HasPrefix(line, "--") {
Expand Down Expand Up @@ -76,30 +82,56 @@ func Parse(t string, commentStyle CommentSyntax) (string, string, error) {
continue
}
if !strings.HasPrefix(rest, " name: ") {
return "", "", fmt.Errorf("invalid metadata: %s", line)
return "", "", CmdParams{}, fmt.Errorf("invalid metadata: %s", line)
}

part := strings.Split(strings.TrimSpace(line), " ")
if prefix == "/*" {
part = part[:len(part)-1] // removes the trailing "*/" element
}
if len(part) == 2 {
return "", "", fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
}
if len(part) != 4 {
return "", "", fmt.Errorf("invalid query comment: %s", line)
if len(part) < 4 {
return "", "", CmdParams{}, fmt.Errorf("missing query type [':one', ':many', ':exec', ':execrows', ':execlastid', ':execresult', ':copyfrom', 'batchexec', 'batchmany', 'batchone']: %s", line)
}
queryName := part[2]
queryType := strings.TrimSpace(part[3])
switch queryType {
case CmdOne, CmdMany, CmdExec, CmdExecResult, CmdExecRows, CmdExecLastId, CmdCopyFrom, CmdBatchExec, CmdBatchMany, CmdBatchOne:
default:
return "", "", fmt.Errorf("invalid query type: %s", queryType)
return "", "", CmdParams{}, fmt.Errorf("invalid query type: %s", queryType)
}
if err := validateQueryName(queryName); err != nil {
return "", "", err
return "", "", CmdParams{}, err
}
cmdParams, err := parseCmdParams(part[4:], queryType)
if err != nil {
return "", "", CmdParams{}, err
}
return queryName, queryType, cmdParams, nil
}
return "", "", CmdParams{}, nil
}

func parseCmdParams(part []string, queryType string) (CmdParams, error) {
var ret CmdParams
for _, p := range part {
if p == "multiple" {
if queryType != CmdExec && queryType != CmdExecResult && queryType != CmdExecRows && queryType != CmdExecLastId {
return ret, fmt.Errorf("query command parameter multiple is invalid for query type %s", queryType)
}
ret.InsertMultiple = true
} else if p == "no-inference" {
if queryType != CmdExec && queryType != CmdExecResult && queryType != CmdExecRows && queryType != CmdExecLastId {
return ret, fmt.Errorf("query command parameter no-inference is invalid for query type %s", queryType)
}
ret.NoInference = true
} else if strings.HasPrefix(p, "key=") {
if queryType != CmdMany {
return ret, fmt.Errorf("query command parameter %s is invalid for query type %s", p, queryType)
}
ret.ManyKey = strings.TrimPrefix(p, "key=")
} else {
return ret, fmt.Errorf("invalid query command parameter %q", p)
}
return queryName, queryType, nil
}
return "", "", nil
return ret, nil
}
57 changes: 44 additions & 13 deletions internal/metadata/meta_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package metadata

import "testing"

func TestParseMetadata(t *testing.T) {
func TestNonMetadata(t *testing.T) {

for _, query := range []string{
`-- name: CreateFoo, :one`,
Expand All @@ -17,7 +17,7 @@ func TestParseMetadata(t *testing.T) {
"-- name:CreateFoo",
`--name:CreateFoo :two`,
} {
if _, _, err := Parse(query, CommentSyntax{Dash: true}); err == nil {
if _, _, _, err := Parse(query, CommentSyntax{Dash: true}); err == nil {
t.Errorf("expected invalid metadata: %q", query)
}
}
Expand All @@ -27,21 +27,52 @@ func TestParseMetadata(t *testing.T) {
`-- name comment`,
`--name comment`,
} {
if _, _, err := Parse(query, CommentSyntax{Dash: true}); err != nil {
if _, _, _, err := Parse(query, CommentSyntax{Dash: true}); err != nil {
t.Errorf("expected valid comment: %q", query)
}
}
}

query := `-- name: CreateFoo :one`
queryName, queryType, err := Parse(query, CommentSyntax{Dash: true})
if err != nil {
t.Errorf("expected valid metadata: %q", query)
}
if queryName != "CreateFoo" {
t.Errorf("incorrect queryName parsed: %q", query)
func TestParse(t *testing.T) {
tests := []struct {
query string
wantName string
wantType string
wantCmdParams CmdParams
}{
{
query: "-- name: CreateFoo :one",
wantName: "CreateFoo",
wantType: CmdOne,
},
{
query: "-- name: InsertMulti :exec multiple",
wantName: "InsertMulti",
wantType: CmdExec,
wantCmdParams: CmdParams{InsertMultiple: true},
},
{
query: "-- name: SelectKey :many key=group_id",
wantName: "SelectKey",
wantType: CmdMany,
wantCmdParams: CmdParams{ManyKey: "group_id"},
},
}
if queryType != CmdOne {
t.Errorf("incorrect queryType parsed: %q", query)
for _, tc := range tests {
t.Run(tc.query, func(t *testing.T) {
name, queryType, cmdParams, err := Parse(tc.query, CommentSyntax{Dash: true})
if err != nil {
t.Fatalf("Parse failed: %v", err)
}
if name != tc.wantName {
t.Errorf("unexpected name: got %q; want %q", name, tc.wantName)
}
if queryType != tc.wantType {
t.Errorf("unexpected queryType: got %q; want %q", queryType, tc.wantType)
}
if cmdParams != tc.wantCmdParams {
t.Errorf("unexpected cmdParams: got %#v; want %#v", cmdParams, tc.wantCmdParams)
}
})
}

}
Loading