use of org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode in project jackrabbit by apache.
the class QueryFormat method visit.
// -------------< QueryNodeVisitor interface >-------------------------------
public Object visit(QueryRootNode node, Object data) throws RepositoryException {
StringBuffer sb = (StringBuffer) data;
try {
sb.append("SELECT");
Name[] selectProps = node.getSelectProperties();
if (selectProps.length == 0) {
sb.append(" *");
} else {
String comma = "";
for (int i = 0; i < selectProps.length; i++) {
sb.append(comma).append(" ");
appendName(selectProps[i], resolver, sb);
comma = ",";
}
}
sb.append(" FROM");
// node type restrictions are within predicates of location nodes
// therefore we write the where clause first to a temp string to
// collect the node types.
StringBuffer tmp = new StringBuffer();
LocationStepQueryNode[] steps = node.getLocationNode().getPathSteps();
QueryNode[] predicates = steps[steps.length - 1].getPredicates();
// are there any relevant predicates?
for (int i = 0; i < predicates.length; i++) {
if (predicates[i].getType() != QueryNode.TYPE_NODETYPE) {
tmp.append(" WHERE ");
}
}
String and = "";
for (int i = 0; i < predicates.length; i++) {
if (predicates[i].getType() != QueryNode.TYPE_NODETYPE) {
tmp.append(and);
and = " AND ";
}
predicates[i].accept(this, tmp);
}
// node types have been collected by now
String comma = "";
int ntCount = 0;
for (Iterator it = nodeTypes.iterator(); it.hasNext(); ntCount++) {
Name nt = (Name) it.next();
sb.append(comma).append(" ");
appendName(nt, resolver, sb);
comma = ",";
}
if (ntCount == 0) {
sb.append(" ");
sb.append(resolver.getJCRName(NameConstants.NT_BASE));
}
// append WHERE clause
sb.append(tmp.toString());
if (steps.length == 2 && steps[1].getIncludeDescendants() && steps[1].getNameTest() == null) {
// then this query selects all paths
} else if (steps.length == 1 && steps[0].getIncludeDescendants() && steps[0].getNameTest() == null) {
// then this query selects all paths
} else {
if (predicates.length > 0) {
sb.append(" AND ");
} else {
sb.append(" WHERE ");
}
node.getLocationNode().accept(this, sb);
}
} catch (NamespaceException e) {
exceptions.add(e);
}
if (node.getOrderNode() != null) {
node.getOrderNode().accept(this, sb);
}
return sb;
}
use of org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode in project jackrabbit by apache.
the class JCRSQLQueryBuilder method visit.
public Object visit(ASTQuery node, Object data) {
root = factory.createQueryRootNode();
root.setLocationNode(factory.createPathQueryNode(root));
// pass to select, from, where, ...
node.childrenAccept(this, root);
// use //* if no path has been set
PathQueryNode pathNode = root.getLocationNode();
pathNode.setAbsolute(true);
if (pathConstraints.size() == 0) {
LocationStepQueryNode step = factory.createLocationStepQueryNode(pathNode);
step.setNameTest(null);
step.setIncludeDescendants(true);
pathNode.addPathStep(step);
} else {
try {
while (pathConstraints.size() > 1) {
// merge path nodes
MergingPathQueryNode path = null;
for (Iterator it = pathConstraints.iterator(); it.hasNext(); ) {
path = (MergingPathQueryNode) it.next();
if (path.needsMerge()) {
break;
} else {
path = null;
}
}
if (path == null) {
throw new IllegalArgumentException("Invalid combination of jcr:path clauses");
} else {
pathConstraints.remove(path);
MergingPathQueryNode[] paths = (MergingPathQueryNode[]) pathConstraints.toArray(new MergingPathQueryNode[pathConstraints.size()]);
paths = path.doMerge(paths);
pathConstraints.clear();
pathConstraints.addAll(Arrays.asList(paths));
}
}
} catch (NoSuchElementException e) {
throw new IllegalArgumentException("Invalid combination of jcr:path clauses");
}
MergingPathQueryNode path = (MergingPathQueryNode) pathConstraints.get(0);
LocationStepQueryNode[] steps = path.getPathSteps();
for (int i = 0; i < steps.length; i++) {
LocationStepQueryNode step = factory.createLocationStepQueryNode(pathNode);
step.setNameTest(steps[i].getNameTest());
step.setIncludeDescendants(steps[i].getIncludeDescendants());
step.setIndex(steps[i].getIndex());
pathNode.addPathStep(step);
}
}
if (constraintNode.getNumOperands() == 1) {
// attach operand to last path step
LocationStepQueryNode[] steps = pathNode.getPathSteps();
steps[steps.length - 1].addPredicate(constraintNode.getOperands()[0]);
} else if (constraintNode.getNumOperands() > 1) {
// attach constraint to last path step
LocationStepQueryNode[] steps = pathNode.getPathSteps();
steps[steps.length - 1].addPredicate(constraintNode);
}
if (nodeTypeName != null) {
// add node type constraint
LocationStepQueryNode[] steps = pathNode.getPathSteps();
NodeTypeQueryNode nodeType = factory.createNodeTypeQueryNode(steps[steps.length - 1], nodeTypeName);
steps[steps.length - 1].addPredicate(nodeType);
}
return root;
}
Aggregations