Skip to content
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
18 changes: 18 additions & 0 deletions internal/cmd/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ func pluginQueries(r *compiler.Result) []*plugin.Query {
Name: q.InsertIntoTable.Name,
}
}
var ut *plugin.Identifier
if q.UpdateTable != nil {
ut = &plugin.Identifier{
Catalog: q.UpdateTable.Catalog,
Schema: q.UpdateTable.Schema,
Name: q.UpdateTable.Name,
}
}
var dft *plugin.Identifier
if q.DeleteFromTable != nil {
dft = &plugin.Identifier{
Catalog: q.DeleteFromTable.Catalog,
Schema: q.DeleteFromTable.Schema,
Name: q.DeleteFromTable.Name,
}
}
out = append(out, &plugin.Query{
Name: q.Metadata.Name,
Cmd: q.Metadata.Cmd,
Expand All @@ -161,6 +177,8 @@ func pluginQueries(r *compiler.Result) []*plugin.Query {
Params: params,
Filename: q.Metadata.Filename,
InsertIntoTable: iit,
UpdateTable: ut,
DeleteFromTable: dft,
})
}
return out
Expand Down
12 changes: 12 additions & 0 deletions internal/compiler/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,18 @@ func (c *Compiler) _analyzeQuery(raw *ast.RawStmt, query string, failfast bool)
if err := check(err); err != nil {
return nil, err
}
case *ast.UpdateStmt:
if n.Relations != nil && len(n.Relations.Items) > 0 {
if rv, ok := n.Relations.Items[0].(*ast.RangeVar); ok {
table, _ = ParseTableName(rv)
}
}
case *ast.DeleteStmt:
if n.Relations != nil && len(n.Relations.Items) > 0 {
if rv, ok := n.Relations.Items[0].(*ast.RangeVar); ok {
table, _ = ParseTableName(rv)
}
}
}

if err := check(validate.FuncCall(c.catalog, c.combo, raw)); err != nil {
Expand Down
43 changes: 32 additions & 11 deletions internal/compiler/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,23 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,
})
}

