diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java index 4f1aaefae..5a5c25dea 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/JDBCStoreQuery.java @@ -707,6 +707,10 @@ public class JDBCStoreQuery return handleTrimVal(value, ob, params, sm); case Val.INDEXOF_VAL: return handleIndexOfVal(value, ob, params, sm); + case Val.ABS_VAL: + return handleAbsVal(value, ob, params, sm); + case Val.SQRT_VAL: + return handleSqrtVal(value, ob, params, sm); default: throw new UnsupportedException(); } @@ -853,11 +857,47 @@ public class JDBCStoreQuery (org.apache.openjpa.jdbc.kernel.exps.IndexOf) value; String val1 = (String) getValue(locateVal.getVal1(), ob, params, sm); Val[] val2 = (Val[]) getValue(locateVal.getVal2(), ob, params, sm); - String strVal = (String)getValue(val2[0], ob, params, sm); + String strVal = (String) getValue(val2[0], ob, params, sm); int idx = ((Long) getValue(val2[1], ob, params, sm)).intValue(); return strVal.indexOf(val1, idx); } + private Object handleAbsVal(Object value, Object ob, Object[] params, + OpenJPAStateManager sm) { + org.apache.openjpa.jdbc.kernel.exps.Abs absVal = + (org.apache.openjpa.jdbc.kernel.exps.Abs) value; + Object val = getValue(absVal.getValue(), ob, params, sm); + Class c = val.getClass(); + if (c == Integer.class) + return new Integer(java.lang.Math.abs(((Integer) val).intValue())); + else if (c == Float.class) + return new Float(java.lang.Math.abs(((Float) val).floatValue())); + else if (c == Double.class) + return new Double(java.lang.Math.abs(((Double) val).doubleValue())); + else if (c == Long.class) + return new Long(java.lang.Math.abs(((Long) val).longValue())); + throw new UnsupportedException(); + } + + private Object handleSqrtVal(Object value, Object ob, Object[] params, + OpenJPAStateManager sm) { + org.apache.openjpa.jdbc.kernel.exps.Sqrt sqrtVal = + (org.apache.openjpa.jdbc.kernel.exps.Sqrt) value; + Object val = getValue(sqrtVal.getValue(), ob, params, sm); + Class c = val.getClass(); + if (c == Integer.class) + return new Double(java.lang.Math.sqrt(((Integer) val). + doubleValue())); + else if (c == Float.class) + return new Double(java.lang.Math.sqrt(((Float) val).floatValue())); + else if (c == Double.class) + return new Double(java.lang.Math.sqrt(((Double) val). + doubleValue())); + else if (c == Long.class) + return new Double(java.lang.Math.sqrt(((Long) val).doubleValue())); + throw new UnsupportedException(); + } + private Object getValue(Object value, Object ob, Object[] params, OpenJPAStateManager sm) { if (value instanceof PCPath) { diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Abs.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Abs.java index 398c87532..9e7f8039b 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Abs.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Abs.java @@ -26,7 +26,7 @@ import org.apache.openjpa.kernel.Filters; /** * Absolute value. */ -class Abs +public class Abs extends UnaryOp { /** @@ -51,5 +51,9 @@ class Abs protected String getOperator() { return "ABS"; } + + public int getId() { + return Val.ABS_VAL; + } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Math.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Math.java index 4ccfb154a..f08db8f6d 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Math.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Math.java @@ -154,5 +154,9 @@ public class Math _val2.acceptVisit(visitor); visitor.exit(this); } + + public int getId() { + return Val.MATH_VAL; + } } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Sqrt.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Sqrt.java index 76db7379c..1fe57961c 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Sqrt.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Sqrt.java @@ -23,7 +23,7 @@ package org.apache.openjpa.jdbc.kernel.exps; * * @author Abe White */ -class Sqrt +public class Sqrt extends UnaryOp { /** @@ -40,5 +40,9 @@ class Sqrt protected String getOperator() { return "SQRT"; } + + public int getId() { + return Val.SQRT_VAL; + } } 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 b9d262b16..64e94eaf7 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 @@ -53,7 +53,7 @@ abstract class UnaryOp _noParen = noParen; } - protected Val getValue() { + public Val getValue() { return _val; } diff --git a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Val.java b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Val.java index 8bcabf377..d217622fa 100644 --- a/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Val.java +++ b/openjpa-jdbc/src/main/java/org/apache/openjpa/jdbc/kernel/exps/Val.java @@ -62,6 +62,8 @@ public interface Val public final int LOCATE_VAL = 8; public final int ARGS_VAL = 9; public final int INDEXOF_VAL = 10; + public final int ABS_VAL = 11; + public final int SQRT_VAL = 12; /** * Initialize the value. This method should recursively initialize any