Skip to content

Commit 77aadba

Browse files
authored
feat: Support "LIMIT ?" in UPDATE and DELETE for MySQL (#2365)
PostgreSQL doesn't support UPDATE-LIMIT issue #2131
1 parent c672ba6 commit 77aadba

File tree

10 files changed

+115
-2
lines changed

10 files changed

+115
-2
lines changed

internal/compiler/find_params.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
6969
case *ast.CallStmt:
7070
p.parent = n.FuncCall
7171

72+
case *ast.DeleteStmt:
73+
if n.LimitCount != nil {
74+
p.limitCount = n.LimitCount
75+
}
76+
7277
case *ast.FuncCall:
7378
p.parent = node
7479

@@ -129,6 +134,9 @@ func (p paramSearch) Visit(node ast.Node) astutils.Visitor {
129134
}
130135
p.seen[ref.Location] = struct{}{}
131136
}
137+
if n.LimitCount != nil {
138+
p.limitCount = n.LimitCount
139+
}
132140

133141
case *ast.RangeVar:
134142
p.rangeVar = n

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

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

internal/endtoend/testdata/limit/mysql/go/models.go

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

internal/endtoend/testdata/limit/mysql/go/query.sql.go

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE foo (bar bool not null);
2+
3+
-- name: LimitMe :exec
4+
UPDATE foo SET bar='baz' LIMIT ?;
5+
6+
-- name: LimitMeToo :exec
7+
DELETE FROM foo LIMIT ?;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "mysql",
7+
"name": "querytest",
8+
"schema": "query.sql",
9+
"queries": "query.sql"
10+
}
11+
]
12+
}

internal/engine/dolphin/convert.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,12 +327,16 @@ func (c *cc) convertDeleteStmt(n *pcast.DeleteStmt) *ast.DeleteStmt {
327327
relations := &ast.List{}
328328
convertToRangeVarList(rels, relations)
329329

330-
return &ast.DeleteStmt{
330+
stmt := &ast.DeleteStmt{
331331
Relations: relations,
332332
WhereClause: c.convert(n.Where),
333333
ReturningList: &ast.List{},
334334
WithClause: c.convertWithClause(n.With),
335335
}
336+
if n.Limit != nil {
337+
stmt.LimitCount = c.convert(n.Limit.Count)
338+
}
339+
return stmt
336340
}
337341

338342
func (c *cc) convertDropTableStmt(n *pcast.DropTableStmt) ast.Node {
@@ -574,14 +578,18 @@ func (c *cc) convertUpdateStmt(n *pcast.UpdateStmt) *ast.UpdateStmt {
574578
for _, a := range n.List {
575579
list.Items = append(list.Items, c.convertAssignment(a))
576580
}
577-
return &ast.UpdateStmt{
581+
stmt := &ast.UpdateStmt{
578582
Relations: relations,
579583
TargetList: list,
580584
WhereClause: c.convert(n.Where),
581585
FromClause: &ast.List{},
582586
ReturningList: &ast.List{},
583587
WithClause: c.convertWithClause(n.With),
584588
}
589+
if n.Limit != nil {
590+
stmt.LimitCount = c.convert(n.Limit.Count)
591+
}
592+
return stmt
585593
}
586594

587595
func (c *cc) convertValueExpr(n *driver.ValueExpr) *ast.A_Const {

internal/sql/ast/delete_stmt.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ type DeleteStmt struct {
44
Relations *List
55
UsingClause *List
66
WhereClause Node
7+
LimitCount Node
78
ReturningList *List
89
WithClause *WithClause
910
}

internal/sql/ast/update_stmt.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type UpdateStmt struct {
55
TargetList *List
66
WhereClause Node
77
FromClause *List
8+
LimitCount Node
89
ReturningList *List
910
WithClause *WithClause
1011
}

internal/sql/astutils/walk.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,9 @@ func Walk(f Visitor, node ast.Node) {
10681068
if n.WhereClause != nil {
10691069
Walk(f, n.WhereClause)
10701070
}
1071+
if n.LimitCount != nil {
1072+
Walk(f, n.LimitCount)
1073+
}
10711074
if n.ReturningList != nil {
10721075
Walk(f, n.ReturningList)
10731076
}
@@ -2038,6 +2041,9 @@ func Walk(f Visitor, node ast.Node) {
20382041
if n.FromClause != nil {
20392042
Walk(f, n.FromClause)
20402043
}
2044+
if n.LimitCount != nil {
2045+
Walk(f, n.LimitCount)
2046+
}
20412047
if n.ReturningList != nil {
20422048
Walk(f, n.ReturningList)
20432049
}

0 commit comments

Comments
 (0)