Search in sources :

Example 46 with Row

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

the class QueryTest method limit.

@Test
public void limit() throws RepositoryException {
    Session session = getAdminSession();
    Node hello1 = session.getRootNode().addNode("hello1");
    hello1.setProperty("id", "1");
    hello1.setProperty("data", "x");
    session.save();
    Node hello3 = session.getRootNode().addNode("hello3");
    hello3.setProperty("id", "3");
    hello3.setProperty("data", "z");
    session.save();
    Node hello2 = session.getRootNode().addNode("hello2");
    hello2.setProperty("id", "2");
    hello2.setProperty("data", "y");
    session.save();
    ValueFactory vf = session.getValueFactory();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery("select id from [nt:base] where data >= $data order by id", Query.JCR_SQL2);
    q.bindValue("data", vf.createValue("x"));
    for (int limit = 0; limit < 5; limit++) {
        q.setLimit(limit);
        for (int offset = 0; offset < 3; offset++) {
            q.setOffset(offset);
            QueryResult r = q.execute();
            RowIterator it = r.getRows();
            int l = Math.min(Math.max(0, 3 - offset), limit);
            assertEquals(l, r.getRows().getSize());
            assertEquals(l, r.getNodes().getSize());
            Row row;
            for (int x = offset + 1, i = 0; i < limit && x < 4; i++, x++) {
                assertTrue(it.hasNext());
                row = it.nextRow();
                assertEquals("" + x, row.getValue("id").getString());
            }
            assertFalse(it.hasNext());
        }
    }
}
Also used : QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) ValueFactory(javax.jcr.ValueFactory) Row(javax.jcr.query.Row) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 47 with Row

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

the class QueryTest method simple.

@SuppressWarnings("deprecation")
@Test
public void simple() throws RepositoryException {
    Session session = getAdminSession();
    Node hello = session.getRootNode().addNode("hello");
    hello.setProperty("id", "1");
    hello.setProperty("text", "hello_world");
    session.save();
    Node hello2 = session.getRootNode().addNode("hello2");
    hello2.setProperty("id", "2");
    hello2.setProperty("text", "hello world");
    session.save();
    ValueFactory vf = session.getValueFactory();
    QueryManager qm = session.getWorkspace().getQueryManager();
    // SQL-2
    Query q = qm.createQuery("select text from [nt:base] where id = $id", Query.JCR_SQL2);
    q.bindValue("id", vf.createValue("1"));
    QueryResult r = q.execute();
    RowIterator it = r.getRows();
    assertTrue(it.hasNext());
    Row row = it.nextRow();
    assertEquals("hello_world", row.getValue("text").getString());
    String[] columns = r.getColumnNames();
    assertEquals(1, columns.length);
    assertEquals("text", columns[0]);
    assertFalse(it.hasNext());
    r = q.execute();
    NodeIterator nodeIt = r.getNodes();
    assertTrue(nodeIt.hasNext());
    Node n = nodeIt.nextNode();
    assertEquals("hello_world", n.getProperty("text").getString());
    assertFalse(it.hasNext());
    // SQL
    q = qm.createQuery("select text from [nt:base] where text like 'hello\\_world' escape '\\'", Query.SQL);
    r = q.execute();
    columns = r.getColumnNames();
    assertEquals(3, columns.length);
    assertEquals("text", columns[0]);
    assertEquals("jcr:path", columns[1]);
    assertEquals("jcr:score", columns[2]);
    nodeIt = r.getNodes();
    assertTrue(nodeIt.hasNext());
    n = nodeIt.nextNode();
    assertEquals("hello_world", n.getProperty("text").getString());
    assertFalse(nodeIt.hasNext());
    // XPath
    q = qm.createQuery("//*[@id=1]", Query.XPATH);
    r = q.execute();
    assertEquals(newHashSet("jcr:path", "jcr:score", "jcr:primaryType"), newHashSet(r.getColumnNames()));
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) Query(javax.jcr.query.Query) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) ValueFactory(javax.jcr.ValueFactory) Row(javax.jcr.query.Row) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 48 with Row

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

the class ColumnTest method testMultiColumn.

public void testMultiColumn() throws RepositoryException {
    Node n = testRootNode.addNode(nodeName1, testNodeType);
    n.setProperty(propertyName1, TEST_VALUE);
    superuser.save();
    final String columnName1 = SELECTOR_1 + "." + propertyName1;
    final String columnName2 = SELECTOR_2 + "." + propertyName1;
    QueryObjectModel qom = qf.createQuery(qf.join(qf.selector(testNodeType, SELECTOR_1), qf.selector(testNodeType, SELECTOR_2), QueryObjectModelConstants.JCR_JOIN_TYPE_INNER, qf.equiJoinCondition(SELECTOR_1, propertyName1, SELECTOR_2, propertyName1)), qf.descendantNode(SELECTOR_1, testRoot), null, new Column[] { qf.column(SELECTOR_1, propertyName1, columnName1), qf.column(SELECTOR_2, propertyName1, columnName2) });
    forQOMandSQL2(qom, new Callable() {

        public Object call(Query query) throws RepositoryException {
            RowIterator rows = query.execute().getRows();
            assertTrue("empty result", rows.hasNext());
            Row r = rows.nextRow();
            assertEquals("unexpected value", TEST_VALUE, r.getValue(columnName1).getString());
            assertEquals("unexpected value", TEST_VALUE, r.getValue(columnName2).getString());
            return null;
        }
    });
}
Also used : Query(javax.jcr.query.Query) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel) RepositoryException(javax.jcr.RepositoryException) Row(javax.jcr.query.Row)

