forked from go-gorm/gorm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback_delete.go
36 lines (30 loc) · 1013 Bytes
/
callback_delete.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package gorm
import "fmt"
func BeforeDelete(scope *Scope) {
scope.CallMethodWithErrorCheck("BeforeDelete")
}
func Delete(scope *Scope) {
if !scope.HasError() {
if !scope.Search.Unscope && scope.HasColumn("DeletedAt") {
scope.Raw(
fmt.Sprintf("UPDATE %v SET deleted_at=%v %v",
scope.QuotedTableName(),
scope.AddToVars(NowFunc()),
scope.CombinedConditionSql(),
))
} else {
scope.Raw(fmt.Sprintf("DELETE FROM %v %v", scope.QuotedTableName(), scope.CombinedConditionSql()))
}
scope.Exec()
}
}
func AfterDelete(scope *Scope) {
scope.CallMethodWithErrorCheck("AfterDelete")
}
func init() {
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)
}