Skip to content

Commit

Permalink
Fix bug with ON DUPLICATE KEY UPDATE
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Mar 7, 2023
1 parent 6977b63 commit 4e3f513
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dialect/mysql/dialect/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (i InsertQuery) WriteSQL(w io.Writer, d bob.Dialect, start int) ([]any, err

// Either this or the values will get expressed
updateArgs, err := bob.ExpressSlice(w, d, start+len(args), i.DuplicateKeyUpdate,
"\nON DUPLICATE KEY UPDATE\n", "\n", "")
"\nON DUPLICATE KEY UPDATE\n", ",\n", "")
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions dialect/mysql/im/qm.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ func (s *dupKeyUpdater) Set(alias string, cols ...string) *dupKeyUpdater {
newCols[i] = dialect.Set{Col: c, Val: expr.Quote(alias, c)}
}

s.sets = append(s.sets, newCols...)
return s
}
2 changes: 2 additions & 0 deletions dialect/mysql/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func TestInsert(t *testing.T) {
im.Values(mysql.Arg(9, "Sentry Distribution")),
im.As("new"),
im.OnDuplicateKeyUpdate().
Set("new", "did").
SetCol("dbname", mysql.Concat(
"new.dname", mysql.S(" (formerly "), "d.dname", mysql.S(")"),
)),
Expand All @@ -71,6 +72,7 @@ func TestInsert(t *testing.T) {
VALUES (?, ?), (?, ?)
AS new
ON DUPLICATE KEY UPDATE
` + " `did` = `new`.`did`," + `
` + "`dbname`" + ` = (new.dname || ' (formerly ' || d.dname || ')')`,
ExpectedArgs: []any{8, "Anvil Distribution", 9, "Sentry Distribution"},
},
Expand Down
2 changes: 2 additions & 0 deletions website/docs/query-builder/mysql/examples/insert.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ INSERT INTO distributors (`did`, `dname`)
VALUES (?, ?), (?, ?)
AS new
ON DUPLICATE KEY UPDATE
` + " `did` = `new`.`did`," + `
`dbname` = (new.dname || ' (formerly ' || d.dname || ')')
```

Expand All @@ -149,6 +150,7 @@ mysql.Insert(
im.Values(mysql.Arg(9, "Sentry Distribution")),
im.As("new"),
im.OnDuplicateKeyUpdate().
Set("new", "did").
SetCol("dbname", mysql.Concat(
"new.dname", mysql.S(" (formerly "), "d.dname", mysql.S(")"),
)),
Expand Down

0 comments on commit 4e3f513

Please sign in to comment.