Search in sources :

Example 21 with InvalidQueryException

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

the class LuceneQueryFactory method getNodeNameQuery.

protected Query getNodeNameQuery(int transform, String operator, StaticOperand right) throws RepositoryException {
    if (transform != TRANSFORM_NONE || !JCR_OPERATOR_EQUAL_TO.equals(operator)) {
        throw new UnsupportedRepositoryOperationException();
    }
    Value value = evaluator.getValue(right);
    int type = value.getType();
    String string = value.getString();
    if (type == PropertyType.URI && string.startsWith("./")) {
        string = string.substring("./".length());
    } else if (type == PropertyType.DOUBLE || type == PropertyType.DECIMAL || type == PropertyType.LONG || type == PropertyType.BOOLEAN || type == PropertyType.REFERENCE || type == PropertyType.WEAKREFERENCE) {
        throw new InvalidQueryException("Invalid name value: " + string);
    }
    try {
        Name name = session.getQName(string);
        Term uri = new Term(NAMESPACE_URI, name.getNamespaceURI());
        Term local = new Term(LOCAL_NAME, name.getLocalName());
        BooleanQuery query = new BooleanQuery();
        query.add(new JackrabbitTermQuery(uri), MUST);
        query.add(new JackrabbitTermQuery(local), MUST);
        return query;
    } catch (IllegalNameException e) {
        throw new InvalidQueryException("Illegal name: " + string, e);
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) BooleanQuery(org.apache.lucene.search.BooleanQuery) Value(javax.jcr.Value) PropertyValue(javax.jcr.query.qom.PropertyValue) Term(org.apache.lucene.index.Term) IllegalNameException(org.apache.jackrabbit.spi.commons.conversion.IllegalNameException) Constraint(javax.jcr.query.qom.Constraint) InvalidQueryException(javax.jcr.query.InvalidQueryException) NodeName(javax.jcr.query.qom.NodeName) NodeLocalName(javax.jcr.query.qom.NodeLocalName) Name(org.apache.jackrabbit.spi.Name)

Example 22 with InvalidQueryException

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

the class LuceneQueryBuilder method visit.

public Object visit(PathQueryNode node, Object data) throws RepositoryException {
    Query context = null;
    LocationStepQueryNode[] steps = node.getPathSteps();
    if (steps.length > 0) {
        if (node.isAbsolute() && !steps[0].getIncludeDescendants()) {
            // eat up first step
            Name nameTest = steps[0].getNameTest();
            if (nameTest == null) {
                // this is equivalent to the root node
                context = new JackrabbitTermQuery(new Term(FieldNames.PARENT, ""));
            } else if (nameTest.getLocalName().length() == 0) {
                // root node
                context = new JackrabbitTermQuery(new Term(FieldNames.PARENT, ""));
            } else {
                // then this is a node != the root node
                // will never match anything!
                BooleanQuery and = new BooleanQuery();
                and.add(new JackrabbitTermQuery(new Term(FieldNames.PARENT, "")), Occur.MUST);
                and.add(new NameQuery(nameTest, indexFormatVersion, nsMappings), Occur.MUST);
                context = and;
            }
            // apply predicates
            Object[] predicates = steps[0].acceptOperands(this, context);
            BooleanQuery andQuery = new BooleanQuery();
            for (Object predicate : predicates) {
                andQuery.add((Query) predicate, Occur.MUST);
            }
            if (andQuery.clauses().size() > 0) {
                andQuery.add(context, Occur.MUST);
                context = andQuery;
            }
            LocationStepQueryNode[] tmp = new LocationStepQueryNode[steps.length - 1];
            System.arraycopy(steps, 1, tmp, 0, steps.length - 1);
            steps = tmp;
        } else {
            // path is 1) relative or 2) descendant-or-self
            // use root node as context
            context = new JackrabbitTermQuery(new Term(FieldNames.PARENT, ""));
        }
    } else {
        exceptions.add(new InvalidQueryException("Number of location steps must be > 0"));
    }
    // loop over steps
    for (LocationStepQueryNode step : steps) {
        context = (Query) step.accept(this, context);
    }
    if (data instanceof BooleanQuery) {
        BooleanQuery constraint = (BooleanQuery) data;
        if (constraint.getClauses().length > 0) {
            constraint.add(context, Occur.MUST);
            context = constraint;
        }
    }
    return context;
}
Also used : LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) Term(org.apache.lucene.index.Term) InvalidQueryException(javax.jcr.query.InvalidQueryException) Name(org.apache.jackrabbit.spi.Name)

Example 23 with InvalidQueryException

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

the class QueryManagerImpl method getQuery.

/**
     * @see QueryManager#getQuery(Node)
     */
public Query getQuery(Node node) throws InvalidQueryException, RepositoryException {
    checkIsAlive();
    NamePathResolver resolver = mgrProvider.getNamePathResolver();
    if (!node.isNodeType(resolver.getJCRName(NameConstants.NT_QUERY))) {
        throw new InvalidQueryException("Node is not of type nt:query");
    }
    if (node.getSession() != session) {
        throw new InvalidQueryException("Node belongs to a different session.");
    }
    String statement = node.getProperty(resolver.getJCRName(NameConstants.JCR_STATEMENT)).getString();
    String language = node.getProperty(resolver.getJCRName(NameConstants.JCR_LANGUAGE)).getString();
    if (Query.JCR_JQOM.equals(language)) {
        QueryObjectModelFactory qomFactory = new QOMFactory(node, resolver);
        QueryObjectModelBuilder builder = QueryObjectModelBuilderRegistry.getQueryObjectModelBuilder(language);
        return builder.createQueryObjectModel(statement, qomFactory, valueFactory);
    } else {
        return new QueryImpl(session, mgrProvider, itemMgr, wspManager, statement, language, node);
    }
}
Also used : NamePathResolver(org.apache.jackrabbit.spi.commons.conversion.NamePathResolver) QueryObjectModelBuilder(org.apache.jackrabbit.commons.query.QueryObjectModelBuilder) QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 24 with InvalidQueryException

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

