Search in sources :

Example 41 with QueryResult

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

the class SimpleQueryTest method testDoubleField.

public void testDoubleField() throws Exception {
    Node n = testRootNode.addNode("node1");
    n.setProperty("value", new Value[] { superuser.getValueFactory().createValue(1.9928375d) });
    n = testRootNode.addNode("node2");
    n.setProperty("value", new Value[] { superuser.getValueFactory().createValue(0.0d) });
    n = testRootNode.addNode("node3");
    n.setProperty("value", new Value[] { superuser.getValueFactory().createValue(-1.42982475d) });
    testRootNode.save();
    String sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND value > 0.1e-0";
    Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
    QueryResult result = q.execute();
    checkResult(result, 1);
    sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND value > -0.1";
    q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
    result = q.execute();
    checkResult(result, 2);
    sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND value > -1.5";
    q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
    result = q.execute();
    checkResult(result, 3);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node)

Example 42 with QueryResult

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

the class SimpleQueryTest method testDateField1.

public void testDateField1() throws Exception {
    Node n = testRootNode.addNode("marcel");
    Calendar marcel = Calendar.getInstance();
    marcel.set(1976, 4, 20, 15, 40);
    n.setProperty("birth", new Value[] { superuser.getValueFactory().createValue(marcel) });
    n = testRootNode.addNode("vanessa");
    Calendar vanessa = Calendar.getInstance();
    vanessa.set(1975, 4, 10, 13, 30);
    n.setProperty("birth", new Value[] { superuser.getValueFactory().createValue(vanessa) });
    testRootNode.save();
    String sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND birth > TIMESTAMP '1976-01-01T00:00:00.000+01:00'";
    Query q = superuser.getWorkspace().getQueryManager().createQuery(sql, Query.SQL);
    QueryResult result = q.execute();
    checkResult(result, 1);
    sql = "SELECT * FROM nt:base WHERE jcr:path LIKE '" + testRoot + "/%' AND birth > TIMESTAMP '1975-01-01T00:00:00.000+01:00'";
    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) Calendar(java.util.Calendar)

Example 43 with QueryResult

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

the class QueryResultTest method testSkipOrderByProperty.

public void testSkipOrderByProperty() throws RepositoryException {
    QueryManager qm = superuser.getWorkspace().getQueryManager();
    for (int i = 0; i < 10; i++) {
        String stmt = testPath + "/*[@" + propertyName1 + " < 1000] order by @" + propertyName1;
        QueryResult result = qm.createQuery(stmt, Query.XPATH).execute();
        for (int j = 0; j < INITIAL_NODE_NUM - i; j++) {
            // skip to each node in the result
            NodeIterator it = result.getNodes();
            it.skip(j);
            long propValue = it.nextNode().getProperty(propertyName1).getLong();
            // expected = number of skipped nodes + number of deleted nodes
            long expected = j + i;
            assertEquals("Wrong node after skip()", expected, propValue);
        }
        try {
            NodeIterator it = result.getNodes();
            it.skip(it.getSize() + 1);
            fail("must throw NoSuchElementException");
        } catch (NoSuchElementException 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) NoSuchElementException(java.util.NoSuchElementException)

Example 44 with QueryResult

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

the class QueryResultTest method testGetSize.

public void testGetSize() 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();
        assertEquals("Wrong size of NodeIterator in result", INITIAL_NODE_NUM - i, result.getNodes().getSize());
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager)

Example 45 with QueryResult

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

the class QueryResultTest method testGetSizeOrderByScore.

public void testGetSizeOrderByScore() 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();
        assertEquals("Wrong size of NodeIterator in result", INITIAL_NODE_NUM - i, result.getNodes().getSize());
        // remove node for the next iteration
        testRootNode.getNode("node" + i).remove();
        testRootNode.save();
    }
}
Also used : QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager)

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