2020-02-02 14:32:27 +03:00
|
|
|
package callbacks
|
|
|
|
|
2020-02-23 16:22:35 +03:00
|
|
|
import (
|
|
|
|
"reflect"
|
2020-09-15 09:28:26 +03:00
|
|
|
"strings"
|
2020-02-23 16:22:35 +03:00
|
|
|
|
2020-06-02 04:16:07 +03:00
|
|
|
"gorm.io/gorm"
|
|
|
|
"gorm.io/gorm/clause"
|
|
|
|
"gorm.io/gorm/schema"
|
2021-10-28 02:24:38 +03:00
|
|
|
"gorm.io/gorm/utils"
|
2020-02-23 16:22:35 +03:00
|
|
|
)
|
2020-02-02 14:32:27 +03:00
|
|
|
|
|
|
|
func BeforeDelete(db *gorm.DB) {
|
2020-11-17 12:49:43 +03:00
|
|
|
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && db.Statement.Schema.BeforeDelete {
|
2020-06-05 16:23:20 +03:00
|
|
|
callMethod(db, func(value interface{}, tx *gorm.DB) bool {
|
2020-08-27 10:03:57 +03:00
|
|
|
if i, ok := value.(BeforeDeleteInterface); ok {
|
2020-06-05 16:23:20 +03:00
|
|
|
db.AddError(i.BeforeDelete(tx))
|
|
|
|
return true
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
|
|
|
|
2020-06-05 16:23:20 +03:00
|
|
|
return false
|
|
|
|
})
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
2020-02-02 14:32:27 +03:00
|
|
|
}
|
|
|
|
|
2020-09-10 16:46:18 +03:00
|
|
|
func DeleteBeforeAssociations(db *gorm.DB) {
|
|
|
|
if db.Error == nil && db.Statement.Schema != nil {
|
|
|
|
selectColumns, restricted := db.Statement.SelectAndOmitColumns(true, false)
|
|
|
|
|
|
|
|
if restricted {
|
|
|
|
for column, v := range selectColumns {
|
|
|
|
if v {
|
|
|
|
if rel, ok := db.Statement.Schema.Relationships.Relations[column]; ok {
|
|
|
|
switch rel.Type {
|
|
|
|
case schema.HasOne, schema.HasMany:
|
|
|
|
queryConds := rel.ToQueryConditions(db.Statement.ReflectValue)
|
|
|
|
modelValue := reflect.New(rel.FieldSchema.ModelType).Interface()
|
2020-11-17 10:41:17 +03:00
|
|
|
tx := db.Session(&gorm.Session{NewDB: true}).Model(modelValue)
|
2020-09-15 07:41:45 +03:00
|
|
|
withoutConditions := false
|
2021-02-07 09:36:27 +03:00
|
|
|
if db.Statement.Unscoped {
|
|
|
|
tx = tx.Unscoped()
|
|
|
|
}
|
2020-09-15 07:41:45 +03:00
|
|
|
|
|
|
|
if len(db.Statement.Selects) > 0 {
|
2021-03-04 14:44:15 +03:00
|
|
|
selects := make([]string, 0, len(db.Statement.Selects))
|
2020-09-15 09:28:26 +03:00
|
|
|
for _, s := range db.Statement.Selects {
|
|
|
|
if s == clause.Associations {
|
|
|
|
selects = append(selects, s)
|
|
|
|
} else if strings.HasPrefix(s, column+".") {
|
|
|
|
selects = append(selects, strings.TrimPrefix(s, column+"."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(selects) > 0 {
|
|
|
|
tx = tx.Select(selects)
|
|
|
|
}
|
2020-09-15 07:41:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, cond := range queryConds {
|
|
|
|
if c, ok := cond.(clause.IN); ok && len(c.Values) == 0 {
|
|
|
|
withoutConditions = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !withoutConditions {
|
|
|
|
if db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-09-10 16:46:18 +03:00
|
|
|
}
|
|
|
|
case schema.Many2Many:
|
|
|
|
var (
|
2021-03-04 14:44:15 +03:00
|
|
|
queryConds = make([]clause.Expression, 0, len(rel.References))
|
|
|
|
foreignFields = make([]*schema.Field, 0, len(rel.References))
|
|
|
|
relForeignKeys = make([]string, 0, len(rel.References))
|
2020-09-10 16:46:18 +03:00
|
|
|
modelValue = reflect.New(rel.JoinTable.ModelType).Interface()
|
|
|
|
table = rel.JoinTable.Table
|
2020-11-17 10:41:17 +03:00
|
|
|
tx = db.Session(&gorm.Session{NewDB: true}).Model(modelValue).Table(table)
|
2020-09-10 16:46:18 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
for _, ref := range rel.References {
|
|
|
|
if ref.OwnPrimaryKey {
|
|
|
|
foreignFields = append(foreignFields, ref.PrimaryKey)
|
|
|
|
relForeignKeys = append(relForeignKeys, ref.ForeignKey.DBName)
|
|
|
|
} else if ref.PrimaryValue != "" {
|
|
|
|
queryConds = append(queryConds, clause.Eq{
|
|
|
|
Column: clause.Column{Table: rel.JoinTable.Table, Name: ref.ForeignKey.DBName},
|
|
|
|
Value: ref.PrimaryValue,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, foreignValues := schema.GetIdentityFieldValuesMap(db.Statement.ReflectValue, foreignFields)
|
|
|
|
column, values := schema.ToQueryValues(table, relForeignKeys, foreignValues)
|
|
|
|
queryConds = append(queryConds, clause.IN{Column: column, Values: values})
|
|
|
|
|
|
|
|
if db.AddError(tx.Clauses(clause.Where{Exprs: queryConds}).Delete(modelValue).Error) != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-28 02:24:38 +03:00
|
|
|
func Delete(config *Config) func(db *gorm.DB) {
|
|
|
|
supportReturning := utils.Contains(config.DeleteClauses, "RETURNING")
|
|
|
|
|
|
|
|
return func(db *gorm.DB) {
|
|
|
|
if db.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Statement.Schema != nil && !db.Statement.Unscoped {
|
|
|
|
for _, c := range db.Statement.Schema.DeleteClauses {
|
|
|
|
db.Statement.AddClause(c)
|
|
|
|
}
|
2020-05-29 02:35:45 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Statement.SQL.String() == "" {
|
2020-06-08 15:23:47 +03:00
|
|
|
db.Statement.SQL.Grow(100)
|
2020-05-31 18:55:56 +03:00
|
|
|
db.Statement.AddClauseIfNotExists(clause.Delete{})
|
2020-03-08 09:51:52 +03:00
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
if db.Statement.Schema != nil {
|
|
|
|
_, queryValues := schema.GetIdentityFieldValuesMap(db.Statement.ReflectValue, db.Statement.Schema.PrimaryFields)
|
2020-07-10 08:08:15 +03:00
|
|
|
column, values := schema.ToQueryValues(db.Statement.Table, db.Statement.Schema.PrimaryFieldDBNames, queryValues)
|
2020-05-25 19:16:41 +03:00
|
|
|
|
|
|
|
if len(values) > 0 {
|
2020-05-29 02:35:45 +03:00
|
|
|
db.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
2020-03-08 09:51:52 +03:00
|
|
|
}
|
2020-05-31 18:55:56 +03:00
|
|
|
|
2020-08-25 12:27:28 +03:00
|
|
|
if db.Statement.ReflectValue.CanAddr() && db.Statement.Dest != db.Statement.Model && db.Statement.Model != nil {
|
2020-05-31 18:55:56 +03:00
|
|
|
_, queryValues = schema.GetIdentityFieldValuesMap(reflect.ValueOf(db.Statement.Model), db.Statement.Schema.PrimaryFields)
|
2020-07-10 08:08:15 +03:00
|
|
|
column, values = schema.ToQueryValues(db.Statement.Table, db.Statement.Schema.PrimaryFieldDBNames, queryValues)
|
2020-05-31 18:55:56 +03:00
|
|
|
|
|
|
|
if len(values) > 0 {
|
|
|
|
db.Statement.AddClause(clause.Where{Exprs: []clause.Expression{clause.IN{Column: column, Values: values}}})
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 09:51:52 +03:00
|
|
|
}
|
|
|
|
|
2020-05-31 18:55:56 +03:00
|
|
|
db.Statement.AddClauseIfNotExists(clause.From{})
|
2021-04-28 12:19:30 +03:00
|
|
|
db.Statement.Build(db.Statement.BuildClauses...)
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-03-08 09:51:52 +03:00
|
|
|
|
2020-11-05 06:20:08 +03:00
|
|
|
if _, ok := db.Statement.Clauses["WHERE"]; !db.AllowGlobalUpdate && !ok && db.Error == nil {
|
2020-08-25 12:27:28 +03:00
|
|
|
db.AddError(gorm.ErrMissingWhereClause)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-24 03:32:50 +03:00
|
|
|
if !db.DryRun && db.Error == nil {
|
2021-10-28 02:24:38 +03:00
|
|
|
if ok, mode := hasReturning(db, supportReturning); ok {
|
|
|
|
if rows, err := db.Statement.ConnPool.QueryContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...); db.AddError(err) == nil {
|
|
|
|
gorm.Scan(rows, db, mode)
|
|
|
|
rows.Close()
|
|
|
|
}
|
2020-06-01 16:26:23 +03:00
|
|
|
} else {
|
2021-10-28 02:24:38 +03:00
|
|
|
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
|
|
|
|
if db.AddError(err) == nil {
|
|
|
|
db.RowsAffected, _ = result.RowsAffected()
|
|
|
|
}
|
2020-06-01 16:26:23 +03:00
|
|
|
}
|
2020-05-31 18:55:56 +03:00
|
|
|
}
|
2020-03-08 09:51:52 +03:00
|
|
|
}
|
2020-02-02 14:32:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func AfterDelete(db *gorm.DB) {
|
2020-11-17 12:49:43 +03:00
|
|
|
if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && db.Statement.Schema.AfterDelete {
|
2020-06-05 16:23:20 +03:00
|
|
|
callMethod(db, func(value interface{}, tx *gorm.DB) bool {
|
2020-08-27 10:03:57 +03:00
|
|
|
if i, ok := value.(AfterDeleteInterface); ok {
|
2020-06-05 16:23:20 +03:00
|
|
|
db.AddError(i.AfterDelete(tx))
|
|
|
|
return true
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
|
|
|
return false
|
2020-06-05 16:23:20 +03:00
|
|
|
})
|
2020-02-23 16:22:35 +03:00
|
|
|
}
|
2020-02-02 14:32:27 +03:00
|
|
|
}
|