Skip to content

Allow for DDL and unsupported statements as a query #2377

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

Closed
wants to merge 3 commits into from
Closed
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
46 changes: 31 additions & 15 deletions internal/compiler/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,43 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
if o.Debug.DumpAST {
debug.Dump(stmt)
}
raw, ok := stmt.(*ast.RawStmt)
if !ok {
return nil, errors.New("node is not a statement")
}
rawSQL, err := source.Pluck(src, raw.StmtLocation, raw.StmtLen)
if err != nil {
return nil, err
}
if rawSQL == "" {
return nil, errors.New("missing semicolon at end of file")
}
name, cmd, cmdParams, err := metadata.Parse(strings.TrimSpace(rawSQL), c.parser.CommentSyntax())
if err != nil {
return nil, err
}
if cmdParams.NoInference {
trimmed, comments, err := source.StripComments(rawSQL)
if err != nil {
return nil, err
}
return &Query{
Cmd: cmd,
Comments: comments,
Name: name,
Params: nil,
Columns: nil,
SQL: trimmed,
CmdParams: cmdParams,
}, nil
}
if err := validate.ParamStyle(stmt); err != nil {
return nil, err
}
numbers, dollar, err := validate.ParamRef(stmt)
if err != nil {
return nil, err
}
raw, ok := stmt.(*ast.RawStmt)
if !ok {
return nil, errors.New("node is not a statement")
}
var table *ast.TableName
switch n := raw.Stmt.(type) {
case *ast.CallStmt:
Expand All @@ -57,23 +83,12 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
return nil, ErrUnsupportedStatementType
}

rawSQL, err := source.Pluck(src, raw.StmtLocation, raw.StmtLen)
if err != nil {
return nil, err
}
if rawSQL == "" {
return nil, errors.New("missing semicolon at end of file")
}
if err := validate.FuncCall(c.catalog, c.combo, raw); err != nil {
return nil, err
}
if err := validate.In(c.catalog, raw); err != nil {
return nil, err
}
name, cmd, err := metadata.Parse(strings.TrimSpace(rawSQL), c.parser.CommentSyntax())
if err != nil {
return nil, err
}
raw, namedParams, edits := rewrite.NamedParameters(c.conf.Engine, raw, numbers, dollar)
if err := validate.Cmd(raw.Stmt, name, cmd); err != nil {
return nil, err
Expand Down Expand Up @@ -133,6 +148,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
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/invalid_params/pgx/v4/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE TABLE bar (id serial not null);
-- name: baz :one
SELECT foo FROM bar WHERE baz = $4;

-- name: bar
-- name: bar :one
SELECT foo FROM bar WHERE baz = $1 AND baz = $3;

-- name: foo :one
Expand Down
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/invalid_params/pgx/v5/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE TABLE bar (id serial not null);
-- name: baz :one
SELECT foo FROM bar WHERE baz = $4;

-- name: bar
-- name: bar :one
SELECT foo FROM bar WHERE baz = $1 AND baz = $3;

-- name: foo :one
Expand Down
2 changes: 1 addition & 1 deletion internal/endtoend/testdata/invalid_params/stdlib/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE TABLE bar (id serial not null);
-- name: baz :one
SELECT foo FROM bar WHERE baz = $4;

-- name: bar
-- name: bar :one
SELECT foo FROM bar WHERE baz = $1 AND baz = $3;

-- name: foo :one
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
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/no_inference/postgresql/go/db.go

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

11 changes: 11 additions & 0 deletions internal/endtoend/testdata/no_inference/postgresql/go/models.go

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

19 changes: 19 additions & 0 deletions internal/endtoend/testdata/no_inference/postgresql/go/query.sql.go

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

2 changes: 2 additions & 0 deletions internal/endtoend/testdata/no_inference/postgresql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- name: CreateTempTable :exec no-inference
CREATE TEMPORARY TABLE bar (LIKE foo);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE foo (val text not null);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/no_inference/postgresql/sqlc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "postgresql",
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
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
Loading