mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-24 23:56:36 +00:00
Let enum arrays be saved as integers or strings (#1467)
* Let enums be saved as integers or strings as needed * Add tests for int and varchar @DbArray enums
This commit is contained in:
@@ -112,14 +112,14 @@ interface ArrayElementConverter<T> {
|
||||
|
||||
@Override
|
||||
public Object toElement(Object rawValue) {
|
||||
return scalarType.parse(rawValue.toString());
|
||||
return scalarType.toBeanType(rawValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object[] toDbArray(Object[] objects) {
|
||||
Object[] dbArray = new Object[objects.length];
|
||||
for (int i = 0; i < objects.length; i++) {
|
||||
dbArray[i] = scalarType.format(objects[i]);
|
||||
dbArray[i] = scalarType.toJdbcType(objects[i]);
|
||||
}
|
||||
return dbArray;
|
||||
}
|
||||
|
||||
@@ -60,7 +60,19 @@ public class ScalarTypeArrayList extends ScalarTypeJsonCollection<List> implemen
|
||||
|
||||
@Override
|
||||
public ScalarTypeArrayList typeForEnum(ScalarType<?> scalarType) {
|
||||
return new ScalarTypeArrayList("varchar", DocPropertyType.TEXT, new ArrayElementConverter.EnumConverter(scalarType));
|
||||
final String arrayType;
|
||||
switch (scalarType.getJdbcType()) {
|
||||
case Types.INTEGER:
|
||||
arrayType = "integer";
|
||||
break;
|
||||
case Types.VARCHAR:
|
||||
arrayType = "varchar";
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("JdbcType [" + scalarType.getJdbcType() + "] not supported for @DbArray mapping on set.");
|
||||
}
|
||||
|
||||
return new ScalarTypeArrayList(arrayType, scalarType.getDocType(), new ArrayElementConverter.EnumConverter(scalarType));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -54,13 +54,24 @@ public class ScalarTypeArraySet<T> extends ScalarTypeJsonCollection<Set<T>> impl
|
||||
if (valueType.equals(String.class)) {
|
||||
return STRING;
|
||||
}
|
||||
throw new IllegalArgumentException("Type [" + valueType + "] not supported for @DbArray mapping on set");
|
||||
throw new IllegalArgumentException("Type [" + valueType + "] not supported for @DbArray mapping on list");
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public ScalarTypeArraySet typeForEnum(ScalarType<?> scalarType) {
|
||||
return new ScalarTypeArraySet("varchar", DocPropertyType.TEXT, new ArrayElementConverter.EnumConverter(scalarType));
|
||||
final String arrayType;
|
||||
switch (scalarType.getJdbcType()) {
|
||||
case Types.INTEGER:
|
||||
arrayType = "integer";
|
||||
break;
|
||||
case Types.VARCHAR:
|
||||
arrayType = "varchar";
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("JdbcType [" + scalarType.getJdbcType() + "] not supported for @DbArray mapping on set.");
|
||||
}
|
||||
return new ScalarTypeArraySet(arrayType, scalarType.getDocType(), new ArrayElementConverter.EnumConverter(scalarType));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user