Skip to content

Commit

Permalink
Adding better binary type support for common SQL dialects
Browse files Browse the repository at this point in the history
  • Loading branch information
Rob Rodriguez authored and Rob Rodriguez committed Apr 19, 2017
1 parent 403487d commit bae0799
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 13 deletions.
5 changes: 5 additions & 0 deletions dialect_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,8 @@ func (DefaultForeignKeyNamer) BuildForeignKeyName(tableName, field, dest string)
keyName = regexp.MustCompile("(_*[^a-zA-Z]+_*|_+)").ReplaceAllString(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))
}
2 changes: 1 addition & 1 deletion dialect_mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (s *mysql) DataTypeOf(field *StructField) string {
}
}
default:
if _, ok := dataValue.Interface().([]byte); ok {
if IsByteArrayOrSlice(dataValue) {
if size > 0 && size < 65532 {
sqlType = fmt.Sprintf("varbinary(%d)", size)
} else {
Expand Down
6 changes: 1 addition & 5 deletions dialect_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *postgres) DataTypeOf(field *StructField) string {
sqlType = "hstore"
}
default:
if isByteArrayOrSlice(dataValue) {
if IsByteArrayOrSlice(dataValue) {
sqlType = "bytea"
} else if isUUID(dataValue) {
sqlType = "uuid"
Expand Down Expand Up @@ -120,10 +120,6 @@ func (postgres) SupportLastInsertID() bool {
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 {
if value.Kind() != reflect.Array || value.Type().Len() != 16 {
return false
Expand Down
2 changes: 1 addition & 1 deletion dialect_sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (s *sqlite3) DataTypeOf(field *StructField) string {
sqlType = "datetime"
}
default:
if _, ok := dataValue.Interface().([]byte); ok {
if IsByteArrayOrSlice(dataValue) {
sqlType = "blob"
}
}
Expand Down
12 changes: 6 additions & 6 deletions dialects/mssql/mssql.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,21 @@ func (s *mssql) DataTypeOf(field *gorm.StructField) string {
case reflect.Float32, reflect.Float64:
sqlType = "float"
case reflect.String:
if size > 0 && size < 65532 {
if size > 0 && size < 8000 {
sqlType = fmt.Sprintf("nvarchar(%d)", size)
} else {
sqlType = "text"
sqlType = "nvarchar(max)"
}
case reflect.Struct:
if _, ok := dataValue.Interface().(time.Time); ok {
sqlType = "datetime2"
}
default:
if _, ok := dataValue.Interface().([]byte); ok {
if size > 0 && size < 65532 {
sqlType = fmt.Sprintf("varchar(%d)", size)
if gorm.IsByteArrayOrSlice(dataValue) {
if size > 0 && size < 8000 {
sqlType = fmt.Sprintf("varbinary(%d)", size)
} else {
sqlType = "text"
sqlType = "varbinary(max)"
}
}
}
Expand Down

0 comments on commit bae0799

Please sign in to comment.