forked from mirror/gorm
Refactor DataTypeOf for postgres, mssql
This commit is contained in:
parent
d7455fa5b1
commit
b4abd125c1
|
@ -3,7 +3,7 @@ package gorm
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -18,49 +18,55 @@ func (commonDialect) Quote(key string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (commonDialect) DataTypeOf(field *StructField) string {
|
func (commonDialect) 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, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
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 {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
return "INTEGER AUTO_INCREMENT"
|
sqlType = "INTEGER AUTO_INCREMENT"
|
||||||
|
} else {
|
||||||
|
sqlType = "INTEGER"
|
||||||
}
|
}
|
||||||
return "INTEGER"
|
|
||||||
case reflect.Int64, reflect.Uint64:
|
case reflect.Int64, reflect.Uint64:
|
||||||
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok {
|
||||||
return "BIGINT AUTO_INCREMENT"
|
sqlType = "BIGINT AUTO_INCREMENT"
|
||||||
|
} else {
|
||||||
|
sqlType = "BIGINT"
|
||||||
}
|
}
|
||||||
return "BIGINT"
|
|
||||||
case reflect.Float32, reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
return "FLOAT"
|
sqlType = "FLOAT"
|
||||||
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 = "VARCHAR(65532)"
|
||||||
}
|
}
|
||||||
return "VARCHAR(65532)"
|
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if _, ok := dataValue.Interface().(time.Time); ok {
|
if _, ok := dataValue.Interface().(time.Time); ok {
|
||||||
return "TIMESTAMP"
|
sqlType = "TIMESTAMP"
|
||||||
}
|
}
|
||||||
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("BINARY(%d)", size)
|
sqlType = fmt.Sprintf("BINARY(%d)", size)
|
||||||
}
|
} else {
|
||||||
return "BINARY(65532)"
|
sqlType = "BINARY(65532)"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if sqlType == "" {
|
||||||
panic(fmt.Sprintf("invalid sql type %s (%s) for commonDialect", dataValue.Type().Name(), dataValue.Kind().String()))
|
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 {
|
func (c commonDialect) HasIndex(scope *Scope, tableName string, indexName string) bool {
|
||||||
|
|
|
@ -3,7 +3,7 @@ package gorm
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,49 +12,55 @@ type mssql struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mssql) DataTypeOf(field *StructField) string {
|
func (mssql) 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 "bit"
|
sqlType = "bit"
|
||||||
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
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 {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
||||||
return "int IDENTITY(1,1)"
|
sqlType = "int IDENTITY(1,1)"
|
||||||
|
} else {
|
||||||
|
sqlType = "int"
|
||||||
}
|
}
|
||||||
return "int"
|
|
||||||
case reflect.Int64, reflect.Uint64:
|
case reflect.Int64, reflect.Uint64:
|
||||||
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
||||||
return "bigint IDENTITY(1,1)"
|
sqlType = "bigint IDENTITY(1,1)"
|
||||||
|
} else {
|
||||||
|
sqlType = "bigint"
|
||||||
}
|
}
|
||||||
return "bigint"
|
|
||||||
case reflect.Float32, reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
return "float"
|
sqlType = "float"
|
||||||
case reflect.String:
|
case reflect.String:
|
||||||
if size > 0 && size < 65532 {
|
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:
|
case reflect.Struct:
|
||||||
if _, ok := dataValue.Interface().(time.Time); ok {
|
if _, ok := dataValue.Interface().(time.Time); ok {
|
||||||
return "datetime2"
|
sqlType = "datetime2"
|
||||||
}
|
}
|
||||||
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("varchar(%d)", size)
|
sqlType = fmt.Sprintf("varchar(%d)", size)
|
||||||
}
|
} else {
|
||||||
return "text"
|
sqlType = "text"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if sqlType == "" {
|
||||||
panic(fmt.Sprintf("invalid sql type %s (%s) for mssql", dataValue.Type().Name(), dataValue.Kind().String()))
|
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 {
|
func (s mssql) HasIndex(scope *Scope, tableName string, indexName string) bool {
|
||||||
|
|
|
@ -5,7 +5,6 @@ import (
|
||||||
"database/sql/driver"
|
"database/sql/driver"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -21,52 +20,57 @@ func (postgres) BindVar(i int) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (postgres) DataTypeOf(field *StructField) string {
|
func (postgres) 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, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr:
|
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 {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
||||||
return "serial"
|
sqlType = "serial"
|
||||||
|
} else {
|
||||||
|
sqlType = "integer"
|
||||||
}
|
}
|
||||||
return "integer"
|
|
||||||
case reflect.Int64, reflect.Uint64:
|
case reflect.Int64, reflect.Uint64:
|
||||||
if _, ok := tagSettings["AUTO_INCREMENT"]; ok {
|
if _, ok := field.TagSettings["AUTO_INCREMENT"]; ok || field.IsPrimaryKey {
|
||||||
return "bigserial"
|
sqlType = "bigserial"
|
||||||
|
} else {
|
||||||
|
sqlType = "bigint"
|
||||||
}
|
}
|
||||||
return "bigint"
|
|
||||||
case reflect.Float32, reflect.Float64:
|
case reflect.Float32, reflect.Float64:
|
||||||
return "numeric"
|
sqlType = "numeric"
|
||||||
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 = "text"
|
||||||
}
|
}
|
||||||
return "text"
|
|
||||||
case reflect.Struct:
|
case reflect.Struct:
|
||||||
if _, ok := dataValue.Interface().(time.Time); ok {
|
if _, ok := dataValue.Interface().(time.Time); ok {
|
||||||
return "timestamp with time zone"
|
sqlType = "timestamp with time zone"
|
||||||
}
|
}
|
||||||
case reflect.Map:
|
case reflect.Map:
|
||||||
if dataValue.Type() == hstoreType {
|
if dataValue.Type() == hstoreType {
|
||||||
return "hstore"
|
sqlType = "hstore"
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
if isByteArrayOrSlice(dataValue) {
|
if isByteArrayOrSlice(dataValue) {
|
||||||
return "bytea"
|
sqlType = "bytea"
|
||||||
} else if isUUID(dataValue) {
|
} 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()))
|
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 {
|
func (s postgres) HasIndex(scope *Scope, tableName string, indexName string) bool {
|
||||||
|
|
Loading…
Reference in New Issue