Change UpdatingColumn to SkipHooks

This commit is contained in:
Jinzhu 2020-11-17 17:49:43 +08:00
parent 26504f5cae
commit 9df9f7688b
7 changed files with 14 additions and 14 deletions

View File

@ -10,7 +10,7 @@ import (
) )
func BeforeCreate(db *gorm.DB) { 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) { callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
if db.Statement.Schema.BeforeSave { if db.Statement.Schema.BeforeSave {
if i, ok := value.(BeforeSaveInterface); ok { if i, ok := value.(BeforeSaveInterface); ok {
@ -203,7 +203,7 @@ func CreateWithReturning(db *gorm.DB) {
} }
func AfterCreate(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) { callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
if db.Statement.Schema.AfterSave { if db.Statement.Schema.AfterSave {
if i, ok := value.(AfterSaveInterface); ok { if i, ok := value.(AfterSaveInterface); ok {

View File

@ -10,7 +10,7 @@ import (
) )
func BeforeDelete(db *gorm.DB) { 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 { callMethod(db, func(value interface{}, tx *gorm.DB) bool {
if i, ok := value.(BeforeDeleteInterface); ok { if i, ok := value.(BeforeDeleteInterface); ok {
db.AddError(i.BeforeDelete(tx)) db.AddError(i.BeforeDelete(tx))
@ -153,7 +153,7 @@ func Delete(db *gorm.DB) {
} }
func AfterDelete(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 { callMethod(db, func(value interface{}, tx *gorm.DB) bool {
if i, ok := value.(AfterDeleteInterface); ok { if i, ok := value.(AfterDeleteInterface); ok {
db.AddError(i.AfterDelete(tx)) db.AddError(i.AfterDelete(tx))

View File

@ -214,7 +214,7 @@ func Preload(db *gorm.DB) {
} }
func AfterQuery(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 { callMethod(db, func(value interface{}, tx *gorm.DB) bool {
if i, ok := value.(AfterFindInterface); ok { if i, ok := value.(AfterFindInterface); ok {
db.AddError(i.AfterFind(tx)) db.AddError(i.AfterFind(tx))

View File

@ -29,7 +29,7 @@ func SetupUpdateReflectValue(db *gorm.DB) {
} }
func BeforeUpdate(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) { callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
if db.Statement.Schema.BeforeSave { if db.Statement.Schema.BeforeSave {
if i, ok := value.(BeforeSaveInterface); ok { if i, ok := value.(BeforeSaveInterface); ok {
@ -87,7 +87,7 @@ func Update(db *gorm.DB) {
} }
func AfterUpdate(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) { callMethod(db, func(value interface{}, tx *gorm.DB) (called bool) {
if db.Statement.Schema.AfterSave { if db.Statement.Schema.AfterSave {
if i, ok := value.(AfterSaveInterface); ok { 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 { for _, dbName := range stmt.Schema.DBNames {
field := stmt.Schema.LookUpField(dbName) field := stmt.Schema.LookUpField(dbName)
if field.AutoUpdateTime > 0 && value[field.Name] == nil && value[field.DBName] == nil { 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 !field.PrimaryKey || (!updatingValue.CanAddr() || stmt.Dest != stmt.Model) {
if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) { if v, ok := selectColumns[field.DBName]; (ok && v) || (!ok && !restricted) {
value, isZero := field.ValueOf(updatingValue) value, isZero := field.ValueOf(updatingValue)
if !stmt.UpdatingColumn { if !stmt.SkipHooks {
if field.AutoUpdateTime > 0 { if field.AutoUpdateTime > 0 {
if field.AutoUpdateTime == schema.UnixNanosecond { if field.AutoUpdateTime == schema.UnixNanosecond {
value = stmt.DB.NowFunc().UnixNano() value = stmt.DB.NowFunc().UnixNano()

View File

@ -307,7 +307,7 @@ func (db *DB) Updates(values interface{}) (tx *DB) {
func (db *DB) UpdateColumn(column string, value interface{}) (tx *DB) { func (db *DB) UpdateColumn(column string, value interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
tx.Statement.Dest = map[string]interface{}{column: value} tx.Statement.Dest = map[string]interface{}{column: value}
tx.Statement.UpdatingColumn = true tx.Statement.SkipHooks = true
tx.callbacks.Update().Execute(tx) tx.callbacks.Update().Execute(tx)
return return
} }
@ -315,7 +315,7 @@ func (db *DB) UpdateColumn(column string, value interface{}) (tx *DB) {
func (db *DB) UpdateColumns(values interface{}) (tx *DB) { func (db *DB) UpdateColumns(values interface{}) (tx *DB) {
tx = db.getInstance() tx = db.getInstance()
tx.Statement.Dest = values tx.Statement.Dest = values
tx.Statement.UpdatingColumn = true tx.Statement.SkipHooks = true
tx.callbacks.Update().Execute(tx) tx.callbacks.Update().Execute(tx)
return return
} }

View File

@ -193,7 +193,7 @@ func (db *DB) Session(config *Session) *DB {
} }
if config.SkipHooks { if config.SkipHooks {
tx.Statement.UpdatingColumn = true tx.Statement.SkipHooks = true
} }
if !config.NewDB { if !config.NewDB {

View File

@ -37,7 +37,7 @@ type Statement struct {
Schema *schema.Schema Schema *schema.Schema
Context context.Context Context context.Context
RaiseErrorOnNotFound bool RaiseErrorOnNotFound bool
UpdatingColumn bool SkipHooks bool
SQL strings.Builder SQL strings.Builder
Vars []interface{} Vars []interface{}
CurDestIndex int CurDestIndex int
@ -421,7 +421,7 @@ func (stmt *Statement) clone() *Statement {
Schema: stmt.Schema, Schema: stmt.Schema,
Context: stmt.Context, Context: stmt.Context,
RaiseErrorOnNotFound: stmt.RaiseErrorOnNotFound, RaiseErrorOnNotFound: stmt.RaiseErrorOnNotFound,
UpdatingColumn: stmt.UpdatingColumn, SkipHooks: stmt.SkipHooks,
} }
for k, c := range stmt.Clauses { for k, c := range stmt.Clauses {