Skip to content

Commit

Permalink
feat(mysql): support for UPDATE ... ORDER BY ... LIMIT ...
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Nov 4, 2024
1 parent c1541c2 commit cfed0ca
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1578,6 +1578,13 @@ func TestQuery(t *testing.T) {
Where("src.id = story.id").Order("src.id").Limit(1)
},
},
{
id: 171,
query: func(db *bun.DB) schema.QueryAppender {
// UPDATE ... SET ... ORDER BY ... LIMIT ... (MySQL, MariaDB)
return db.NewUpdate().Model(new(Story)).Set("name = ?", "new-name").WherePK().Order("id").Limit(1)
},
},
}

timeRE := regexp.MustCompile(`'2\d{3}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(\.\d+)?(\+\d{2}:\d{2})?'`)
Expand Down
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mariadb-171
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `stories` AS `story` SET name = 'new-name' WHERE (`story`.`id` = NULL) ORDER BY `id` LIMIT 1
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-171
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: limit is not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-171
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
UPDATE `stories` AS `story` SET name = 'new-name' WHERE (`story`.`id` = NULL) ORDER BY `id` LIMIT 1
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-171
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: limit is not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-171
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: limit is not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-171
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: limit is not supported for current dialect
36 changes: 36 additions & 0 deletions query_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (

type UpdateQuery struct {
whereBaseQuery
orderLimitOffsetQuery
returningQuery
customValueQuery
setQuery
Expand Down Expand Up @@ -200,6 +201,31 @@ func (q *UpdateQuery) WhereAllWithDeleted() *UpdateQuery {
return q
}

// ------------------------------------------------------------------------------
func (q *UpdateQuery) Order(orders ...string) *UpdateQuery {
if q.Dialect().Name() != dialect.MySQL {
q.err = errors.New("bun: order is not supported for current dialect")
}
q.addOrder(orders...)
return q
}

func (q *UpdateQuery) OrderExpr(query string, args ...interface{}) *UpdateQuery {
if q.Dialect().Name() != dialect.MySQL {
q.err = errors.New("bun: order is not supported for current dialect")
}
q.addOrderExpr(query, args...)
return q
}

func (q *UpdateQuery) Limit(n int) *UpdateQuery {
if q.Dialect().Name() != dialect.MySQL {
q.err = errors.New("bun: limit is not supported for current dialect")
}
q.addLimit(n)
return q
}

//------------------------------------------------------------------------------

// Returning adds a RETURNING clause to the query.
Expand Down Expand Up @@ -278,6 +304,16 @@ func (q *UpdateQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e
return nil, err
}

b, err = q.appendOrder(fmter, b)
if err != nil {
return nil, err
}

b, err = q.appendLimitOffset(fmter, b)
if err != nil {
return nil, err
}

if q.hasFeature(feature.Returning) && q.hasReturning() {
b = append(b, " RETURNING "...)
b, err = q.appendReturning(fmter, b)
Expand Down

0 comments on commit cfed0ca

Please sign in to comment.