Search in sources :

Example 36 with InvalidQueryException

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

the class NodeNameTest method testBooleanLiteral.

public void testBooleanLiteral() throws RepositoryException {
    Value literal = vf.createValue(true);
    try {
        createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute();
        fail("NodeName comparison with BOOLEAN must fail with InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
    try {
        String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST(" + literal.getString() + " AS BOOLEAN)";
        qm.createQuery(stmt, Query.JCR_SQL2).execute();
        fail("NAME() comparison with BOOLEAN must fail with InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
}
Also used : Value(javax.jcr.Value) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 37 with InvalidQueryException

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

the class NodeLocalNameTest method testPathLiteral.

public void testPathLiteral() throws RepositoryException {
    Value literal = superuser.getValueFactory().createValue(nodeLocalName, PropertyType.PATH);
    QueryObjectModel qom = createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal);
    checkQOM(qom, new Node[] { node1 });
    literal = superuser.getValueFactory().createValue(node1.getPath(), PropertyType.PATH);
    try {
        createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute();
        fail("NodeName comparison with absolute PATH must fail with InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
    literal = superuser.getValueFactory().createValue(nodeName1 + "/" + nodeName1, PropertyType.PATH);
    try {
        createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute();
        fail("NodeName comparison with PATH length >1 must fail with InvalidQueryException");
    } catch (InvalidQueryException e) {
    // expected
    }
}
Also used : Value(javax.jcr.Value) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 38 with InvalidQueryException

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

the class QueryManagerImpl method executeQuery.

public QueryResult executeQuery(String statement, String language, long limit, long offset, HashMap<String, Value> bindVariableMap) throws RepositoryException {
    try {
        Map<String, PropertyValue> bindMap = convertMap(bindVariableMap);
        TimerStats.Context context = queryDuration.time();
        Result r = queryEngine.executeQuery(statement, language, limit, offset, bindMap, sessionContext.getSessionLocalMappings());
        queryCount.mark();
        long millis = TimeUnit.NANOSECONDS.toMillis(context.stop());
        queryOpsLogger.debug("Executed query [{}] in [{}] ms", statement, millis);
        sessionContext.getStatisticManager().logQueryEvaluationTime(language, statement, millis);
        return new QueryResultImpl(sessionContext, r);
    } catch (IllegalArgumentException e) {
        throw new InvalidQueryException(e);
    } catch (ParseException e) {
        throw new InvalidQueryException(e);
    }
}
Also used : PropertyValue(org.apache.jackrabbit.oak.api.PropertyValue) TimerStats(org.apache.jackrabbit.oak.stats.TimerStats) ParseException(java.text.ParseException) InvalidQueryException(javax.jcr.query.InvalidQueryException) QueryResult(javax.jcr.query.QueryResult) Result(org.apache.jackrabbit.oak.api.Result)

Example 39 with InvalidQueryException

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

the class QueryTest method noLiterals.

@Test
public void noLiterals() throws RepositoryException {
    Session session = getAdminSession();
    ValueFactory vf = session.getValueFactory();
    QueryManager qm = session.getWorkspace().getQueryManager();
    // insecure
    try {
        Query q = qm.createQuery("select text from [nt:base] where password = 'x'", Query.JCR_SQL2 + "-noLiterals");
        q.execute();
        fail();
    } catch (InvalidQueryException e) {
        assertTrue(e.toString(), e.toString().indexOf("literals of this type not allowed") > 0);
    }
    // secure
    Query q = qm.createQuery("select text from [nt:base] where password = $p", Query.JCR_SQL2 + "-noLiterals");
    q.bindValue("p", vf.createValue("x"));
    q.execute();
}
Also used : Query(javax.jcr.query.Query) QueryManager(javax.jcr.query.QueryManager) ValueFactory(javax.jcr.ValueFactory) 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 40 with InvalidQueryException

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

the class QueryTest method doubleQuote.

@Test
public void doubleQuote() throws RepositoryException {
    Session session = getAdminSession();
    Node hello = session.getRootNode().addNode("hello");
    hello.setProperty("x", 1);
    Node world = hello.addNode("world");
    world.setProperty("x", 2);
    session.save();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q;
    q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE(s,[/hello])", Query.JCR_SQL2);
    assertEquals("/hello/world", getPaths(q));
    q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE(s,\"/hello\")", Query.JCR_SQL2);
    assertEquals("/hello/world", getPaths(q));
    try {
        q = qm.createQuery("SELECT * FROM [nt:base] AS s WHERE ISDESCENDANTNODE(s,[\"/hello\"])", Query.JCR_SQL2);
        getPaths(q);
        fail();
    } catch (InvalidQueryException e) {
    // expected: absolute path
    }
}
Also used : Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) 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)

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