Search in sources :

Example 1 with JDOQLSymbolResolver

use of org.datanucleus.query.compiler.JDOQLSymbolResolver 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)

Aggregations

Iterator (java.util.Iterator)1 JDOQLSymbolResolver (org.datanucleus.query.compiler.JDOQLSymbolResolver)1 PropertySymbol (org.datanucleus.query.compiler.PropertySymbol)1 QueryCompilation (org.datanucleus.query.compiler.QueryCompilation)1 SymbolTable (org.datanucleus.query.compiler.SymbolTable)1 DyadicExpression (org.datanucleus.query.expression.DyadicExpression)1 Expression (org.datanucleus.query.expression.Expression)1 InvokeExpression (org.datanucleus.query.expression.InvokeExpression)1 ParameterExpression (org.datanucleus.query.expression.ParameterExpression)1 PrimaryExpression (org.datanucleus.query.expression.PrimaryExpression)1 VariableExpression (org.datanucleus.query.expression.VariableExpression)1