2014-01-26 08:41:37 +04:00
|
|
|
package gorm
|
|
|
|
|
2014-08-23 12:00:04 +04:00
|
|
|
import "fmt"
|
2014-01-26 08:41:37 +04:00
|
|
|
|
2016-01-17 11:28:32 +03:00
|
|
|
func init() {
|
|
|
|
defaultCallback.Delete().Register("gorm:begin_transaction", beginTransactionCallback)
|
|
|
|
defaultCallback.Delete().Register("gorm:before_delete", beforeDeleteCallback)
|
|
|
|
defaultCallback.Delete().Register("gorm:delete", deleteCallback)
|
|
|
|
defaultCallback.Delete().Register("gorm:after_delete", afterDeleteCallback)
|
|
|
|
defaultCallback.Delete().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
|
|
|
|
}
|
|
|
|
|
2016-01-17 10:30:42 +03:00
|
|
|
func beforeDeleteCallback(scope *Scope) {
|
2016-01-17 11:14:14 +03:00
|
|
|
if !scope.HasError() {
|
|
|
|
scope.CallMethod("BeforeDelete")
|
|
|
|
}
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|
|
|
|
|
2016-01-17 10:30:42 +03:00
|
|
|
func deleteCallback(scope *Scope) {
|
2014-01-26 15:34:06 +04:00
|
|
|
if !scope.HasError() {
|
2015-03-12 10:01:59 +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(),
|
2014-08-23 12:00:04 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 10:30:42 +03:00
|
|
|
func afterDeleteCallback(scope *Scope) {
|
2016-01-17 11:14:14 +03:00
|
|
|
if !scope.HasError() {
|
|
|
|
scope.CallMethod("AfterDelete")
|
|
|
|
}
|
2014-01-26 08:41:37 +04:00
|
|
|
}
|