// Determine the insert table if applicable
// Determine the target table if applicable
var table *ast.TableName
if insert, ok := expandedRaw.Stmt.(*ast.InsertStmt); ok {
table, _ = ParseTableName(insert.Relation)
switch n := expandedRaw.Stmt.(type) {
case *ast.InsertStmt:
table, _ = ParseTableName(n.Relation)
case *ast.UpdateStmt:
if n.Relations != nil && len(n.Relations.Items) > 0 {
if rv, ok := n.Relations.Items[0].(*ast.RangeVar); ok {
table, _ = ParseTableName(rv)
}
}
case *ast.DeleteStmt:
if n.Relations != nil && len(n.Relations.Items) > 0 {
if rv, ok := n.Relations.Items[0].(*ast.RangeVar); ok {
table, _ = ParseTableName(rv)
}
}
}

anlys = &analysis{
Expand Down Expand Up @@ -171,14 +184,22 @@ func (c *Compiler) parseQuery(stmt ast.Node, src string, o opts.Parser) (*Query,

md.Comments = comments

return &Query{
RawStmt: raw,
Metadata: md,
Params: anlys.Parameters,
Columns: anlys.Columns,
SQL: trimmed,
InsertIntoTable: anlys.Table,
}, nil
q := &Query{
RawStmt: raw,
Metadata: md,
Params: anlys.Parameters,
Columns: anlys.Columns,
SQL: trimmed,
}
switch raw.Stmt.(type) {
case *ast.InsertStmt:
q.InsertIntoTable = anlys.Table
case *ast.UpdateStmt:
q.UpdateTable = anlys.Table
case *ast.DeleteStmt:
q.DeleteFromTable = anlys.Table
}
return q, nil
}

func rangeVars(root ast.Node) []*ast.RangeVar {
Expand Down
6 changes: 6 additions & 0 deletions internal/compiler/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ type Query struct {
// Needed for CopyFrom
InsertIntoTable *ast.TableName

// Target table for UPDATE queries
UpdateTable *ast.TableName

// Target table for DELETE queries
DeleteFromTable *ast.TableName

// Needed for vet
RawStmt *ast.RawStmt
}
Expand Down
95 changes: 91 additions & 4 deletions internal/endtoend/testdata/codegen_json/gen/codegen.json
Original file line number Diff line number Diff line change
Expand Up @@ -65079,7 +65079,9 @@
],
"comments": [],
"filename": "query.sql",
"insert_into_table": null
"insert_into_table": null,
"update_table": null,
"delete_from_table": null
},
{
"text": "SELECT id, name, bio FROM authors\nORDER BY name",
Expand Down Expand Up @@ -65168,7 +65170,9 @@
"params": [],
"comments": [],
"filename": "query.sql",
"insert_into_table": null
"insert_into_table": null,
"update_table": null,
"delete_from_table": null
},
{
"text": "INSERT INTO authors (\n name, bio\n) VALUES (\n $1, $2\n)\nRETURNING id, name, bio",
Expand Down Expand Up @@ -65320,7 +65324,84 @@
"catalog": "",
"schema": "",
"name": "authors"
}
},
"update_table": null,
"delete_from_table": null
},
{
"text": "UPDATE authors SET bio = $1\nWHERE id = $2",
"name": "UpdateAuthorBio",
"cmd": ":exec",
"columns": [],
"params": [
{
"number": 1,
"column": {
"name": "bio",
"not_null": false,
"is_array": false,
"comment": "",
"length": -1,
"is_named_param": false,
"is_func_call": false,
"scope": "",
"table": {
"catalog": "",
"schema": "public",
"name": "authors"
},
"table_alias": "",
"type": {
"catalog": "",
"schema": "",
"name": "text"
},
"is_sqlc_slice": false,
"embed_table": null,
"original_name": "bio",
"unsigned": false,
"array_dims": 0
}
},
{
"number": 2,
"column": {
"name": "id",
"not_null": true,
"is_array": false,
"comment": "",
"length": -1,
"is_named_param": false,
"is_func_call": false,
"scope": "",
"table": {
"catalog": "",
"schema": "",
"name": "authors"
},
"table_alias": "",
"type": {
"catalog": "",
"schema": "",
"name": "bigserial"
},
"is_sqlc_slice": false,
"embed_table": null,
"original_name": "id",
"unsigned": false,
"array_dims": 0
}
}
],
"comments": [],
"filename": "query.sql",
"insert_into_table": null,
"update_table": {
"catalog": "",
"schema": "",
"name": "authors"
},
"delete_from_table": null
},
{
"text": "DELETE FROM authors\nWHERE id = $1",
Expand Down Expand Up @@ -65360,7 +65441,13 @@
],
"comments": [],
"filename": "query.sql",
"insert_into_table": null
"insert_into_table": null,
"update_table": null,
"delete_from_table": {
"catalog": "",
"schema": "",
"name": "authors"
}
}
],
"sqlc_version": "v1.30.0",
Expand Down
4 changes: 4 additions & 0 deletions internal/endtoend/testdata/codegen_json/postgresql/query.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ INSERT INTO authors (
)
RETURNING *;

-- name: UpdateAuthorBio :exec
UPDATE authors SET bio = $1
WHERE id = $2;

-- name: DeleteAuthor :exec
DELETE FROM authors
WHERE id = $1;
Original file line number Diff line number Diff line change
Expand Up @@ -65081,7 +65081,9 @@
],
"comments": [],
"filename": "query.sql",
"insert_into_table": null
"insert_into_table": null,
"update_table": null,
"delete_from_table": null
},
{
"text": "SELECT id, name, bio FROM authors\nORDER BY name",
Expand Down Expand Up @@ -65170,7 +65172,9 @@
"params": [],
"comments": [],
"filename": "query.sql",
"insert_into_table": null
"insert_into_table": null,
"update_table": null,
"delete_from_table": null
},
{
"text": "INSERT INTO authors (\n name, bio\n) VALUES (\n $1, $2\n)\nRETURNING id, name, bio",
Expand Down Expand Up @@ -65322,7 +65326,9 @@
"catalog": "",
"schema": "",
"name": "authors"
}
},
"update_table": null,
"delete_from_table": null
},
{
"text": "DELETE FROM authors\nWHERE id = $1",
Expand Down Expand Up @@ -65362,7 +65368,13 @@
],
"comments": [],
"filename": "query.sql",
"insert_into_table": null
"insert_into_table": null,
"update_table": null,
"delete_from_table": {
"catalog": "",
"schema": "",
"name": "authors"
}
}
],
"sqlc_version": "v1.30.0",
Expand Down
Loading
Loading