forked from mirror/gorm
Refactor DataTypeOf for mysql
This commit is contained in:
parent
552d9bf455
commit
d7455fa5b1
109
dialect_mysql.go
109
dialect_mysql.go
|
@ -3,7 +3,7 @@ package gorm
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -15,60 +15,69 @@ 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 {
|
if sqlType == "" {
|
||||||
size, _ = strconv.Atoi(num)
|
switch dataValue.Kind() {
|
||||||
}
|
case reflect.Bool:
|
||||||
|
sqlType = "boolean"
|
||||||
switch dataValue.Kind() {
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
||||||
case reflect.Bool:
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
||||||
return "boolean"
|
sqlType = "int AUTO_INCREMENT"
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32:
|
} else {
|
||||||
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
|
sqlType = "int"
|
||||||
return "int AUTO_INCREMENT"
|
}
|
||||||
}
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
||||||
return "int"
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
||||||
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
sqlType = "int unsigned AUTO_INCREMENT"
|
||||||
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
|
} else {
|
||||||
return "int unsigned AUTO_INCREMENT"
|
sqlType = "int unsigned"
|
||||||
}
|
}
|
||||||
return "int unsigned"
|
case reflect.Int64:
|
||||||
case reflect.Int64:
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
||||||
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
|
sqlType = "bigint AUTO_INCREMENT"
|
||||||
return "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 {
|
||||||
return "bigint unsigned"
|
sqlType = "bigint unsigned"
|
||||||
case reflect.Float32, reflect.Float64:
|
}
|
||||||
return "double"
|
case reflect.Float32, reflect.Float64:
|
||||||
case reflect.String:
|
sqlType = "double"
|
||||||
if size > 0 && size < 65532 {
|
case reflect.String:
|
||||||
return fmt.Sprintf("varchar(%d)", size)
|
if size > 0 && size < 65532 {
|
||||||
}
|
sqlType = fmt.Sprintf("varchar(%d)", size)
|
||||||
return "longtext"
|
} else {
|
||||||
case reflect.Struct:
|
sqlType = "longtext"
|
||||||
if _, ok := dataValue.Interface().(time.Time); ok {
|
}
|
||||||
return "timestamp NULL"
|
case reflect.Struct:
|
||||||
}
|
if _, ok := dataValue.Interface().(time.Time); ok {
|
||||||
default:
|
sqlType = "timestamp NULL"
|
||||||
if _, ok := dataValue.Interface().([]byte); ok {
|
}
|
||||||
if size > 0 && size < 65532 {
|
default:
|
||||||
return fmt.Sprintf("varbinary(%d)", size)
|
if _, ok := dataValue.Interface().([]byte); ok {
|
||||||
|
if size > 0 && size < 65532 {
|
||||||
|
sqlType = fmt.Sprintf("varbinary(%d)", size)
|
||||||
|
} else {
|
||||||
|
sqlType = "longblob"
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return "longblob"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
panic(fmt.Sprintf("invalid sql type %s (%s) for mysql", dataValue.Type().Name(), dataValue.Kind().String()))
|
|
||||||
|
if sqlType == "" {
|
||||||
|
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) {
|
||||||
|
|
|
@ -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() {
|
||||||
|
|
Loading…
Reference in New Issue