Search in sources :

Example 6 with PrimaryExpression

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

the class MapGetMethod 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 Map)) {
        throw new NucleusException(Localiser.msg("021011", method, invokedValue.getClass().getName()));
    }
    Object param = expr.getArguments().get(0);
    Object paramValue = null;
    if (param instanceof Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else 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 VariableExpression) {
        VariableExpression varExpr = (VariableExpression) param;
        try {
            paramValue = eval.getValueForVariableExpression(varExpr);
        } catch (VariableNotSetException vnse) {
            // Throw an exception with the possible values of values
            throw new VariableNotSetException(varExpr, ((Map) invokedValue).values().toArray());
        }
    } else {
        throw new NucleusException("Dont currently support use of get(" + param.getClass().getName() + ")");
    }
    return ((Map) invokedValue).get(paramValue);
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) VariableNotSetException(org.datanucleus.query.inmemory.VariableNotSetException) Literal(org.datanucleus.query.expression.Literal) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) NucleusException(org.datanucleus.exceptions.NucleusException) Map(java.util.Map)

Example 7 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression 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 8 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression 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 9 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression 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 10 with PrimaryExpression

use of org.datanucleus.query.expression.PrimaryExpression 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)

Aggregations

PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)77 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)73 Literal (org.datanucleus.query.expression.Literal)60 NucleusException (org.datanucleus.exceptions.NucleusException)59 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)54 VariableExpression (org.datanucleus.query.expression.VariableExpression)48 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)46 Expression (org.datanucleus.query.expression.Expression)46 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)25 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)23 OrderExpression (org.datanucleus.query.expression.OrderExpression)22 HashMap (java.util.HashMap)16 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)15 ClassExpression (org.datanucleus.query.expression.ClassExpression)15 JoinExpression (org.datanucleus.query.expression.JoinExpression)15 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)15 Product (org.datanucleus.samples.store.Product)15 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)12 JPQLCompiler (org.datanucleus.query.compiler.JPQLCompiler)9 List (java.util.List)8