mirror of https://github.com/go-gorm/gorm.git
Don't execute SET IDENTITY_INSERT if dialect is not mssql
This commit is contained in:
parent
2522f03c1f
commit
b6a2710a15
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue