properly handle fields of type 'text' and better handling of numeric type definitions

This commit is contained in:
Jim Lambert 2020-02-14 09:21:09 -05:00
parent b126f0ab13
commit fe0ca50a57

View File

@ -139,14 +139,29 @@ func (s *oci8) DataTypeOf(field *gorm.StructField) string {
if sqlType == "" {
switch dataValue.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8,
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Uint, reflect.Uint8,
reflect.Uint16, reflect.Uintptr, reflect.Int64, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64:
if s.fieldCanAutoIncrement(field) {
sqlType = "NUMBER GENERATED BY DEFAULT AS IDENTITY"
} else {
sqlType = "NUMBER"
switch dataValue.Kind() {
case reflect.Int8,
reflect.Uint8,
reflect.Uintptr:
sqlType = "SHORTINTEGER"
case reflect.Int, reflect.Int16, reflect.Int32,
reflect.Uint, reflect.Uint16, reflect.Uint32:
sqlType = "INTEGER"
case reflect.Int64,
reflect.Uint64:
sqlType = "INTEGER"
default:
sqlType = "NUMBER"
}
}
case reflect.Bool:
sqlType = "INTEGER"
case reflect.String:
if _, ok := field.TagSettingsGet("SIZE"); !ok {
size = 0 // if SIZE haven't been set, use `text` as the default type, as there are no performance different
@ -164,24 +179,15 @@ func (s *oci8) DataTypeOf(field *gorm.StructField) string {
if _, ok := dataValue.Interface().(time.Time); ok {
sqlType = "TIMESTAMP WITH TIME ZONE"
}
case reflect.Map:
if dataValue.Type().Name() == "Hstore" {
sqlType = "hstore"
}
default:
if gorm.IsByteArrayOrSlice(dataValue) {
// switch {
// case size > 0 && size < 4000:
// sqlType = fmt.Sprintf("VARCHAR2(%d)", size)
// case size == 0:
// sqlType = "VARCHAR2 (4000)" // no size specified, so default to something that can be indexed
// default:
sqlType = "BLOB"
// }
}
}
}
if strings.EqualFold(sqlType, "text") {
sqlType = "CLOB"
}
if sqlType == "" {
panic(fmt.Sprintf("invalid sql type %s (%s) for oracle", dataValue.Type().Name(), dataValue.Kind().String()))
}