Search in sources :

Example 71 with Expression

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

the class InMemoryExpressionEvaluator method processNotInExpression.

/* (non-Javadoc)
     * @see org.datanucleus.query.evaluator.AbstractExpressionEvaluator#processNotInExpression(org.datanucleus.query.expression.Expression)
     */
@Override
protected Object processNotInExpression(Expression expr) {
    if (expr instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) expr;
        Expression left = dyExpr.getLeft();
        Expression right = dyExpr.getRight();
        Object leftVal = getValueForExpression(left);
        Object rightVal = getValueForExpression(right);
        if (rightVal instanceof Collection) {
            Boolean result = Boolean.TRUE;
            Iterator rightValIter = ((Collection) rightVal).iterator();
            while (rightValIter.hasNext()) {
                Object rightElem = rightValIter.next();
                if (leftVal.equals(rightElem)) {
                    result = Boolean.FALSE;
                    break;
                }
            }
            stack.push(result);
            return result;
        }
        Boolean result = leftVal.equals(rightVal) ? Boolean.FALSE : Boolean.TRUE;
        stack.push(result);
        return result;
    }
    return super.processNotInExpression(expr);
}
Also used : CaseExpression(org.datanucleus.query.expression.CaseExpression) ArrayExpression(org.datanucleus.query.expression.ArrayExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) 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) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) Iterator(java.util.Iterator) Collection(java.util.Collection) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 72 with Expression

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

the class NullIfFunction 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 ignored, InMemoryExpressionEvaluator eval) {
    List<Expression> args = expr.getArguments();
    if (args == null || args.isEmpty()) {
        throw new NucleusException("NULLIF requires two arguments");
    } else if (args.size() == 1) {
        return getValueForArgExpression(args.get(0), eval);
    }
    Expression argExpr1 = args.get(0);
    Expression argExpr2 = args.get(1);
    Object argValue1 = getValueForArgExpression(argExpr1, eval);
    Object argValue2 = getValueForArgExpression(argExpr2, eval);
    if (argValue1 == argValue2) {
        return null;
    }
    return argValue1;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) NucleusException(org.datanucleus.exceptions.NucleusException)

Example 73 with Expression

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

the class CoalesceFunction 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 ignored, InMemoryExpressionEvaluator eval) {
    List<Expression> args = expr.getArguments();
    if (args == null || args.isEmpty()) {
        return null;
    }
    Iterator<Expression> iter = args.iterator();
    Object argValue = null;
    while (iter.hasNext()) {
        Expression argExpr = iter.next();
        argValue = getValueForArgExpression(argExpr, eval);
        if (argValue != null) {
            return argValue;
        }
    }
    return null;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression)

Example 74 with Expression

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

the class QueryUtils method getParameterExpressionForPosition.

/**
 * Convenience method to return the ParameterExpression for the specified position
 * if found in the expression tree starting at <pre>rootExpr</pre>
 * @param rootExpr The expression
 * @param pos The position
 * @return The ParameterExpression (if found)
 */
public static ParameterExpression getParameterExpressionForPosition(Expression rootExpr, int pos) {
    if (rootExpr instanceof ParameterExpression && ((ParameterExpression) rootExpr).getPosition() == pos) {
        return (ParameterExpression) rootExpr;
    }
    if (rootExpr.getLeft() != null) {
        ParameterExpression paramExpr = getParameterExpressionForPosition(rootExpr.getLeft(), pos);
        if (paramExpr != null) {
            return paramExpr;
        }
    }
    if (rootExpr.getRight() != null) {
        ParameterExpression paramExpr = getParameterExpressionForPosition(rootExpr.getRight(), pos);
        if (paramExpr != null) {
            return paramExpr;
        }
    }
    if (rootExpr instanceof InvokeExpression) {
        InvokeExpression invokeExpr = (InvokeExpression) rootExpr;
        List<Expression> args = invokeExpr.getArguments();
        if (args != null) {
            Iterator<Expression> argIter = args.iterator();
            while (argIter.hasNext()) {
                ParameterExpression paramExpr = getParameterExpressionForPosition(argIter.next(), pos);
                if (paramExpr != null) {
                    return paramExpr;
                }
            }
        }
    }
    return null;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) OrderExpression(org.datanucleus.query.expression.OrderExpression) ParameterExpression(org.datanucleus.query.expression.ParameterExpression)

Example 75 with Expression

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

the class JavaQueryCompiler method compileHaving.

public Expression compileHaving() {
    if (having == null) {
        return null;
    }
    Node node = parser.parse(having);
    if (candidateAliasOrig != null) {
        swapCandidateAliasNodeName(node);
    }
    if (parameterSubtitutionMap != null) {
        node = swapSubqueryParameters(node);
    }
    ExpressionCompiler comp = new ExpressionCompiler();
    comp.setSymbolTable(symtbl);
    comp.setMethodAliases(queryMgr.getQueryMethodAliasesByPrefix());
    Expression expr = comp.compileExpression(node);
    expr.bind(symtbl);
    return expr;
}
Also used : ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Expression(org.datanucleus.query.expression.Expression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ExpressionCompiler(org.datanucleus.query.expression.ExpressionCompiler)

Aggregations

Expression (org.datanucleus.query.expression.Expression)93 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)81 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)66 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)65 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)65 VariableExpression (org.datanucleus.query.expression.VariableExpression)57 NucleusException (org.datanucleus.exceptions.NucleusException)41 Literal (org.datanucleus.query.expression.Literal)37 OrderExpression (org.datanucleus.query.expression.OrderExpression)31 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)28 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)25 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)24 CreatorExpression (org.datanucleus.query.expression.CreatorExpression)23 ClassExpression (org.datanucleus.query.expression.ClassExpression)22 JoinExpression (org.datanucleus.query.expression.JoinExpression)22 SubqueryExpression (org.datanucleus.query.expression.SubqueryExpression)22 HashMap (java.util.HashMap)17 ArrayExpression (org.datanucleus.query.expression.ArrayExpression)17 CaseExpression (org.datanucleus.query.expression.CaseExpression)17 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)16