Search in sources :

Example 1 with AndQueryNode

use of org.apache.jackrabbit.spi.commons.query.AndQueryNode in project jackrabbit by apache.

the class JCRSQLQueryBuilder method visit.

public Object visit(ASTAndExpression node, Object data) {
    NAryQueryNode parent = (NAryQueryNode) data;
    AndQueryNode andQuery = factory.createAndQueryNode(parent);
    // pass to operands
    node.childrenAccept(this, andQuery);
    if (andQuery.getNumOperands() > 0) {
        parent.addOperand(andQuery);
    }
    return parent;
}
Also used : NAryQueryNode(org.apache.jackrabbit.spi.commons.query.NAryQueryNode) AndQueryNode(org.apache.jackrabbit.spi.commons.query.AndQueryNode)

Example 2 with AndQueryNode

use of org.apache.jackrabbit.spi.commons.query.AndQueryNode in project jackrabbit by apache.

the class JCRSQLQueryBuilder method visit.

public Object visit(ASTPredicate node, Object data) {
    NAryQueryNode parent = (NAryQueryNode) data;
    int type = node.getOperationType();
    QueryNode predicateNode;
    try {
        final Name[] tmp = new Name[2];
        final ASTLiteral[] value = new ASTLiteral[1];
        node.childrenAccept(new DefaultParserVisitor() {

            public Object visit(ASTIdentifier node, Object data) {
                if (tmp[0] == null) {
                    tmp[0] = node.getName();
                } else if (tmp[1] == null) {
                    tmp[1] = node.getName();
                }
                return data;
            }

            public Object visit(ASTLiteral node, Object data) {
                value[0] = node;
                return data;
            }

            public Object visit(ASTLowerFunction node, Object data) {
                getIdentifier(node);
                return data;
            }

            public Object visit(ASTUpperFunction node, Object data) {
                getIdentifier(node);
                return data;
            }

            private void getIdentifier(SimpleNode node) {
                if (node.jjtGetNumChildren() > 0) {
                    Node n = node.jjtGetChild(0);
                    if (n instanceof ASTIdentifier) {
                        ASTIdentifier identifier = (ASTIdentifier) n;
                        if (tmp[0] == null) {
                            tmp[0] = identifier.getName();
                        } else if (tmp[1] == null) {
                            tmp[1] = identifier.getName();
                        }
                    }
                }
            }
        }, data);
        Name identifier = tmp[0];
        if (identifier != null && identifier.equals(NameConstants.JCR_PATH)) {
            if (tmp[1] != null) {
            // simply ignore, this is a join of a mixin node type
            } else {
                createPathQuery(value[0].getValue(), parent.getType());
            }
            // done
            return data;
        }
        if (type == QueryConstants.OPERATION_BETWEEN) {
            AndQueryNode between = factory.createAndQueryNode(parent);
            RelationQueryNode rel = createRelationQueryNode(between, identifier, QueryConstants.OPERATION_GE_GENERAL, (ASTLiteral) node.children[1]);
            node.childrenAccept(this, rel);
            between.addOperand(rel);
            rel = createRelationQueryNode(between, identifier, QueryConstants.OPERATION_LE_GENERAL, (ASTLiteral) node.children[2]);
            node.childrenAccept(this, rel);
            between.addOperand(rel);
            predicateNode = between;
        } else if (type == QueryConstants.OPERATION_GE_GENERAL || type == QueryConstants.OPERATION_GT_GENERAL || type == QueryConstants.OPERATION_LE_GENERAL || type == QueryConstants.OPERATION_LT_GENERAL || type == QueryConstants.OPERATION_NE_GENERAL || type == QueryConstants.OPERATION_EQ_GENERAL) {
            predicateNode = createRelationQueryNode(parent, identifier, type, value[0]);
            node.childrenAccept(this, predicateNode);
        } else if (type == QueryConstants.OPERATION_LIKE) {
            ASTLiteral pattern = value[0];
            if (node.getEscapeString() != null) {
                if (node.getEscapeString().length() == 1) {
                    // backslash is the escape character we use internally
                    pattern.setValue(translateEscaping(pattern.getValue(), node.getEscapeString().charAt(0), '\\'));
                } else {
                    throw new IllegalArgumentException("ESCAPE string value must have length 1: '" + node.getEscapeString() + "'");
                }
            } else {
                // no escape character specified.
                // if the pattern contains any backslash characters we need
                // to escape them.
                pattern.setValue(pattern.getValue().replaceAll("\\\\", "\\\\\\\\"));
            }
            predicateNode = createRelationQueryNode(parent, identifier, type, pattern);
            node.childrenAccept(this, predicateNode);
        } else if (type == QueryConstants.OPERATION_IN) {
            OrQueryNode in = factory.createOrQueryNode(parent);
            for (int i = 1; i < node.children.length; i++) {
                RelationQueryNode rel = createRelationQueryNode(in, identifier, QueryConstants.OPERATION_EQ_VALUE, (ASTLiteral) node.children[i]);
                node.childrenAccept(this, rel);
                in.addOperand(rel);
            }
            predicateNode = in;
        } else if (type == QueryConstants.OPERATION_NULL || type == QueryConstants.OPERATION_NOT_NULL) {
            predicateNode = createRelationQueryNode(parent, identifier, type, null);
        } else if (type == QueryConstants.OPERATION_SIMILAR) {
            ASTLiteral literal;
            if (node.children.length == 1) {
                literal = (ASTLiteral) node.children[0];
            } else {
                literal = (ASTLiteral) node.children[1];
            }
            predicateNode = createRelationQueryNode(parent, identifier, type, literal);
        } else if (type == QueryConstants.OPERATION_SPELLCHECK) {
            predicateNode = createRelationQueryNode(parent, NameConstants.JCR_PRIMARYTYPE, type, (ASTLiteral) node.children[0]);
        } else {
            throw new IllegalArgumentException("Unknown operation type: " + type);
        }
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new IllegalArgumentException("Too few arguments in predicate");
    }
    if (predicateNode != null) {
        parent.addOperand(predicateNode);
    }
    return data;
}
Also used : NAryQueryNode(org.apache.jackrabbit.spi.commons.query.NAryQueryNode) OrQueryNode(org.apache.jackrabbit.spi.commons.query.OrQueryNode) 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) NAryQueryNode(org.apache.jackrabbit.spi.commons.query.NAryQueryNode) 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) QueryRootNode(org.apache.jackrabbit.spi.commons.query.QueryRootNode) PropertyFunctionQueryNode(org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) RelationQueryNode(org.apache.jackrabbit.spi.commons.query.RelationQueryNode) Name(org.apache.jackrabbit.spi.Name) 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) NAryQueryNode(org.apache.jackrabbit.spi.commons.query.NAryQueryNode) 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) AndQueryNode(org.apache.jackrabbit.spi.commons.query.AndQueryNode)

