H2 Oracle解码函数

发布于 2024-12-05 08:27:11 字数 4227 浏览 0 评论 0原文

在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,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;
 }

我的解码函数有什么问题吗?


我尝试在解码函数中返回 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 解码将是最好的解决方案。这是你能提供给托马斯的吗?

In H2, I have written a Java decode function. It works with the code:

String sql = "select decode(1.0,2.0,3.0,4.0) from dual ;";
PreparedStatement stmt = connection.prepareStatement(sql);
ResultSet resultSet = (ResultSet) stmt.executeQuery();

But the code

String sql = "select 6.0 - decode(1.0,2.0,3.0,4.0) from dual ;";

gives the error:

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)

My decode function is as:

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;
 }

Is there anything wrong with my decode function?


I have tried to return Object instead of Value in the decode function, and added this code at the end:

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;

But the I got:

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)
...

I also did some try with ValueExpression without luck.

Full support for decode in H2 would be the best solution. Is that something you can provide Thomas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不打扰别人 2024-12-12 08:27:11

H2认为数据类型是JAVA_OBJECT,因此想将参数(6.0和解码的结果)转换为JAVA_OBJECT,即先转换为字节数组。这失败了。

我自己没有测试过,但显式 CAST 应该可以工作:

select 6.0 - cast(decode(1.0,2.0,3.0,4.0) as double) from dual

我知道这有点难看。

H2 thinks the data type is JAVA_OBJECT, and therefore wants to convert the parameters (6.0 and the result of the decode) to JAVA_OBJECT, which means first convert to a byte array. This fails.

I didn't test it myself, but explicit CAST should work:

select 6.0 - cast(decode(1.0,2.0,3.0,4.0) as double) from dual

It's a bit ugly I know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文