Search in sources :

Example 46 with QueryResult

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

the class QueryResultTest method testGetPosition.

public void testGetPosition() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000]";
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        NodeIterator it = result.getNodes();
        assertEquals("Wrong position", 0, it.getPosition());
        int count = 0;
        while (it.hasNext()) {
            long position = it.getPosition();
            it.nextNode();
            assertEquals("Wrong position", count++, position);
        }
        try {
            it.next();
            fail("must throw NoSuchElementException");
        } catch (Exception e) {
        // correct
        }
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager) RepositoryException(javax.jcr.RepositoryException) NoSuchElementException(java.util.NoSuchElementException)

Example 47 with QueryResult

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

the class QueryResultTest method testIteratorNextOrderByScore.

public void testIteratorNextOrderByScore() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000] order by jcr:score()";
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        int size = 0;
        for (NodeIterator it = result.getNodes(); it.hasNext(); ) {
            it.nextNode();
            size++;
        }
        assertEquals("Wrong size of NodeIterator in result", INITIAL_NODE_NUM - i, size);
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager)

Example 48 with QueryResult

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

the class SQLTest method testSimpleQuery1.

public void testSimpleQuery1() throws Exception {
    Node foo = testRootNode.addNode("foo");
    foo.setProperty("bla", new String[] { "bla" });
    testRootNode.save();
    String sql = "SELECT * FROM nt:unstructured WHERE bla='bla' " + "AND jcr:path LIKE '" + testRoot + "/%'";
    Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
    QueryResult result = q.execute();
    checkResult(result, 1);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node)

Example 49 with QueryResult

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

the class SQLTest method _testFulltextComplex.

/**
     * I'm not sure what this test is supposed to verify. It only works because
     * the sql parser is not strict enough and the column values are never
     * actually checked
     */
public void _testFulltextComplex() throws Exception {
    Node foo = testRootNode.addNode("foo");
    foo.setProperty("mytext", new String[] { "the quick brown fox jumps over the lazy dog." });
    testRootNode.save();
    String sql = "SELECT foo.mytext, bla.foo FROM nt:unstructured WHERE " + "contains(., 'fox') AND NOT contains(., 'bla') " + "AND jcr:path LIKE '" + testRoot + "/%'";
    Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
    QueryResult result = q.execute();
    checkResult(result, 1);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node)

Example 50 with QueryResult

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

the class SelectClauseTest method testSelectSQL.

public void testSelectSQL() throws RepositoryException {
    Node n = testRootNode.addNode("node1");
    n.setProperty("myvalue", "foo");
    n = testRootNode.addNode("node2");
    n.setProperty("myvalue", "bar");
    n = testRootNode.addNode("node3");
    n.setProperty("yourvalue", "foo");
    testRootNode.save();
    String sql = "SELECT myvalue FROM " + ntBase + " WHERE " + "jcr:path LIKE '" + testRoot + "/%' AND myvalue IS NOT NULL";
    Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
    QueryResult result = q.execute();
    checkResult(result, 2);
    sql = "SELECT myvalue FROM " + ntBase + " WHERE jcr:path LIKE '" + testRoot + "/%'" + " AND yourvalue = 'foo' AND myvalue IS NOT NULL";
    q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
    result = q.execute();
    checkResult(result, 0);
    sql = "SELECT myvalue FROM " + ntBase + " WHERE myvalue IS NOT NULL";
    q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
    result = q.execute();
    checkResult(result, 2);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node)

Aggregations

QueryResult (javax.jcr.query.QueryResult)202 Node (javax.jcr.Node)109 Query (javax.jcr.query.Query)98 QueryManager (javax.jcr.query.QueryManager)55 NodeIterator (javax.jcr.NodeIterator)52 RowIterator (javax.jcr.query.RowIterator)46 Session (javax.jcr.Session)36 Test (org.junit.Test)32 Row (javax.jcr.query.Row)21 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)21 RepositoryException (javax.jcr.RepositoryException)15 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)15 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)9 Value (javax.jcr.Value)8 NoSuchElementException (java.util.NoSuchElementException)7 ArrayList (java.util.ArrayList)6 ValueFactory (javax.jcr.ValueFactory)6 InvalidItemStateException (javax.jcr.InvalidItemStateException)5 JackrabbitQueryResult (org.apache.jackrabbit.api.query.JackrabbitQueryResult)5 TreeSet (java.util.TreeSet)4