提交 9b9dc7e2 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add SysProperties.RETURN_OFFSET_DATE_TIME

上级 0c1e2a39
......@@ -342,6 +342,17 @@ public class SysProperties {
public static final boolean BIG_DECIMAL_IS_DECIMAL =
Utils.getProperty("h2.bigDecimalIsDecimal", true);
/**
* System property {@code h2.returnOffsetDateTime}, {@code false} by
* default. If {@code true}, {@link java.sql.ResultSet#getObject(int)} and
* {@link java.sql.ResultSet#getObject(String)} return
* {@code TIMESTAMP WITH TIME ZONE} values as
* {@code java.time.OffsetDateTime} instead of
* {@code org.h2.api.TimestampWithTimeZone}. This property has effect only
* on Java 8 / Android API 26 and later versions.
*/
public static final boolean RETURN_OFFSET_DATE_TIME =
Utils.getProperty("h2.returnOffsetDateTime", false);
/**
* System property {@code h2.unlimitedTimeRange}, {@code false} by default.
......
......@@ -59,6 +59,7 @@ import org.h2.value.ValueShort;
import org.h2.value.ValueString;
import org.h2.value.ValueTime;
import org.h2.value.ValueTimestamp;
import org.h2.value.ValueTimestampTimeZone;
/**
* <p>
......@@ -3929,7 +3930,8 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS
return type.cast(value == ValueNull.INSTANCE
? null : new JdbcSQLXML(conn, value, JdbcLob.State.WITH_VALUE, id));
} else if (type == TimestampWithTimeZone.class) {
return type.cast(value.convertTo(Value.TIMESTAMP_TZ).getObject());
ValueTimestampTimeZone v = (ValueTimestampTimeZone) value.convertTo(Value.TIMESTAMP_TZ);
return type.cast(new TimestampWithTimeZone(v.getDateValue(), v.getTimeNanos(), v.getTimeZoneOffsetMins()));
} else if (DataType.isGeometryClass(type)) {
return type.cast(value.convertTo(Value.GEOMETRY).getObject());
} else if (type == LocalDateTimeUtils.LOCAL_DATE) {
......
......@@ -431,7 +431,7 @@ public class LocalDateTimeUtils {
* @param offsetDateTime the OffsetDateTime to convert, not {@code null}
* @return the value
*/
public static Value offsetDateTimeToValue(Object offsetDateTime) {
public static ValueTimestampTimeZone offsetDateTimeToValue(Object offsetDateTime) {
try {
Object localDateTime = OFFSET_DATE_TIME_TO_LOCAL_DATE_TIME.invoke(offsetDateTime);
Object localDate = LOCAL_DATE_TIME_TO_LOCAL_DATE.invoke(localDateTime);
......
......@@ -583,9 +583,16 @@ public class DataType {
break;
}
case Value.TIMESTAMP_TZ: {
TimestampWithTimeZone value = (TimestampWithTimeZone) rs.getObject(columnIndex);
v = value == null ? (Value) ValueNull.INSTANCE :
ValueTimestampTimeZone.get(value);
Object obj = rs.getObject(columnIndex);
if (obj == null) {
v = ValueNull.INSTANCE;
} else if (LocalDateTimeUtils.isJava8DateApiPresent()
&& LocalDateTimeUtils.OFFSET_DATE_TIME.isInstance(obj)) {
v = LocalDateTimeUtils.offsetDateTimeToValue(obj);
} else {
TimestampWithTimeZone value = (TimestampWithTimeZone) obj;
v = ValueTimestampTimeZone.get(value);
}
break;
}
case Value.DECIMAL: {
......@@ -772,6 +779,10 @@ public class DataType {
// "java.sql.Timestamp";
return Timestamp.class.getName();
case Value.TIMESTAMP_TZ:
if (SysProperties.RETURN_OFFSET_DATE_TIME && LocalDateTimeUtils.isJava8DateApiPresent()) {
// "java.time.OffsetDateTime";
return LocalDateTimeUtils.OFFSET_DATE_TIME.getName();
}
// "org.h2.api.TimestampWithTimeZone";
return TimestampWithTimeZone.class.getName();
case Value.BYTES:
......
......@@ -10,8 +10,10 @@ import java.sql.SQLException;
import java.sql.Timestamp;
import org.h2.api.ErrorCode;
import org.h2.api.TimestampWithTimeZone;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.util.DateTimeUtils;
import org.h2.util.LocalDateTimeUtils;
/**
* Implementation of the TIMESTAMP WITH TIME ZONE data type.
......@@ -273,8 +275,10 @@ public class ValueTimestampTimeZone extends Value {
@Override
public Object getObject() {
return new TimestampWithTimeZone(dateValue, timeNanos,
timeZoneOffsetMins);
if (SysProperties.RETURN_OFFSET_DATE_TIME && LocalDateTimeUtils.isJava8DateApiPresent()) {
return LocalDateTimeUtils.valueToOffsetDateTime(this);
}
return new TimestampWithTimeZone(dateValue, timeNanos, timeZoneOffsetMins);
}
@Override
......
......@@ -14,6 +14,7 @@ import java.sql.Statement;
import java.util.TimeZone;
import org.h2.api.TimestampWithTimeZone;
import org.h2.engine.SysProperties;
import org.h2.test.TestBase;
import org.h2.test.TestDb;
import org.h2.util.DateTimeUtils;
......@@ -64,7 +65,7 @@ public class TestTimeStampWithTimeZone extends TestDb {
ResultSet rs = stat.executeQuery("select t1 from test");
rs.next();
assertEquals("1970-01-01 12:00:00+00:15", rs.getString(1));
TimestampWithTimeZone ts = (TimestampWithTimeZone) rs.getObject(1);
TimestampWithTimeZone ts = test1_getTimestamp(rs);
assertEquals(1970, ts.getYear());
assertEquals(1, ts.getMonth());
assertEquals(1, ts.getDay());
......@@ -76,7 +77,7 @@ public class TestTimeStampWithTimeZone extends TestDb {
LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
}
rs.next();
ts = (TimestampWithTimeZone) rs.getObject(1);
ts = test1_getTimestamp(rs);
assertEquals(2016, ts.getYear());
assertEquals(9, ts.getMonth());
assertEquals(24, ts.getDay());
......@@ -87,7 +88,7 @@ public class TestTimeStampWithTimeZone extends TestDb {
LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
}
rs.next();
ts = (TimestampWithTimeZone) rs.getObject(1);
ts = test1_getTimestamp(rs);
assertEquals(2016, ts.getYear());
assertEquals(9, ts.getMonth());
assertEquals(24, ts.getDay());
......@@ -98,7 +99,7 @@ public class TestTimeStampWithTimeZone extends TestDb {
LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
}
rs.next();
ts = (TimestampWithTimeZone) rs.getObject(1);
ts = test1_getTimestamp(rs);
assertEquals(2016, ts.getYear());
assertEquals(1, ts.getMonth());
assertEquals(1, ts.getDay());
......@@ -107,7 +108,7 @@ public class TestTimeStampWithTimeZone extends TestDb {
LocalDateTimeUtils.OFFSET_DATE_TIME).toString());
}
rs.next();
ts = (TimestampWithTimeZone) rs.getObject(1);
ts = test1_getTimestamp(rs);
assertEquals(2015, ts.getYear());
assertEquals(12, ts.getMonth());
assertEquals(31, ts.getDay());
......@@ -124,6 +125,11 @@ public class TestTimeStampWithTimeZone extends TestDb {
// Types.TIMESTAMP_WITH_TIMEZONE
// once Java 1.8 is required.
assertEquals(2014, columnType);
if (SysProperties.RETURN_OFFSET_DATE_TIME && LocalDateTimeUtils.isJava8DateApiPresent()) {
assertEquals("java.time.OffsetDateTime", metaData.getColumnClassName(1));
} else {
assertEquals("org.h2.api.TimestampWithTimeZone", metaData.getColumnClassName(1));
}
rs.close();
......@@ -135,6 +141,16 @@ public class TestTimeStampWithTimeZone extends TestDb {
conn.close();
}
private static TimestampWithTimeZone test1_getTimestamp(ResultSet rs) throws SQLException {
Object o = rs.getObject(1);
if (SysProperties.RETURN_OFFSET_DATE_TIME && LocalDateTimeUtils.isJava8DateApiPresent()) {
ValueTimestampTimeZone value = LocalDateTimeUtils.offsetDateTimeToValue(o);
return new TimestampWithTimeZone(value.getDateValue(), value.getTimeNanos(),
value.getTimeZoneOffsetMins());
}
return (TimestampWithTimeZone) o;
}
private void test2() {
ValueTimestampTimeZone a = ValueTimestampTimeZone.parse("1970-01-01 12:00:00.00+00:15");
ValueTimestampTimeZone b = ValueTimestampTimeZone.parse("1970-01-01 12:00:01.00+01:15");
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论