Add support for LocalDate and Instant with DbArray

This commit is contained in:
robin.bygrave
2023-12-06 14:54:30 +13:00
parent f9c9bea623
commit 7cd9e5ee03
5 changed files with 97 additions and 2 deletions
@@ -1,9 +1,12 @@
package io.ebeaninternal.server.type;
import io.ebean.core.type.ScalarType;
import io.ebean.core.type.BasicTypeConverter;
import io.ebean.core.type.ScalarType;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.util.UUID;
/**
@@ -32,6 +35,8 @@ interface ArrayElementConverter<T> {
ArrayElementConverter<Double> DOUBLE = new DoubleConverter();
ArrayElementConverter<Float> FLOAT = new FloatConverter();
ArrayElementConverter<BigDecimal> BIG_DECIMAL = new BigDecimalConverter();
ArrayElementConverter<Instant> INSTANT = new InstantConverter();
ArrayElementConverter<LocalDate> LOCAL_DATE = new LocalDateConverter();
class LongConverter implements ArrayElementConverter<Long> {
@@ -101,6 +106,48 @@ interface ArrayElementConverter<T> {
}
}
class InstantConverter implements ArrayElementConverter<Instant> {
@Override
public Instant fromSerialized(Object rawValue) {
return fromDbArray(rawValue);
}
@Override
public Instant fromDbArray(Object rawValue) {
if (rawValue instanceof Instant) {
return (Instant) rawValue;
} else if (rawValue instanceof Timestamp) {
return ((Timestamp) rawValue).toInstant();
} else if (rawValue instanceof String) {
return Instant.parse((String) rawValue);
} else {
throw new IllegalStateException("Instant fromDbArray unsupported for " + rawValue);
}
}
}
class LocalDateConverter implements ArrayElementConverter<LocalDate> {
@Override
public LocalDate fromSerialized(Object rawValue) {
return fromDbArray(rawValue);
}
@Override
public LocalDate fromDbArray(Object rawValue) {
if (rawValue instanceof LocalDate) {
return (LocalDate) rawValue;
} else if (rawValue instanceof java.sql.Date) {
return ((java.sql.Date) rawValue).toLocalDate();
} else if (rawValue instanceof String) {
return LocalDate.parse((String) rawValue);
} else {
throw new IllegalStateException("Instant fromDbArray unsupported for " + rawValue);
}
}
}
class BigDecimalConverter implements ArrayElementConverter<BigDecimal> {
@Override
@@ -141,7 +188,7 @@ interface ArrayElementConverter<T> {
@Override
public java.util.UUID fromSerialized(Object rawValue) {
return java.util.UUID.fromString((String)rawValue);
return java.util.UUID.fromString((String) rawValue);
}
@Override
@@ -16,6 +16,8 @@ import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDate;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
@@ -63,6 +65,12 @@ class ScalarTypeArrayList extends ScalarTypeArrayBase<List> implements ScalarTyp
if (valueType.equals(String.class)) {
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayList(nullable, "varchar", DocPropertyType.TEXT, ArrayElementConverter.STRING));
}
if (valueType.equals(Instant.class)) {
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayList(nullable, "timestamptz", DocPropertyType.TEXT, ArrayElementConverter.INSTANT));
}
if (valueType.equals(LocalDate.class)) {
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayList(nullable, "date", DocPropertyType.TEXT, ArrayElementConverter.LOCAL_DATE));
}
throw new IllegalArgumentException("Type [" + valueType + "] not supported for @DbArray mapping");
} finally {
lock.unlock();
@@ -8,6 +8,8 @@ import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -58,6 +60,12 @@ final class ScalarTypeArrayListH2 extends ScalarTypeArrayList {
if (valueType.equals(String.class)) {
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayListH2(nullable, "varchar", DocPropertyType.TEXT, ArrayElementConverter.STRING));
}
if (valueType.equals(Instant.class)) {
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayListH2(nullable, "timestamp", DocPropertyType.TEXT, ArrayElementConverter.INSTANT));
}
if (valueType.equals(LocalDate.class)) {
return cache.computeIfAbsent(key, s -> new ScalarTypeArrayListH2(nullable, "date", DocPropertyType.TEXT, ArrayElementConverter.LOCAL_DATE));
}
throw new IllegalArgumentException("Type [" + valueType + "] not supported for @DbArray mapping");
} finally {
lock.unlock();