Skip to content

Commit

Permalink
Set identity insert on after create transaction, close go-gorm#841
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Mar 5, 2016
1 parent c811590 commit 2522f03
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 deletions.
2 changes: 1 addition & 1 deletion callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions callback_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions callback_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions callback_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions callback_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion dialects/mssql/mssql.go
Original file line number Diff line number Diff line change
@@ -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)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 2522f03

Please sign in to comment.