gorm/dialects/mssql/mssql.go

134 lines
3.1 KiB
Go
Raw Normal View History

2020-02-22 12:53:57 +03:00
package mssql
import (
"database/sql"
2020-05-30 20:21:16 +03:00
"database/sql/driver"
2020-02-22 12:53:57 +03:00
"fmt"
2020-02-23 07:39:26 +03:00
"regexp"
2020-02-22 20:02:07 +03:00
"strconv"
2020-02-22 12:53:57 +03:00
_ "github.com/denisenkom/go-mssqldb"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/callbacks"
2020-03-09 12:07:00 +03:00
"github.com/jinzhu/gorm/clause"
2020-02-23 07:39:26 +03:00
"github.com/jinzhu/gorm/logger"
2020-02-22 12:53:57 +03:00
"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
2020-03-12 08:05:22 +03:00
callbacks.RegisterDefaultCallbacks(db, &callbacks.Config{})
2020-05-30 05:43:41 +03:00
db.Callback().Create().Replace("gorm:create", Create)
2020-03-09 08:10:48 +03:00
db.ConnPool, err = sql.Open("sqlserver", dialector.DSN)
2020-05-30 05:43:41 +03:00
for k, v := range dialector.ClauseBuilders() {
db.ClauseBuilders[k] = v
}
2020-02-22 12:53:57 +03:00
return
}
2020-05-30 05:43:41 +03:00
func (dialector Dialector) ClauseBuilders() map[string]clause.ClauseBuilder {
return map[string]clause.ClauseBuilder{
"LIMIT": func(c clause.Clause, builder clause.Builder) {
if limit, ok := c.Expression.(clause.Limit); ok {
if limit.Offset > 0 {
builder.WriteString("OFFSET ")
builder.WriteString(strconv.Itoa(limit.Offset))
builder.WriteString("ROWS")
}
if limit.Limit > 0 {
if limit.Offset == 0 {
builder.WriteString(" OFFSET 0 ROWS")
}
builder.WriteString(" FETCH NEXT ")
builder.WriteString(strconv.Itoa(limit.Limit))
builder.WriteString(" ROWS ONLY")
}
}
},
}
}
2020-02-22 12:53:57 +03:00
func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator {
2020-02-22 14:41:01 +03:00
return Migrator{migrator.Migrator{Config: migrator.Config{
2020-02-22 20:02:07 +03:00
DB: db,
Dialector: dialector,
CreateIndexAfterCreateTable: true,
2020-02-22 14:41:01 +03:00
}}}
2020-02-22 12:53:57 +03:00
}
2020-03-09 12:59:54 +03:00
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
writer.WriteString("@p")
writer.WriteString(strconv.Itoa(len(stmt.Vars)))
2020-02-22 12:53:57 +03:00
}
2020-03-09 12:07:00 +03:00
func (dialector Dialector) QuoteTo(writer clause.Writer, str string) {
writer.WriteByte('"')
writer.WriteString(str)
writer.WriteByte('"')
2020-02-22 12:53:57 +03:00
}
2020-02-23 07:39:26 +03:00
var numericPlaceholder = regexp.MustCompile("@p(\\d+)")
func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
2020-05-30 20:21:16 +03:00
for idx, v := range vars {
if valuer, ok := v.(driver.Valuer); ok {
v, _ = valuer.Value()
}
if v, ok := v.(bool); ok {
vars[idx] = strconv.FormatBool(v)
}
}
2020-02-23 07:39:26 +03:00
return logger.ExplainSQL(sql, numericPlaceholder, `'`, vars...)
}
2020-02-22 12:53:57 +03:00
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"
}
2020-03-12 03:39:42 +03:00
if field.AutoIncrement || field == field.Schema.PrioritizedPrimaryField {
2020-02-22 12:53:57 +03:00
return sqlType + " IDENTITY(1,1)"
}
return sqlType
case schema.Float:
2020-05-30 20:21:16 +03:00
return "float"
2020-02-22 12:53:57 +03:00
case schema.String:
2020-02-22 20:02:07 +03:00
size := field.Size
2020-02-23 03:29:59 +03:00
if field.PrimaryKey && size == 0 {
2020-02-22 20:02:07 +03:00
size = 256
}
if size > 0 && size <= 4000 {
return fmt.Sprintf("nvarchar(%d)", size)
2020-02-22 12:53:57 +03:00
}
2020-05-30 07:46:30 +03:00
return "nvarchar(MAX)"
2020-02-22 12:53:57 +03:00
case schema.Time:
return "datetimeoffset"
case schema.Bytes:
2020-05-30 20:21:16 +03:00
return "varbinary(MAX)"
2020-02-22 12:53:57 +03:00
}
return ""
}