Set identity insert on after create transaction, close #841

This commit is contained in:
Jinzhu 2016-03-05 19:22:29 +08:00
parent c811590d4e
commit 2522f03c1f
7 changed files with 43 additions and 29 deletions

View File

@ -5,7 +5,7 @@ import (
)
// defaultCallback hold default callbacks defined by gorm
var defaultCallback = &Callback{}
var DefaultCallback = &Callback{}
// Callback contains callbacks that used when CURD objects
// Field `creates` hold callbacks will be call when creating object

View File

@ -7,15 +7,15 @@ import (
// 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", 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)
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", 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

View File

@ -4,11 +4,11 @@ import "fmt"
// Define callbacks for deleting
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)
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)
}
// beforeDeleteCallback will invoke `BeforeDelete` method before deleting

View File

@ -8,9 +8,9 @@ import (
// Define callbacks for querying
func init() {
defaultCallback.Query().Register("gorm:query", queryCallback)
defaultCallback.Query().Register("gorm:preload", preloadCallback)
defaultCallback.Query().Register("gorm:after_query", afterQueryCallback)
DefaultCallback.Query().Register("gorm:query", queryCallback)
DefaultCallback.Query().Register("gorm:preload", preloadCallback)
DefaultCallback.Query().Register("gorm:after_query", afterQueryCallback)
}
// queryCallback used to query data from database

View File

@ -7,15 +7,15 @@ import (
// Define callbacks for updating
func init() {
defaultCallback.Update().Register("gorm:assign_updating_attributes", assignUpdatingAttributesCallback)
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", 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)
DefaultCallback.Update().Register("gorm:assign_updating_attributes", assignUpdatingAttributesCallback)
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", 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)
}
// assignUpdatingAttributesCallback assign updating attributes to model

View File

@ -1,3 +1,17 @@
package mssql
import _ "github.com/denisenkom/go-mssqldb"
import (
"fmt"
_ "github.com/denisenkom/go-mssqldb"
"github.com/jinzhu/gorm"
)
func setIdentityInsert(scope *gorm.Scope) {
scope.NewDB().Exec(fmt.Sprintf("SET IDENTITY_INSERT %v ON", scope.TableName()))
}
func init() {
gorm.DefaultCallback.Update().After("gorm:begin_transaction").Register("mssql:set_identity_insert", setIdentityInsert)
gorm.DefaultCallback.Create().After("gorm:begin_transaction").Register("mssql:set_identity_insert", setIdentityInsert)
}

View File

@ -64,7 +64,7 @@ func Open(dialect string, args ...interface{}) (*DB, error) {
db = DB{
dialect: newDialect(dialect, dbSql.(*sql.DB)),
logger: defaultLogger,
callbacks: defaultCallback,
callbacks: DefaultCallback,
source: source,
values: map[string]interface{}{},
db: dbSql,