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;
}
Aggregations