Skip to content
项目
群组
代码片段
帮助
正在加载...
帮助
为 GitLab 提交贡献
登录/注册
切换导航
H
h2database
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
分枝图
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
计划
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
分枝图
统计图
创建新议题
作业
提交
议题看板
打开侧边栏
Administrator
h2database
Commits
f44a74c8
提交
f44a74c8
authored
9月 16, 2016
作者:
Noel Grandin
提交者:
GitHub
9月 16, 2016
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #358 from marschall/support-getObject-Class
Add support for getObject(int|String, Class)
上级
4e59f570
79965db6
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
114 行增加
和
6 行删除
+114
-6
JdbcCallableStatement.java
h2/src/main/org/h2/jdbc/JdbcCallableStatement.java
+2
-2
JdbcResultSet.java
h2/src/main/org/h2/jdbc/JdbcResultSet.java
+70
-4
TestCallableStatement.java
h2/src/test/org/h2/test/jdbc/TestCallableStatement.java
+2
-0
TestResultSet.java
h2/src/test/org/h2/test/jdbc/TestResultSet.java
+40
-0
没有找到文件。
h2/src/main/org/h2/jdbc/JdbcCallableStatement.java
浏览文件 @
f44a74c8
...
...
@@ -1571,7 +1571,7 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements
*/
@Override
public
<
T
>
T
getObject
(
int
parameterIndex
,
Class
<
T
>
type
)
throws
SQLException
{
throw
unsupported
(
"getObject"
);
return
getOpenResultSet
().
getObject
(
parameterIndex
,
type
);
}
/**
...
...
@@ -1582,7 +1582,7 @@ public class JdbcCallableStatement extends JdbcPreparedStatement implements
*/
@Override
public
<
T
>
T
getObject
(
String
parameterName
,
Class
<
T
>
type
)
throws
SQLException
{
throw
unsupported
(
"getObject"
);
return
getObject
(
getIndexForName
(
parameterName
),
type
);
}
private
ResultSetMetaData
getCheckedMetaData
()
throws
SQLException
{
...
...
h2/src/main/org/h2/jdbc/JdbcResultSet.java
浏览文件 @
f44a74c8
...
...
@@ -27,7 +27,10 @@ import java.sql.Timestamp;
import
java.util.Calendar
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.UUID
;
import
org.h2.api.ErrorCode
;
import
org.h2.api.TimestampWithTimeZone
;
import
org.h2.engine.SysProperties
;
import
org.h2.message.DbException
;
import
org.h2.message.TraceObject
;
...
...
@@ -55,6 +58,8 @@ import org.h2.value.ValueString;
import
org.h2.value.ValueTime
;
import
org.h2.value.ValueTimestamp
;
import
com.vividsolutions.jts.geom.Geometry
;
/**
* <p>
* Represents a result set.
...
...
@@ -3679,25 +3684,86 @@ public class JdbcResultSet extends TraceObject implements ResultSet, JdbcResultS
}
/**
* [Not supported]
* Returns a column value as a Java object. The data is
* de-serialized into a Java object (on the client side).
*
* @param columnIndex the column index (1, 2, ...)
* @param type the class of the returned value
* @throws SQLException if the column is not found or if the result set is
* closed
*/
@Override
public
<
T
>
T
getObject
(
int
columnIndex
,
Class
<
T
>
type
)
throws
SQLException
{
throw
unsupported
(
"getObject"
);
try
{
if
(
type
==
null
)
{
throw
DbException
.
getInvalidValueException
(
"type"
,
type
);
}
debugCodeCall
(
"getObject"
,
columnIndex
);
Value
value
=
get
(
columnIndex
);
return
extractObjectOfType
(
type
,
value
);
}
catch
(
Exception
e
)
{
throw
logAndConvert
(
e
);
}
}
/**
* [Not supported]
* Returns a column value as a Java object. The data is
* de-serialized into a Java object (on the client side).
*
* @param columnName the column name
* @param type the class of the returned value
*/
@Override
public
<
T
>
T
getObject
(
String
columnName
,
Class
<
T
>
type
)
throws
SQLException
{
throw
unsupported
(
"getObject"
);
try
{
if
(
type
==
null
)
{
throw
DbException
.
getInvalidValueException
(
"type"
,
type
);
}
debugCodeCall
(
"getObject"
,
columnName
);
Value
value
=
get
(
columnName
);
return
extractObjectOfType
(
type
,
value
);
}
catch
(
Exception
e
)
{
throw
logAndConvert
(
e
);
}
}
private
<
T
>
T
extractObjectOfType
(
Class
<
T
>
type
,
Value
value
)
throws
SQLException
{
if
(
value
==
ValueNull
.
INSTANCE
)
{
return
null
;
}
if
(
type
==
BigDecimal
.
class
)
{
return
type
.
cast
(
value
.
getBigDecimal
());
}
else
if
(
type
==
String
.
class
)
{
return
type
.
cast
(
value
.
getString
());
}
else
if
(
type
==
Boolean
.
class
)
{
return
type
.
cast
(
value
.
getBoolean
());
}
else
if
(
type
==
Byte
.
class
)
{
return
type
.
cast
(
value
.
getByte
());
}
else
if
(
type
==
Short
.
class
)
{
return
type
.
cast
(
value
.
getShort
());
}
else
if
(
type
==
Integer
.
class
)
{
return
type
.
cast
(
value
.
getInt
());
}
else
if
(
type
==
Long
.
class
)
{
return
type
.
cast
(
value
.
getLong
());
}
else
if
(
type
==
Float
.
class
)
{
return
type
.
cast
(
value
.
getFloat
());
}
else
if
(
type
==
Double
.
class
)
{
return
type
.
cast
(
value
.
getDouble
());
}
else
if
(
type
==
Date
.
class
)
{
return
type
.
cast
(
value
.
getDate
());
}
else
if
(
type
==
Time
.
class
)
{
return
type
.
cast
(
value
.
getTime
());
}
else
if
(
type
==
Timestamp
.
class
)
{
return
type
.
cast
(
value
.
getTimestamp
());
}
else
if
(
type
==
UUID
.
class
)
{
return
type
.
cast
(
value
.
getObject
());
}
else
if
(
type
==
TimestampWithTimeZone
.
class
)
{
return
type
.
cast
(
value
.
getObject
());
}
else
if
(
type
.
isAssignableFrom
(
Geometry
.
class
))
{
return
type
.
cast
(
value
.
getObject
());
}
else
{
throw
unsupported
(
type
.
getClass
().
getName
());
}
}
/**
...
...
h2/src/test/org/h2/test/jdbc/TestCallableStatement.java
浏览文件 @
f44a74c8
...
...
@@ -23,6 +23,7 @@ import java.sql.Types;
import
java.util.Collections
;
import
org.h2.api.ErrorCode
;
import
org.h2.jdbc.JdbcCallableStatementBackwardsCompat
;
import
org.h2.test.TestBase
;
import
org.h2.tools.SimpleResultSet
;
import
org.h2.util.IOUtils
;
...
...
@@ -147,6 +148,7 @@ public class TestCallableStatement extends TestBase {
assertEquals
(
1
,
call
.
getLong
(
1
));
assertEquals
(
1
,
call
.
getByte
(
1
));
assertEquals
(
1
,
((
Long
)
call
.
getObject
(
1
)).
longValue
());
assertEquals
(
1
,
((
JdbcCallableStatementBackwardsCompat
)
call
).
getObject
(
1
,
Long
.
class
).
longValue
());
assertFalse
(
call
.
wasNull
());
call
.
setFloat
(
2
,
1.1f
);
...
...
h2/src/test/org/h2/test/jdbc/TestResultSet.java
浏览文件 @
f44a74c8
...
...
@@ -35,6 +35,7 @@ import java.util.Collections;
import
java.util.TimeZone
;
import
org.h2.api.ErrorCode
;
import
org.h2.jdbc.JdbcResultSetBackwardsCompat
;
import
org.h2.test.TestBase
;
import
org.h2.util.IOUtils
;
...
...
@@ -670,10 +671,18 @@ public class TestResultSet extends TestBase {
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
Integer
);
assertTrue
(((
Integer
)
o
).
intValue
()
==
-
1
);
o
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
"value"
,
Integer
.
class
);
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
Integer
);
assertTrue
(((
Integer
)
o
).
intValue
()
==
-
1
);
o
=
rs
.
getObject
(
2
);
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
Integer
);
assertTrue
(((
Integer
)
o
).
intValue
()
==
-
1
);
o
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
2
,
Integer
.
class
);
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
Integer
);
assertTrue
(((
Integer
)
o
).
intValue
()
==
-
1
);
assertTrue
(
rs
.
getBoolean
(
"Value"
));
assertTrue
(
rs
.
getByte
(
"Value"
)
==
(
byte
)
-
1
);
assertTrue
(
rs
.
getShort
(
"Value"
)
==
(
short
)
-
1
);
...
...
@@ -721,6 +730,9 @@ public class TestResultSet extends TestBase {
o
=
rs
.
getObject
(
2
);
assertTrue
(
o
==
null
);
assertTrue
(
rs
.
wasNull
());
o
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
2
,
Integer
.
class
);
assertTrue
(
o
==
null
);
assertTrue
(
rs
.
wasNull
());
assertFalse
(
rs
.
next
());
assertEquals
(
0
,
rs
.
getRow
());
// there is one more row, but because of setMaxRows we don't get it
...
...
@@ -780,6 +792,10 @@ public class TestResultSet extends TestBase {
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
String
);
assertTrue
(
o
.
toString
().
equals
(
"Hi"
));
o
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
"value"
,
String
.
class
);
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
String
);
assertTrue
(
o
.
equals
(
"Hi"
));
rs
.
next
();
value
=
rs
.
getString
(
2
);
trace
(
"Value: <"
+
value
+
"> (should be: < Hi >)"
);
...
...
@@ -845,6 +861,10 @@ public class TestResultSet extends TestBase {
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
BigDecimal
);
assertTrue
(((
BigDecimal
)
o
).
compareTo
(
new
BigDecimal
(
"-1.00"
))
==
0
);
o
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
2
,
BigDecimal
.
class
);
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
BigDecimal
);
assertTrue
(((
BigDecimal
)
o
).
compareTo
(
new
BigDecimal
(
"-1.00"
))
==
0
);
rs
.
next
();
assertTrue
(
rs
.
getInt
(
1
)
==
2
);
...
...
@@ -905,10 +925,18 @@ public class TestResultSet extends TestBase {
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
Double
);
assertTrue
(((
Double
)
o
).
compareTo
(
new
Double
(
"-1.00"
))
==
0
);
o
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
2
,
Double
.
class
);
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
Double
);
assertTrue
(((
Double
)
o
).
compareTo
(
new
Double
(
"-1.00"
))
==
0
);
o
=
rs
.
getObject
(
3
);
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
Float
);
assertTrue
(((
Float
)
o
).
compareTo
(
new
Float
(
"-1.00"
))
==
0
);
o
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
3
,
Float
.
class
);
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
Float
);
assertTrue
(((
Float
)
o
).
compareTo
(
new
Float
(
"-1.00"
))
==
0
);
rs
.
next
();
assertTrue
(
rs
.
getInt
(
1
)
==
2
);
assertTrue
(!
rs
.
wasNull
());
...
...
@@ -1009,6 +1037,12 @@ public class TestResultSet extends TestBase {
assertTrue
(((
java
.
sql
.
Timestamp
)
o
).
equals
(
java
.
sql
.
Timestamp
.
valueOf
(
"2011-11-11 00:00:00.0"
)));
assertFalse
(
rs
.
wasNull
());
o
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
2
,
java
.
sql
.
Timestamp
.
class
);
trace
(
o
.
getClass
().
getName
());
assertTrue
(
o
instanceof
java
.
sql
.
Timestamp
);
assertTrue
(((
java
.
sql
.
Timestamp
)
o
).
equals
(
java
.
sql
.
Timestamp
.
valueOf
(
"2011-11-11 00:00:00.0"
)));
assertFalse
(
rs
.
wasNull
());
rs
.
next
();
date
=
rs
.
getDate
(
"VALUE"
);
...
...
@@ -1048,6 +1082,12 @@ public class TestResultSet extends TestBase {
assertEquals
(
"2001-02-03"
,
date
.
toString
());
assertEquals
(
"14:15:16"
,
time
.
toString
());
assertEquals
(
"2007-08-09 10:11:12.141516171"
,
ts
.
toString
());
date
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
1
,
Date
.
class
);
time
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
2
,
Time
.
class
);
ts
=
((
JdbcResultSetBackwardsCompat
)
rs
).
getObject
(
3
,
Timestamp
.
class
);
assertEquals
(
"2001-02-03"
,
date
.
toString
());
assertEquals
(
"14:15:16"
,
time
.
toString
());
assertEquals
(
"2007-08-09 10:11:12.141516171"
,
ts
.
toString
());
stat
.
execute
(
"DROP TABLE TEST"
);
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论