mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 14:58:47 +00:00
WIP Java8 types - ScalarTypeDuration etc
This commit is contained in:
1 parent
b71b0fc8e2
commit
fd4d757f87
44 files changed
+981
-145
No files matched your search
@@ -414,7 +414,7 @@ public class DeployBeanProperty {
|
||||
ScalarTypeEnum etype = (ScalarTypeEnum) scalarType;
|
||||
|
||||
// check dbColName IN ('A', 'I', 'D')
|
||||
return "check (" + dbColumn + " in " + etype.getContraintInValues() + ")";
|
||||
return "check (" + dbColumn + " in " + etype.getConstraintInValues() + ")";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ public final class DecimalUtils {
|
||||
public static Instant toInstant(BigDecimal value) {
|
||||
|
||||
long seconds = value.longValue();
|
||||
int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds);
|
||||
int nanoseconds = extractNanosecondDecimal(value, seconds);
|
||||
return Instant.ofEpochSecond(seconds, nanoseconds);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ import java.sql.Date;
|
||||
import java.sql.Time;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Calendar;
|
||||
@@ -144,7 +144,7 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
this.extraTypeFactory = new DefaultTypeFactory(config);
|
||||
|
||||
initialiseStandard(jsonDateTime, clobType, blobType, config.isUuidStoreAsBinary());
|
||||
initialiseJavaTimeTypes(jsonDateTime);
|
||||
initialiseJavaTimeTypes(jsonDateTime, config);
|
||||
initialiseJodaTypes(jsonDateTime);
|
||||
|
||||
if (bootupClasses != null) {
|
||||
@@ -599,12 +599,28 @@ public final class DefaultTypeManager implements TypeManager, KnownImmutable {
|
||||
return propParamTypes[1];
|
||||
}
|
||||
|
||||
protected void initialiseJavaTimeTypes(JsonConfig.DateTime mode) {
|
||||
protected void initialiseJavaTimeTypes(JsonConfig.DateTime mode, ServerConfig config) {
|
||||
if (ClassUtil.isPresent("java.time.LocalDate", this.getClass())) {
|
||||
logger.debug("Registering java.time data types");
|
||||
typeMap.put(java.time.LocalDate.class, new ScalarTypeLocalDate());
|
||||
typeMap.put(java.time.LocalDateTime.class, new ScalarTypeLocalDateTime(mode));
|
||||
typeMap.put(OffsetDateTime.class, new ScalarTypeOffsetDateTime(mode));
|
||||
typeMap.put(ZonedDateTime.class, new ScalarTypeZonedDateTime(mode));
|
||||
typeMap.put(Instant.class, new ScalarTypeInstant(mode));
|
||||
|
||||
typeMap.put(DayOfWeek.class, new ScalarTypeDayOfWeek());
|
||||
typeMap.put(Month.class, new ScalarTypeMonth());
|
||||
typeMap.put(Year.class, new ScalarTypeYear());
|
||||
typeMap.put(YearMonth.class, new ScalarTypeYearMonthDate());
|
||||
typeMap.put(OffsetTime.class, new ScalarTypeOffsetTime());
|
||||
typeMap.put(ZoneId.class, new ScalarTypeZoneId());
|
||||
typeMap.put(ZoneOffset.class, new ScalarTypeZoneOffset());
|
||||
|
||||
boolean localTimeNanos = config.isLocalTimeWithNanos();
|
||||
typeMap.put(java.time.LocalTime.class, (localTimeNanos)? new ScalarTypeLocalTimeWithNanos() : new ScalarTypeLocalTime());
|
||||
|
||||
boolean durationNanos = config.isDurationWithNanos();
|
||||
typeMap.put(Duration.class, (durationNanos)? new ScalarTypeDurationWithNanos() : new ScalarTypeDuration());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,12 @@ public class EnumToDbIntegerMap extends EnumToDbValueMap<Integer> {
|
||||
return Types.INTEGER;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumToDbIntegerMap add(Object beanValue, Integer dbValue) {
|
||||
addInternal(beanValue, dbValue);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public EnumToDbIntegerMap add(Object beanValue, String stringDbValue) {
|
||||
|
||||
try {
|
||||
|
||||
@@ -23,7 +23,7 @@ public abstract class ScalarTypeBaseDate<T> extends ScalarTypeBase<T> {
|
||||
/**
|
||||
* Convert the target value to millis.
|
||||
*/
|
||||
public abstract long convertToMillis(Object value);
|
||||
public abstract long convertToMillis(T value);
|
||||
|
||||
/**
|
||||
* Convert to java.sql.Date from the target Date type.
|
||||
|
||||
@@ -93,9 +93,8 @@ public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
|
||||
}
|
||||
case VALUE_NUMBER_FLOAT: {
|
||||
BigDecimal value = ctx.getDecimalValue();
|
||||
//Instant instant = DecimalUtils.toInstant(value);
|
||||
Timestamp timestamp = DecimalUtils.toTimestamp(value);
|
||||
convertFromTimestamp(timestamp);//Timestamp.from(instant));
|
||||
return convertFromTimestamp(timestamp);
|
||||
}
|
||||
default: {
|
||||
String jsonDateTime = ctx.getText();
|
||||
@@ -109,10 +108,12 @@ public abstract class ScalarTypeBaseDateTime<T> extends ScalarTypeBase<T> {
|
||||
|
||||
switch (mode) {
|
||||
case ISO8601: {
|
||||
generator.writeNumber(toJsonISO8601(value));
|
||||
generator.writeFieldName(name);
|
||||
generator.writeString(toJsonISO8601(value));
|
||||
break;
|
||||
}
|
||||
case NANOS: {
|
||||
generator.writeFieldName(name);
|
||||
generator.writeNumber(toJsonNanos(value));
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -16,8 +16,8 @@ public class ScalarTypeDate extends ScalarTypeBaseDate<java.sql.Date> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((Date) value).getTime();
|
||||
public long convertToMillis(Date value) {
|
||||
return value.getTime();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
|
||||
/**
|
||||
* ScalarType mapping for Month enum.
|
||||
*/
|
||||
public class ScalarTypeDayOfWeek extends ScalarTypeEnumWithMapping {
|
||||
|
||||
static final EnumToDbIntegerMap beanDbMap = new EnumToDbIntegerMap();
|
||||
|
||||
static {
|
||||
DayOfWeek[] values = DayOfWeek.values();
|
||||
for (DayOfWeek value : values) {
|
||||
beanDbMap.add(value, value.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public ScalarTypeDayOfWeek() {
|
||||
super(beanDbMap, DayOfWeek.class, 1);
|
||||
}
|
||||
}
|
||||
@@ -15,7 +15,7 @@ import java.sql.Types;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* ScalarType for java.time.Duration
|
||||
* ScalarType for java.time.Duration (with seconds precision).
|
||||
*/
|
||||
public class ScalarTypeDuration extends ScalarTypeBase<Duration> {
|
||||
|
||||
@@ -81,7 +81,6 @@ public class ScalarTypeDuration extends ScalarTypeBase<Duration> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String formatValue(Duration v) {
|
||||
return v.toString();
|
||||
|
||||
@@ -15,7 +15,7 @@ import java.sql.Types;
|
||||
import java.time.Duration;
|
||||
|
||||
/**
|
||||
* ScalarType for java.time.Duration with Nanos precision.
|
||||
* ScalarType for java.time.Duration (with Nanos precision).
|
||||
* <p>
|
||||
* Stored in the DB as DECIMAL value.
|
||||
* </p>
|
||||
|
||||
@@ -8,6 +8,6 @@ public interface ScalarTypeEnum {
|
||||
/**
|
||||
* Return the IN values for DB constraint construction.
|
||||
*/
|
||||
public String getContraintInValues();
|
||||
public String getConstraintInValues();
|
||||
|
||||
}
|
||||
@@ -41,7 +41,7 @@ public class ScalarTypeEnumStandard {
|
||||
/**
|
||||
* Return the IN values for DB constraint construction.
|
||||
*/
|
||||
public String getContraintInValues() {
|
||||
public String getConstraintInValues() {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
@@ -129,7 +129,7 @@ public class ScalarTypeEnumStandard {
|
||||
/**
|
||||
* Return the IN values for DB constraint construction.
|
||||
*/
|
||||
public String getContraintInValues() {
|
||||
public String getConstraintInValues() {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ public class ScalarTypeEnumWithMapping extends ScalarTypeEnumStandard.EnumBase i
|
||||
/**
|
||||
* Return the IN values for DB constraint construction.
|
||||
*/
|
||||
public String getContraintInValues() {
|
||||
public String getConstraintInValues() {
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
|
||||
@@ -20,8 +20,8 @@ public class ScalarTypeJodaDateMidnight extends ScalarTypeBaseDate<DateMidnight>
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((DateMidnight) value).getMillis();
|
||||
public long convertToMillis(DateMidnight value) {
|
||||
return value.getMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -17,8 +17,8 @@ public class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate<LocalDate> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
return ((LocalDate) value).toDateMidnight().getMillis();
|
||||
public long convertToMillis(LocalDate value) {
|
||||
return value.toDateMidnight().getMillis();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -19,9 +19,8 @@ public class ScalarTypeLocalDate extends ScalarTypeBaseDate<LocalDate> {
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
LocalDate localDate = (LocalDate) value;
|
||||
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
|
||||
public long convertToMillis(LocalDate value) {
|
||||
ZonedDateTime zonedDateTime = value.atStartOfDay(ZoneId.systemDefault());
|
||||
return zonedDateTime.toInstant().toEpochMilli();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
@@ -34,7 +35,7 @@ public class ScalarTypeLocalDateTime extends ScalarTypeBaseDateTime<LocalDateTim
|
||||
|
||||
@Override
|
||||
protected String toJsonISO8601(LocalDateTime value) {
|
||||
return value.toString();
|
||||
return value.atZone(ZoneId.systemDefault()).toInstant().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.time.Month;
|
||||
|
||||
/**
|
||||
* ScalarType mapping for Month enum.
|
||||
*/
|
||||
public class ScalarTypeMonth extends ScalarTypeEnumWithMapping {
|
||||
|
||||
static final EnumToDbIntegerMap beanDbMap = new EnumToDbIntegerMap();
|
||||
|
||||
static {
|
||||
Month[] values = Month.values();
|
||||
for (Month value : values) {
|
||||
beanDbMap.add(value, value.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public ScalarTypeMonth() {
|
||||
super(beanDbMap, Month.class, 1);
|
||||
}
|
||||
}
|
||||
@@ -24,7 +24,7 @@ public class ScalarTypeOffsetDateTime extends ScalarTypeBaseDateTime<OffsetDateT
|
||||
|
||||
@Override
|
||||
protected String toJsonISO8601(OffsetDateTime value) {
|
||||
return value.toString();
|
||||
return value.toInstant().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.time.OffsetTime;
|
||||
|
||||
/**
|
||||
* ScalarType for java.time.OffsetTime stored as DB VARCHAR
|
||||
*/
|
||||
public class ScalarTypeOffsetTime extends ScalarTypeBaseVarchar<OffsetTime> {
|
||||
|
||||
public ScalarTypeOffsetTime() {
|
||||
super(OffsetTime.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return 25;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(OffsetTime v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetTime parse(String value) {
|
||||
return OffsetTime.parse(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OffsetTime convertFromDbString(String dbValue) {
|
||||
return OffsetTime.parse(dbValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(OffsetTime beanValue) {
|
||||
return beanValue.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -91,12 +91,10 @@ public class ScalarTypeUtilDate {
|
||||
|
||||
|
||||
@Override
|
||||
public long convertToMillis(Object value) {
|
||||
java.sql.Date date = BasicTypeConverter.toDate(value);
|
||||
return date.getTime();
|
||||
public long convertToMillis(java.util.Date value) {
|
||||
return value.getTime();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Date convertFromDate(java.sql.Date ts) {
|
||||
return new java.util.Date(ts.getTime());
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
import java.sql.Date;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.LocalDate;
|
||||
import java.time.YearMonth;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
* ScalarType for java.time.LocalDate. This maps to a JDBC Date.
|
||||
*/
|
||||
public class ScalarTypeYearMonthDate extends ScalarTypeBaseDate<YearMonth> {
|
||||
|
||||
public ScalarTypeYearMonthDate() {
|
||||
super(YearMonth.class, false, Types.DATE);
|
||||
}
|
||||
|
||||
protected LocalDate toLocalDate(YearMonth yearMonth) {
|
||||
return LocalDate.of(yearMonth.getYear(), yearMonth.getMonth(), 1);
|
||||
}
|
||||
|
||||
protected YearMonth fromLocalDate(LocalDate localDate) {
|
||||
return YearMonth.of(localDate.getYear(), localDate.getMonth());
|
||||
}
|
||||
|
||||
@Override
|
||||
public YearMonth convertFromMillis(long systemTimeMillis) {
|
||||
return fromLocalDate(new Timestamp(systemTimeMillis).toLocalDateTime().toLocalDate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(YearMonth value) {
|
||||
LocalDate localDate = toLocalDate(value);
|
||||
ZonedDateTime zonedDateTime = localDate.atStartOfDay(ZoneId.systemDefault());
|
||||
return zonedDateTime.toInstant().toEpochMilli();
|
||||
}
|
||||
|
||||
@Override
|
||||
public YearMonth convertFromDate(Date ts) {
|
||||
return fromLocalDate(ts.toLocalDate());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Date convertToDate(YearMonth t) {
|
||||
return Date.valueOf(toLocalDate(t));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Date) return value;
|
||||
if (value instanceof YearMonth) return Date.valueOf(toLocalDate((YearMonth) value));
|
||||
if (value instanceof LocalDate) return Date.valueOf((LocalDate)value);
|
||||
return BasicTypeConverter.toDate(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public YearMonth toBeanType(Object value) {
|
||||
if (value instanceof YearMonth) return (YearMonth) value;
|
||||
if (value instanceof LocalDate) return fromLocalDate((LocalDate) value);
|
||||
return fromLocalDate(BasicTypeConverter.toDate(value).toLocalDate());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.time.OffsetTime;
|
||||
import java.time.ZoneId;
|
||||
|
||||
/**
|
||||
* ScalarType for java.time.OffsetTime stored as DB VARCHAR
|
||||
*/
|
||||
public class ScalarTypeZoneId extends ScalarTypeBaseVarchar<ZoneId> {
|
||||
|
||||
public ScalarTypeZoneId() {
|
||||
super(ZoneId.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return 60;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(ZoneId v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZoneId parse(String value) {
|
||||
return ZoneId.of(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZoneId convertFromDbString(String dbValue) {
|
||||
return ZoneId.of(dbValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(ZoneId beanValue) {
|
||||
return beanValue.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
/**
|
||||
* ScalarType for java.time.OffsetTime stored as DB VARCHAR
|
||||
*/
|
||||
public class ScalarTypeZoneOffset extends ScalarTypeBaseVarchar<ZoneOffset> {
|
||||
|
||||
public ScalarTypeZoneOffset() {
|
||||
super(ZoneOffset.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getLength() {
|
||||
return 60;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(ZoneOffset v) {
|
||||
return v.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZoneOffset parse(String value) {
|
||||
return ZoneOffset.of(value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZoneOffset convertFromDbString(String dbValue) {
|
||||
return ZoneOffset.of(dbValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String convertToDbString(ZoneOffset beanValue) {
|
||||
return beanValue.toString();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.avaje.ebeaninternal.server.type;
|
||||
|
||||
import com.avaje.ebean.config.JsonConfig;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneId;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
/**
|
||||
* ScalarType for ZonedDateTime.
|
||||
*/
|
||||
public class ScalarTypeZonedDateTime extends ScalarTypeBaseDateTime<ZonedDateTime> {
|
||||
|
||||
public ScalarTypeZonedDateTime(JsonConfig.DateTime mode) {
|
||||
super(mode, ZonedDateTime.class, true, Types.TIMESTAMP);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String toJsonNanos(ZonedDateTime value) {
|
||||
return toJsonNanos(value.toEpochSecond(), value.getNano());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String toJsonISO8601(ZonedDateTime value) {
|
||||
return value.toInstant().toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public long convertToMillis(ZonedDateTime value) {
|
||||
return value.toInstant().toEpochMilli();
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZonedDateTime convertFromMillis(long systemTimeMillis) {
|
||||
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(systemTimeMillis), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZonedDateTime convertFromTimestamp(Timestamp ts) {
|
||||
return ZonedDateTime.ofInstant(ts.toInstant(), ZoneId.systemDefault());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Timestamp convertToTimestamp(ZonedDateTime t) {
|
||||
return Timestamp.from(t.toInstant());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object toJdbcType(Object value) {
|
||||
if (value instanceof Timestamp) return value;
|
||||
return convertToTimestamp((ZonedDateTime)value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ZonedDateTime toBeanType(Object value) {
|
||||
if (value instanceof ZonedDateTime) return (ZonedDateTime) value;
|
||||
return convertFromTimestamp((Timestamp)value);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user