H2 Oracle解码函数
在H2中,我编写了一个Java解码函数。它与代码一起工作:H2 Oracle解码函数,h2,H2,在H2中,我编写了一个Java解码函数。它与代码一起工作: String sql = "select decode(1.0,2.0,3.0,4.0) from dual ;"; PreparedStatement stmt = connection.prepareStatement(sql); ResultSet resultSet = (ResultSet) stmt.executeQuery(); 但是代码 String sql = "select 6.0 - decode(1.0,2.0
String sql = "select decode(1.0,2.0,3.0,4.0) from dual ;";
PreparedStatement stmt = connection.prepareStatement(sql);
ResultSet resultSet = (ResultSet) stmt.executeQuery();
但是代码
String sql = "select 6.0 - decode(1.0,2.0,3.0,4.0) from dual ;";
给出了错误:
org.h2.jdbc.JdbcSQLException: Hexadecimal string with odd number of characters: "6.0"; SQL statement:
select 6.0 - decode(1.0,2.0,3.0,4.0) from dual ; [90003-157]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
at org.h2.message.DbException.get(DbException.java:167)
at org.h2.message.DbException.get(DbException.java:144)
at org.h2.util.StringUtils.convertHexToBytes(StringUtils.java:943)
at org.h2.value.Value.convertTo(Value.java:826)
at org.h2.expression.Operation.getValue(Operation.java:108)
at org.h2.command.dml.Select.queryFlat(Select.java:518)
at org.h2.command.dml.Select.queryWithoutCache(Select.java:617)
at org.h2.command.dml.Query.query(Query.java:298)
at org.h2.command.dml.Query.query(Query.java:268)
at org.h2.command.dml.Query.query(Query.java:37)
at org.h2.command.CommandContainer.query(CommandContainer.java:80)
at org.h2.command.Command.executeQuery(Command.java:181)
at org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:96)
我的解码功能如下:
public final static Value decode(Value expression, Value ... paramValues) {
boolean param = true;
boolean hit = false;
Value returnValue = null;
Value defaultValue = null;
// Walk through all parameters, alternately the 'param' and corresponding 'value'.
// If 'param' is equal the expression, then return the next 'value'.
// If no hit, the return the last 'param' value as default value.
for (Value str : paramValues) {
if (param) {
defaultValue = str; // In case this is the last parameter.
// Remember the hit. The next value will be returned.
hit = (MiscUtil.equals(expression, str));
} else {
if (hit) {
returnValue = str;
break; // return str;
}
defaultValue = null;
}
param = ! param;
}
return (returnValue==null) ? defaultValue : returnValue;
}
我的解码功能有什么问题吗
我尝试在decode函数中返回Object而不是Value,并在末尾添加了以下代码:
Object returnObject=null;
if (returnValue instanceof ValueDecimal) {
returnObject = ((ValueDecimal)returnValue).getBigDecimal();
} else if (returnValue instanceof ValueString) {
returnObject = ((ValueString)returnValue).getString();
} else if (returnValue instanceof ValueDate) {
returnObject = ((ValueDate)returnValue).getDate();
}
return returnValue;
但我得到的结果是:
org.h2.jdbc.JdbcSQLException: Data conversion error converting "aced0005737200146a6176612e6d6174682e426967446563696d616c54c71557f981284f0300024900057363616c654c0006696e7456616c7400164c6a6176612f6d6174682f426967496e74656765723b787200106a6176612e6c616e672e4e756d62657286ac951d0b94e08b020000787000000001737200146a6176612e6d6174682e426967496e74656765728cfc9f1fa93bfb1d030006490008626974436f756e744900096269744c656e67746849001366697273744e6f6e7a65726f427974654e756d49000c6c6f776573745365744269744900067369676e756d5b00096d61676e69747564657400025b427871007e0002fffffffffffffffffffffffefffffffe00000001757200025b42acf317f8060854e0020000787000000001287878"; SQL statement:
select 6.0 - cast(decode(1.0,2.0,3.0,4.0) as double) xxx from dual ; [22018-157]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
at org.h2.message.DbException.get(DbException.java:156)
at org.h2.value.Value.convertTo(Value.java:855)
at org.h2.expression.Function.getSimpleValue(Function.java:733)
at org.h2.expression.Function.getValueWithArgs(Function.java:893)
at org.h2.expression.Function.getValue(Function.java:432)
at org.h2.expression.Operation.getValue(Operation.java:113)
at org.h2.expression.Alias.getValue(Alias.java:35)
...
我还尝试了一些没有运气的ValueExpression
完全支持H2中的解码将是最好的解决方案。这是您可以提供给Thomas的吗?H2认为数据类型是JAVA_对象,因此希望将参数(6.0和解码结果)转换为JAVA_对象,这意味着首先转换为字节数组。这是失败的 我没有亲自测试,但显式CAST应该可以工作:
select 6.0 - cast(decode(1.0,2.0,3.0,4.0) as double) from dual
我知道这有点难看。你的建议给出了完全相同的例外。如果我将“6.0-”移到decode中,它可以正常工作:从dual中选择decode(1.0、2.0、6.0-3.0、6.0-4.0),但我们在许多视图和SQL调用中都有很多这样的构造,因此查找和更改所有这些是一个糟糕的解决方案。有没有办法对DECODE函数进行编码以处理“6.0-DECODE”(…”?对不起,你的建议很有效!!!但我仍然希望使用另一种解决方案来避免更改代码。一种可能的解决方案是在H2数据库本身中添加对
decode
的支持。另一种解决方案是提供一种方法来计算函数内的数据类型,而不必运行函数。支持decode对我来说,H2本身当然是最好的解决方案。使用标准SQL[CASE WHEN
而不是适当的解码[1]?[1]: