Search in sources :

Example 11 with LocationStepQueryNode

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;
}
Also used : ExactQueryNode(org.apache.jackrabbit.spi.commons.query.ExactQueryNode) RelationQueryNode(org.apache.jackrabbit.spi.commons.query.RelationQueryNode) OrQueryNode(org.apache.jackrabbit.spi.commons.query.OrQueryNode) OrderQueryNode(org.apache.jackrabbit.spi.commons.query.OrderQueryNode) QueryNode(org.apache.jackrabbit.spi.commons.query.QueryNode) DerefQueryNode(org.apache.jackrabbit.spi.commons.query.DerefQueryNode) AndQueryNode(org.apache.jackrabbit.spi.commons.query.AndQueryNode) NotQueryNode(org.apache.jackrabbit.spi.commons.query.NotQueryNode) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode) PathQueryNode(org.apache.jackrabbit.spi.commons.query.PathQueryNode) TextsearchQueryNode(org.apache.jackrabbit.spi.commons.query.TextsearchQueryNode) PropertyFunctionQueryNode(org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) Iterator(java.util.Iterator) NamespaceException(javax.jcr.NamespaceException) Name(org.apache.jackrabbit.spi.Name)

Example 12 with LocationStepQueryNode

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;
}
Also used : PathQueryNode(org.apache.jackrabbit.spi.commons.query.PathQueryNode) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) Iterator(java.util.Iterator) NoSuchElementException(java.util.NoSuchElementException) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)

Aggregations

LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)12 Name (org.apache.jackrabbit.spi.Name)9 NodeTypeQueryNode (org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)8 PathQueryNode (org.apache.jackrabbit.spi.commons.query.PathQueryNode)8 DerefQueryNode (org.apache.jackrabbit.spi.commons.query.DerefQueryNode)7 PropertyFunctionQueryNode (org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode)7 RelationQueryNode (org.apache.jackrabbit.spi.commons.query.RelationQueryNode)7 TextsearchQueryNode (org.apache.jackrabbit.spi.commons.query.TextsearchQueryNode)7 NamespaceException (javax.jcr.NamespaceException)6 NotQueryNode (org.apache.jackrabbit.spi.commons.query.NotQueryNode)6 OrderQueryNode (org.apache.jackrabbit.spi.commons.query.OrderQueryNode)6 QueryNode (org.apache.jackrabbit.spi.commons.query.QueryNode)6 InvalidQueryException (javax.jcr.query.InvalidQueryException)5 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)4 AndQueryNode (org.apache.jackrabbit.spi.commons.query.AndQueryNode)4 RepositoryException (javax.jcr.RepositoryException)3 DefaultQueryNodeVisitor (org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor)3 ExactQueryNode (org.apache.jackrabbit.spi.commons.query.ExactQueryNode)3 NAryQueryNode (org.apache.jackrabbit.spi.commons.query.NAryQueryNode)3 OrQueryNode (org.apache.jackrabbit.spi.commons.query.OrQueryNode)3