Search in sources :

Example 31 with DyadicExpression

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

the class ExpressionImpl method eq.

/* (non-Javadoc)
     * @see org.datanucleus.query.typesafe.Expression#eq(java.lang.Object)
     */
public BooleanExpression eq(T t) {
    org.datanucleus.query.expression.Expression leftQueryExpr = queryExpr;
    org.datanucleus.query.expression.Expression rightQueryExpr = new Literal(t);
    return new BooleanExpressionImpl(new DyadicExpression(leftQueryExpr, org.datanucleus.query.expression.Expression.OP_EQ, rightQueryExpr));
}
Also used : Literal(org.datanucleus.query.expression.Literal) DyadicExpression(org.datanucleus.query.expression.DyadicExpression)

Example 32 with DyadicExpression

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

the class AbstractJDOQLTypedQuery method compile.

/**
 * Method to compile the query as it is currently defined.
 * @param mmgr Metadata manager
 * @param clr ClassLoader resolver
 * @return The generic compilation
 */
protected QueryCompilation compile(MetaDataManager mmgr, ClassLoaderResolver clr) {
    SymbolTable symtbl = new SymbolTable();
    symtbl.setSymbolResolver(new JDOQLSymbolResolver(mmgr, clr, symtbl, candidateCls, candidateAlias));
    symtbl.addSymbol(new PropertySymbol(candidateAlias, candidateCls));
    org.datanucleus.query.expression.Expression[] resultExprs = null;
    if (result != null && !result.isEmpty()) {
        resultExprs = new org.datanucleus.query.expression.Expression[result.size()];
        Iterator iter = result.iterator();
        int i = 0;
        while (iter.hasNext()) {
            ExpressionImpl result = (ExpressionImpl) iter.next();
            org.datanucleus.query.expression.Expression resultExpr = result.getQueryExpression();
            resultExpr.bind(symtbl);
            resultExprs[i++] = resultExpr;
        }
        if (resultExprs.length == 1 && resultExprs[0] instanceof PrimaryExpression) {
            // Check for special case of "Object(p)" in result, which means no special result
            String resultExprId = ((PrimaryExpression) resultExprs[0]).getId();
            if (resultExprId.equalsIgnoreCase(candidateAlias)) {
                resultExprs = null;
            }
        }
    }
    org.datanucleus.query.expression.Expression filterExpr = null;
    if (filter != null) {
        filterExpr = filter.getQueryExpression();
        if (filterExpr != null) {
            filterExpr.bind(symtbl);
        }
    }
    org.datanucleus.query.expression.Expression[] groupingExprs = null;
    if (grouping != null && !grouping.isEmpty()) {
        groupingExprs = new org.datanucleus.query.expression.Expression[grouping.size()];
        Iterator iter = grouping.iterator();
        int i = 0;
        while (iter.hasNext()) {
            ExpressionImpl grp = (ExpressionImpl) iter.next();
            org.datanucleus.query.expression.Expression groupingExpr = grp.getQueryExpression();
            groupingExpr.bind(symtbl);
            groupingExprs[i++] = groupingExpr;
        }
    }
    org.datanucleus.query.expression.Expression havingExpr = null;
    if (having != null) {
        havingExpr = having.getQueryExpression();
        havingExpr.bind(symtbl);
    }
    org.datanucleus.query.expression.Expression[] orderExprs = null;
    if (ordering != null && !ordering.isEmpty()) {
        orderExprs = new org.datanucleus.query.expression.Expression[ordering.size()];
        Iterator<OrderExpressionImpl> iter = ordering.iterator();
        int i = 0;
        while (iter.hasNext()) {
            OrderExpressionImpl order = iter.next();
            org.datanucleus.query.expression.OrderExpression orderExpr = new org.datanucleus.query.expression.OrderExpression(((ExpressionImpl) order.getExpression()).getQueryExpression(), order.getDirection() == OrderDirection.ASC ? "ascending" : "descending");
            orderExpr.bind(symtbl);
            orderExprs[i++] = orderExpr;
        }
    }
    org.datanucleus.query.expression.Expression[] updateExprs = null;
    if (this.updateExprs != null) {
        Iterator<ExpressionImpl> expIter = this.updateExprs.iterator();
        Iterator<ExpressionImpl> valIter = this.updateVals.iterator();
        updateExprs = new Expression[this.updateExprs.size()];
        int i = 0;
        while (expIter.hasNext()) {
            ExpressionImpl updateExpr = expIter.next();
            ExpressionImpl updateVal = valIter.next();
            updateExprs[i++] = new DyadicExpression(updateExpr.getQueryExpression(), Expression.OP_EQ, updateVal.getQueryExpression());
        }
    }
    compilation = new QueryCompilation(candidateCls, candidateAlias, symtbl, resultExprs, null, filterExpr, groupingExprs, havingExpr, orderExprs, updateExprs);
    compilation.setQueryLanguage(Query.LANGUAGE_JDOQL);
    return compilation;
}
Also used : PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) PropertySymbol(org.datanucleus.query.compiler.PropertySymbol) Expression(org.datanucleus.query.expression.Expression) SymbolTable(org.datanucleus.query.compiler.SymbolTable) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) 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) JDOQLSymbolResolver(org.datanucleus.query.compiler.JDOQLSymbolResolver) QueryCompilation(org.datanucleus.query.compiler.QueryCompilation)

Example 33 with DyadicExpression

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

the class AbstractJDOQLTypedQuery method getJDOQLForExpression.

