Merge pull request #1420 from ModelRocket/master

Adding more complete binary support for standard dialects
This commit is contained in:
Jinzhu 2017-04-19 15:35:50 +08:00 committed by GitHub
commit 717654b31c
5 changed files with 14 additions and 13 deletions

View File

@ -149,3 +149,8 @@ func (DefaultForeignKeyNamer) BuildForeignKeyName(tableName, field, dest string)
keyName = regexp.MustCompile("(_*[^a-zA-Z]+_*|_+)").ReplaceAllString(keyName, "_") keyName = regexp.MustCompile("(_*[^a-zA-Z]+_*|_+)").ReplaceAllString(keyName, "_")
return keyName return keyName
} }
// IsByteArrayOrSlice returns true of the reflected value is an array or slice
func IsByteArrayOrSlice(value reflect.Value) bool {
return (value.Kind() == reflect.Array || value.Kind() == reflect.Slice) && value.Type().Elem() == reflect.TypeOf(uint8(0))
}

View File

@ -101,7 +101,7 @@ func (s *mysql) DataTypeOf(field *StructField) string {
} }
} }
default: default:
if _, ok := dataValue.Interface().([]byte); ok { if IsByteArrayOrSlice(dataValue) {
if size > 0 && size < 65532 { if size > 0 && size < 65532 {
sqlType = fmt.Sprintf("varbinary(%d)", size) sqlType = fmt.Sprintf("varbinary(%d)", size)
} else { } else {

View File

@ -65,7 +65,7 @@ func (s *postgres) DataTypeOf(field *StructField) string {
sqlType = "hstore" sqlType = "hstore"
} }
default: default:
if isByteArrayOrSlice(dataValue) { if IsByteArrayOrSlice(dataValue) {
sqlType = "bytea" sqlType = "bytea"
} else if isUUID(dataValue) { } else if isUUID(dataValue) {
sqlType = "uuid" sqlType = "uuid"
@ -120,10 +120,6 @@ func (postgres) SupportLastInsertID() bool {
return false return false
} }
func isByteArrayOrSlice(value reflect.Value) bool {
return (value.Kind() == reflect.Array || value.Kind() == reflect.Slice) && value.Type().Elem() == reflect.TypeOf(uint8(0))
}
func isUUID(value reflect.Value) bool { func isUUID(value reflect.Value) bool {
if value.Kind() != reflect.Array || value.Type().Len() != 16 { if value.Kind() != reflect.Array || value.Type().Len() != 16 {
return false return false

View File

@ -54,7 +54,7 @@ func (s *sqlite3) DataTypeOf(field *StructField) string {
sqlType = "datetime" sqlType = "datetime"
} }
default: default:
if _, ok := dataValue.Interface().([]byte); ok { if IsByteArrayOrSlice(dataValue) {
sqlType = "blob" sqlType = "blob"
} }
} }

View File

@ -81,21 +81,21 @@ func (s *mssql) DataTypeOf(field *gorm.StructField) string {
case reflect.Float32, reflect.Float64: case reflect.Float32, reflect.Float64:
sqlType = "float" sqlType = "float"
case reflect.String: case reflect.String:
if size > 0 && size < 65532 { if size > 0 && size < 8000 {
sqlType = fmt.Sprintf("nvarchar(%d)", size) sqlType = fmt.Sprintf("nvarchar(%d)", size)
} else { } else {
sqlType = "text" sqlType = "nvarchar(max)"
} }
case reflect.Struct: case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok { if _, ok := dataValue.Interface().(time.Time); ok {
sqlType = "datetime2" sqlType = "datetime2"
} }
default: default:
if _, ok := dataValue.Interface().([]byte); ok { if gorm.IsByteArrayOrSlice(dataValue) {
if size > 0 && size < 65532 { if size > 0 && size < 8000 {
sqlType = fmt.Sprintf("varchar(%d)", size) sqlType = fmt.Sprintf("varbinary(%d)", size)
} else { } else {
sqlType = "text" sqlType = "varbinary(max)"
} }
} }
} }