Search in sources :

Example 26 with Expression

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

the class VarThisCompilationOptimiser method replaceVariableWithCandidateInExpression.

/**
 * Method that replaces any occurrence of the specified variable in the provided expression with the
 * candidate primary expression. Recurses to sub-expressions.
 * @param varName Variable name
 * @param expr The expression to update
 * @return Updated expression
 */
private Expression replaceVariableWithCandidateInExpression(String varName, Expression expr) {
    if (expr == null) {
        return null;
    }
    if (expr instanceof VariableExpression && ((VariableExpression) expr).getId().equals(varName)) {
        List<String> tuples = new ArrayList<String>();
        tuples.add(compilation.getCandidateAlias());
        Expression replExpr = new PrimaryExpression(tuples);
        replExpr.bind(compilation.getSymbolTable());
        return replExpr;
    } else if (expr instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) expr;
        if (dyExpr.getLeft() != null) {
            dyExpr.setLeft(replaceVariableWithCandidateInExpression(varName, dyExpr.getLeft()));
        }
        if (dyExpr.getRight() != null) {
            dyExpr.setRight(replaceVariableWithCandidateInExpression(varName, dyExpr.getRight()));
        }
    } else if (expr instanceof PrimaryExpression) {
        if (expr.getLeft() != null) {
            if (expr.getLeft() instanceof VariableExpression && ((VariableExpression) expr.getLeft()).getId().equals(varName)) {
                // Needs to be relative to candidate so just remove the "left"
                expr.setLeft(null);
            } else {
                expr.setLeft(replaceVariableWithCandidateInExpression(varName, expr.getLeft()));
            }
        }
    } else if (expr instanceof InvokeExpression) {
        InvokeExpression invokeExpr = (InvokeExpression) expr;
        if (invokeExpr.getLeft() != null) {
            invokeExpr.setLeft(replaceVariableWithCandidateInExpression(varName, invokeExpr.getLeft()));
        }
    // List<Expression> args = invokeExpr.getArguments(); // TODO Process Invoke args
    }
    // TODO More combinations
    return expr;
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) ArrayList(java.util.ArrayList) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 27 with Expression

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

the class JavaQueryInMemoryEvaluator method handleAggregates.

/**
 * Checks if there are aggregates and handle it.
 * @param resultSet The resultSet containing all elements
 * @return A list with aggregated elements
 */
private List handleAggregates(List resultSet) {
    final Expression[] grouping = compilation.getExprGrouping();
    Comparator c = new Comparator() {

        public int compare(Object arg0, Object arg1) {
            for (int i = 0; i < grouping.length; i++) {
                state.put(candidateAlias, arg0);
                Object a = grouping[i].evaluate(evaluator);
                state.put(candidateAlias, arg1);
                Object b = grouping[i].evaluate(evaluator);
                // Put any null values at the end
                if (a == null && b == null) {
                    return 0;
                } else if (a == null) {
                    return -1;
                } else if (b == null) {
                    return 1;
                } else {
                    int result = ((Comparable) a).compareTo(b);
                    if (result != 0) {
                        return result;
                    }
                }
            }
            return 0;
        }
    };
    List groups = new ArrayList();
    List group = new ArrayList();
    groups.add(group);
    for (int i = 0; i < resultSet.size(); i++) {
        if (i > 0) {
            if (c.compare(resultSet.get(i - 1), resultSet.get(i)) != 0) {
                group = new ArrayList();
                groups.add(group);
            }
        }
        group.add(resultSet.get(i));
    }
    List result = new ArrayList();
    Expression having = compilation.getExprHaving();
    if (having != null) {
        for (int i = 0; i < groups.size(); i++) {
            if (satisfiesHavingClause((List) groups.get(i))) {
                result.addAll((Collection) groups.get(i));
            }
        }
    } else {
        for (int i = 0; i < groups.size(); i++) {
            result.addAll((Collection) groups.get(i));
        }
    }
    return result;
}
Also used : Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) CreatorExpression(org.datanucleus.query.expression.CreatorExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Comparator(java.util.Comparator)

Example 28 with Expression

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

the class ComparableExpressionImpl method lt.

/* (non-Javadoc)
     * @see org.datanucleus.query.typesafe.Expression#lt(java.lang.Object)
     */
public BooleanExpression lt(T t) {
    Expression leftQueryExpr = queryExpr;
    Expression rightQueryExpr = new Literal(t);
    org.datanucleus.query.expression.Expression queryExpr = new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_LT, rightQueryExpr);
    return new BooleanExpressionImpl(queryExpr);
}
Also used : DyadicExpression(org.datanucleus.query.expression.DyadicExpression) NumericExpression(javax.jdo.query.NumericExpression) ComparableExpression(javax.jdo.query.ComparableExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) OrderExpression(javax.jdo.query.OrderExpression) PersistableExpression(javax.jdo.query.PersistableExpression) BooleanExpression(javax.jdo.query.BooleanExpression) Literal(org.datanucleus.query.expression.Literal) Expression(org.datanucleus.query.expression.Expression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 29 with Expression

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

the class ComparableExpressionImpl method gt.

/* (non-Javadoc)
     * @see org.datanucleus.query.typesafe.Expression#gt(org.datanucleus.query.typesafe.ComparableExpression)
     */
public BooleanExpression gt(ComparableExpression expr) {
    Expression leftQueryExpr = queryExpr;
    Expression rightQueryExpr = ((ExpressionImpl) expr).getQueryExpression();
    org.datanucleus.query.expression.Expression queryExpr = new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_GT, rightQueryExpr);
    return new BooleanExpressionImpl(queryExpr);
}
Also used : DyadicExpression(org.datanucleus.query.expression.DyadicExpression) NumericExpression(javax.jdo.query.NumericExpression) ComparableExpression(javax.jdo.query.ComparableExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) OrderExpression(javax.jdo.query.OrderExpression) PersistableExpression(javax.jdo.query.PersistableExpression) BooleanExpression(javax.jdo.query.BooleanExpression) Expression(org.datanucleus.query.expression.Expression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 30 with Expression

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

the class ComparableExpressionImpl method gt.

/* (non-Javadoc)
     * @see org.datanucleus.query.typesafe.Expression#gt(java.lang.Object)
     */
public BooleanExpression gt(T t) {
    Expression leftQueryExpr = queryExpr;
    Expression rightQueryExpr = new Literal(t);
    org.datanucleus.query.expression.Expression queryExpr = new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_GT, rightQueryExpr);
    return new BooleanExpressionImpl(queryExpr);
}
Also used : DyadicExpression(org.datanucleus.query.expression.DyadicExpression) NumericExpression(javax.jdo.query.NumericExpression) ComparableExpression(javax.jdo.query.ComparableExpression) Expression(org.datanucleus.query.expression.Expression) InvokeExpression(org.datanucleus.query.expression.InvokeExpression) OrderExpression(javax.jdo.query.OrderExpression) PersistableExpression(javax.jdo.query.PersistableExpression) BooleanExpression(javax.jdo.query.BooleanExpression) Literal(org.datanucleus.query.expression.Literal) Expression(org.datanucleus.query.expression.Expression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

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