public String getJDOQLForExpression(Expression expr) {
    if (expr instanceof DyadicExpression) {
        DyadicExpression dyExpr = (DyadicExpression) expr;
        Expression left = dyExpr.getLeft();
        Expression right = dyExpr.getRight();
        StringBuilder str = new StringBuilder("(");
        if (dyExpr.getOperator() == Expression.OP_DISTINCT) {
            // Distinct goes in front of the left expression
            str.append("DISTINCT ");
        }
        if (left != null) {
            str.append(getJDOQLForExpression(left));
        }
        // Special cases
        if (dyExpr.getOperator() == Expression.OP_AND) {
            str.append(" && ");
        } else if (dyExpr.getOperator() == Expression.OP_OR) {
            str.append(" || ");
        } else if (dyExpr.getOperator() == Expression.OP_ADD) {
            str.append(" + ");
        } else if (dyExpr.getOperator() == Expression.OP_SUB) {
            str.append(" - ");
        } else if (dyExpr.getOperator() == Expression.OP_MUL) {
            str.append(" * ");
        } else if (dyExpr.getOperator() == Expression.OP_DIV) {
            str.append(" / ");
        } else if (dyExpr.getOperator() == Expression.OP_EQ) {
            str.append(" == ");
        } else if (dyExpr.getOperator() == Expression.OP_GT) {
            str.append(" > ");
        } else if (dyExpr.getOperator() == Expression.OP_LT) {
            str.append(" < ");
        } else if (dyExpr.getOperator() == Expression.OP_GTEQ) {
            str.append(" >= ");
        } else if (dyExpr.getOperator() == Expression.OP_LTEQ) {
            str.append(" <= ");
        } else if (dyExpr.getOperator() == Expression.OP_NOTEQ) {
            str.append(" != ");
        } else if (dyExpr.getOperator() == Expression.OP_DISTINCT) {
        // Processed above
        } else {
            // TODO Support other operators
            throw new UnsupportedOperationException("Dont currently support operator " + dyExpr.getOperator() + " in JDOQL conversion");
        }
        if (right != null) {
            str.append(getJDOQLForExpression(right));
        }
        str.append(")");
        return str.toString();
    } else if (expr instanceof PrimaryExpression) {
        PrimaryExpression primExpr = (PrimaryExpression) expr;
        if (primExpr.getLeft() != null) {
            return getJDOQLForExpression(primExpr.getLeft()) + "." + primExpr.getId();
        }
        return primExpr.getId();
    } else if (expr instanceof ParameterExpression) {
        ParameterExpression paramExpr = (ParameterExpression) expr;
        if (paramExpr.getId() != null) {
            return ":" + paramExpr.getId();
        }
        return "?" + paramExpr.getPosition();
    } else if (expr instanceof VariableExpression) {
        VariableExpression varExpr = (VariableExpression) expr;
        return varExpr.getId();
    } else if (expr instanceof InvokeExpression) {
        InvokeExpression invExpr = (InvokeExpression) expr;
        StringBuilder str = new StringBuilder();
        if (invExpr.getLeft() != null) {
            str.append(getJDOQLForExpression(invExpr.getLeft())).append(".");
        }
        str.append(invExpr.getOperation());
        str.append("(");
        List<Expression> args = invExpr.getArguments();
        if (args != null) {
            Iterator<Expression> iter = args.iterator();
            while (iter.hasNext()) {
                str.append(getJDOQLForExpression(iter.next()));
                if (iter.hasNext()) {
                    str.append(",");
                }
            }
        }
        str.append(")");
        return str.toString();
    } else if (expr instanceof Literal) {
        Literal litExpr = (Literal) expr;
        Object value = litExpr.getLiteral();
        if (value instanceof String || value instanceof Character) {
            return "'" + value.toString() + "'";
        } else if (value instanceof Boolean) {
            return ((Boolean) value ? "TRUE" : "FALSE");
        } else {
            if (litExpr.getLiteral() == null) {
                return "null";
            }
            return litExpr.getLiteral().toString();
        }
    } else {
        throw new UnsupportedOperationException("Dont currently support " + expr.getClass().getName() + " in JDOQLHelper");
    }
}
Also used : InvokeExpression(org.datanucleus.query.expression.InvokeExpression) PrimaryExpression(org.datanucleus.query.expression.PrimaryExpression) VariableExpression(org.datanucleus.query.expression.VariableExpression) DyadicExpression(org.datanucleus.query.expression.DyadicExpression) 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) ParameterExpression(org.datanucleus.query.expression.ParameterExpression) Literal(org.datanucleus.query.expression.Literal)

Example 34 with DyadicExpression

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

the class BooleanExpressionImpl method or.

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

Example 35 with DyadicExpression

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

the class BooleanExpressionImpl method and.

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

Aggregations

DyadicExpression (org.datanucleus.query.expression.DyadicExpression)51 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)43 Expression (org.datanucleus.query.expression.Expression)42 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)36 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)34 Literal (org.datanucleus.query.expression.Literal)32 VariableExpression (org.datanucleus.query.expression.VariableExpression)30 NucleusException (org.datanucleus.exceptions.NucleusException)21 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)20 JavaQueryCompiler (org.datanucleus.query.compiler.JavaQueryCompiler)18 OrderExpression (org.datanucleus.query.expression.OrderExpression)13 BooleanExpression (javax.jdo.query.BooleanExpression)12 PersistableExpression (javax.jdo.query.PersistableExpression)12 JDOQLCompiler (org.datanucleus.query.compiler.JDOQLCompiler)12 Product (org.datanucleus.samples.store.Product)12 HashMap (java.util.HashMap)11 NumericExpression (javax.jdo.query.NumericExpression)9 ComparableExpression (javax.jdo.query.ComparableExpression)8 OrderExpression (javax.jdo.query.OrderExpression)8 NucleusUserException (org.datanucleus.exceptions.NucleusUserException)7