Example 49 with Row

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

the class AbstractQOMTest method checkResult.

protected void checkResult(QueryResult result, String[] selectorNames, Node[][] nodes) throws RepositoryException {
    // collect rows
    Set<String> expectedPaths = new HashSet<String>();
    log.println("expected:");
    for (int i = 0; i < nodes.length; i++) {
        StringBuffer aggregatedPaths = new StringBuffer();
        for (int j = 0; j < nodes[i].length; j++) {
            aggregatedPaths.append(getPath(nodes[i][j]));
            aggregatedPaths.append("|");
        }
        expectedPaths.add(aggregatedPaths.toString());
        log.println(aggregatedPaths.toString());
    }
    Set<String> resultPaths = new HashSet<String>();
    log.println("result:");
    for (RowIterator it = result.getRows(); it.hasNext(); ) {
        Row r = it.nextRow();
        StringBuffer aggregatedPaths = new StringBuffer();
        for (int i = 0; i < selectorNames.length; i++) {
            aggregatedPaths.append(getPath(r.getNode(selectorNames[i])));
            aggregatedPaths.append("|");
        }
        resultPaths.add(aggregatedPaths.toString());
        log.println(aggregatedPaths.toString());
    }
    // check if all expected are in result
    for (Iterator<String> it = expectedPaths.iterator(); it.hasNext(); ) {
        String path = it.next();
        assertTrue(path + " is not part of the result set", resultPaths.contains(path));
    }
    // check result does not contain more than expected
    for (Iterator<String> it = resultPaths.iterator(); it.hasNext(); ) {
        String path = it.next();
        assertTrue(path + " is not expected to be part of the result set", expectedPaths.contains(path));
    }
}
Also used : RowIterator(javax.jcr.query.RowIterator) Row(javax.jcr.query.Row) HashSet(java.util.HashSet)

Example 50 with Row

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

the class AbstractQOMTest method checkResultOrder.

protected void checkResultOrder(QueryResult result, String[] selectorNames, Node[][] nodes) throws RepositoryException {
    // collect rows
    List<String> expectedPaths = new ArrayList<String>();
    log.println("expected:");
    for (int i = 0; i < nodes.length; i++) {
        StringBuffer aggregatedPaths = new StringBuffer();
        for (int j = 0; j < nodes[i].length; j++) {
            aggregatedPaths.append(getPath(nodes[i][j]));
            aggregatedPaths.append("|");
        }
        expectedPaths.add(aggregatedPaths.toString());
        log.println(aggregatedPaths.toString());
    }
    List<String> resultPaths = new ArrayList<String>();
    log.println("result:");
    for (RowIterator it = result.getRows(); it.hasNext(); ) {
        Row r = it.nextRow();
        StringBuffer aggregatedPaths = new StringBuffer();
        for (int i = 0; i < selectorNames.length; i++) {
            aggregatedPaths.append(getPath(r.getNode(selectorNames[i])));
            aggregatedPaths.append("|");
        }
        resultPaths.add(aggregatedPaths.toString());
        log.println(aggregatedPaths.toString());
    }
    assertEquals("wrong result order", expectedPaths, resultPaths);
}
Also used : RowIterator(javax.jcr.query.RowIterator) ArrayList(java.util.ArrayList) Row(javax.jcr.query.Row)

Aggregations

Row (javax.jcr.query.Row)52 RowIterator (javax.jcr.query.RowIterator)27 QueryResult (javax.jcr.query.QueryResult)21 QueryManager (javax.jcr.query.QueryManager)14 ArrayList (java.util.ArrayList)13 Node (javax.jcr.Node)13 Value (javax.jcr.Value)9 Query (javax.jcr.query.Query)9 Test (org.junit.Test)9 Constraint (javax.jcr.query.qom.Constraint)8 RepositoryException (javax.jcr.RepositoryException)7 Session (javax.jcr.Session)7 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)6 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)6 ValueFactory (javax.jcr.ValueFactory)5 HashSet (java.util.HashSet)4 TreeSet (java.util.TreeSet)4 HashMap (java.util.HashMap)3 NoSuchElementException (java.util.NoSuchElementException)3 NodeIterator (javax.jcr.NodeIterator)2