mirror of https://github.com/go-gorm/gorm.git
Make callback delete works
This commit is contained in:
parent
4985d7bd96
commit
261ece5696
|
@ -10,6 +10,8 @@ func BeforeDelete(scope *Scope) {
|
|||
}
|
||||
|
||||
func Delete(scope *Scope) {
|
||||
defer scope.Trace(time.Now())
|
||||
|
||||
if scope.HasError() {
|
||||
return
|
||||
}
|
||||
|
@ -33,7 +35,9 @@ func AfterDelete(scope *Scope) {
|
|||
}
|
||||
|
||||
func init() {
|
||||
DefaultCallback.Delete().Register("begin_transaction", BeginTransaction)
|
||||
DefaultCallback.Delete().Register("before_delete", BeforeDelete)
|
||||
DefaultCallback.Delete().Register("delete", Delete)
|
||||
DefaultCallback.Delete().Register("after_delete", AfterDelete)
|
||||
DefaultCallback.Delete().Register("commit_or_rollback_transaction", CommitOrRollbackTransaction)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
package gorm
|
||||
|
||||
func BeginTransaction(scope *Scope) {
|
||||
scope.Begin()
|
||||
}
|
||||
|
||||
func CommitOrRollbackTransaction(scope *Scope) {
|
||||
scope.CommitOrRollback()
|
||||
}
|
|
@ -1504,7 +1504,6 @@ func TestTransaction(t *testing.T) {
|
|||
if err := tx2.Save(&u2).Error; err != nil {
|
||||
t.Errorf("No error should raise, but got", err)
|
||||
}
|
||||
tx2.Update("age", 90)
|
||||
|
||||
if err := tx2.First(&User{}, "name = ?", "transcation-2").Error; err != nil {
|
||||
t.Errorf("Should find saved record, but got", err)
|
||||
|
|
45
scope.go
45
scope.go
|
@ -5,6 +5,7 @@ import (
|
|||
"fmt"
|
||||
"github.com/jinzhu/gorm/dialect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"reflect"
|
||||
"regexp"
|
||||
|
@ -16,6 +17,7 @@ type Scope struct {
|
|||
Sql string
|
||||
SqlVars []interface{}
|
||||
db *DB
|
||||
startedTransaction bool
|
||||
}
|
||||
|
||||
func (db *DB) newScope(value interface{}) *Scope {
|
||||
|
@ -52,6 +54,21 @@ func (scope *Scope) PrimaryKey() string {
|
|||
return "id"
|
||||
}
|
||||
|
||||
func (scope *Scope) PrimaryKeyZero() bool {
|
||||
return isBlank(reflect.ValueOf(scope.PrimaryKeyValue()))
|
||||
}
|
||||
|
||||
func (scope *Scope) PrimaryKeyValue() interface{} {
|
||||
data := reflect.Indirect(reflect.ValueOf(scope.Value))
|
||||
|
||||
if data.Kind() == reflect.Struct {
|
||||
if field := data.FieldByName(snakeToUpperCamel(scope.PrimaryKey())); field.IsValid() {
|
||||
return field.Interface()
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func (scope *Scope) HasColumn(name string) bool {
|
||||
data := reflect.Indirect(reflect.ValueOf(scope.Value))
|
||||
|
||||
|
@ -146,3 +163,31 @@ func (scope *Scope) Exec() {
|
|||
scope.Err(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (scope *Scope) Trace(t time.Time) {
|
||||
if len(scope.Sql) > 0 {
|
||||
scope.db.slog(scope.Sql, t, scope.SqlVars...)
|
||||
}
|
||||
}
|
||||
|
||||
func (scope *Scope) Begin() *Scope {
|
||||
if tx, err := scope.DB().(sqlDb).Begin(); err == nil {
|
||||
scope.db.db = interface{}(tx).(sqlCommon)
|
||||
scope.startedTransaction = true
|
||||
}
|
||||
return scope
|
||||
}
|
||||
|
||||
func (scope *Scope) CommitOrRollback() *Scope {
|
||||
if scope.startedTransaction {
|
||||
if db, ok := scope.db.db.(sqlTx); ok {
|
||||
if scope.HasError() {
|
||||
db.Rollback()
|
||||
} else {
|
||||
db.Commit()
|
||||
}
|
||||
scope.db.db = scope.db.parent.db
|
||||
}
|
||||
}
|
||||
return scope
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue