Search in sources :

Example 16 with ParameterExpression

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

the class CoalesceFunction method getValueForArgExpression.

protected Object getValueForArgExpression(Expression argExpr, InMemoryExpressionEvaluator eval) {
    Object argValue = null;
    if (argExpr instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) argExpr;
        argValue = eval.getValueForPrimaryExpression(primExpr);
    } else if (argExpr instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) argExpr;
        argValue = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), paramExpr);
    } else if (argExpr instanceof Literal) {
        argValue = ((Literal) argExpr).getLiteral();
    } else {
        throw new NucleusException("Don't support COALESCE with argument of type " + argExpr.getClass().getName());
    }
    return argValue;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 17 with ParameterExpression

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

the class ConcatFunction 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 Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else {
        throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    Object param2 = expr.getArguments().get(1);
    Object param2Value = null;
    if (param2 instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) param;
        param2Value = eval.getValueForPrimaryExpression(primExpr);
    } else if (param2 instanceof ParameterExpression) {
        ParameterExpression param2Expr = (ParameterExpression) param2;
        param2Value = QueryUtils.getValueForParameterExpression(eval.getParameterValues(), param2Expr);
    } else if (param2 instanceof Literal) {
        param2Value = ((Literal) param).getLiteral();
    } else {
        throw new NucleusException(method + "(param, param2) where param2 is instanceof " + param2.getClass().getName() + " not supported");
    }
    if (paramValue == null) {
        return null;
    }
    return ((String) paramValue).concat((String) param2Value);
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 18 with ParameterExpression

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

the class LengthFunction 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 Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else {
        throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    if (paramValue == null) {
        return Integer.valueOf(0);
    }
    if (!(paramValue instanceof String)) {
        throw new NucleusException(Localiser.msg("021011", method, paramValue.getClass().getName()));
    }
    return Integer.valueOf(((String) paramValue).length());
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 19 with ParameterExpression

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

the class UpperFunction 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 Literal) {
        paramValue = ((Literal) param).getLiteral();
    } else {
        throw new NucleusException(method + "(param) where param is instanceof " + param.getClass().getName() + " not supported");
    }
    if (paramValue == null) {
        return null;
    }
    if (!(paramValue instanceof String)) {
        throw new NucleusException(Localiser.msg("021011", method, paramValue.getClass().getName()));
    }
    return ((String) paramValue).toUpperCase();
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 20 with ParameterExpression

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

the class JDOQLCompiler method containsOnlyGroupingOrAggregates.

/**
 * Convenience method to check the provided expression for whether it contains only grouping expressions
 * or aggregates
 * @param expr The expression to check
 * @param exprGrouping The grouping expressions
 * @return Whether it contains only grouping or aggregates
 */
private static boolean containsOnlyGroupingOrAggregates(Expression expr, Expression[] exprGrouping) {
    if (expr == null) {
        return true;
    } else if (expr instanceof DyadicExpression) {
        Expression left = expr.getLeft();
        Expression right = expr.getRight();
        if (!containsOnlyGroupingOrAggregates(left, exprGrouping)) {
            return false;
        }
        if (!containsOnlyGroupingOrAggregates(right, exprGrouping)) {
            return false;
        }
        return true;
    } else if (expr instanceof InvokeExpression) {
        InvokeExpression invExpr = (InvokeExpression) expr;
        if (isExpressionGroupingOrAggregate(invExpr, exprGrouping)) {
            return true;
        }
        Expression invokedExpr = invExpr.getLeft();
        if (invokedExpr != null && !containsOnlyGroupingOrAggregates(invokedExpr, exprGrouping)) {
            // Check invoked object
            return false;
        }
        List<Expression> invArgs = invExpr.getArguments();
        if (invArgs != null) {
            // Check invocation arguments
            Iterator<Expression> iter = invArgs.iterator();
            while (iter.hasNext()) {
                Expression argExpr = iter.next();
                if (!containsOnlyGroupingOrAggregates(argExpr, exprGrouping)) {
                    return false;
                }
            }
        }
        return true;
    } else if (expr instanceof PrimaryExpression) {
        return isExpressionGroupingOrAggregate(expr, exprGrouping);
    } else if (expr instanceof Literal) {
        return true;
    } else if (expr instanceof ParameterExpression) {
        return true;
    } else if (expr instanceof VariableExpression) {
        return true;
    }
    return false;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Literal(org.datanucleus.query.expression.Literal) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

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