Keep refactoring callbacks

This commit is contained in:
Jinzhu 2016-01-17 16:28:32 +08:00
parent 31366f388f
commit 317e1a9a48
5 changed files with 44 additions and 38 deletions

View File

@ -5,6 +5,20 @@ import (
"strings" "strings"
) )
// Define callbacks for creating
func init() {
defaultCallback.Create().Register("gorm:begin_transaction", beginTransactionCallback)
defaultCallback.Create().Register("gorm:before_create", beforeCreateCallback)
defaultCallback.Create().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
defaultCallback.Create().Register("gorm:update_time_stamp_when_create", updateTimeStampForCreateCallback)
defaultCallback.Create().Register("gorm:create", createCallback)
defaultCallback.Create().Register("gorm:force_reload_after_create", forceReloadAfterCreateCallback)
defaultCallback.Create().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
defaultCallback.Create().Register("gorm:after_create", afterCreateCallback)
defaultCallback.Create().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
}
// beforeCreateCallback will invoke `BeforeSave`, `BeforeCreate` method before creating
func beforeCreateCallback(scope *Scope) { func beforeCreateCallback(scope *Scope) {
if !scope.HasError() { if !scope.HasError() {
scope.CallMethod("BeforeSave") scope.CallMethod("BeforeSave")
@ -14,6 +28,7 @@ func beforeCreateCallback(scope *Scope) {
} }
} }
// updateTimeStampForCreateCallback will set `CreatedAt`, `UpdatedAt` when creating
func updateTimeStampForCreateCallback(scope *Scope) { func updateTimeStampForCreateCallback(scope *Scope) {
if !scope.HasError() { if !scope.HasError() {
now := NowFunc() now := NowFunc()
@ -22,6 +37,7 @@ func updateTimeStampForCreateCallback(scope *Scope) {
} }
} }
// createCallback the callback used to insert data into database
func createCallback(scope *Scope) { func createCallback(scope *Scope) {
defer scope.trace(NowFunc()) defer scope.trace(NowFunc())
@ -106,12 +122,14 @@ func createCallback(scope *Scope) {
} }
} }
// forceReloadAfterCreateCallback will reload columns that having default value, and set it back to current object
func forceReloadAfterCreateCallback(scope *Scope) { func forceReloadAfterCreateCallback(scope *Scope) {
if columns, ok := scope.InstanceGet("gorm:force_reload_after_create_attrs"); ok { if columns, ok := scope.InstanceGet("gorm:force_reload_after_create_attrs"); ok {
scope.DB().New().Select(columns.([]string)).First(scope.Value) scope.DB().New().Select(columns.([]string)).First(scope.Value)
} }
} }
// beforeCreateCallback will invoke `AfterCreate`, `AfterSave` method after creating
func afterCreateCallback(scope *Scope) { func afterCreateCallback(scope *Scope) {
if !scope.HasError() { if !scope.HasError() {
scope.CallMethod("AfterCreate") scope.CallMethod("AfterCreate")
@ -120,15 +138,3 @@ func afterCreateCallback(scope *Scope) {
scope.CallMethod("AfterSave") scope.CallMethod("AfterSave")
} }
} }
func init() {
defaultCallback.Create().Register("gorm:begin_transaction", beginTransactionCallback)
defaultCallback.Create().Register("gorm:before_create", beforeCreateCallback)
defaultCallback.Create().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
defaultCallback.Create().Register("gorm:update_time_stamp_when_create", updateTimeStampForCreateCallback)
defaultCallback.Create().Register("gorm:create", createCallback)
defaultCallback.Create().Register("gorm:force_reload_after_create", forceReloadAfterCreateCallback)
defaultCallback.Create().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
defaultCallback.Create().Register("gorm:after_create", afterCreateCallback)
defaultCallback.Create().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
}

View File

@ -2,6 +2,14 @@ package gorm
import "fmt" import "fmt"
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)
}
func beforeDeleteCallback(scope *Scope) { func beforeDeleteCallback(scope *Scope) {
if !scope.HasError() { if !scope.HasError() {
scope.CallMethod("BeforeDelete") scope.CallMethod("BeforeDelete")
@ -30,11 +38,3 @@ func afterDeleteCallback(scope *Scope) {
scope.CallMethod("AfterDelete") scope.CallMethod("AfterDelete")
} }
} }
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)
}

View File

@ -6,6 +6,12 @@ import (
"reflect" "reflect"
) )
func init() {
defaultCallback.Query().Register("gorm:query", queryCallback)
defaultCallback.Query().Register("gorm:after_query", afterQueryCallback)
defaultCallback.Query().Register("gorm:preload", preloadCallback)
}
func queryCallback(scope *Scope) { func queryCallback(scope *Scope) {
defer scope.trace(NowFunc()) defer scope.trace(NowFunc())
@ -83,9 +89,3 @@ func afterQueryCallback(scope *Scope) {
scope.CallMethod("AfterFind") scope.CallMethod("AfterFind")
} }
} }
func init() {
defaultCallback.Query().Register("gorm:query", queryCallback)
defaultCallback.Query().Register("gorm:after_query", afterQueryCallback)
defaultCallback.Query().Register("gorm:preload", preloadCallback)
}

View File

@ -5,6 +5,18 @@ import (
"strings" "strings"
) )
func init() {
defaultCallback.Update().Register("gorm:assign_update_attributes", assignUpdateAttributesCallback)
defaultCallback.Update().Register("gorm:begin_transaction", beginTransactionCallback)
defaultCallback.Update().Register("gorm:before_update", beforeUpdateCallback)
defaultCallback.Update().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
defaultCallback.Update().Register("gorm:update_time_stamp_when_update", updateTimeStampForUpdateCallback)
defaultCallback.Update().Register("gorm:update", updateCallback)
defaultCallback.Update().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
defaultCallback.Update().Register("gorm:after_update", afterUpdateCallback)
defaultCallback.Update().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
}
func assignUpdateAttributesCallback(scope *Scope) { func assignUpdateAttributesCallback(scope *Scope) {
if attrs, ok := scope.InstanceGet("gorm:update_interface"); ok { if attrs, ok := scope.InstanceGet("gorm:update_interface"); ok {
if maps := convertInterfaceToMap(attrs); len(maps) > 0 { if maps := convertInterfaceToMap(attrs); len(maps) > 0 {
@ -89,15 +101,3 @@ func afterUpdateCallback(scope *Scope) {
} }
} }
} }
func init() {
defaultCallback.Update().Register("gorm:assign_update_attributes", assignUpdateAttributesCallback)
defaultCallback.Update().Register("gorm:begin_transaction", beginTransactionCallback)
defaultCallback.Update().Register("gorm:before_update", beforeUpdateCallback)
defaultCallback.Update().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
defaultCallback.Update().Register("gorm:update_time_stamp_when_update", updateTimeStampForUpdateCallback)
defaultCallback.Update().Register("gorm:update", updateCallback)
defaultCallback.Update().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
defaultCallback.Update().Register("gorm:after_update", afterUpdateCallback)
defaultCallback.Update().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
}