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 == "" { if sqlType == "" {
switch dataValue.Kind() { 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.Uint16, reflect.Uintptr, reflect.Int64, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64: reflect.Float32, reflect.Float64:
if s.fieldCanAutoIncrement(field) { if s.fieldCanAutoIncrement(field) {
sqlType = "NUMBER GENERATED BY DEFAULT AS IDENTITY" sqlType = "NUMBER GENERATED BY DEFAULT AS IDENTITY"
} else { } else {
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" sqlType = "NUMBER"
} }
}
case reflect.Bool:
sqlType = "INTEGER"
case reflect.String: case reflect.String:
if _, ok := field.TagSettingsGet("SIZE"); !ok { 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 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 { if _, ok := dataValue.Interface().(time.Time); ok {
sqlType = "TIMESTAMP WITH TIME ZONE" sqlType = "TIMESTAMP WITH TIME ZONE"
} }
case reflect.Map:
if dataValue.Type().Name() == "Hstore" {
sqlType = "hstore"
}
default: default:
if gorm.IsByteArrayOrSlice(dataValue) { 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" sqlType = "BLOB"
// }
} }
} }
} }
if strings.EqualFold(sqlType, "text") {
sqlType = "CLOB"
}
if sqlType == "" { if sqlType == "" {
panic(fmt.Sprintf("invalid sql type %s (%s) for oracle", dataValue.Type().Name(), dataValue.Kind().String())) panic(fmt.Sprintf("invalid sql type %s (%s) for oracle", dataValue.Type().Name(), dataValue.Kind().String()))
} }