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

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

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- name: SetBoth :exec
UPDATE t SET (name, score) = (?, ?) WHERE id = ?;

-- name: SetBothReversed :exec
UPDATE t SET (score, name) = (?, ?) WHERE id = ?;

-- name: SetBothAndSingle :exec
UPDATE t SET (name, score) = (?, ?), created = ? WHERE id = ?;

-- name: UpsertSetBoth :exec
INSERT INTO t (id, name, score) VALUES (?, ?, ?)
ON CONFLICT (id) DO UPDATE SET (name, score) = (?, ?);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CREATE TABLE t (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
score INTEGER NOT NULL,
created TEXT
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": "1",
"packages": [
{
"engine": "sqlite",
"path": "go",
"name": "querytest",
"schema": "schema.sql",
"queries": "query.sql"
}
]
}
39 changes: 35 additions & 4 deletions internal/engine/sqlite/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -502,13 +502,24 @@ func (c *cc) convertColumnNames(cols []*meyer.Ident) *ast.List {
}

// convertSetPairs converts the assignment list of an UPDATE or of an upsert's
// DO UPDATE. SQLite allows "(a, b) = expr"; sqlc has no node for a
// multi-column assignment, so each name is given the same value expression.
// DO UPDATE. SQLite allows "(a, b) = (x, y)", which assigns each column the
// element of the row value at its position, so it becomes "a = x, b = y".
// sqlc has no node for a multi-column assignment from a single expression,
// such as "(a, b) = (SELECT ...)", so there each name is given the whole
// value expression.
func (c *cc) convertSetPairs(pairs []*meyer.SetPair) *ast.List {
list := &ast.List{}
for _, pair := range pairs {
val := c.convertExpr(pair.Value)
for _, col := range pair.Columns {
elems := rowElements(pair.Value, len(pair.Columns))
var whole ast.Node
if elems == nil {
whole = c.convertExpr(pair.Value)
}
for i, col := range pair.Columns {
val := whole
if elems != nil {
val = c.convertExpr(elems[i])
}
name := identifier(col)
list.Items = append(list.Items, &ast.ResTarget{
Name: &name,
Expand All @@ -520,6 +531,26 @@ func (c *cc) convertSetPairs(pairs []*meyer.SetPair) *ast.List {
return list
}

// rowElements returns the elements of a row value with exactly n elements,
// looking through redundant parentheses, or nil if e is not one.
func rowElements(e meyer.Expr, n int) []meyer.Expr {
if n < 2 {
return nil
}
for {
p, ok := e.(*meyer.ParenExpr)
if !ok {
break
}
e = p.X
}
v, ok := e.(*meyer.VectorExpr)
if !ok || len(v.List) != n {
return nil
}
return v.List
}

func (c *cc) convertUpdateStmt(n *meyer.UpdateStmt) ast.Node {
return &ast.UpdateStmt{
Relations: &ast.List{Items: []ast.Node{parseRangeVar(n.Table, n.Alias)}},
Expand Down
Loading