mirror of https://github.com/go-gorm/gorm.git
Set identity insert on after create transaction, close #841
This commit is contained in:
parent
c811590d4e
commit
2522f03c1f
|
@ -5,7 +5,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// defaultCallback hold default callbacks defined by gorm
|
// defaultCallback hold default callbacks defined by gorm
|
||||||
var defaultCallback = &Callback{}
|
var DefaultCallback = &Callback{}
|
||||||
|
|
||||||
// Callback contains callbacks that used when CURD objects
|
// Callback contains callbacks that used when CURD objects
|
||||||
// Field `creates` hold callbacks will be call when creating object
|
// Field `creates` hold callbacks will be call when creating object
|
||||||
|
|
|
@ -7,15 +7,15 @@ import (
|
||||||
|
|
||||||
// Define callbacks for creating
|
// Define callbacks for creating
|
||||||
func init() {
|
func init() {
|
||||||
defaultCallback.Create().Register("gorm:begin_transaction", beginTransactionCallback)
|
DefaultCallback.Create().Register("gorm:begin_transaction", beginTransactionCallback)
|
||||||
defaultCallback.Create().Register("gorm:before_create", beforeCreateCallback)
|
DefaultCallback.Create().Register("gorm:before_create", beforeCreateCallback)
|
||||||
defaultCallback.Create().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
|
DefaultCallback.Create().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
|
||||||
defaultCallback.Create().Register("gorm:update_time_stamp", updateTimeStampForCreateCallback)
|
DefaultCallback.Create().Register("gorm:update_time_stamp", updateTimeStampForCreateCallback)
|
||||||
defaultCallback.Create().Register("gorm:create", createCallback)
|
DefaultCallback.Create().Register("gorm:create", createCallback)
|
||||||
defaultCallback.Create().Register("gorm:force_reload_after_create", forceReloadAfterCreateCallback)
|
DefaultCallback.Create().Register("gorm:force_reload_after_create", forceReloadAfterCreateCallback)
|
||||||
defaultCallback.Create().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
|
DefaultCallback.Create().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
|
||||||
defaultCallback.Create().Register("gorm:after_create", afterCreateCallback)
|
DefaultCallback.Create().Register("gorm:after_create", afterCreateCallback)
|
||||||
defaultCallback.Create().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
|
DefaultCallback.Create().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
|
||||||
}
|
}
|
||||||
|
|
||||||
// beforeCreateCallback will invoke `BeforeSave`, `BeforeCreate` method before creating
|
// beforeCreateCallback will invoke `BeforeSave`, `BeforeCreate` method before creating
|
||||||
|
|
|
@ -4,11 +4,11 @@ import "fmt"
|
||||||
|
|
||||||
// Define callbacks for deleting
|
// Define callbacks for deleting
|
||||||
func init() {
|
func init() {
|
||||||
defaultCallback.Delete().Register("gorm:begin_transaction", beginTransactionCallback)
|
DefaultCallback.Delete().Register("gorm:begin_transaction", beginTransactionCallback)
|
||||||
defaultCallback.Delete().Register("gorm:before_delete", beforeDeleteCallback)
|
DefaultCallback.Delete().Register("gorm:before_delete", beforeDeleteCallback)
|
||||||
defaultCallback.Delete().Register("gorm:delete", deleteCallback)
|
DefaultCallback.Delete().Register("gorm:delete", deleteCallback)
|
||||||
defaultCallback.Delete().Register("gorm:after_delete", afterDeleteCallback)
|
DefaultCallback.Delete().Register("gorm:after_delete", afterDeleteCallback)
|
||||||
defaultCallback.Delete().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
|
DefaultCallback.Delete().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
|
||||||
}
|
}
|
||||||
|
|
||||||
// beforeDeleteCallback will invoke `BeforeDelete` method before deleting
|
// beforeDeleteCallback will invoke `BeforeDelete` method before deleting
|
||||||
|
|
|
@ -8,9 +8,9 @@ import (
|
||||||
|
|
||||||
// Define callbacks for querying
|
// Define callbacks for querying
|
||||||
func init() {
|
func init() {
|
||||||
defaultCallback.Query().Register("gorm:query", queryCallback)
|
DefaultCallback.Query().Register("gorm:query", queryCallback)
|
||||||
defaultCallback.Query().Register("gorm:preload", preloadCallback)
|
DefaultCallback.Query().Register("gorm:preload", preloadCallback)
|
||||||
defaultCallback.Query().Register("gorm:after_query", afterQueryCallback)
|
DefaultCallback.Query().Register("gorm:after_query", afterQueryCallback)
|
||||||
}
|
}
|
||||||
|
|
||||||
// queryCallback used to query data from database
|
// queryCallback used to query data from database
|
||||||
|
|
|
@ -7,15 +7,15 @@ import (
|
||||||
|
|
||||||
// Define callbacks for updating
|
// Define callbacks for updating
|
||||||
func init() {
|
func init() {
|
||||||
defaultCallback.Update().Register("gorm:assign_updating_attributes", assignUpdatingAttributesCallback)
|
DefaultCallback.Update().Register("gorm:assign_updating_attributes", assignUpdatingAttributesCallback)
|
||||||
defaultCallback.Update().Register("gorm:begin_transaction", beginTransactionCallback)
|
DefaultCallback.Update().Register("gorm:begin_transaction", beginTransactionCallback)
|
||||||
defaultCallback.Update().Register("gorm:before_update", beforeUpdateCallback)
|
DefaultCallback.Update().Register("gorm:before_update", beforeUpdateCallback)
|
||||||
defaultCallback.Update().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
|
DefaultCallback.Update().Register("gorm:save_before_associations", saveBeforeAssociationsCallback)
|
||||||
defaultCallback.Update().Register("gorm:update_time_stamp", updateTimeStampForUpdateCallback)
|
DefaultCallback.Update().Register("gorm:update_time_stamp", updateTimeStampForUpdateCallback)
|
||||||
defaultCallback.Update().Register("gorm:update", updateCallback)
|
DefaultCallback.Update().Register("gorm:update", updateCallback)
|
||||||
defaultCallback.Update().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
|
DefaultCallback.Update().Register("gorm:save_after_associations", saveAfterAssociationsCallback)
|
||||||
defaultCallback.Update().Register("gorm:after_update", afterUpdateCallback)
|
DefaultCallback.Update().Register("gorm:after_update", afterUpdateCallback)
|
||||||
defaultCallback.Update().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
|
DefaultCallback.Update().Register("gorm:commit_or_rollback_transaction", commitOrRollbackTransactionCallback)
|
||||||
}
|
}
|
||||||
|
|
||||||
// assignUpdatingAttributesCallback assign updating attributes to model
|
// assignUpdatingAttributesCallback assign updating attributes to model
|
||||||
|
|
|
@ -1,3 +1,17 @@
|
||||||
package mssql
|
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
main.go
2
main.go
|
@ -64,7 +64,7 @@ func Open(dialect string, args ...interface{}) (*DB, error) {
|
||||||
db = DB{
|
db = DB{
|
||||||
dialect: newDialect(dialect, dbSql.(*sql.DB)),
|
dialect: newDialect(dialect, dbSql.(*sql.DB)),
|
||||||
logger: defaultLogger,
|
logger: defaultLogger,
|
||||||
callbacks: defaultCallback,
|
callbacks: DefaultCallback,
|
||||||
source: source,
|
source: source,
|
||||||
values: map[string]interface{}{},
|
values: map[string]interface{}{},
|
||||||
db: dbSql,
|
db: dbSql,
|
||||||
|
|
Loading…
Reference in New Issue