gorm/callback_delete.go

42 lines
994 B
Go
Raw Normal View History

2014-01-26 08:41:37 +04:00
package gorm
import (
"fmt"
"time"
)
func BeforeDelete(scope *Scope) {
scope.CallMethod("BeforeDelete")
}
func Delete(scope *Scope) {
2014-01-26 13:10:33 +04:00
defer scope.Trace(time.Now())
2014-01-26 15:34:06 +04:00
if !scope.HasError() {
2014-01-28 12:56:51 +04:00
if !scope.Search.Unscope && scope.HasColumn("DeletedAt") {
2014-01-26 15:34:06 +04:00
scope.Raw(
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
scope.TableName(),
scope.AddToVars(time.Now()),
scope.CombinedConditionSql(),
))
} else {
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.TableName(), scope.CombinedConditionSql()))
}
scope.Exec()
2014-01-26 08:41:37 +04:00
}
}
func AfterDelete(scope *Scope) {
scope.CallMethod("AfterDelete")
}
func init() {
2014-01-26 13:10:33 +04:00
DefaultCallback.Delete().Register("begin_transaction", BeginTransaction)
2014-01-26 08:41:37 +04:00
DefaultCallback.Delete().Register("before_delete", BeforeDelete)
DefaultCallback.Delete().Register("delete", Delete)
DefaultCallback.Delete().Register("after_delete", AfterDelete)
2014-01-26 13:10:33 +04:00
DefaultCallback.Delete().Register("commit_or_rollback_transaction", CommitOrRollbackTransaction)
2014-01-26 08:41:37 +04:00
}