Refactor DataTypeOf for mysql

This commit is contained in:
Jinzhu 2016-02-14 13:34:32 +08:00
parent 552d9bf455
commit d7455fa5b1
2 changed files with 60 additions and 53 deletions

View File

@ -3,7 +3,7 @@ package gorm
import ( import (
"fmt" "fmt"
"reflect" "reflect"
"strconv" "strings"
"time" "time"
) )
@ -15,62 +15,71 @@ func (mysql) Quote(key string) string {
return fmt.Sprintf("`%s`", key) return fmt.Sprintf("`%s`", key)
} }
// Get Data Type for MySQL Dialect
func (mysql) DataTypeOf(field *StructField) string { func (mysql) DataTypeOf(field *StructField) string {
var ( var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
size int
dataValue = reflect.Indirect(reflect.New(field.Struct.Type))
tagSettings = field.TagSettings
)
if num, ok := tagSettings["SIZE"]; ok {
size, _ = strconv.Atoi(num)
}
if sqlType == "" {
switch dataValue.Kind() { switch dataValue.Kind() {
case reflect.Bool: case reflect.Bool:
return "boolean" sqlType = "boolean"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32: case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok { if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
return "int AUTO_INCREMENT" sqlType = "int AUTO_INCREMENT"
} else {
sqlType = "int"
} }
return "int"
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr: case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok { if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
return "int unsigned AUTO_INCREMENT" sqlType = "int unsigned AUTO_INCREMENT"
} else {
sqlType = "int unsigned"
} }
return "int unsigned"
case reflect.Int64: case reflect.Int64:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok { if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
return "bigint AUTO_INCREMENT" sqlType = "bigint AUTO_INCREMENT"
} else {
sqlType = "bigint"
} }
return "bigint"
case reflect.Uint64: case reflect.Uint64:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok { if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
return "bigint unsigned AUTO_INCREMENT" sqlType = "bigint unsigned AUTO_INCREMENT"
} else {
sqlType = "bigint unsigned"
} }
return "bigint unsigned"
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
return "double" sqlType = "double"
case reflect.String: case reflect.String:
if size > 0 && size < 65532 { if size > 0 && size < 65532 {
return fmt.Sprintf("varchar(%d)", size) sqlType = fmt.Sprintf("varchar(%d)", size)
} else {
sqlType = "longtext"
} }
return "longtext"
case reflect.Struct: case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok { if _, ok := dataValue.Interface().(time.Time); ok {
return "timestamp NULL" sqlType = "timestamp NULL"
} }
default: default:
if _, ok := dataValue.Interface().([]byte); ok { if _, ok := dataValue.Interface().([]byte); ok {
if size > 0 && size < 65532 { if size > 0 && size < 65532 {
return fmt.Sprintf("varbinary(%d)", size) sqlType = fmt.Sprintf("varbinary(%d)", size)
} } else {
return "longblob" sqlType = "longblob"
} }
} }
}
}
if sqlType == "" {
panic(fmt.Sprintf("invalid sql type %s (%s) for mysql", dataValue.Type().Name(), dataValue.Kind().String())) panic(fmt.Sprintf("invalid sql type %s (%s) for mysql", dataValue.Type().Name(), dataValue.Kind().String()))
} }
if strings.TrimSpace(additionalType) == "" {
return sqlType
}
return fmt.Sprintf("%v %v", sqlType, additionalType)
}
func (s mysql) currentDatabase(scope *Scope) (name string) { func (s mysql) currentDatabase(scope *Scope) (name string) {
s.RawScanString(scope, &name, "SELECT DATABASE()") s.RawScanString(scope, &name, "SELECT DATABASE()")
return return

View File

@ -13,9 +13,7 @@ type sqlite3 struct {
// Get Data Type for Sqlite Dialect // Get Data Type for Sqlite Dialect
func (sqlite3) DataTypeOf(field *StructField) string { func (sqlite3) DataTypeOf(field *StructField) string {
var ( var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
)
if sqlType == "" { if sqlType == "" {
switch dataValue.Kind() { switch dataValue.Kind() {