gorm/callbacks/delete.go

110 lines
3.1 KiB
Go
Raw Normal View History

2020-02-02 14:32:27 +03:00
package callbacks
2020-02-23 16:22:35 +03:00
import (
"reflect"
"github.com/jinzhu/gorm"
2020-03-08 09:51:52 +03:00
"github.com/jinzhu/gorm/clause"
2020-05-25 19:16:41 +03:00
"github.com/jinzhu/gorm/schema"
2020-02-23 16:22:35 +03:00
)
2020-02-02 14:32:27 +03:00
func BeforeDelete(db *gorm.DB) {
2020-05-31 18:55:56 +03:00
if db.Error == nil && db.Statement.Schema != nil && db.Statement.Schema.BeforeDelete {
tx := db.Session(&gorm.Session{})
2020-02-23 16:22:35 +03:00
callMethod := func(value interface{}) bool {
if db.Statement.Schema.BeforeDelete {
if i, ok := value.(gorm.BeforeDeleteInterface); ok {
2020-05-31 18:55:56 +03:00
db.AddError(i.BeforeDelete(tx))
2020-02-23 16:22:35 +03:00
return true
}
}
return false
}
if ok := callMethod(db.Statement.Dest); !ok {
switch db.Statement.ReflectValue.Kind() {
case reflect.Slice, reflect.Array:
2020-05-31 18:55:56 +03:00
for i := 0; i < db.Statement.ReflectValue.Len(); i++ {
2020-02-23 16:22:35 +03:00
callMethod(db.Statement.ReflectValue.Index(i).Interface())
}
case reflect.Struct:
callMethod(db.Statement.ReflectValue.Interface())
}
}
}
2020-02-02 14:32:27 +03:00
}
func Delete(db *gorm.DB) {
2020-05-31 18:55:56 +03:00
if db.Error == nil {
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() == "" {
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)
column, values := schema.ToQueryValues(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
if db.Statement.Dest != db.Statement.Model && db.Statement.Model != nil {
_, queryValues = schema.GetIdentityFieldValuesMap(reflect.ValueOf(db.Statement.Model), db.Statement.Schema.PrimaryFields)
column, values = schema.ToQueryValues(db.Statement.Schema.PrimaryFieldDBNames, queryValues)
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
if _, ok := db.Statement.Clauses["WHERE"]; !ok {
db.AddError(gorm.ErrMissingWhereClause)
return
}
2020-03-08 09:51:52 +03:00
2020-05-31 18:55:56 +03:00
db.Statement.AddClauseIfNotExists(clause.From{})
db.Statement.Build("DELETE", "FROM", "WHERE")
}
2020-03-08 09:51:52 +03:00
2020-05-31 18:55:56 +03:00
result, err := db.Statement.ConnPool.ExecContext(db.Statement.Context, db.Statement.SQL.String(), db.Statement.Vars...)
2020-03-08 09:51:52 +03:00
2020-05-31 18:55:56 +03:00
if err == nil {
db.RowsAffected, _ = result.RowsAffected()
} else {
db.AddError(err)
}
2020-03-08 09:51:52 +03:00
}
2020-02-02 14:32:27 +03:00
}
func AfterDelete(db *gorm.DB) {
2020-05-31 18:55:56 +03:00
if db.Error == nil && db.Statement.Schema != nil && db.Statement.Schema.AfterDelete {
tx := db.Session(&gorm.Session{})
2020-02-23 16:22:35 +03:00
callMethod := func(value interface{}) bool {
if db.Statement.Schema.AfterDelete {
if i, ok := value.(gorm.AfterDeleteInterface); ok {
2020-05-31 18:55:56 +03:00
db.AddError(i.AfterDelete(tx))
2020-02-23 16:22:35 +03:00
return true
}
}
return false
}
if ok := callMethod(db.Statement.Dest); !ok {
switch db.Statement.ReflectValue.Kind() {
case reflect.Slice, reflect.Array:
2020-05-31 18:55:56 +03:00
for i := 0; i < db.Statement.ReflectValue.Len(); i++ {
2020-02-23 16:22:35 +03:00
callMethod(db.Statement.ReflectValue.Index(i).Interface())
}
case reflect.Struct:
callMethod(db.Statement.ReflectValue.Interface())
}
}
}
2020-02-02 14:32:27 +03:00
}