mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
#2788 - Support mapping @DbArray List<Float> to Postgres float4[]
This commit is contained in:
@@ -17,7 +17,7 @@ interface ArrayElementConverter<T> {
|
||||
T fromSerialized(Object rawValue);
|
||||
|
||||
/**
|
||||
* Convert the array element from it's DB array form.
|
||||
* Convert the array element from its DB array form.
|
||||
*/
|
||||
T fromDbArray(Object rawValue);
|
||||
|
||||
@@ -25,31 +25,12 @@ interface ArrayElementConverter<T> {
|
||||
return objects;
|
||||
}
|
||||
|
||||
/**
|
||||
* The UUID converter implementation.
|
||||
*/
|
||||
ArrayElementConverter<UUID> UUID = new UuidConverter();
|
||||
|
||||
/**
|
||||
* The String converter implementation.
|
||||
*/
|
||||
ArrayElementConverter<String> STRING = new StringConverter();
|
||||
|
||||
/**
|
||||
* The Long converter implementation.
|
||||
*/
|
||||
ArrayElementConverter<Long> LONG = new LongConverter();
|
||||
|
||||
/**
|
||||
* The Integer converter implementation.
|
||||
*/
|
||||
ArrayElementConverter<Integer> INTEGER = new IntegerConverter();
|
||||
|
||||
/**
|
||||
* The Double converter implementation.
|
||||
*/
|
||||
ArrayElementConverter<Double> DOUBLE = new DoubleConverter();
|
||||
|
||||
ArrayElementConverter<Float> FLOAT = new FloatConverter();
|
||||
ArrayElementConverter<BigDecimal> BIG_DECIMAL = new BigDecimalConverter();
|
||||
|
||||
class LongConverter implements ArrayElementConverter<Long> {
|
||||
@@ -86,6 +67,23 @@ interface ArrayElementConverter<T> {
|
||||
}
|
||||
}
|
||||
|
||||
class FloatConverter implements ArrayElementConverter<Float> {
|
||||
|
||||
@Override
|
||||
public Float fromSerialized(Object rawValue) {
|
||||
return fromDbArray(rawValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Float fromDbArray(Object rawValue) {
|
||||
if (rawValue instanceof Float) {
|
||||
return (Float) rawValue;
|
||||
} else {
|
||||
return ((Number) rawValue).floatValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class DoubleConverter implements ArrayElementConverter<Double> {
|
||||
|
||||
@Override
|
||||
|
||||
@@ -57,6 +57,9 @@ class ScalarTypeArrayList extends ScalarTypeArrayBase<List> implements ScalarTyp
|
||||
if (valueType.equals(Integer.class)) {
|
||||
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayList(nullable, "integer", DocPropertyType.INTEGER, ArrayElementConverter.INTEGER));
|
||||
}
|
||||
if (valueType.equals(Float.class)) {
|
||||
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayList(nullable, "float4", DocPropertyType.DOUBLE, ArrayElementConverter.FLOAT));
|
||||
}
|
||||
if (valueType.equals(Double.class)) {
|
||||
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayList(nullable, "float", DocPropertyType.DOUBLE, ArrayElementConverter.DOUBLE));
|
||||
}
|
||||
|
||||
@@ -48,6 +48,9 @@ final class ScalarTypeArrayListH2 extends ScalarTypeArrayList {
|
||||
if (valueType.equals(Integer.class)) {
|
||||
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayListH2(nullable, "integer", DocPropertyType.INTEGER, ArrayElementConverter.INTEGER));
|
||||
}
|
||||
if (valueType.equals(Float.class)) {
|
||||
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayListH2(nullable, "real", DocPropertyType.DOUBLE, ArrayElementConverter.FLOAT));
|
||||
}
|
||||
if (valueType.equals(Double.class)) {
|
||||
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayListH2(nullable, "float", DocPropertyType.DOUBLE, ArrayElementConverter.DOUBLE));
|
||||
}
|
||||
|
||||
@@ -41,6 +41,11 @@ public class EArrayBean {
|
||||
@DbArray
|
||||
List<BigDecimal> decimals;
|
||||
|
||||
@DbArray
|
||||
List<Double> doubs;
|
||||
@DbArray
|
||||
List<Float> floats;
|
||||
|
||||
@DbArray
|
||||
List<Status> statuses;
|
||||
|
||||
@@ -112,6 +117,22 @@ public class EArrayBean {
|
||||
this.decimals = decimals;
|
||||
}
|
||||
|
||||
public List<Double> getDoubs() {
|
||||
return doubs;
|
||||
}
|
||||
|
||||
public void setDoubs(List<Double> doubs) {
|
||||
this.doubs = doubs;
|
||||
}
|
||||
|
||||
public List<Float> getFloats() {
|
||||
return floats;
|
||||
}
|
||||
|
||||
public void setFloats(List<Float> floats) {
|
||||
this.floats = floats;
|
||||
}
|
||||
|
||||
public List<Status> getStatuses() {
|
||||
return statuses;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,8 @@ class TestDbArray_basic extends BaseTestCase {
|
||||
bean.getOtherIds().add(96L);
|
||||
bean.getOtherIds().add(97L);
|
||||
bean.setDecimals(doubles);
|
||||
bean.setDoubs(List.of(1.1d, 2.3d));
|
||||
bean.setFloats(List.of(1.01f, 2.02f));
|
||||
bean.setStatuses(new ArrayList<>());
|
||||
bean.getStatuses().add(EArrayBean.Status.ONE);
|
||||
bean.getStatuses().add(EArrayBean.Status.THREE);
|
||||
@@ -60,6 +62,8 @@ class TestDbArray_basic extends BaseTestCase {
|
||||
found = DB.find(EArrayBean.class, bean.getId());
|
||||
|
||||
assertThat(found.getPhoneNumbers()).containsExactly("4321", "9823");
|
||||
assertThat(found.getDoubs()).hasSize(2);
|
||||
assertThat(found.getFloats()).hasSize(2);
|
||||
|
||||
if (isPostgresCompatible()) {
|
||||
Query<EArrayBean> query = DB.find(EArrayBean.class)
|
||||
@@ -277,9 +281,9 @@ class TestDbArray_basic extends BaseTestCase {
|
||||
List<String> phNumbers = bean.getPhoneNumbers();
|
||||
phNumbers.add("4321");
|
||||
phNumbers.add("9823");
|
||||
List<BigDecimal> doubs = bean.getDecimals();
|
||||
doubs.add(BigDecimal.valueOf(1.23));
|
||||
doubs.add(BigDecimal.valueOf(4.56));
|
||||
List<BigDecimal> decimals = bean.getDecimals();
|
||||
decimals.add(BigDecimal.valueOf(1.23));
|
||||
decimals.add(BigDecimal.valueOf(4.56));
|
||||
DB.save(bean);
|
||||
// Data is saved correctly
|
||||
|
||||
|
||||
Reference in New Issue
Block a user