Skip to content

fix: update panic #1590 #2154

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 2 commits into from
Apr 7, 2023
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
31 changes: 31 additions & 0 deletions internal/endtoend/testdata/update_two_table/mysql/go/db.go

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

23 changes: 23 additions & 0 deletions internal/endtoend/testdata/update_two_table/mysql/go/models.go

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

27 changes: 27 additions & 0 deletions internal/endtoend/testdata/update_two_table/mysql/go/query.sql.go

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

25 changes: 25 additions & 0 deletions internal/endtoend/testdata/update_two_table/mysql/query.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
-- https://github.com/kyleconroy/sqlc/issues/1590
CREATE TABLE authors (
name text NOT NULL,
deleted_at datetime NOT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
);

CREATE TABLE books (
is_amazing tinyint(1) NOT NULL,
deleted_at datetime NOT NULL,
created_at datetime NOT NULL,
updated_at datetime NOT NULL
);

-- name: DeleteAuthor :exec
UPDATE
authors,
books
SET
authors.deleted_at = now(),
books.deleted_at = now()
WHERE
books.is_amazing = 1
AND authors.name = sqlc.arg(name);
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/update_two_table/mysql/sqlc.json
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"
}
]
}
23 changes: 1 addition & 22 deletions internal/engine/dolphin/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -565,28 +565,7 @@ func (c *cc) convertUpdateStmt(n *pcast.UpdateStmt) *ast.UpdateStmt {
}

relations := &ast.List{}
switch rel := rels.Items[0].(type) {

// Special case for joins in updates
case *ast.JoinExpr:
left, ok := rel.Larg.(*ast.RangeVar)
if !ok {
panic("expected range var")
}
relations.Items = append(relations.Items, left)

right, ok := rel.Rarg.(*ast.RangeVar)
if !ok {
panic("expected range var")
}
relations.Items = append(relations.Items, right)

case *ast.RangeVar:
relations.Items = append(relations.Items, rel)

default:
panic("expected range var")
}
convertToRangeVarList(rels, relations)

// TargetList
list := &ast.List{}
Expand Down
40 changes: 40 additions & 0 deletions internal/engine/dolphin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,43 @@ func isNotNull(n *pcast.ColumnDef) bool {
}
return false
}

func convertToRangeVarList(list *ast.List, result *ast.List) {
if len(list.Items) == 0 {
return
}
switch rel := list.Items[0].(type) {

// Special case for joins in updates
case *ast.JoinExpr:
left, ok := rel.Larg.(*ast.RangeVar)
if !ok {
if list, check := rel.Larg.(*ast.List); check {
convertToRangeVarList(list, result)
} else {
panic("expected range var")
}
}
if left != nil {
result.Items = append(result.Items, left)
}

right, ok := rel.Rarg.(*ast.RangeVar)
if !ok {
if list, check := rel.Rarg.(*ast.List); check {
convertToRangeVarList(list, result)
} else {
panic("expected range var")
}
}
if right != nil {
result.Items = append(result.Items, right)
}

case *ast.RangeVar:
result.Items = append(result.Items, rel)

default:
panic("expected range var")
}
}