提交 04673de8 authored 作者: Philippe Marschall's avatar Philippe Marschall

TimestampWithTimeZone can not be bound

Clients that are still on Java 7 and can not use OffsetDateTime need a
way to bind TimestampWithTimeZone. Unfortunately TimestampWithTimeZone
can currently not be used as a bind parameter
The obvious way to bind TimestampWithTimeZone would be to use
PreparedStatement.setObject(int, Object). This does not work because
DataTye#convertTo currently does not check for TimestampWithTimeZone.

This commit fixes this by adding a check and conversion in
DataTye#convertTo and also adding a unit test.
上级 b1c09339
......@@ -1121,6 +1121,8 @@ public class DataType {
return LocalDateTimeUtils.localDateTimeToValue(x);
} else if (LocalDateTimeUtils.isOffsetDateTime(x.getClass())) {
return LocalDateTimeUtils.offsetDateTimeToValue(x);
} else if (x instanceof TimestampWithTimeZone) {
return ValueTimestampTimeZone.get((TimestampWithTimeZone) x);
} else {
if (JdbcUtils.customDataTypesHandler != null) {
return JdbcUtils.customDataTypesHandler.getValue(type, x,
......
......@@ -6,13 +6,16 @@
package org.h2.test.unit;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import org.h2.api.TimestampWithTimeZone;
import org.h2.test.TestBase;
import org.h2.util.LocalDateTimeUtils;
import org.h2.value.Value;
import org.h2.value.ValueTimestampTimeZone;
/**
......@@ -35,6 +38,7 @@ public class TestTimeStampWithTimeZone extends TestBase {
test2();
test3();
test4();
test5();
testOrder();
deleteDb(getTestName());
}
......@@ -139,6 +143,31 @@ public class TestTimeStampWithTimeZone extends TestBase {
assertEquals(c, 0);
}
private void test5() throws SQLException {
Connection conn = getConnection(getTestName());
Statement stat = conn.createStatement();
stat.execute("create table test5(id identity, t1 timestamp with time zone)");
stat.execute("insert into test5(t1) values('2016-09-24 00:00:00.000000001+00:01')");
stat.execute("insert into test5(t1) values('2017-04-20 00:00:00.000000001+00:01')");
PreparedStatement preparedStatement = conn.prepareStatement("select id"
+ " from test5"
+ " where (t1 < ?)");
Value value = ValueTimestampTimeZone.parse("2016-12-24 00:00:00.000000001+00:01");
preparedStatement.setObject(1, value.getObject());
ResultSet rs = preparedStatement.executeQuery();
assertTrue(rs.next());
assertEquals(1, rs.getInt(1));
assertFalse(rs.next());
rs.close();
preparedStatement.close();
stat.close();
conn.close();
}
private void testOrder() throws SQLException {
Connection conn = getConnection(getTestName());
Statement stat = conn.createStatement();
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论