2020-02-02 03:35:01 +03:00
|
|
|
package mysql
|
|
|
|
|
|
|
|
import (
|
2020-02-22 12:53:57 +03:00
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"math"
|
|
|
|
|
2020-02-02 03:35:01 +03:00
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
"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"
|
2020-02-02 03:35:01 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Dialector struct {
|
2020-02-22 12:53:57 +03:00
|
|
|
DSN string
|
2020-02-02 03:35:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Open(dsn string) gorm.Dialector {
|
2020-02-22 12:53:57 +03:00
|
|
|
return &Dialector{DSN: dsn}
|
2020-02-02 03:35:01 +03:00
|
|
|
}
|
|
|
|
|
2020-02-22 12:53:57 +03:00
|
|
|
func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
|
2020-02-02 03:35:01 +03:00
|
|
|
// register callbacks
|
|
|
|
callbacks.RegisterDefaultCallbacks(db)
|
2020-03-09 08:10:48 +03:00
|
|
|
db.ConnPool, err = sql.Open("mysql", dialector.DSN)
|
2020-02-22 18:08:20 +03:00
|
|
|
return
|
2020-02-02 03:35:01 +03:00
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
DB: db,
|
|
|
|
Dialector: dialector,
|
|
|
|
}}}
|
2020-02-02 03:35:01 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:59:54 +03:00
|
|
|
func (dialector Dialector) BindVarTo(writer clause.Writer, stmt *gorm.Statement, v interface{}) {
|
|
|
|
writer.WriteByte('?')
|
2020-02-02 03:35:01 +03:00
|
|
|
}
|
2020-02-05 06:14:58 +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-05 06:14:58 +03:00
|
|
|
}
|
2020-02-22 12:53:57 +03:00
|
|
|
|
2020-02-23 07:39:26 +03:00
|
|
|
func (dialector Dialector) Explain(sql string, vars ...interface{}) string {
|
|
|
|
return logger.ExplainSQL(sql, nil, `"`, vars...)
|
|
|
|
}
|
|
|
|
|
2020-02-22 12:53:57 +03:00
|
|
|
func (dialector Dialector) DataTypeOf(field *schema.Field) string {
|
|
|
|
switch field.DataType {
|
|
|
|
case schema.Bool:
|
|
|
|
return "boolean"
|
|
|
|
case schema.Int, schema.Uint:
|
|
|
|
sqlType := "int"
|
|
|
|
switch {
|
|
|
|
case field.Size <= 8:
|
|
|
|
sqlType = "tinyint"
|
|
|
|
case field.Size <= 16:
|
|
|
|
sqlType = "smallint"
|
|
|
|
case field.Size <= 32:
|
|
|
|
sqlType = "int"
|
|
|
|
default:
|
|
|
|
sqlType = "bigint"
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.DataType == schema.Uint {
|
|
|
|
sqlType += " unsigned"
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.AutoIncrement {
|
|
|
|
sqlType += " AUTO_INCREMENT"
|
|
|
|
}
|
|
|
|
return sqlType
|
|
|
|
case schema.Float:
|
|
|
|
if field.Size <= 32 {
|
|
|
|
return "float"
|
|
|
|
}
|
|
|
|
return "double"
|
|
|
|
case schema.String:
|
|
|
|
size := field.Size
|
2020-02-23 03:29:59 +03:00
|
|
|
if field.PrimaryKey && size == 0 {
|
2020-02-22 18:08:20 +03:00
|
|
|
size = 256
|
|
|
|
}
|
|
|
|
|
2020-02-22 12:53:57 +03:00
|
|
|
if size >= 65536 && size <= int(math.Pow(2, 24)) {
|
|
|
|
return "mediumtext"
|
2020-02-22 18:08:20 +03:00
|
|
|
} else if size > int(math.Pow(2, 24)) || size <= 0 {
|
2020-02-22 12:53:57 +03:00
|
|
|
return "longtext"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("varchar(%d)", size)
|
|
|
|
case schema.Time:
|
|
|
|
precision := ""
|
|
|
|
if field.Precision > 0 {
|
|
|
|
precision = fmt.Sprintf("(%d)", field.Precision)
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.NotNull || field.PrimaryKey {
|
|
|
|
return "datetime" + precision
|
|
|
|
}
|
|
|
|
return "datetime" + precision + " NULL"
|
|
|
|
case schema.Bytes:
|
|
|
|
if field.Size > 0 && field.Size < 65536 {
|
|
|
|
return fmt.Sprintf("varbinary(%d)", field.Size)
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.Size >= 65536 && field.Size <= int(math.Pow(2, 24)) {
|
|
|
|
return "mediumblob"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "longblob"
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|