diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Args.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Args.java index 390282138..ed9520d64 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Args.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Args.java @@ -171,11 +171,17 @@ public class Args return 0; } - public void appendTo(Select sel, ExpContext ctx, ExpState state, - SQLBuffer sql, int index) { + public void appendTo(Select sel, ExpContext ctx, ExpState state, SQLBuffer sql, int index) { + appendTo(sel, ctx, state, sql, index, null); + } + + public void appendTo(Select sel, ExpContext ctx, ExpState state, SQLBuffer sql, int index, String operator) { ArgsExpState astate = (ArgsExpState) state; for (int i = 0; i < _args.length; i++) { _args[i].appendTo(sel, ctx, astate.states[i], sql, index); + if( operator != null ) { + sql.addCastForParam(operator, _args[i]); + } if (i < _args.length-1) sql.append(", "); } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/DatastoreFunction.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/DatastoreFunction.java index 9a0c6172b..49586d1d3 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/DatastoreFunction.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/DatastoreFunction.java @@ -46,16 +46,12 @@ public class DatastoreFunction extends UnaryOp { public void appendTo(Select sel, ExpContext ctx, ExpState state, SQLBuffer sql, int index) { Args args = (Args) getValue(); - if (!ctx.store.getDBDictionary().requiresCastForMathFunctions || args.getValues().length == 1) + if (!ctx.store.getDBDictionary().requiresCastForMathFunctions || args.getValues().length == 1) { super.appendTo(sel, ctx, state, sql, index); - else { + } else { sql.append(getOperator()); sql.append("("); - args.appendTo(sel, ctx, state, sql, 0); - Val[] vals = args.getVals(); - for (int i = 1; i < vals.length; i++) { - sql.addCastForParam(getOperator(), vals[i]); - } + args.appendTo(sel, ctx, state, sql, 0, getOperator()); sql.append(")"); } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java index be543b648..ad44936e8 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/UnaryOp.java @@ -139,7 +139,16 @@ abstract class UnaryOp sql.append(getOperator()); sql.append(_noParen ? " " : "("); _val.appendTo(sel, ctx, state, sql, 0); - sql.addCastForParam(getOperator(), _val); + + // OPENJPA-2149: If _val (Val) is an 'Arg', we need to get the Val[] + // from it, and the single element it contains because the + // 'addCastForParam' method gets the 'type' from the Val it receives. + // In the case where _val is an Arg, when addCastForParam gets the + // type, it will be getting the type of the Val (an Object) rather + // the type of the Arg. + sql.addCastForParam(getOperator(), + (_val instanceof Args) ? (((Args) _val).getVals())[0] + : _val); if (!_noParen) sql.append(")"); }