Example 3 with AndQueryNode

use of org.apache.jackrabbit.spi.commons.query.AndQueryNode in project jackrabbit by apache.

the class QueryImpl method getColumns.

/**
 * Returns the columns for this query.
 *
 * @return array of columns.
 * @throws RepositoryException if an error occurs.
 */
protected ColumnImpl[] getColumns() throws RepositoryException {
    SessionImpl session = sessionContext.getSessionImpl();
    QueryObjectModelFactory qomFactory = session.getWorkspace().getQueryManager().getQOMFactory();
    // get columns
    Map<Name, ColumnImpl> columns = new LinkedHashMap<Name, ColumnImpl>();
    for (Name name : root.getSelectProperties()) {
        String pn = sessionContext.getJCRName(name);
        ColumnImpl col = (ColumnImpl) qomFactory.column(sessionContext.getJCRName(DEFAULT_SELECTOR_NAME), pn, pn);
        columns.put(name, col);
    }
    if (columns.size() == 0) {
        // use node type constraint
        LocationStepQueryNode[] steps = root.getLocationNode().getPathSteps();
        final Name[] ntName = new Name[1];
        steps[steps.length - 1].acceptOperands(new DefaultQueryNodeVisitor() {

            public Object visit(AndQueryNode node, Object data) throws RepositoryException {
                return node.acceptOperands(this, data);
            }

            public Object visit(NodeTypeQueryNode node, Object data) {
                ntName[0] = node.getValue();
                return data;
            }
        }, null);
        if (ntName[0] == null) {
            ntName[0] = NameConstants.NT_BASE;
        }
        NodeTypeImpl nt = session.getNodeTypeManager().getNodeType(ntName[0]);
        PropertyDefinition[] propDefs = nt.getPropertyDefinitions();
        for (PropertyDefinition pd : propDefs) {
            QPropertyDefinition propDef = ((PropertyDefinitionImpl) pd).unwrap();
            if (!propDef.definesResidual() && !propDef.isMultiple()) {
                columns.put(propDef.getName(), columnForName(propDef.getName()));
            }
        }
    }
    // add jcr:path and jcr:score if not selected already
    if (!columns.containsKey(NameConstants.JCR_PATH)) {
        columns.put(NameConstants.JCR_PATH, columnForName(NameConstants.JCR_PATH));
    }
    if (!columns.containsKey(NameConstants.JCR_SCORE)) {
        columns.put(NameConstants.JCR_SCORE, columnForName(NameConstants.JCR_SCORE));
    }
    return columns.values().toArray(new ColumnImpl[columns.size()]);
}
Also used : NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Name(org.apache.jackrabbit.spi.Name) LinkedHashMap(java.util.LinkedHashMap) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) SessionImpl(org.apache.jackrabbit.core.SessionImpl) QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) ColumnImpl(org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl) DefaultQueryNodeVisitor(org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode) AndQueryNode(org.apache.jackrabbit.spi.commons.query.AndQueryNode)

Aggregations

AndQueryNode (org.apache.jackrabbit.spi.commons.query.AndQueryNode)3 Name (org.apache.jackrabbit.spi.Name)2 LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)2 NAryQueryNode (org.apache.jackrabbit.spi.commons.query.NAryQueryNode)2 NodeTypeQueryNode (org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)2 LinkedHashMap (java.util.LinkedHashMap)1 RepositoryException (javax.jcr.RepositoryException)1 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)1 QueryObjectModelFactory (javax.jcr.query.qom.QueryObjectModelFactory)1 SessionImpl (org.apache.jackrabbit.core.SessionImpl)1 NodeTypeImpl (org.apache.jackrabbit.core.nodetype.NodeTypeImpl)1 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)1 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)1 DefaultQueryNodeVisitor (org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor)1 NotQueryNode (org.apache.jackrabbit.spi.commons.query.NotQueryNode)1 OrQueryNode (org.apache.jackrabbit.spi.commons.query.OrQueryNode)1 OrderQueryNode (org.apache.jackrabbit.spi.commons.query.OrderQueryNode)1 PathQueryNode (org.apache.jackrabbit.spi.commons.query.PathQueryNode)1 PropertyFunctionQueryNode (org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode)1 QueryNode (org.apache.jackrabbit.spi.commons.query.QueryNode)1