Search in sources :

Example 1 with InvalidQueryException

use of javax.jcr.query.InvalidQueryException in project jackrabbit-oak by apache.

the class QueryTest method nodeType.

@Test
public void nodeType() throws Exception {
    Session session = createAdminSession();
    String xpath = "/jcr:root//element(*,rep:User)[xyz/@jcr:primaryType]";
    assertPlan(getPlan(session, xpath), "[rep:User] as [a] /* nodeType");
    session.getNode("/oak:index/nodetype").setProperty("declaringNodeTypes", new String[] { "oak:Unstructured" }, PropertyType.NAME);
    session.save();
    assertPlan(getPlan(session, xpath), "[rep:User] as [a] /* traverse ");
    xpath = "/jcr:root//element(*,oak:Unstructured)[xyz/@jcr:primaryType] option(traversal fail)";
    // the plan might still use traversal, so we can't just check the plan;
    // but using "option(traversal fail)" we have ensured that there is an index
    // (the nodetype index) that can serve this query
    getPlan(session, xpath);
    // and without the node type index, it is supposed to fail
    Node nodeTypeIndex = session.getRootNode().getNode("oak:index").getNode("nodetype");
    nodeTypeIndex.setProperty("declaringNodeTypes", new String[] {}, PropertyType.NAME);
    session.save();
    try {
        getPlan(session, xpath);
        fail();
    } catch (InvalidQueryException e) {
    // expected
    }
    session.logout();
}
Also used : Node(javax.jcr.Node) InvalidQueryException(javax.jcr.query.InvalidQueryException) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 2 with InvalidQueryException

use of javax.jcr.query.InvalidQueryException in project jackrabbit by apache.

the class Parser method readDecimal.

private void readDecimal(int start, int i) throws RepositoryException {
    char[] chars = statementChars;
    int[] types = characterTypes;
    while (true) {
        int t = types[i];
        if (t != CHAR_DECIMAL && t != CHAR_VALUE) {
            break;
        }
        i++;
    }
    if (chars[i] == 'E' || chars[i] == 'e') {
        i++;
        if (chars[i] == '+' || chars[i] == '-') {
            i++;
        }
        if (types[i] != CHAR_VALUE) {
            throw getSyntaxError();
        }
        do {
            // go until the first non-number
            i++;
        } while (types[i] == CHAR_VALUE);
    }
    parseIndex = i;
    String sub = statement.substring(start, i);
    BigDecimal bd;
    try {
        bd = new BigDecimal(sub);
    } catch (NumberFormatException e) {
        throw new InvalidQueryException("Data conversion error converting " + sub + " to BigDecimal: " + e);
    }
    checkLiterals(false);
    currentValue = valueFactory.createValue(bd);
    currentTokenType = VALUE;
}
Also used : Constraint(javax.jcr.query.qom.Constraint) BigDecimal(java.math.BigDecimal) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 3 with InvalidQueryException

use of javax.jcr.query.InvalidQueryException in project jackrabbit by apache.

the class Parser method getSyntaxError.

private InvalidQueryException getSyntaxError(String expected) {
    int index = Math.min(parseIndex, statement.length() - 1);
    String query = statement.substring(0, index) + "(*)" + statement.substring(index).trim();
    if (expected != null) {
        query += "; expected: " + expected;
    }
    return new InvalidQueryException("Query:\n" + query);
}
Also used : Constraint(javax.jcr.query.qom.Constraint) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 4 with InvalidQueryException

use of javax.jcr.query.InvalidQueryException in project jackrabbit by apache.

the class QueryObjectModelImpl method init.

/**
 * Initializes a query instance from a query object model.
 *
 * @param sessionContext component context of the current session
 * @param handler  the query handler of the search index.
 * @param qomTree  the query object model tree.
 * @param language the original query syntax from where the JQOM was
 *                 created.
 * @param node     a nt:query node where the query was read from or
 *                 <code>null</code> if it is not a stored query.
 * @throws InvalidQueryException if the qom tree cannot be serialized
 *                               according to the given language.
 * @throws RepositoryException   if another error occurs
 */
public void init(SessionContext sessionContext, QueryHandler handler, QueryObjectModelTree qomTree, String language, Node node) throws InvalidQueryException, RepositoryException {
    checkNotInitialized();
    this.sessionContext = sessionContext;
    this.language = language;
    this.handler = handler;
    this.qomTree = qomTree;
    this.node = node;
    this.statement = QueryObjectModelBuilderRegistry.getQueryObjectModelBuilder(language).toString(this);
    try {
        qomTree.accept(new DefaultTraversingQOMTreeVisitor() {

            @Override
            public Object visit(BindVariableValueImpl node, Object data) {
                variables.put(node.getBindVariableName(), null);
                return data;
            }
        }, null);
    } catch (Exception ignore) {
    }
    this.lqf = new LuceneQueryFactory(sessionContext.getSessionImpl(), (SearchIndex) handler, variables);
    setInitialized();
}
Also used : DefaultTraversingQOMTreeVisitor(org.apache.jackrabbit.spi.commons.query.qom.DefaultTraversingQOMTreeVisitor) BindVariableValueImpl(org.apache.jackrabbit.spi.commons.query.qom.BindVariableValueImpl) SearchIndex(org.apache.jackrabbit.core.query.lucene.SearchIndex) LuceneQueryFactory(org.apache.jackrabbit.core.query.lucene.LuceneQueryFactory) RepositoryException(javax.jcr.RepositoryException) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 5 with InvalidQueryException

