Refactor DataTypeOf for postgres, mssql

This commit is contained in:
Jinzhu 2016-02-14 13:51:34 +08:00
parent d7455fa5b1
commit b4abd125c1
3 changed files with 139 additions and 123 deletions

View File

@ -3,7 +3,7 @@ package gorm
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
@ -18,49 +18,55 @@ func (commonDialect) Quote(key string) string {
}
func (commonDialect) DataTypeOf(field *StructField) string {
var (
size int
dataValue = reflect.Indirect(reflect.New(field.Struct.Type))
tagSettings = field.TagSettings
)
if num, ok := tagSettings["SIZE"]; ok {
size, _ = strconv.Atoi(num)
}
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
if sqlType == "" {
switch dataValue.Kind() {
case reflect.Bool:
return "BOOLEAN"
sqlType = "BOOLEAN"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
return "INTEGER AUTO_INCREMENT"
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
sqlType = "INTEGER AUTO_INCREMENT"
} else {
sqlType = "INTEGER"
}
return "INTEGER"
case reflect.Int64, reflect.Uint64:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
return "BIGINT AUTO_INCREMENT"
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
sqlType = "BIGINT AUTO_INCREMENT"
} else {
sqlType = "BIGINT"
}
return "BIGINT"
case reflect.Float32, reflect.Float64:
return "FLOAT"
sqlType = "FLOAT"
case reflect.String:
if size > 0 && size < 65532 {
return fmt.Sprintf("VARCHAR(%d)", size)
sqlType = fmt.Sprintf("VARCHAR(%d)", size)
} else {
sqlType = "VARCHAR(65532)"
}
return "VARCHAR(65532)"
case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok {
return "TIMESTAMP"
sqlType = "TIMESTAMP"
}
default:
if _, ok := dataValue.Interface().([]byte); ok {
if size > 0 && size < 65532 {
return fmt.Sprintf("BINARY(%d)", size)
}
return "BINARY(65532)"
sqlType = fmt.Sprintf("BINARY(%d)", size)
} else {
sqlType = "BINARY(65532)"
}
}
}
}
if sqlType == "" {
panic(fmt.Sprintf("invalid sql type %s (%s) for commonDialect", dataValue.Type().Name(), dataValue.Kind().String()))
}
if strings.TrimSpace(additionalType) == "" {
return sqlType
}
return fmt.Sprintf("%v %v", sqlType, additionalType)
}
func (c commonDialect) HasIndex(scope *Scope, tableName string, indexName string) bool {

View File

@ -3,7 +3,7 @@ package gorm
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
)
@ -12,49 +12,55 @@ type mssql struct {
}
func (mssql) DataTypeOf(field *StructField) string {
var (
size int
dataValue = reflect.Indirect(reflect.New(field.Struct.Type))
tagSettings = field.TagSettings
)
if num, ok := tagSettings["SIZE"]; ok {
size, _ = strconv.Atoi(num)
}
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
if sqlType == "" {
switch dataValue.Kind() {
case reflect.Bool:
return "bit"
sqlType = "bit"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
return "int IDENTITY(1,1)"
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
sqlType = "int IDENTITY(1,1)"
} else {
sqlType = "int"
}
return "int"
case reflect.Int64, reflect.Uint64:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
return "bigint IDENTITY(1,1)"
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
sqlType = "bigint IDENTITY(1,1)"
} else {
sqlType = "bigint"
}
return "bigint"
case reflect.Float32, reflect.Float64:
return "float"
sqlType = "float"
case reflect.String:
if size > 0 && size < 65532 {
return fmt.Sprintf("nvarchar(%d)", size)
sqlType = fmt.Sprintf("nvarchar(%d)", size)
} else {
sqlType = "text"
}
return "text"
case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok {
return "datetime2"
sqlType = "datetime2"
}
default:
if _, ok := dataValue.Interface().([]byte); ok {
if size > 0 && size < 65532 {
return fmt.Sprintf("varchar(%d)", size)
}
return "text"
sqlType = fmt.Sprintf("varchar(%d)", size)
} else {
sqlType = "text"
}
}
}
}
if sqlType == "" {
panic(fmt.Sprintf("invalid sql type %s (%s) for mssql", dataValue.Type().Name(), dataValue.Kind().String()))
}
if strings.TrimSpace(additionalType) == "" {
return sqlType
}
return fmt.Sprintf("%v %v", sqlType, additionalType)
}
func (s mssql) HasIndex(scope *Scope, tableName string, indexName string) bool {

View File

@ -5,7 +5,6 @@ import (
"database/sql/driver"
"fmt"
"reflect"
"strconv"
"strings"
"time"
@ -21,52 +20,57 @@ func (postgres) BindVar(i int) string {
}
func (postgres) DataTypeOf(field *StructField) string {
var (
size int
dataValue = reflect.Indirect(reflect.New(field.Struct.Type))
tagSettings = field.TagSettings
)
if num, ok := tagSettings["SIZE"]; ok {
size, _ = strconv.Atoi(num)
}
var dataValue, sqlType, size, additionalType = ParseFieldStructForDialect(field)
if sqlType == "" {
switch dataValue.Kind() {
case reflect.Bool:
return "boolean"
sqlType = "boolean"
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
return "serial"
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
sqlType = "serial"
} else {
sqlType = "integer"
}
return "integer"
case reflect.Int64, reflect.Uint64:
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
return "bigserial"
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
sqlType = "bigserial"
} else {
sqlType = "bigint"
}
return "bigint"
case reflect.Float32, reflect.Float64:
return "numeric"
sqlType = "numeric"
case reflect.String:
if size > 0 && size < 65532 {
return fmt.Sprintf("varchar(%d)", size)
sqlType = fmt.Sprintf("varchar(%d)", size)
} else {
sqlType = "text"
}
return "text"
case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok {
return "timestamp with time zone"
sqlType = "timestamp with time zone"
}
case reflect.Map:
if dataValue.Type() == hstoreType {
return "hstore"
sqlType = "hstore"
}
default:
if isByteArrayOrSlice(dataValue) {
return "bytea"
sqlType = "bytea"
} else if isUUID(dataValue) {
return "uuid"
sqlType = "uuid"
}
}
}
if sqlType == "" {
panic(fmt.Sprintf("invalid sql type %s (%s) for postgres", dataValue.Type().Name(), dataValue.Kind().String()))
}
if strings.TrimSpace(additionalType) == "" {
return sqlType
}
return fmt.Sprintf("%v %v", sqlType, additionalType)
}
func (s postgres) HasIndex(scope *Scope, tableName string, indexName string) bool {