[#7952] Add SQLDataType.INSTANT

This commit is contained in:
lukaseder 2018-11-01 16:04:10 +01:00
parent 2382e7049b
commit 446cce4a74
6 changed files with 77 additions and 0 deletions

View File

@ -453,6 +453,7 @@ public interface DataType<T> extends Serializable {
* <li> {@link SQLDataType#LOCALDATETIME}</li>
* <li> {@link SQLDataType#OFFSETTIME}</li>
* <li> {@link SQLDataType#OFFSETDATETIME}</li>
* <li> {@link SQLDataType#INSTANT}</li>
* </ul>
*/
boolean isDateTime();

View File

@ -114,6 +114,7 @@ import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@ -256,6 +257,8 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
return new DefaultOffsetDateTimeBinding(converter, isLob);
else if (type == OffsetTime.class)
return new DefaultOffsetTimeBinding(converter, isLob);
else if (type == Instant.class)
return new DefaultInstantBinding(converter, isLob);
else if (type == Short.class || type == short.class)
return new DefaultShortBinding(converter, isLob);
@ -2787,6 +2790,58 @@ public class DefaultBinding<T, U> implements Binding<T, U> {
}
}
static final class DefaultInstantBinding<U> extends AbstractBinding<Instant, U> {
/**
* Generated UID
*/
private static final long serialVersionUID = -1850495302106551527L;
private static final Converter<OffsetDateTime, Instant> CONVERTER = Converter.ofNullable(
OffsetDateTime.class,
Instant.class,
o -> o.toInstant(),
i -> OffsetDateTime.ofInstant(i, ZoneOffset.UTC)
);
private final DefaultOffsetDateTimeBinding<U> delegate;
DefaultInstantBinding(Converter<Instant, U> converter, boolean isLob) {
super(converter, isLob);
delegate = new DefaultOffsetDateTimeBinding<U>(Converters.of(CONVERTER, converter()), isLob);
}
@Override
final void set0(BindingSetStatementContext<U> ctx, Instant value) throws SQLException {
delegate.set0(ctx, CONVERTER.to(value));
}
@Override
final void set0(BindingSetSQLOutputContext<U> ctx, Instant value) throws SQLException {
delegate.set0(ctx, CONVERTER.to(value));
}
@Override
final Instant get0(BindingGetResultSetContext<U> ctx) throws SQLException {
return CONVERTER.from(delegate.get0(ctx));
}
@Override
final Instant get0(BindingGetStatementContext<U> ctx) throws SQLException {
return CONVERTER.from(delegate.get0(ctx));
}
@Override
final Instant get0(BindingGetSQLInputContext<U> ctx) throws SQLException {
return CONVERTER.from(delegate.get0(ctx));
}
@Override
final int sqltype(Statement statement, Configuration configuration) throws SQLException {
return delegate.sqltype(statement, configuration);
}
}
static final class DefaultOtherBinding<U> extends AbstractBinding<Object, U> {

View File

@ -44,6 +44,7 @@ import java.sql.ResultSet;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
@ -494,6 +495,20 @@ public final class SQLDataType {
return TIMESTAMPWITHTIMEZONE.precision(precision);
}
/**
* A {@link Types#TIMESTAMP_WITH_TIMEZONE} type that uses UTC as time zone.
* <p>
* Neither JDBC, nor most SQL databases support the <code>INSTANT</code>
* data type, which is often the only kind of timestamp which can be
* expected to behave across all server and client time zone settings. This
* implementation is backed by the database vendor's
* <code>TIMESTAMP WITH TIME ZONE</code> data type implementation, which may
* (e.g. Oracle) or may not (e.g. PostgreSQL) store the timestamp
* information. Irrespective of that storage, this type will always produce
* time zone agnostic instants in client code.
*/
public static final DataType<Instant> INSTANT = new DefaultDataType<Instant>(null, Instant.class, "instant");
// -------------------------------------------------------------------------
// Binary types

View File

@ -42,6 +42,7 @@ import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.UUID;
@ -105,6 +106,7 @@ public class H2DataType {
public static final DataType<Timestamp> DATETIME = new DefaultDataType<Timestamp>(SQLDialect.H2, SQLDataType.TIMESTAMP, "datetime");
public static final DataType<OffsetDateTime> TIMESTAMPWITHTIMEZONE = new DefaultDataType<OffsetDateTime>(SQLDialect.H2, SQLDataType.TIMESTAMPWITHTIMEZONE, "timestamp with time zone");
public static final DataType<Instant> INSTANT = new DefaultDataType<Instant>(SQLDialect.H2, SQLDataType.INSTANT, "timestamp with time zone");
public static final DataType<byte[]> BINARY = new DefaultDataType<byte[]>(SQLDialect.H2, SQLDataType.BINARY, "binary");
public static final DataType<byte[]> VARBINARY = new DefaultDataType<byte[]>(SQLDialect.H2, SQLDataType.VARBINARY, "varbinary");

View File

@ -43,6 +43,7 @@ import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.UUID;
@ -114,6 +115,7 @@ public class HSQLDBDataType {
public static final DataType<Timestamp> DATETIME = new DefaultDataType<Timestamp>(SQLDialect.HSQLDB, SQLDataType.TIMESTAMP, "datetime");
public static final DataType<OffsetDateTime> TIMESTAMPWITHTIMEZONE = new DefaultDataType<OffsetDateTime>(SQLDialect.HSQLDB, SQLDataType.TIMESTAMPWITHTIMEZONE, "timestamp with time zone");
public static final DataType<Instant> INSTANT = new DefaultDataType<Instant>(SQLDialect.HSQLDB, SQLDataType.INSTANT, "timestamp with time zone");
public static final DataType<byte[]> LONGVARBINARY = new DefaultDataType<byte[]>(SQLDialect.HSQLDB, SQLDataType.LONGVARBINARY, "longvarbinary");
public static final DataType<byte[]> VARBINARY = new DefaultDataType<byte[]>(SQLDialect.HSQLDB, SQLDataType.VARBINARY, "varbinary", "varbinary(32672)");

View File

@ -43,6 +43,7 @@ import java.math.BigInteger;
import java.sql.Date;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.util.UUID;
@ -113,6 +114,7 @@ public class PostgresDataType {
public static final DataType<OffsetDateTime> TIMESTAMPWITHTIMEZONE = new DefaultDataType<OffsetDateTime>(SQLDialect.POSTGRES, SQLDataType.TIMESTAMPWITHTIMEZONE, "timestamp with time zone");
public static final DataType<OffsetDateTime> TIMESTAMPTZ = new DefaultDataType<OffsetDateTime>(SQLDialect.POSTGRES, SQLDataType.TIMESTAMPWITHTIMEZONE, "timestamptz");
public static final DataType<Instant> INSTANT = new DefaultDataType<Instant>(SQLDialect.POSTGRES, SQLDataType.INSTANT, "timestamp with time zone");
public static final DataType<byte[]> BYTEA = new DefaultDataType<byte[]>(SQLDialect.POSTGRES, SQLDataType.BLOB, "bytea");
public static final DataType<YearToMonth> INTERVALYEARTOMONTH = new DefaultDataType<YearToMonth>(SQLDialect.POSTGRES, SQLDataType.INTERVALYEARTOMONTH, "interval year to month");