for (Iterator i = resultSets.iterator(); i.hasNext();) {
MappingSourceNode rsNode = (MappingSourceNode)i.next();
ResultSetInfo childRsInfo = rsNode.getResultSetInfo();
QueryNode planNode = QueryUtil.getQueryNode(childRsInfo.getResultSetName(), planEnv.getGlobalMetadata());
Command command = QueryUtil.getQuery(childRsInfo.getResultSetName(), planNode, planEnv);
String inlineViewName = planEnv.getAliasName(childRsInfo.getResultSetName());
updateSymbolMap(symbolMap, childRsInfo.getResultSetName(), inlineViewName, planEnv.getGlobalMetadata());
// check if the criteria has been raised, if it is then we can update this as a join.
if (childRsInfo.isCriteriaRaised()) {
Query transformationQuery = (Query) command;
SubqueryFromClause sfc = (SubqueryFromClause)transformationQuery.getFrom().getClauses().get(0);
Criteria joinCriteria = ((Query)childRsInfo.getCommand()).getCriteria();
if (joinCriteria == null) {
joinCriteria = QueryRewriter.TRUE_CRITERIA;
}
joinCriteria = (Criteria)joinCriteria.clone();
//update the from clause
FromClause clause = (FromClause)currentQuery.getFrom().getClauses().remove(0);
JoinPredicate join = null;
if (clause instanceof JoinPredicate) {
join = (JoinPredicate)clause;
FromClause right = join.getRightClause();
JoinPredicate newRight = new JoinPredicate(right, sfc, JoinType.JOIN_LEFT_OUTER, Criteria.separateCriteriaByAnd(joinCriteria));
join.setRightClause(newRight);
} else {
join = new JoinPredicate(clause, sfc, JoinType.JOIN_LEFT_OUTER, Criteria.separateCriteriaByAnd(joinCriteria));
}
currentQuery.getFrom().addClause(join);
currentQuery.getSelect().setDistinct(true);
continue;
}
if (!singleParentage) {
throw new QueryPlannerException(QueryPlugin.Util.getString("XMLQueryPlanner.cannot_plan", rsInfo.getCriteria())); //$NON-NLS-1$
}
Query subQuery = QueryUtil.wrapQuery(new SubqueryFromClause(inlineViewName, command), inlineViewName);
currentQuery.setCriteria(Criteria.combineCriteria(currentQuery.getCriteria(), new ExistsCriteria(subQuery)));
currentQuery = subQuery;
}
Criteria userCrit = (Criteria)rsInfo.getCriteria().clone();
currentQuery.setCriteria(Criteria.combineCriteria(currentQuery.getCriteria(), userCrit));
StaticSymbolMappingVisitor.mapSymbols(contextQuery, symbolMap);
if (rsInfo.isCriteriaRaised()) {
//if allowing ancestor bindings, we need to update the bindings for the query node...
prepareQuery(contextNode, planEnv, contextQuery);
QueryUtil.rewriteQuery(contextQuery, planEnv.getGlobalMetadata(), planEnv.context);
//selectively replace correlated references with their actual element symbols
List<Reference> bindings = QueryUtil.getReferences(contextQuery);
QueryNode modifiedNode = new QueryNode(null);
modifiedNode.setCommand(contextQuery);
for (Iterator<Reference> i = bindings.iterator(); i.hasNext();) {
Reference ref = i.next();
modifiedNode.addBinding(ref.getExpression().toString());
}
GroupSymbol groupSymbol = QueryUtil.createResolvedGroup(rsInfo.getResultSetName(), planEnv.getGlobalMetadata());
planEnv.addQueryNodeToMetadata(groupSymbol.getMetadataID(), modifiedNode);
}