the class JCRSQLQueryBuilder method createQuery.

/**
     * Creates a <code>QueryNode</code> tree from a SQL <code>statement</code>
     * using the passed query node <code>factory</code>.
     *
     * @param statement the SQL statement.
     * @param resolver  the namespace resolver to use.
     * @return the <code>QueryNode</code> tree.
     * @throws InvalidQueryException if <code>statement</code> is malformed.
     */
public static QueryRootNode createQuery(String statement, NameResolver resolver, QueryNodeFactory factory) throws InvalidQueryException {
    try {
        // get parser
        JCRSQLParser parser;
        synchronized (parsers) {
            parser = (JCRSQLParser) parsers.get(resolver);
            if (parser == null) {
                parser = new JCRSQLParser(new StringReader(statement));
                parser.setNameResolver(resolver);
                parsers.put(resolver, parser);
            }
        }
        JCRSQLQueryBuilder builder;
        // guard against concurrent use within same session
        synchronized (parser) {
            parser.ReInit(new StringReader(statement));
            builder = new JCRSQLQueryBuilder(parser.Query(), resolver, factory);
        }
        return builder.getRootNode();
    } catch (ParseException e) {
        throw new InvalidQueryException(e.getMessage());
    } catch (IllegalArgumentException e) {
        throw new InvalidQueryException(e.getMessage());
    } catch (Throwable t) {
        // javacc parser may also throw an error in some cases
        throw new InvalidQueryException(t.getMessage());
    }
}
Also used : StringReader(java.io.StringReader) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 25 with InvalidQueryException

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

the class QueryFormat method visit.

public Object visit(RelationQueryNode node, Object data) throws RepositoryException {
    StringBuffer sb = (StringBuffer) data;
    try {
        StringBuffer propName = new StringBuffer();
        PathQueryNode relPath = node.getRelativePath();
        if (relPath == null) {
            propName.append(".");
        } else if (relPath.getPathSteps().length > 1) {
            exceptions.add(new InvalidQueryException("Child axis not supported in SQL"));
            return data;
        } else {
            visit(relPath, data);
        }
        // surround name with property function
        node.acceptOperands(this, propName);
        if (node.getOperation() == OPERATION_EQ_VALUE || node.getOperation() == OPERATION_EQ_GENERAL) {
            sb.append(propName);
            sb.append(" = ");
            appendValue(node, sb);
        } else if (node.getOperation() == OPERATION_GE_VALUE || node.getOperation() == OPERATION_GE_GENERAL) {
            sb.append(propName);
            sb.append(" >= ");
            appendValue(node, sb);
        } else if (node.getOperation() == OPERATION_GT_VALUE || node.getOperation() == OPERATION_GT_GENERAL) {
            sb.append(propName);
            sb.append(" > ");
            appendValue(node, sb);
        } else if (node.getOperation() == OPERATION_LE_VALUE || node.getOperation() == OPERATION_LE_GENERAL) {
            sb.append(propName);
            sb.append(" <= ");
            appendValue(node, sb);
        } else if (node.getOperation() == OPERATION_LIKE) {
            sb.append(propName);
            sb.append(" LIKE ");
            appendValue(node, sb);
        } else if (node.getOperation() == OPERATION_LT_VALUE || node.getOperation() == OPERATION_LT_GENERAL) {
            sb.append(propName);
            sb.append(" < ");
            appendValue(node, sb);
        } else if (node.getOperation() == OPERATION_NE_VALUE || node.getOperation() == OPERATION_NE_GENERAL) {
            sb.append(propName);
            sb.append(" <> ");
            appendValue(node, sb);
        } else if (node.getOperation() == OPERATION_NULL) {
            sb.append(propName);
            sb.append(" IS NULL");
        } else if (node.getOperation() == OPERATION_NOT_NULL) {
            sb.append(propName);
            sb.append(" IS NOT NULL");
        } else if (node.getOperation() == OPERATION_SIMILAR) {
            sb.append("SIMILAR(");
            sb.append(propName);
            sb.append(", ");
            appendValue(node, sb);
            sb.append(")");
        } else if (node.getOperation() == OPERATION_SPELLCHECK) {
            sb.append("SPELLCHECK(");
            appendValue(node, sb);
            sb.append(")");
        } else {
            exceptions.add(new InvalidQueryException("Invalid operation: " + node.getOperation()));
        }
        if (node.getOperation() == OPERATION_LIKE && node.getStringValue().indexOf('\\') > -1) {
            sb.append(" ESCAPE '\\'");
        }
    } catch (NamespaceException e) {
        exceptions.add(e);
    }
    return sb;
}
Also used : PathQueryNode(org.apache.jackrabbit.spi.commons.query.PathQueryNode) NamespaceException(javax.jcr.NamespaceException) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Aggregations

InvalidQueryException (javax.jcr.query.InvalidQueryException)50 Value (javax.jcr.Value)15 Query (javax.jcr.query.Query)13 NamespaceException (javax.jcr.NamespaceException)9 Name (org.apache.jackrabbit.spi.Name)7 LocationStepQueryNode (org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode)6 PathQueryNode (org.apache.jackrabbit.spi.commons.query.PathQueryNode)6 RepositoryException (javax.jcr.RepositoryException)5 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 Session (javax.jcr.Session)3 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)3 NAryQueryNode (org.apache.jackrabbit.spi.commons.query.NAryQueryNode)3 NodeTypeQueryNode (org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode)3