[英]Why can't a bind variable be used from ojdbc with Oracle JSON_ARRAY() in the presence of FORMAT JSON
In Oracle, the following query: Oracle,查询如下:
select
json_arrayagg(
json_array(
(select json_arrayagg(json_array(1) format json) from dual) format json,
2
)
format json
)
from dual
Produces the following JSON document:生成以下 JSON 文档:
[[[[1]],2]]
When I try to run this query with bind variables from JDBC like this:当我尝试使用来自 JDBC 的绑定变量运行此查询时,如下所示:
try (PreparedStatement s = connection.prepareStatement(
"""
select
json_arrayagg(
json_array(
(select json_arrayagg(json_array(?) format json) from dual) format json,
?
)
format json
)
from dual
"""
)) {
s.setInt(1, 1);
s.setInt(2, 2); // This fails
try (ResultSet rs = s.executeQuery()) {
while (rs.next())
System.out.println(rs.getString(1));
}
}
Then I'm getting:然后我得到:
Exception in thread "main" java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.OraclePreparedStatement.setIntInternal(OraclePreparedStatement.java:4956)
at oracle.jdbc.driver.OraclePreparedStatement.setInt(OraclePreparedStatement.java:4947)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setInt(OraclePreparedStatementWrapper.java:202)
at org.jooq.testscripts.JDBC.main(JDBC.java:50)
I'm using these versions:我正在使用这些版本:
Is this a bug?这是一个错误吗? Can it be worked around?
可以解决吗?
In my opinion, it's a bug in ojdbc.在我看来,这是 ojdbc 中的一个错误。 Workarounds include:
解决方法包括:
try (PreparedStatement s = connection.prepareStatement(
"""
select
json_arrayagg(
json_array(
(select json_arrayagg(json_array(?) format json) from dual) format json,
cast(? as number) -- cast here
)
format json
)
from dual
"""
)) {
s.setInt(1, 1);
s.setInt(2, 2);
try (ResultSet rs = s.executeQuery()) {
while (rs.next())
System.out.println(rs.getString(1));
}
}
FORMAT JSON
directive just before the bind parameter markerFORMAT JSON
指令try (PreparedStatement s = connection.prepareStatement(
"""
select
json_arrayagg(
json_array(
(select json_arrayagg(json_array(?) format json) from dual) /* no format json here */,
?
)
format json
)
from dual
"""
)) {
s.setInt(1, 1);
s.setInt(2, 2);
try (ResultSet rs = s.executeQuery()) {
while (rs.next())
System.out.println(rs.getString(1));
}
}
try (PreparedStatement s = connection.prepareStatement(
"""
select
json_arrayagg(
json_array(
(select json_arrayagg(json_array(?) format json) from dual) format json,
nvl(null, ?) -- Dummy expression here
)
format json
)
from dual
"""
)) {
s.setInt(1, 1);
s.setInt(2, 2);
try (ResultSet rs = s.executeQuery()) {
while (rs.next())
System.out.println(rs.getString(1));
}
}
null
value, and remove it again with absent on null
null
值,并absent on null
上不存在时再次删除它try (PreparedStatement s = connection.prepareStatement(
"""
select
json_arrayagg(
json_array(
(select json_arrayagg(json_array(?) format json) from dual) format json,
null, -- This is ignored
?
absent on null
)
format json
)
from dual
"""
)) {
s.setInt(1, 1);
s.setInt(2, 2);
try (ResultSet rs = s.executeQuery()) {
while (rs.next())
System.out.println(rs.getString(1));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.