Skip to content

Commit 5eb649d

Browse files
authored
fix(engine/mysql): Add support for MySQL ON DUPLICATE KEY UPDATE (#1286)
* fix(engine/mysql): Add support for MySQL ON DUPLICATE KEY UPDATE * Remove unnecessary action
1 parent 734b4b3 commit 5eb649d

File tree

11 files changed

+196
-0
lines changed

11 files changed

+196
-0
lines changed

internal/endtoend/testdata/on_duplicate_key_update/mysql/db/db.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/on_duplicate_key_update/mysql/db/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/on_duplicate_key_update/mysql/db/query.sql.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- https://github.com/kyleconroy/sqlc/issues/921
2+
CREATE TABLE authors (
3+
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
4+
name text NOT NULL,
5+
bio text,
6+
UNIQUE(name)
7+
);
8+
9+
-- name: UpsertAuthor :exec
10+
INSERT INTO authors (name, bio)
11+
VALUES (?, ?)
12+
ON DUPLICATE KEY
13+
UPDATE bio = ?;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version":"1",
3+
"packages":[
4+
{
5+
"path":"db",
6+
"engine":"mysql",
7+
"schema":"query.sql",
8+
"queries":"query.sql"
9+
}
10+
]
11+
}

internal/endtoend/testdata/on_duplicate_key_update/postgresql/db/db.go

Lines changed: 29 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/on_duplicate_key_update/postgresql/db/models.go

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/on_duplicate_key_update/postgresql/db/query.sql.go

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- https://github.com/kyleconroy/sqlc/issues/921
2+
CREATE TABLE authors (
3+
id BIGSERIAL PRIMARY KEY,
4+
name text NOT NULL UNIQUE,
5+
bio text
6+
);
7+
8+
-- name: UpsertAuthor :exec
9+
INSERT INTO authors (name, bio)
10+
VALUES ($1, $2)
11+
ON CONFLICT (name) DO UPDATE
12+
SET bio = $2;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"version":"1",
3+
"packages":[
4+
{
5+
"path":"db",
6+
"engine":"postgresql",
7+
"schema":"query.sql",
8+
"queries":"query.sql"
9+
}
10+
]
11+
}

internal/engine/dolphin/convert.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,18 @@ func (c *cc) convertInsertStmt(n *pcast.InsertStmt) *ast.InsertStmt {
407407
ValuesLists: c.convertLists(n.Lists),
408408
}
409409
}
410+
411+
if n.OnDuplicate != nil {
412+
targetList := &ast.List{}
413+
for _, a := range n.OnDuplicate {
414+
targetList.Items = append(targetList.Items, c.convertAssignment(a))
415+
}
416+
insert.OnConflictClause = &ast.OnConflictClause{
417+
TargetList: targetList,
418+
Location: n.OriginTextPosition(),
419+
}
420+
}
421+
410422
return insert
411423
}
412424

0 commit comments

Comments
 (0)