From 317e1a9a48981563a6cf940a98640916adaa51ba Mon Sep 17 00:00:00 2001 From: Jinzhu Date: Sun, 17 Jan 2016 16:28:32 +0800 Subject: [PATCH] Keep refactoring callbacks --- callback_create.go | 30 +++++++++++++++----------- callback_delete.go | 16 +++++++------- callback_query.go | 12 +++++------ callback_shared.go => callback_save.go | 0 callback_update.go | 24 ++++++++++----------- 5 files changed, 44 insertions(+), 38 deletions(-) rename callback_shared.go => callback_save.go (100%) diff --git a/callback_create.go b/callback_create.go index 6054aa91..11082f8a 100644 --- a/callback_create.go +++ b/callback_create.go @@ -5,6 +5,20 @@ import ( "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) { if !scope.HasError() { scope.CallMethod("BeforeSave") @@ -14,6 +28,7 @@ func beforeCreateCallback(scope *Scope) { } } +// updateTimeStampForCreateCallback will set `CreatedAt`, `UpdatedAt` when creating func updateTimeStampForCreateCallback(scope *Scope) { if !scope.HasError() { now := NowFunc() @@ -22,6 +37,7 @@ func updateTimeStampForCreateCallback(scope *Scope) { } } +// createCallback the callback used to insert data into database func createCallback(scope *Scope) { 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) { if columns, ok := scope.InstanceGet("gorm:force_reload_after_create_attrs"); ok { scope.DB().New().Select(columns.([]string)).First(scope.Value) } } +// beforeCreateCallback will invoke `AfterCreate`, `AfterSave` method after creating func afterCreateCallback(scope *Scope) { if !scope.HasError() { scope.CallMethod("AfterCreate") @@ -120,15 +138,3 @@ func afterCreateCallback(scope *Scope) { 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) -} diff --git a/callback_delete.go b/callback_delete.go index 804bdf71..17b5cfb4 100644 --- a/callback_delete.go +++ b/callback_delete.go @@ -2,6 +2,14 @@ package gorm 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) { if !scope.HasError() { scope.CallMethod("BeforeDelete") @@ -30,11 +38,3 @@ func afterDeleteCallback(scope *Scope) { 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) -} diff --git a/callback_query.go b/callback_query.go index 9ebff6b7..f6fa0aa1 100644 --- a/callback_query.go +++ b/callback_query.go @@ -6,6 +6,12 @@ import ( "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) { defer scope.trace(NowFunc()) @@ -83,9 +89,3 @@ func afterQueryCallback(scope *Scope) { scope.CallMethod("AfterFind") } } - -func init() { - defaultCallback.Query().Register("gorm:query", queryCallback) - defaultCallback.Query().Register("gorm:after_query", afterQueryCallback) - defaultCallback.Query().Register("gorm:preload", preloadCallback) -} diff --git a/callback_shared.go b/callback_save.go similarity index 100% rename from callback_shared.go rename to callback_save.go diff --git a/callback_update.go b/callback_update.go index 9463c994..b9d2bcbc 100644 --- a/callback_update.go +++ b/callback_update.go @@ -5,6 +5,18 @@ import ( "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) { if attrs, ok := scope.InstanceGet("gorm:update_interface"); ok { 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) -}