Skip to content

Commit

Permalink
feat(mariadb): support RETURNING clause in DELETE statement
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Nov 13, 2024
1 parent b10bd39 commit b8dec9d
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 5 deletions.
1 change: 1 addition & 0 deletions dialect/feature/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ const (
CompositeIn // ... WHERE (A,B) IN ((N, NN), (N, NN)...)
UpdateOrderLimit // UPDATE ... ORDER BY ... LIMIT ...
DeleteOrderLimit // DELETE ... ORDER BY ... LIMIT ...
DeleteReturning
)
3 changes: 3 additions & 0 deletions dialect/mysqldialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ func (d *Dialect) Init(db *sql.DB) {

if strings.Contains(version, "MariaDB") {
version = semver.MajorMinor("v" + cleanupVersion(version))
if semver.Compare(version, "v10.0.5") >= 0 {
d.features |= feature.DeleteReturning
}
if semver.Compare(version, "v10.5.0") >= 0 {
d.features |= feature.InsertReturning
}
Expand Down
3 changes: 2 additions & 1 deletion dialect/oracledialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func New() *Dialect {
feature.TableNotExists |
feature.SelectExists |
feature.AutoIncrement |
feature.CompositeIn
feature.CompositeIn |
feature.DeleteReturning
return d
}

Expand Down
3 changes: 2 additions & 1 deletion dialect/pgdialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ func New() *Dialect {
feature.InsertOnConflict |
feature.SelectExists |
feature.GeneratedIdentity |
feature.CompositeIn
feature.CompositeIn |
feature.DeleteReturning
return d
}

Expand Down
3 changes: 2 additions & 1 deletion dialect/sqlitedialect/dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func New() *Dialect {
feature.TableNotExists |
feature.SelectExists |
feature.AutoIncrement |
feature.CompositeIn
feature.CompositeIn |
feature.DeleteReturning
return d
}

Expand Down
6 changes: 6 additions & 0 deletions internal/dbtest/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,12 @@ func TestQuery(t *testing.T) {
return db.NewUpdate().Model(new(Story)).Set("name = ?", "new-name").WherePK().Order("id").Limit(1)
},
},
{
id: 172,
query: func(db *bun.DB) schema.QueryAppender {
return db.NewDelete().Model(&Model{}).WherePK().Returning("*")
},
},
}

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-172
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM `models` WHERE (`id` = NULL) RETURNING *
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mssql2019-172
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: returning is not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql5-172
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: returning is not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-mysql8-172
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bun: returning is not supported for current dialect
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pg-172
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM "models" AS "model" WHERE ("model"."id" = NULL) RETURNING *
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-pgx-172
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM "models" AS "model" WHERE ("model"."id" = NULL) RETURNING *
1 change: 1 addition & 0 deletions internal/dbtest/testdata/snapshots/TestQuery-sqlite-172
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DELETE FROM "models" AS "model" WHERE ("model"."id" = NULL) RETURNING *
9 changes: 7 additions & 2 deletions query_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ func (q *DeleteQuery) Limit(n int) *DeleteQuery {
//
// To suppress the auto-generated RETURNING clause, use `Returning("NULL")`.
func (q *DeleteQuery) Returning(query string, args ...interface{}) *DeleteQuery {
if !q.hasFeature(feature.DeleteReturning) {
q.err = errors.New("bun: returning is not supported for current dialect")
return q
}

q.addReturning(schema.SafeQuery(query, args))
return q
}
Expand Down Expand Up @@ -249,7 +254,7 @@ func (q *DeleteQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, e
return nil, err
}

if q.hasFeature(feature.Returning) && q.hasReturning() {
if q.hasFeature(feature.DeleteReturning) && q.hasReturning() {
b = append(b, " RETURNING "...)
b, err = q.appendReturning(fmter, b)
if err != nil {
Expand Down Expand Up @@ -311,7 +316,7 @@ func (q *DeleteQuery) scanOrExec(
return nil, err
}

useScan := hasDest || (q.hasReturning() && q.hasFeature(feature.Returning|feature.Output))
useScan := hasDest || (q.hasReturning() && q.hasFeature(feature.DeleteReturning|feature.Output))
var model Model

if useScan {
Expand Down

0 comments on commit b8dec9d

Please sign in to comment.