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-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-02-22 12:53:57 +03:00
|
|
|
db.DB, err = sql.Open("sqlite3", dialector.DSN)
|
2020-02-02 03:35:01 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-02-22 12:53:57 +03:00
|
|
|
func (dialector Dialector) BindVar(stmt *gorm.Statement, v interface{}) string {
|
2020-02-02 03:35:01 +03:00
|
|
|
return "?"
|
|
|
|
}
|
2020-02-05 06:14:58 +03:00
|
|
|
|
2020-02-22 12:53:57 +03:00
|
|
|
func (dialector Dialector) QuoteChars() [2]byte {
|
2020-02-05 06:14:58 +03:00
|
|
|
return [2]byte{'`', '`'} // `name`
|
|
|
|
}
|
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
|
|
|
|
if size >= 65536 && size <= int(math.Pow(2, 24)) {
|
|
|
|
return "mediumtext"
|
|
|
|
} else if size > int(math.Pow(2, 24)) || size < 0 {
|
|
|
|
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 ""
|
|
|
|
}
|