H2 функция декодирования Oracle - PullRequest
1 голос
/ 20 сентября 2011

В 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. Это то, что ты можешь дать Томасу?

1 Ответ

2 голосов
/ 20 сентября 2011

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

Это немного некрасиво, я знаю.

...