gorm/callback_delete.go

37 lines
1014 B
Go
Raw Normal View History

2014-01-26 08:41:37 +04:00
package gorm
import "fmt"
2014-01-26 08:41:37 +04:00
func BeforeDelete(scope *Scope) {
2015-02-24 11:28:15 +03:00
scope.CallMethodWithErrorCheck("BeforeDelete")
2014-01-26 08:41:37 +04:00
}
func Delete(scope *Scope) {
2014-01-26 15:34:06 +04:00
if !scope.HasError() {
2015-03-12 08:52:29 +03:00
if !scope.Search.unscoped && scope.HasColumn("DeletedAt") {
2014-01-26 15:34:06 +04:00
scope.Raw(
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
2014-06-03 13:15:05 +04:00
scope.QuotedTableName(),
scope.AddToVars(NowFunc()),
2014-01-26 15:34:06 +04:00
scope.CombinedConditionSql(),
))
} else {
2014-06-03 13:15:05 +04:00
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.QuotedTableName(), scope.CombinedConditionSql()))
2014-01-26 15:34:06 +04:00
}
scope.Exec()
2014-01-26 08:41:37 +04:00
}
}
func AfterDelete(scope *Scope) {
2015-02-24 11:28:15 +03:00
scope.CallMethodWithErrorCheck("AfterDelete")
2014-01-26 08:41:37 +04:00
}
func init() {
2014-01-29 06:28:20 +04:00
DefaultCallback.Delete().Register("gorm:begin_transaction", BeginTransaction)
DefaultCallback.Delete().Register("gorm:before_delete", BeforeDelete)
DefaultCallback.Delete().Register("gorm:delete", Delete)
DefaultCallback.Delete().Register("gorm:after_delete", AfterDelete)
DefaultCallback.Delete().Register("gorm:commit_or_rollback_transaction", CommitOrRollbackTransaction)
2014-01-26 08:41:37 +04:00
}