use of javax.jcr.query.InvalidQueryException in project jackrabbit by apache.

the class LuceneQueryFactory method mapConstraintToQueryAndFilter.

protected Predicate mapConstraintToQueryAndFilter(QueryPair query, Constraint constraint, Map<String, NodeType> selectorMap, JackrabbitIndexSearcher searcher, IndexReader reader) throws RepositoryException, IOException {
    Predicate filter = Predicate.TRUE;
    if (constraint instanceof And) {
        And and = (And) constraint;
        filter = mapConstraintToQueryAndFilter(query, and.getConstraint1(), selectorMap, searcher, reader);
        Predicate other = mapConstraintToQueryAndFilter(query, and.getConstraint2(), selectorMap, searcher, reader);
        if (filter == Predicate.TRUE) {
            filter = other;
        } else if (other != Predicate.TRUE) {
            filter = Predicates.and(filter, other);
        }
    } else if (constraint instanceof Comparison) {
        Comparison c = (Comparison) constraint;
        Transform transform = new Transform(c.getOperand1());
        DynamicOperand left = transform.operand;
        final String operator = c.getOperator();
        StaticOperand right = c.getOperand2();
        if (left instanceof Length || left instanceof FullTextSearchScore || (((!JCR_OPERATOR_EQUAL_TO.equals(operator) && !JCR_OPERATOR_LIKE.equals(operator)) || transform.transform != TRANSFORM_NONE) && (left instanceof NodeName || left instanceof NodeLocalName))) {
            try {
                int type = PropertyType.UNDEFINED;
                if (left instanceof Length) {
                    type = PropertyType.LONG;
                } else if (left instanceof FullTextSearchScore) {
                    type = PropertyType.DOUBLE;
                }
                final DynamicOperand operand = c.getOperand1();
                final Value value = evaluator.getValue(right, type);
                filter = new RowPredicate() {

                    @Override
                    protected boolean evaluate(Row row) throws RepositoryException {
                        return new ValueComparator().evaluate(operator, evaluator.getValue(operand, row), value);
                    }
                };
            } catch (ValueFormatException e) {
                throw new InvalidQueryException(e);
            }
        } else {
            Query cq = getComparisonQuery(left, transform.transform, operator, right, selectorMap);
            query.subQuery.add(cq, MUST);
        }
    } else if (constraint instanceof DescendantNode) {
        final DescendantNode descendantNode = (DescendantNode) constraint;
        Query context = getNodeIdQuery(UUID, descendantNode.getAncestorPath());
        query.mainQuery = new DescendantSelfAxisQuery(context, query.subQuery, false);
    } else {
        query.subQuery.add(create(constraint, selectorMap, searcher), MUST);
    }
    return filter;
}
Also used : Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) DynamicOperand(javax.jcr.query.qom.DynamicOperand) StaticOperand(javax.jcr.query.qom.StaticOperand) NodeName(javax.jcr.query.qom.NodeName) ValueComparator(org.apache.jackrabbit.core.query.lucene.join.ValueComparator) Predicate(org.apache.jackrabbit.commons.predicate.Predicate) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) NodeLocalName(javax.jcr.query.qom.NodeLocalName) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) Comparison(javax.jcr.query.qom.Comparison) Length(javax.jcr.query.qom.Length) And(javax.jcr.query.qom.And) DescendantNode(javax.jcr.query.qom.DescendantNode) Value(javax.jcr.Value) PropertyValue(javax.jcr.query.qom.PropertyValue) ValueFormatException(javax.jcr.ValueFormatException) FullTextSearchScore(javax.jcr.query.qom.FullTextSearchScore) SelectorRow(org.apache.jackrabbit.core.query.lucene.join.SelectorRow) Row(javax.jcr.query.Row) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Aggregations

InvalidQueryException (javax.jcr.query.InvalidQueryException)51 Value (javax.jcr.Value)15 Query (javax.jcr.query.Query)14 NamespaceException (javax.jcr.NamespaceException)9 Name (org.apache.jackrabbit.spi.Name)7 RepositoryException (javax.jcr.RepositoryException)6 LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)6 PathQueryNode (org.apache.jackrabbit.spi.commons.query.PathQueryNode)6 Constraint (javax.jcr.query.qom.Constraint)5 IllegalNameException (org.apache.jackrabbit.spi.commons.conversion.IllegalNameException)5 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)5 PropertyFunctionQueryNode (org.apache.jackrabbit.spi.commons.query.PropertyFunctionQueryNode)5 RelationQueryNode (org.apache.jackrabbit.spi.commons.query.RelationQueryNode)5 Path (org.apache.jackrabbit.spi.Path)4 DerefQueryNode (org.apache.jackrabbit.spi.commons.query.DerefQueryNode)4 TextsearchQueryNode (org.apache.jackrabbit.spi.commons.query.TextsearchQueryNode)4 Node (javax.jcr.Node)3 Session (javax.jcr.Session)3 QueryManager (javax.jcr.query.QueryManager)3 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)3