Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
eee80b52
Unverified
提交
eee80b52
authored
1月 10, 2018
作者:
Noel Grandin
提交者:
GitHub
1月 10, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #735 from katzyn/Instant2
Add support of java.time.Instant V2
上级
ec7148b2
6ebf22cb
显示空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
113 行增加
和
7 行删除
+113
-7
JdbcResultSet.java
h2/src/main/org/h2/jdbc/JdbcResultSet.java
+2
-0
LocalDateTimeUtils.java
h2/src/main/org/h2/util/LocalDateTimeUtils.java
+79
-7
DataType.java
h2/src/main/org/h2/value/DataType.java
+2
-0
TestPreparedStatement.java
h2/src/test/org/h2/test/jdbc/TestPreparedStatement.java
+30
-0
没有找到文件。
h2/src/main/org/h2/jdbc/JdbcResultSet.java
浏览文件 @
eee80b52
...
...
@@ -3826,6 +3826,8 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS
}
else
if
(
LocalDateTimeUtils
.
isLocalDateTime
(
type
))
{
return
type
.
cast
(
LocalDateTimeUtils
.
valueToLocalDateTime
(
(
ValueTimestamp
)
value
));
}
else
if
(
LocalDateTimeUtils
.
isInstant
(
type
))
{
return
type
.
cast
(
LocalDateTimeUtils
.
valueToInstant
(
value
));
}
else
if
(
LocalDateTimeUtils
.
isOffsetDateTime
(
type
)
&&
value
instanceof
ValueTimestampTimeZone
)
{
return
type
.
cast
(
LocalDateTimeUtils
.
valueToOffsetDateTime
(
...
...
h2/src/main/org/h2/util/LocalDateTimeUtils.java
浏览文件 @
eee80b52
...
...
@@ -9,6 +9,7 @@ package org.h2.util;
import
java.lang.reflect.InvocationTargetException
;
import
java.lang.reflect.Method
;
import
java.sql.Timestamp
;
import
java.util.Arrays
;
import
java.util.concurrent.TimeUnit
;
...
...
@@ -28,11 +29,15 @@ import org.h2.value.ValueTimestampTimeZone;
* Java 7 as well.</p>
*
* <p>Custom conversion methods between H2 internal values and JSR-310 classes
* are used
without intermediate conversions to java.sql classes. Direct
*
conversion is simpler, faster, and it does not inherit limitations and
* issues from java.sql classes and conversion methods provided by JDK.</p>
* are used
in most cases without intermediate conversions to java.sql classes.
*
Direct conversion is simpler, faster, and it does not inherit limitations
*
and
issues from java.sql classes and conversion methods provided by JDK.</p>
*
* <p>Once the driver requires Java 8 all the reflection can be removed.</p>
* <p>The only one exclusion is a conversion between {@link Timestamp} and
* Instant.</p>
*
* <p>Once the driver requires Java 8 and Android API 26 all the reflection
* can be removed.</p>
*/
public
class
LocalDateTimeUtils
{
...
...
@@ -42,6 +47,8 @@ public class LocalDateTimeUtils {
private
static
final
Class
<?>
LOCAL_TIME
;
// Class<java.time.LocalDateTime>
private
static
final
Class
<?>
LOCAL_DATE_TIME
;
// Class<java.time.Instant>
private
static
final
Class
<?>
INSTANT
;
// Class<java.time.OffsetDateTime>
private
static
final
Class
<?>
OFFSET_DATE_TIME
;
// Class<java.time.ZoneOffset>
...
...
@@ -66,6 +73,11 @@ public class LocalDateTimeUtils {
// java.time.LocalDate#atStartOfDay()
private
static
final
Method
LOCAL_DATE_AT_START_OF_DAY
;
// java.sql.Timestamp.from(java.time.Instant)
private
static
final
Method
TIMESTAMP_FROM
;
// java.sql.Timestamp.toInstant()
private
static
final
Method
TIMESTAMP_TO_INSTANT
;
// java.time.LocalTime#parse(CharSequence)
private
static
final
Method
LOCAL_TIME_PARSE
;
...
...
@@ -99,11 +111,12 @@ public class LocalDateTimeUtils {
LOCAL_DATE
=
tryGetClass
(
"java.time.LocalDate"
);
LOCAL_TIME
=
tryGetClass
(
"java.time.LocalTime"
);
LOCAL_DATE_TIME
=
tryGetClass
(
"java.time.LocalDateTime"
);
INSTANT
=
tryGetClass
(
"java.time.Instant"
);
OFFSET_DATE_TIME
=
tryGetClass
(
"java.time.OffsetDateTime"
);
ZONE_OFFSET
=
tryGetClass
(
"java.time.ZoneOffset"
);
IS_JAVA8_DATE_API_PRESENT
=
LOCAL_DATE
!=
null
&&
LOCAL_TIME
!=
null
&&
LOCAL_DATE_TIME
!=
null
&&
OFFSET_DATE_TIME
!=
null
&&
ZONE_OFFSET
!=
null
;
LOCAL_DATE_TIME
!=
null
&&
INSTANT
!=
null
&&
OFFSET_DATE_TIME
!=
null
&&
ZONE_OFFSET
!=
null
;
if
(
IS_JAVA8_DATE_API_PRESENT
)
{
LOCAL_TIME_OF_NANO
=
getMethod
(
LOCAL_TIME
,
"ofNanoOfDay"
,
long
.
class
);
...
...
@@ -119,6 +132,9 @@ public class LocalDateTimeUtils {
LOCAL_DATE_GET_DAY_OF_MONTH
=
getMethod
(
LOCAL_DATE
,
"getDayOfMonth"
);
LOCAL_DATE_AT_START_OF_DAY
=
getMethod
(
LOCAL_DATE
,
"atStartOfDay"
);
TIMESTAMP_FROM
=
getMethod
(
Timestamp
.
class
,
"from"
,
INSTANT
);
TIMESTAMP_TO_INSTANT
=
getMethod
(
Timestamp
.
class
,
"toInstant"
);
LOCAL_TIME_PARSE
=
getMethod
(
LOCAL_TIME
,
"parse"
,
CharSequence
.
class
);
LOCAL_DATE_TIME_PLUS_NANOS
=
getMethod
(
LOCAL_DATE_TIME
,
"plusNanos"
,
long
.
class
);
...
...
@@ -144,6 +160,8 @@ public class LocalDateTimeUtils {
LOCAL_DATE_GET_MONTH_VALUE
=
null
;
LOCAL_DATE_GET_DAY_OF_MONTH
=
null
;
LOCAL_DATE_AT_START_OF_DAY
=
null
;
TIMESTAMP_FROM
=
null
;
TIMESTAMP_TO_INSTANT
=
null
;
LOCAL_TIME_PARSE
=
null
;
LOCAL_DATE_TIME_PLUS_NANOS
=
null
;
LOCAL_DATE_TIME_TO_LOCAL_DATE
=
null
;
...
...
@@ -202,6 +220,15 @@ public class LocalDateTimeUtils {
return
LOCAL_DATE_TIME
;
}
/**
* Returns the class java.time.Instant.
*
* @return the class java.time.Instant, null on Java 7
*/
public
static
Class
<?>
getInstantClass
()
{
return
INSTANT
;
}
/**
* Returns the class java.time.OffsetDateTime.
*
...
...
@@ -322,6 +349,18 @@ public class LocalDateTimeUtils {
return
LOCAL_DATE_TIME
==
clazz
;
}
/**
* Checks if the given class is Instant.
*
* <p>This method can be called from Java 7.</p>
*
* @param clazz the class to check
* @return if the class is Instant
*/
public
static
boolean
isInstant
(
Class
<?>
clazz
)
{
return
INSTANT
==
clazz
;
}
/**
* Checks if the given class is OffsetDateTime.
*
...
...
@@ -380,7 +419,6 @@ public class LocalDateTimeUtils {
* @return the LocalDateTime
*/
public
static
Object
valueToLocalDateTime
(
ValueTimestamp
value
)
{
long
dateValue
=
value
.
getDateValue
();
long
timeNanos
=
value
.
getTimeNanos
();
try
{
...
...
@@ -394,6 +432,24 @@ public class LocalDateTimeUtils {
}
}
/**
* Converts a value to a Instant.
*
* <p>This method should only called from Java 8 or later.</p>
*
* @param value the value to convert
* @return the Instant
*/
public
static
Object
valueToInstant
(
Value
value
)
{
try
{
return
TIMESTAMP_TO_INSTANT
.
invoke
(
value
.
getTimestamp
());
}
catch
(
IllegalAccessException
e
)
{
throw
DbException
.
convert
(
e
);
}
catch
(
InvocationTargetException
e
)
{
throw
DbException
.
convertInvocation
(
e
,
"timestamp conversion failed"
);
}
}
/**
* Converts a value to a OffsetDateTime.
*
...
...
@@ -479,6 +535,22 @@ public class LocalDateTimeUtils {
}
}
/**
* Converts a Instant to a Value.
*
* @param instant the Instant to convert, not {@code null}
* @return the value
*/
public
static
Value
instantToValue
(
Object
instant
)
{
try
{
return
ValueTimestamp
.
get
((
Timestamp
)
TIMESTAMP_FROM
.
invoke
(
null
,
instant
));
}
catch
(
IllegalAccessException
e
)
{
throw
DbException
.
convert
(
e
);
}
catch
(
InvocationTargetException
e
)
{
throw
DbException
.
convertInvocation
(
e
,
"instant conversion failed"
);
}
}
/**
* Converts a OffsetDateTime to a Value.
*
...
...
h2/src/main/org/h2/value/DataType.java
浏览文件 @
eee80b52
...
...
@@ -1122,6 +1122,8 @@ public class DataType {
return
LocalDateTimeUtils
.
localTimeToTimeValue
(
x
);
}
else
if
(
LocalDateTimeUtils
.
isLocalDateTime
(
x
.
getClass
()))
{
return
LocalDateTimeUtils
.
localDateTimeToValue
(
x
);
}
else
if
(
LocalDateTimeUtils
.
isInstant
(
x
.
getClass
()))
{
return
LocalDateTimeUtils
.
instantToValue
(
x
);
}
else
if
(
LocalDateTimeUtils
.
isOffsetDateTime
(
x
.
getClass
()))
{
return
LocalDateTimeUtils
.
offsetDateTimeToValue
(
x
);
}
else
if
(
x
instanceof
TimestampWithTimeZone
)
{
...
...
h2/src/test/org/h2/test/jdbc/TestPreparedStatement.java
浏览文件 @
eee80b52
...
...
@@ -9,6 +9,7 @@ import java.io.ByteArrayInputStream;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.io.StringReader
;
import
java.lang.reflect.Method
;
import
java.math.BigDecimal
;
import
java.math.BigInteger
;
import
java.net.URL
;
...
...
@@ -76,6 +77,7 @@ public class TestPreparedStatement extends TestBase {
testTime8
(
conn
);
testDateTime8
(
conn
);
testOffsetDateTime8
(
conn
);
testInstant8
(
conn
);
testArray
(
conn
);
testUUIDGeneratedKeys
(
conn
);
testSetObject
(
conn
);
...
...
@@ -702,6 +704,34 @@ public class TestPreparedStatement extends TestBase {
rs
.
close
();
}
private
void
testInstant8
(
Connection
conn
)
throws
Exception
{
if
(!
LocalDateTimeUtils
.
isJava8DateApiPresent
())
{
return
;
}
Method
timestampToInstant
=
Timestamp
.
class
.
getMethod
(
"toInstant"
),
now
=
LocalDateTimeUtils
.
getInstantClass
().
getMethod
(
"now"
);
PreparedStatement
prep
=
conn
.
prepareStatement
(
"SELECT ?"
);
Object
instant1
=
now
.
invoke
(
null
);
prep
.
setObject
(
1
,
instant1
);
ResultSet
rs
=
prep
.
executeQuery
();
rs
.
next
();
Object
instant2
=
rs
.
getObject
(
1
,
LocalDateTimeUtils
.
getInstantClass
());
assertEquals
(
instant1
,
instant2
);
Timestamp
ts
=
rs
.
getTimestamp
(
1
);
assertEquals
(
instant1
,
timestampToInstant
.
invoke
(
ts
));
assertFalse
(
rs
.
next
());
rs
.
close
();
prep
.
setTimestamp
(
1
,
ts
);
rs
=
prep
.
executeQuery
();
rs
.
next
();
instant2
=
rs
.
getObject
(
1
,
LocalDateTimeUtils
.
getInstantClass
());
assertEquals
(
instant1
,
instant2
);
assertFalse
(
rs
.
next
());
rs
.
close
();
}
private
void
testPreparedSubquery
(
Connection
conn
)
throws
SQLException
{
Statement
s
=
conn
.
createStatement
();
s
.
executeUpdate
(
"CREATE TABLE TEST(ID IDENTITY, FLAG BIT)"
);
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论