package mssql import ( "database/sql" "fmt" "regexp" "strconv" _ "github.com/denisenkom/go-mssqldb" "github.com/jinzhu/gorm" "github.com/jinzhu/gorm/callbacks" "github.com/jinzhu/gorm/clause" "github.com/jinzhu/gorm/logger" "github.com/jinzhu/gorm/migrator" "github.com/jinzhu/gorm/schema" ) type Dialector struct { DSN string } func Open(dsn string) gorm.Dialector { return &Dialector{DSN: dsn} } func (dialector Dialector) Initialize(db *gorm.DB) (err error) { // register callbacks callbacks.RegisterDefaultCallbacks(db) db.ConnPool, err = sql.Open("sqlserver", dialector.DSN) return } func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator { return Migrator{migrator.Migrator{Config: migrator.Config{ DB: db, Dialector: dialector, CreateIndexAfterCreateTable: true, }}} } func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) { writer.WriteString("@p") writer.WriteString(strconv.Itoa(len(stmt.Vars))) } func (dialector Dialector) QuoteTo(writer clause.Writer, str string) { writer.WriteByte('"') writer.WriteString(str) writer.WriteByte('"') } var numericPlaceholder = regexp.MustCompile("@p(\\d+)") func (dialector Dialector) Explain(sql string, vars ...interface{}) string { return logger.ExplainSQL(sql, numericPlaceholder, `'`, vars...) } func (dialector Dialector) DataTypeOf(field *schema.Field) string { switch field.DataType { case schema.Bool: return "bit" case schema.Int, schema.Uint: var sqlType string switch { case field.Size < 16: sqlType = "smallint" case field.Size < 31: sqlType = "int" default: sqlType = "bigint" } if field.AutoIncrement || field == field.Schema.PrioritizedPrimaryField { return sqlType + " IDENTITY(1,1)" } return sqlType case schema.Float: return "decimal" case schema.String: size := field.Size if field.PrimaryKey && size == 0 { size = 256 } if size > 0 && size <= 4000 { return fmt.Sprintf("nvarchar(%d)", size) } return "ntext" case schema.Time: return "datetimeoffset" case schema.Bytes: return "binary" } return "" }