Bind LocalDate directly using JDBC driver to convert

This addresses issues with dates before 1900-01-01 with H2Database
This commit is contained in:
Rob Bygrave committed 2022-01-25 21:27:32 +13:00
1 parent 860aad11ce
commit 8d29a3a575
4 files changed
+43 -2

No files matched your search

@@ -73,6 +73,11 @@ public class RsetDataReader implements DataReader {
return rset.getObject(pos());
}
@Override
public <T> T getObject(Class<T> cls) throws SQLException {
return rset.getObject(pos(), cls);
}
@Override
public BigDecimal getBigDecimal() throws SQLException {
return rset.getBigDecimal(pos());
@@ -1,11 +1,14 @@
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;
/**
@@ -40,7 +43,7 @@ final class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate<LocalDate> {
@SuppressWarnings("deprecation")
@Override
public Date convertToDate(LocalDate value) {
return new Date(value .getYear() - 1900, value .getMonthOfYear() - 1, value .getDayOfMonth());
return new Date(value.getYear() - 1900, value.getMonthOfYear() - 1, value.getDayOfMonth());
}
@Override
@@ -54,9 +57,23 @@ final class ScalarTypeJodaLocalDate extends ScalarTypeBaseDate<LocalDate> {
@Override
public LocalDate toBeanType(Object value) {
if (value instanceof java.util.Date) {
return LocalDate.fromDateFields((java.util.Date)value);
return LocalDate.fromDateFields((java.util.Date) value);
}
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());
}
}
@@ -1,9 +1,12 @@
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;
@@ -61,4 +64,18 @@ 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);
}
}