From 9df9f7688bd67062fa9f178cbd2179a1372c992f Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Tue, 17 Nov 2020 17:49:43 +0800 Subject: [PATCH] Change UpdatingColumn to SkipHooks --- callbacks/create.go | 4 ++-- callbacks/delete.go | 4 ++-- callbacks/query.go | 2 +- callbacks/update.go | 8 ++++---- finisher_api.go | 4 ++-- gorm.go | 2 +- statement.go | 4 ++-- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/callbacks/create.go b/callbacks/create.go index a58549a5..3ca56d73 100644 --- a/callbacks/create.go +++ b/callbacks/create.go @@ -10,7 +10,7 @@ import ( ) func BeforeCreate(db *gorm.DB) { - if db.Error == nil && db.Statement.Schema != nil && !db.Statement.UpdatingColumn && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeCreate) { + if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeCreate) { callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) { if db.Statement.Schema.BeforeSave { if i, ok := value.(BeforeSaveInterface); ok { @@ -203,7 +203,7 @@ func CreateWithReturning(db *gorm.DB) { } func AfterCreate(db *gorm.DB) { - if db.Error == nil && db.Statement.Schema != nil && !db.Statement.UpdatingColumn && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterCreate) { + if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterCreate) { callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) { if db.Statement.Schema.AfterSave { if i, ok := value.(AfterSaveInterface); ok { diff --git a/callbacks/delete.go b/callbacks/delete.go index 4a289e0c..867aa697 100644 --- a/callbacks/delete.go +++ b/callbacks/delete.go @@ -10,7 +10,7 @@ import ( ) func BeforeDelete(db *gorm.DB) { - if db.Error == nil && db.Statement.Schema != nil && db.Statement.Schema.BeforeDelete { + if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && db.Statement.Schema.BeforeDelete { callMethod(db, func(value interface{}, tx *gorm.DB) bool { if i, ok := value.(BeforeDeleteInterface); ok { db.AddError(i.BeforeDelete(tx)) @@ -153,7 +153,7 @@ func Delete(db *gorm.DB) { } func AfterDelete(db *gorm.DB) { - if db.Error == nil && db.Statement.Schema != nil && db.Statement.Schema.AfterDelete { + if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && db.Statement.Schema.AfterDelete { callMethod(db, func(value interface{}, tx *gorm.DB) bool { if i, ok := value.(AfterDeleteInterface); ok { db.AddError(i.AfterDelete(tx)) diff --git a/callbacks/query.go b/callbacks/query.go index 92f711f5..89f02f58 100644 --- a/callbacks/query.go +++ b/callbacks/query.go @@ -214,7 +214,7 @@ func Preload(db *gorm.DB) { } func AfterQuery(db *gorm.DB) { - if db.Error == nil && db.Statement.Schema != nil && db.Statement.Schema.AfterFind { + if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && db.Statement.Schema.AfterFind { callMethod(db, func(value interface{}, tx *gorm.DB) bool { if i, ok := value.(AfterFindInterface); ok { db.AddError(i.AfterFind(tx)) diff --git a/callbacks/update.go b/callbacks/update.go index 46f59157..c8f3922e 100644 --- a/callbacks/update.go +++ b/callbacks/update.go @@ -29,7 +29,7 @@ func SetupUpdateReflectValue(db *gorm.DB) { } func BeforeUpdate(db *gorm.DB) { - if db.Error == nil && db.Statement.Schema != nil && !db.Statement.UpdatingColumn && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeUpdate) { + if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.BeforeSave || db.Statement.Schema.BeforeUpdate) { callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) { if db.Statement.Schema.BeforeSave { if i, ok := value.(BeforeSaveInterface); ok { @@ -87,7 +87,7 @@ func Update(db *gorm.DB) { } func AfterUpdate(db *gorm.DB) { - if db.Error == nil && db.Statement.Schema != nil && !db.Statement.UpdatingColumn && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterUpdate) { + if db.Error == nil && db.Statement.Schema != nil && !db.Statement.SkipHooks && (db.Statement.Schema.AfterSave || db.Statement.Schema.AfterUpdate) { callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) { if db.Statement.Schema.AfterSave { if i, ok := value.(AfterSaveInterface); ok { @@ -198,7 +198,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) { } } - if !stmt.UpdatingColumn && stmt.Schema != nil { + if !stmt.SkipHooks && stmt.Schema != nil { for _, dbName := range stmt.Schema.DBNames { field := stmt.Schema.LookUpField(dbName) if field.AutoUpdateTime > 0 && value[field.Name] == nil && value[field.DBName] == nil { @@ -228,7 +228,7 @@ func ConvertToAssignments(stmt *gorm.Statement) (set clause.Set) { if !field.PrimaryKey || (!updatingValue.CanAddr() || stmt.Dest != stmt.Model) { if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) { value, isZero := field.ValueOf(updatingValue) - if !stmt.UpdatingColumn { + if !stmt.SkipHooks { if field.AutoUpdateTime > 0 { if field.AutoUpdateTime == schema.UnixNanosecond { value = stmt.DB.NowFunc().UnixNano() diff --git a/finisher_api.go b/finisher_api.go index d1390a15..1efa2e46 100644 --- a/finisher_api.go +++ b/finisher_api.go @@ -307,7 +307,7 @@ func (db *DB) Updates(values interface{}) (tx *DB) { func (db *DB) UpdateColumn(column string, value interface{}) (tx *DB) { tx = db.getInstance() tx.Statement.Dest = map[string]interface{}{column: value} - tx.Statement.UpdatingColumn = true + tx.Statement.SkipHooks = true tx.callbacks.Update().Execute(tx) return } @@ -315,7 +315,7 @@ func (db *DB) UpdateColumn(column string, value interface{}) (tx *DB) { func (db *DB) UpdateColumns(values interface{}) (tx *DB) { tx = db.getInstance() tx.Statement.Dest = values - tx.Statement.UpdatingColumn = true + tx.Statement.SkipHooks = true tx.callbacks.Update().Execute(tx) return } diff --git a/gorm.go b/gorm.go index f7c18b08..59e4fd6c 100644 --- a/gorm.go +++ b/gorm.go @@ -193,7 +193,7 @@ func (db *DB) Session(config *Session) *DB { } if config.SkipHooks { - tx.Statement.UpdatingColumn = true + tx.Statement.SkipHooks = true } if !config.NewDB { diff --git a/statement.go b/statement.go index 3f46ae0a..27edf9da 100644 --- a/statement.go +++ b/statement.go @@ -37,7 +37,7 @@ type Statement struct { Schema *schema.Schema Context context.Context RaiseErrorOnNotFound bool - UpdatingColumn bool + SkipHooks bool SQL strings.Builder Vars []interface{} CurDestIndex int @@ -421,7 +421,7 @@ func (stmt *Statement) clone() *Statement { Schema: stmt.Schema, Context: stmt.Context, RaiseErrorOnNotFound: stmt.RaiseErrorOnNotFound, - UpdatingColumn: stmt.UpdatingColumn, + SkipHooks: stmt.SkipHooks, } for k, c := range stmt.Clauses {