Search in sources :

Example 6 with ParameterExpression

use of org.datanucleus.query.expression.ParameterExpression in project datanucleus-core by datanucleus.

the class MathFunction method evaluate.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
     */
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
    String method = expr.getOperation();
    Object param = expr.getArguments().get(0);
    Object paramValue = null;
    if (param instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) param;
        paramValue = eval.getValueForPrimaryExpression(primExpr);
    } else if (param instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) param;
        paramValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
    } else if (param instanceof InvokeExpression) {
        InvokeExpression invokeExpr = (InvokeExpression) param;
        paramValue = eval.getValueForInvokeExpression(invokeExpr);
    } else if (param instanceof Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else if (param instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) param;
        paramValue = dyExpr.evaluate(eval);
    } else {
        throw new NucleusException(method + "(num) where num is instanceof " + param.getClass().getName() + " not supported");
    }
    Object result = null;
    if (paramValue instanceof Double) {
        result = new Double(evaluateMathFunction(((Double) paramValue).doubleValue()));
    } else if (paramValue instanceof Float) {
        result = new Float(evaluateMathFunction(((Float) paramValue).floatValue()));
    } else if (paramValue instanceof BigDecimal) {
        result = new BigDecimal(evaluateMathFunction(((BigDecimal) paramValue).doubleValue()));
    } else if (paramValue instanceof Integer) {
        result = new Double(evaluateMathFunction(((Integer) paramValue).doubleValue()));
    } else if (paramValue instanceof Long) {
        result = new Double(evaluateMathFunction(((Long) paramValue).doubleValue()));
    } else {
        throw new NucleusException("Not possible to use " + getFunctionName() + " on value of type " + paramValue.getClass().getName());
    }
    return result;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) BigDecimal(java.math.BigDecimal) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 7 with ParameterExpression

use of org.datanucleus.query.expression.ParameterExpression in project datanucleus-core by datanucleus.

the class ObjectGetClassMethod method evaluate.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
     */
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
    Expression argExpr = expr.getArguments().get(0);
    if (argExpr instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) argExpr;
        Object value = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
        return value.getClass();
    }
    return eval.getValueForPrimaryExpression((PrimaryExpression) argExpr).getClass();
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression)

Example 8 with ParameterExpression

use of org.datanucleus.query.expression.ParameterExpression in project datanucleus-core by datanucleus.

the class StringCharAtMethod method evaluate.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
     */
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
    String method = expr.getOperation();
    if (invokedValue == null) {
        return Integer.valueOf(-1);
    }
    if (!(invokedValue instanceof String)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    // Evaluate the argument
    Object arg1Obj = null;
    Object param = expr.getArguments().get(0);
    if (param instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) param;
        arg1Obj = eval.getValueForPrimaryExpression(primExpr);
    } else if (param instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) param;
        arg1Obj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
    } else if (param instanceof Literal) {
        arg1Obj = ((Literal) param).getLiteral();
    } else if (param instanceof InvokeExpression) {
        arg1Obj = eval.getValueForInvokeExpression((InvokeExpression) param);
    } else if (param instanceof DyadicExpression) {
        arg1Obj = ((DyadicExpression) param).evaluate(eval);
    } else {
        throw new NucleusException(method + "(param1) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    if (!(arg1Obj instanceof Number)) {
        throw new NucleusException(method + "(param1]) : param1 must be numeric");
    }
    int arg1 = ((Number) arg1Obj).intValue();
    return Integer.valueOf(((String) invokedValue).indexOf(arg1));
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 9 with ParameterExpression

use of org.datanucleus.query.expression.ParameterExpression in project datanucleus-core by datanucleus.

the class StringConcatMethod method evaluate.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
     */
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
    String method = expr.getOperation();
    if (invokedValue == null) {
        return null;
    }
    if (!(invokedValue instanceof String)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    Expression arg0Expr = expr.getArguments().get(0);
    Object arg0Val = null;
    if (arg0Expr instanceof PrimaryExpression) {
        arg0Val = eval.getValueForPrimaryExpression((PrimaryExpression) arg0Expr);
    } else if (arg0Expr instanceof ParameterExpression) {
        arg0Val = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), (ParameterExpression) arg0Expr);
    } else if (arg0Expr instanceof Literal) {
        arg0Val = ((Literal) arg0Expr).getLiteral();
    } else if (arg0Expr instanceof DyadicExpression) {
        arg0Val = ((DyadicExpression) arg0Expr).evaluate(eval);
    }
    if (!(arg0Val instanceof String)) {
        throw new NucleusException(method + "(param1) : param1 must be String");
    }
    return ((String) invokedValue).concat((String) arg0Val);
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 10 with ParameterExpression

use of org.datanucleus.query.expression.ParameterExpression in project datanucleus-core by datanucleus.

the class StringEqualsIgnoreCaseMethod method evaluate.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.memory.InvocationEvaluator#evaluate(org.datanucleus.query.expression.InvokeExpression, org.datanucleus.query.evaluator.memory.InMemoryExpressionEvaluator)
     */
public Object evaluate(InvokeExpression expr, Object invokedValue, InMemoryExpressionEvaluator eval) {
    String method = expr.getOperation();
    if (invokedValue == null) {
        return Boolean.FALSE;
    }
    if (!(invokedValue instanceof String)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    String arg1 = null;
    Object argObj = null;
    Object param = expr.getArguments().get(0);
    if (param instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) param;
        argObj = eval.getValueForPrimaryExpression(primExpr);
    } else if (param instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) param;
        argObj = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
    } else if (param instanceof Literal) {
        argObj = ((Literal) param).getLiteral();
    } else if (param instanceof InvokeExpression) {
        argObj = eval.getValueForInvokeExpression((InvokeExpression) param);
    } else {
        throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    arg1 = QueryUtils.getStringValue(argObj);
    return ((String) invokedValue).equalsIgnoreCase(arg1) ? Boolean.TRUE : Boolean.FALSE;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

ParameterExpression (org.datanucleus.query.expression.ParameterExpression)56 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)51 Literal (org.datanucleus.query.expression.Literal)43 NucleusException (org.datanucleus.exceptions.NucleusException)42 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)31 VariableExpression (org.datanucleus.query.expression.VariableExpression)25 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)23 Expression (org.datanucleus.query.expression.Expression)23 OrderExpression (org.datanucleus.query.expression.OrderExpression)10 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)8 List (java.util.List)7 VariableNotSetException (org.datanucleus.query.inmemory.VariableNotSetException)7 Map (java.util.Map)6 ArrayExpression (org.datanucleus.query.expression.ArrayExpression)6 CaseExpression (org.datanucleus.query.expression.CaseExpression)6 CreatorExpression (org.datanucleus.query.expression.CreatorExpression)6 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)5 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)5 ClassExpression (org.datanucleus.query.expression.ClassExpression)5 JoinExpression (org.datanucleus.query.expression.JoinExpression)5