gorm/callback_delete.go

40 lines
788 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) {
if scope.HasError() {
return
}
if !scope.Search.unscope && scope.HasColumn("DeletedAt") {
2014-01-26 10:18:21 +04:00
scope.Raw(
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
scope.TableName(),
scope.AddToVars(time.Now()),
scope.CombinedConditionSql(),
))
2014-01-26 08:41:37 +04:00
} else {
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.TableName(), scope.CombinedConditionSql()))
}
2014-01-26 10:18:21 +04:00
2014-01-26 08:41:37 +04:00
scope.Exec()
}
func AfterDelete(scope *Scope) {
scope.CallMethod("AfterDelete")
}
func init() {
DefaultCallback.Delete().Register("before_delete", BeforeDelete)
DefaultCallback.Delete().Register("delete", Delete)
DefaultCallback.Delete().Register("after_delete", AfterDelete)
}