DB2: Fix: DataBind/Read of native LocalDates

This commit is contained in:
Roland Praml committed 2022-01-26 16:49:12 +01:00
1 parent 0504b88923
commit 7c05ed6876
7 files changed
+103 -34

No files matched your search

@@ -763,7 +763,11 @@ public final class DefaultTypeManager implements TypeManager {
typeMap.put(java.nio.file.Path.class, new ScalarTypePath());
addType(java.time.Period.class, new ScalarTypePeriod());
addType(java.time.LocalDate.class, new ScalarTypeLocalDate(jsonDate));
if (config.getDatabasePlatform().supportsNativeJavaTime()) {
addType(java.time.LocalDate.class, new ScalarTypeLocalDateNative(jsonDate));
} else {
addType(java.time.LocalDate.class, new ScalarTypeLocalDate(jsonDate));
}
addType(java.time.LocalDateTime.class, new ScalarTypeLocalDateTime(jsonDateTime));
addType(OffsetDateTime.class, new ScalarTypeOffsetDateTime(jsonDateTime, zoneId));
addType(ZonedDateTime.class, new ScalarTypeZonedDateTime(jsonDateTime, zoneId));
@@ -803,7 +807,11 @@ public final class DefaultTypeManager implements TypeManager {
log.debug("Registering Joda data types");
addType(LocalDateTime.class, new ScalarTypeJodaLocalDateTime(jsonDateTime));
addType(DateTime.class, new ScalarTypeJodaDateTime(jsonDateTime));
addType(LocalDate.class, new ScalarTypeJodaLocalDate(jsonDate));
if (config.getDatabasePlatform().supportsNativeJavaTime()) {
addType(LocalDate.class, new ScalarTypeJodaLocalDateNative(jsonDate));
} else {
addType(LocalDate.class, new ScalarTypeJodaLocalDate(jsonDate));
}
addType(org.joda.time.DateMidnight.class, new ScalarTypeJodaDateMidnight(jsonDate));
addType(org.joda.time.Period.class, new ScalarTypeJodaPeriod());
@@ -14,7 +14,7 @@ import java.sql.Types;
/**
* ScalarType for Joda LocalDate. This maps to a JDBC Date.
*/
final class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate<LocalDate> {
class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate<LocalDate> {
ScalarTypeJodaLocalDate(JsonConfig.Date mode) {
super(mode, LocalDate.class, false, Types.DATE);
@@ -61,19 +61,4 @@ final class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate<LocalDate> {
}
return (LocalDate) value;
}
@Override
public void bind(DataBinder binder, LocalDate value) throws SQLException {
if (value == null) {
binder.setNull(Types.DATE);
} else {
binder.setObject(java.time.LocalDate.of(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth()));
}
}
@Override
public LocalDate read(DataReader reader) throws SQLException {
java.time.LocalDate jtDate = reader.getObject(java.time.LocalDate.class);
return jtDate == null ? null : new org.joda.time.LocalDate(jtDate.getYear(), jtDate.getMonthValue(), jtDate.getDayOfMonth());
}
}
@@ -0,0 +1,37 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebeaninternal.server.core.BasicTypeConverter;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Types;
/**
* ScalarType for Joda LocalDate. This maps to a LocalDate. Not all drivers/platforms may support this.
*/
final class ScalarTypeJodaLocalDateNative extends ScalarTypeJodaLocalDate{
ScalarTypeJodaLocalDateNative(JsonConfig.Date mode) {
super(mode);
}
@Override
public void bind(DataBinder binder, LocalDate value) throws SQLException {
if (value == null) {
binder.setNull(Types.DATE);
} else {
binder.setObject(java.time.LocalDate.of(value.getYear(), value.getMonthOfYear(), value.getDayOfMonth()));
}
}
@Override
public LocalDate read(DataReader reader) throws SQLException {
java.time.LocalDate jtDate = reader.getObject(java.time.LocalDate.class);
return jtDate == null ? null : new org.joda.time.LocalDate(jtDate.getYear(), jtDate.getMonthValue(), jtDate.getDayOfMonth());
}
}
@@ -16,7 +16,7 @@ import java.time.ZonedDateTime;
/**
* ScalarType for java.time.LocalDate. This maps to a JDBC Date.
*/
final class ScalarTypeLocalDate extends ScalarTypeBaseDate<LocalDate> {
class ScalarTypeLocalDate extends ScalarTypeBaseDate<LocalDate> {
ScalarTypeLocalDate(JsonConfig.Date mode) {
super(mode, LocalDate.class, false, Types.DATE);
@@ -63,19 +63,4 @@ final class ScalarTypeLocalDate extends ScalarTypeBaseDate<LocalDate> {
}
return (LocalDate) value;
}
@Override
public void bind(DataBinder binder, LocalDate value) throws SQLException {
if (value == null) {
binder.setNull(Types.DATE);
} else {
binder.setObject(value);
}
}
@Override
public LocalDate read(DataReader reader) throws SQLException {
return reader.getObject(LocalDate.class);
}
}
@@ -0,0 +1,39 @@
package io.ebeaninternal.server.type;
import io.ebean.config.JsonConfig;
import io.ebean.core.type.DataBinder;
import io.ebean.core.type.DataReader;
import io.ebeaninternal.server.core.BasicTypeConverter;
import java.sql.Date;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
/**
* ScalarType for java.time.LocalDate. This maps to a LocalDate. Not all drivers/platforms may support this.
*/
final class ScalarTypeLocalDateNative extends ScalarTypeLocalDate {
ScalarTypeLocalDateNative(JsonConfig.Date mode) {
super(mode);
}
@Override
public void bind(DataBinder binder, LocalDate value) throws SQLException {
if (value == null) {
binder.setNull(Types.DATE);
} else {
binder.setObject(value);
}
}
@Override
public LocalDate read(DataReader reader) throws SQLException {
return reader.getObject(LocalDate.class);
}
}