Skip to content

Commit

Permalink
Don't execute SET IDENTITY_INSERT if dialect is not mssql
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Mar 5, 2016
1 parent 2522f03 commit b6a2710
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 2 deletions.
3 changes: 3 additions & 0 deletions dialect.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import (

// Dialect interface contains behaviors that differ across SQL database
type Dialect interface {
// GetName get dialect's name
GetName() string

// SetDB set db for dialect
SetDB(db *sql.DB)

Expand Down
4 changes: 4 additions & 0 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func init() {
RegisterDialect("common", &commonDialect{})
}

func (commonDialect) GetName() string {
return "common"
}

func (s *commonDialect) SetDB(db *sql.DB) {
s.db = db
}
Expand Down
4 changes: 4 additions & 0 deletions dialect_mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func init() {
RegisterDialect("mssql", &mssql{})
}

func (mssql) GetName() string {
return "mssql"
}

func (mssql) DataTypeOf(field *StructField) string {
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)

Expand Down
4 changes: 4 additions & 0 deletions dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func init() {
RegisterDialect("mysql", &mysql{})
}

func (mysql) GetName() string {
return "mysql"
}

func (mysql) Quote(key string) string {
return fmt.Sprintf("`%s`", key)
}
Expand Down
4 changes: 4 additions & 0 deletions dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ func init() {
RegisterDialect("postgres", &postgres{})
}

func (postgres) GetName() string {
return "postgres"
}

func (postgres) BindVar(i int) string {
return fmt.Sprintf("$%v", i)
}
Expand Down
4 changes: 4 additions & 0 deletions dialect_sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ func init() {
RegisterDialect("sqlite3", &sqlite3{})
}

func (sqlite3) GetName() string {
return "sqlite3"
}

// Get Data Type for Sqlite Dialect
func (sqlite3) DataTypeOf(field *StructField) string {
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
Expand Down
5 changes: 3 additions & 2 deletions dialects/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
)

func setIdentityInsert(scope *gorm.Scope) {
scope.NewDB().Exec(fmt.Sprintf("SET IDENTITY_INSERT %v ON", scope.TableName()))
if scope.Dialect().GetName() == "mssql" {
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)
}

0 comments on commit b6a2710

Please sign in to comment.