2021-08-25 14:34:33 +01:00
|
|
|
package bun
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2021-09-08 20:05:26 +01:00
|
|
|
"time"
|
2021-08-25 14:34:33 +01:00
|
|
|
|
|
|
|
"github.com/uptrace/bun/dialect/feature"
|
|
|
|
"github.com/uptrace/bun/internal"
|
|
|
|
"github.com/uptrace/bun/schema"
|
|
|
|
)
|
|
|
|
|
|
|
|
type DeleteQuery struct {
|
|
|
|
whereBaseQuery
|
|
|
|
returningQuery
|
|
|
|
}
|
|
|
|
|
2022-03-07 10:08:26 +00:00
|
|
|
var _ Query = (*DeleteQuery)(nil)
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func NewDeleteQuery(db *DB) *DeleteQuery {
|
|
|
|
q := &DeleteQuery{
|
|
|
|
whereBaseQuery: whereBaseQuery{
|
|
|
|
baseQuery: baseQuery{
|
|
|
|
db: db,
|
|
|
|
conn: db.DB,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) Conn(db IConn) *DeleteQuery {
|
|
|
|
q.setConn(db)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) Model(model interface{}) *DeleteQuery {
|
2022-11-28 10:19:39 +00:00
|
|
|
q.setModel(model)
|
2021-08-25 14:34:33 +01:00
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply calls the fn passing the DeleteQuery as an argument.
|
|
|
|
func (q *DeleteQuery) Apply(fn func(*DeleteQuery) *DeleteQuery) *DeleteQuery {
|
|
|
|
return fn(q)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) With(name string, query schema.QueryAppender) *DeleteQuery {
|
2022-11-28 10:19:39 +00:00
|
|
|
q.addWith(name, query, false)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) WithRecursive(name string, query schema.QueryAppender) *DeleteQuery {
|
|
|
|
q.addWith(name, query, true)
|
2021-08-25 14:34:33 +01:00
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) Table(tables ...string) *DeleteQuery {
|
|
|
|
for _, table := range tables {
|
|
|
|
q.addTable(schema.UnsafeIdent(table))
|
|
|
|
}
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) TableExpr(query string, args ...interface{}) *DeleteQuery {
|
|
|
|
q.addTable(schema.SafeQuery(query, args))
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) ModelTableExpr(query string, args ...interface{}) *DeleteQuery {
|
2022-03-07 10:08:26 +00:00
|
|
|
q.modelTableName = schema.SafeQuery(query, args)
|
2021-08-25 14:34:33 +01:00
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2021-11-13 11:29:08 +00:00
|
|
|
func (q *DeleteQuery) WherePK(cols ...string) *DeleteQuery {
|
|
|
|
q.addWhereCols(cols)
|
2021-08-25 14:34:33 +01:00
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) Where(query string, args ...interface{}) *DeleteQuery {
|
|
|
|
q.addWhere(schema.SafeQueryWithSep(query, args, " AND "))
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) WhereOr(query string, args ...interface{}) *DeleteQuery {
|
|
|
|
q.addWhere(schema.SafeQueryWithSep(query, args, " OR "))
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) WhereGroup(sep string, fn func(*DeleteQuery) *DeleteQuery) *DeleteQuery {
|
|
|
|
saved := q.where
|
|
|
|
q.where = nil
|
|
|
|
|
|
|
|
q = fn(q)
|
|
|
|
|
|
|
|
where := q.where
|
|
|
|
q.where = saved
|
|
|
|
|
|
|
|
q.addWhereGroup(sep, where)
|
|
|
|
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) WhereDeleted() *DeleteQuery {
|
|
|
|
q.whereDeleted()
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) WhereAllWithDeleted() *DeleteQuery {
|
|
|
|
q.whereAllWithDeleted()
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) ForceDelete() *DeleteQuery {
|
|
|
|
q.flags = q.flags.Set(forceDeleteFlag)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// Returning adds a RETURNING clause to the query.
|
|
|
|
//
|
|
|
|
// To suppress the auto-generated RETURNING clause, use `Returning("NULL")`.
|
|
|
|
func (q *DeleteQuery) Returning(query string, args ...interface{}) *DeleteQuery {
|
|
|
|
q.addReturning(schema.SafeQuery(query, args))
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) hasReturning() bool {
|
|
|
|
if !q.db.features.Has(feature.Returning) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return q.returningQuery.hasReturning()
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2021-09-10 13:42:14 +01:00
|
|
|
func (q *DeleteQuery) Operation() string {
|
|
|
|
return "DELETE"
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
func (q *DeleteQuery) AppendQuery(fmter schema.Formatter, b []byte) (_ []byte, err error) {
|
2022-03-07 10:08:26 +00:00
|
|
|
if q.err != nil {
|
|
|
|
return nil, q.err
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
fmter = formatterWithModel(fmter, q)
|
|
|
|
|
|
|
|
if q.isSoftDelete() {
|
2021-09-08 20:05:26 +01:00
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
if err := q.tableModel.updateSoftDeleteField(now); err != nil {
|
2021-08-25 14:34:33 +01:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-08 20:05:26 +01:00
|
|
|
upd := &UpdateQuery{
|
2021-08-25 14:34:33 +01:00
|
|
|
whereBaseQuery: q.whereBaseQuery,
|
|
|
|
returningQuery: q.returningQuery,
|
|
|
|
}
|
2021-09-08 20:05:26 +01:00
|
|
|
upd.Set(q.softDeleteSet(fmter, now))
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
return upd.AppendQuery(fmter, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
withAlias := q.db.features.Has(feature.DeleteTableAlias)
|
|
|
|
|
|
|
|
b, err = q.appendWith(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b = append(b, "DELETE FROM "...)
|
|
|
|
|
|
|
|
if withAlias {
|
|
|
|
b, err = q.appendFirstTableWithAlias(fmter, b)
|
|
|
|
} else {
|
|
|
|
b, err = q.appendFirstTable(fmter, b)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.hasMultiTables() {
|
|
|
|
b = append(b, " USING "...)
|
|
|
|
b, err = q.appendOtherTables(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = q.mustAppendWhere(fmter, b, withAlias)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-24 11:26:22 +01:00
|
|
|
if q.hasFeature(feature.Returning) && q.hasReturning() {
|
|
|
|
b = append(b, " RETURNING "...)
|
2021-08-25 14:34:33 +01:00
|
|
|
b, err = q.appendReturning(fmter, b)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) isSoftDelete() bool {
|
|
|
|
return q.tableModel != nil && q.table.SoftDeleteField != nil && !q.flags.Has(forceDeleteFlag)
|
|
|
|
}
|
|
|
|
|
2021-09-08 20:05:26 +01:00
|
|
|
func (q *DeleteQuery) softDeleteSet(fmter schema.Formatter, tm time.Time) string {
|
|
|
|
b := make([]byte, 0, 32)
|
|
|
|
if fmter.HasFeature(feature.UpdateMultiTable) {
|
|
|
|
b = append(b, q.table.SQLAlias...)
|
|
|
|
b = append(b, '.')
|
|
|
|
}
|
|
|
|
b = append(b, q.table.SoftDeleteField.SQLName...)
|
|
|
|
b = append(b, " = "...)
|
2021-10-24 12:14:37 +01:00
|
|
|
b = schema.Append(fmter, b, tm)
|
2021-09-08 20:05:26 +01:00
|
|
|
return internal.String(b)
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
func (q *DeleteQuery) Exec(ctx context.Context, dest ...interface{}) (sql.Result, error) {
|
2021-10-24 12:14:37 +01:00
|
|
|
if q.err != nil {
|
|
|
|
return nil, q.err
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
if q.table != nil {
|
|
|
|
if err := q.beforeDeleteHook(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-24 12:14:37 +01:00
|
|
|
if err := q.beforeAppendModel(ctx, q); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:34:33 +01:00
|
|
|
queryBytes, err := q.AppendQuery(q.db.fmter, q.db.makeQueryBytes())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
query := internal.String(queryBytes)
|
|
|
|
|
|
|
|
var res sql.Result
|
|
|
|
|
|
|
|
if hasDest := len(dest) > 0; hasDest || q.hasReturning() {
|
|
|
|
model, err := q.getModel(dest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err = q.scan(ctx, q, query, model, hasDest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res, err = q.exec(ctx, q, query)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.table != nil {
|
|
|
|
if err := q.afterDeleteHook(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) beforeDeleteHook(ctx context.Context) error {
|
|
|
|
if hook, ok := q.table.ZeroIface.(BeforeDeleteHook); ok {
|
|
|
|
if err := hook.BeforeDelete(ctx, q); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) afterDeleteHook(ctx context.Context) error {
|
|
|
|
if hook, ok := q.table.ZeroIface.(AfterDeleteHook); ok {
|
|
|
|
if err := hook.AfterDelete(ctx, q); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-04-24 11:26:22 +01:00
|
|
|
|
2022-08-15 11:35:05 +01:00
|
|
|
func (q *DeleteQuery) String() string {
|
|
|
|
buf, err := q.AppendQuery(q.db.Formatter(), nil)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(buf)
|
|
|
|
}
|
|
|
|
|
2022-04-24 11:26:22 +01:00
|
|
|
//------------------------------------------------------------------------------
|
2022-08-15 11:35:05 +01:00
|
|
|
|
|
|
|
func (q *DeleteQuery) QueryBuilder() QueryBuilder {
|
|
|
|
return &deleteQueryBuilder{q}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *DeleteQuery) ApplyQueryBuilder(fn func(QueryBuilder) QueryBuilder) *DeleteQuery {
|
|
|
|
return fn(q.QueryBuilder()).Unwrap().(*DeleteQuery)
|
|
|
|
}
|
|
|
|
|
2022-04-24 11:26:22 +01:00
|
|
|
type deleteQueryBuilder struct {
|
|
|
|
*DeleteQuery
|
|
|
|
}
|
|
|
|
|
2022-08-15 11:35:05 +01:00
|
|
|
func (q *deleteQueryBuilder) WhereGroup(
|
|
|
|
sep string, fn func(QueryBuilder) QueryBuilder,
|
|
|
|
) QueryBuilder {
|
2022-04-24 11:26:22 +01:00
|
|
|
q.DeleteQuery = q.DeleteQuery.WhereGroup(sep, func(qs *DeleteQuery) *DeleteQuery {
|
|
|
|
return fn(q).(*deleteQueryBuilder).DeleteQuery
|
|
|
|
})
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *deleteQueryBuilder) Where(query string, args ...interface{}) QueryBuilder {
|
|
|
|
q.DeleteQuery.Where(query, args...)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *deleteQueryBuilder) WhereOr(query string, args ...interface{}) QueryBuilder {
|
|
|
|
q.DeleteQuery.WhereOr(query, args...)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *deleteQueryBuilder) WhereDeleted() QueryBuilder {
|
|
|
|
q.DeleteQuery.WhereDeleted()
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *deleteQueryBuilder) WhereAllWithDeleted() QueryBuilder {
|
|
|
|
q.DeleteQuery.WhereAllWithDeleted()
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *deleteQueryBuilder) WherePK(cols ...string) QueryBuilder {
|
|
|
|
q.DeleteQuery.WherePK(cols...)
|
|
|
|
return q
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *deleteQueryBuilder) Unwrap() interface{} {
|
|
|
|
return q.DeleteQuery
|
|
|
|
}
|