mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-22 15:10:02 +00:00
Refactor move ScalarTypeBaseDate to ebean-core-type module
This commit is contained in:
1 parent
5b22506b9b
commit
09e20934d7
7 files changed
+12
-6
No files matched your search
@@ -1,135 +0,0 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.core.JsonParser;
|
||||
import com.fasterxml.jackson.core.JsonToken;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.core.type.DataBinder;
|
||||
import io.ebean.core.type.DataReader;
|
||||
import io.ebean.core.type.DocPropertyType;
|
||||
import io.ebean.core.type.ScalarTypeBase;
|
||||
|
||||
import java.io.DataInput;
|
||||
import java.io.DataOutput;
|
||||
import java.io.IOException;
|
||||
import java.sql.Date;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
|
||||
/**
|
||||
* Base class for Date types.
|
||||
*/
|
||||
abstract class ScalarTypeBaseDate<T> extends ScalarTypeBase<T> {
|
||||
|
||||
protected final JsonConfig.Date mode;
|
||||
|
||||
ScalarTypeBaseDate(JsonConfig.Date mode, Class<T> type, boolean jdbcNative, int jdbcType) {
|
||||
super(type, jdbcNative, jdbcType);
|
||||
this.mode = mode;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the target value to millis.
|
||||
*/
|
||||
public abstract long convertToMillis(T value);
|
||||
|
||||
/**
|
||||
* Convert to java.sql.Date from the target Date type.
|
||||
*/
|
||||
public abstract java.sql.Date convertToDate(T t);
|
||||
|
||||
/**
|
||||
* Convert from java.sql.Date to the target Date type.
|
||||
*/
|
||||
public abstract T convertFromDate(java.sql.Date ts);
|
||||
|
||||
@Override
|
||||
public void bind(DataBinder binder, T value) throws SQLException {
|
||||
if (value == null) {
|
||||
binder.setNull(Types.DATE);
|
||||
} else {
|
||||
binder.setDate(convertToDate(value));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T read(DataReader reader) throws SQLException {
|
||||
Date ts = reader.getDate();
|
||||
return ts == null ? null : convertFromDate(ts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String formatValue(T t) {
|
||||
Date date = convertToDate(t);
|
||||
// format all dates into epoch millis
|
||||
long epochMillis = date.getTime();
|
||||
return Long.toString(epochMillis);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T parse(String value) {
|
||||
try {
|
||||
long epochMillis = Long.parseLong(value);
|
||||
return convertFromDate(new Date(epochMillis));
|
||||
} catch (NumberFormatException e) {
|
||||
Date date = Date.valueOf(value);
|
||||
return convertFromDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
public T convertFromMillis(long systemTimeMillis) {
|
||||
Date ts = new Date(systemTimeMillis);
|
||||
return convertFromDate(ts);
|
||||
}
|
||||
|
||||
@Override
|
||||
public T jsonRead(JsonParser parser) throws IOException {
|
||||
if (JsonToken.VALUE_NUMBER_INT == parser.getCurrentToken()) {
|
||||
return convertFromMillis(parser.getLongValue());
|
||||
} else {
|
||||
return convertFromDate(Date.valueOf(parser.getText()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void jsonWrite(JsonGenerator writer, T value) throws IOException {
|
||||
if (mode == JsonConfig.Date.ISO8601) {
|
||||
writer.writeString(toIsoFormat(value));
|
||||
} else {
|
||||
writer.writeNumber(convertToMillis(value));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert the value to ISO8601 format.
|
||||
*/
|
||||
protected abstract String toIsoFormat(T value);
|
||||
|
||||
@Override
|
||||
public DocPropertyType docType() {
|
||||
return DocPropertyType.DATE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T readData(DataInput dataInput) throws IOException {
|
||||
if (!dataInput.readBoolean()) {
|
||||
return null;
|
||||
} else {
|
||||
long val = dataInput.readLong();
|
||||
Date date = new Date(val);
|
||||
return convertFromDate(date);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeData(DataOutput dataOutput, T value) throws IOException {
|
||||
if (value == null) {
|
||||
dataOutput.writeBoolean(false);
|
||||
} else {
|
||||
dataOutput.writeBoolean(true);
|
||||
Date date = convertToDate(value);
|
||||
dataOutput.writeLong(date.getTime());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package io.ebeaninternal.server.type;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.core.type.DataBinder;
|
||||
import io.ebean.core.type.DataReader;
|
||||
import io.ebean.core.type.ScalarTypeBaseDate;
|
||||
import io.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
import java.sql.Date;
|
||||
@@ -12,7 +13,7 @@ import java.sql.Types;
|
||||
/**
|
||||
* ScalarType for java.sql.Date.
|
||||
*/
|
||||
final class ScalarTypeDate extends ScalarTypeBaseDate<java.sql.Date> {
|
||||
final class ScalarTypeDate extends ScalarTypeBaseDate<Date> {
|
||||
|
||||
ScalarTypeDate(JsonConfig.Date mode) {
|
||||
super(mode, Date.class, true, Types.DATE);
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.core.type.ScalarTypeBaseDate;
|
||||
import io.ebeaninternal.server.core.BasicTypeConverter;
|
||||
import org.joda.time.DateMidnight;
|
||||
|
||||
@@ -11,7 +12,7 @@ import java.sql.Types;
|
||||
* ScalarType for Joda DateMidnight. This maps to a JDBC Date.
|
||||
*/
|
||||
@SuppressWarnings("deprecation")
|
||||
final class ScalarTypeJodaDateMidnight extends ScalarTypeBaseDate<org.joda.time.DateMidnight> {
|
||||
final class ScalarTypeJodaDateMidnight extends ScalarTypeBaseDate<DateMidnight> {
|
||||
|
||||
/**
|
||||
* Instantiates a new scalar type joda date midnight.
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.ebeaninternal.server.type;
|
||||
import java.sql.Date;
|
||||
import java.sql.Types;
|
||||
|
||||
import io.ebean.core.type.ScalarTypeBaseDate;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
|
||||
@@ -8,6 +8,7 @@ import java.time.ZoneOffset;
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.core.type.ScalarTypeBaseDate;
|
||||
import io.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
/**
|
||||
|
||||
@@ -3,6 +3,7 @@ package io.ebeaninternal.server.type;
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.core.type.DataBinder;
|
||||
import io.ebean.core.type.DataReader;
|
||||
import io.ebean.core.type.ScalarTypeBaseDate;
|
||||
import io.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
import java.sql.SQLException;
|
||||
@@ -89,7 +90,7 @@ final class ScalarTypeUtilDate {
|
||||
}
|
||||
}
|
||||
|
||||
static final class DateType extends ScalarTypeBaseDate<java.util.Date> {
|
||||
static final class DateType extends ScalarTypeBaseDate<Date> {
|
||||
|
||||
DateType(JsonConfig.Date mode) {
|
||||
super(mode, Date.class, false, Types.DATE);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package io.ebeaninternal.server.type;
|
||||
|
||||
import io.ebean.config.JsonConfig;
|
||||
import io.ebean.core.type.ScalarTypeBaseDate;
|
||||
import io.ebeaninternal.server.core.BasicTypeConverter;
|
||||
|
||||
import java.sql.Date;
|
||||
|
||||
Reference in new issue
Block a user