Search in sources :

Example 66 with RowIterator

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

the class QueryPlanTest method propertyEqualsVersusPropertyNotNull.

@Test
public // OAK-1903
void propertyEqualsVersusPropertyNotNull() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node testRootNode = session.getRootNode().addNode("testroot");
    createPropertyIndex(session, "notNull");
    createPropertyIndex(session, "equals");
    for (int i = 0; i < 100; i++) {
        Node n = testRootNode.addNode("n" + i, "oak:Unstructured");
        if (i % 2 == 0) {
            n.setProperty("notNull", i);
        }
        n.setProperty("equals", 1);
    }
    session.save();
    String xpath = "/jcr:root//*[@notNull and @equals=1]";
    Query q;
    QueryResult result;
    RowIterator it;
    q = qm.createQuery("explain " + xpath, "xpath");
    result = q.execute();
    it = result.getRows();
    assertTrue(it.hasNext());
    String plan = it.nextRow().getValue("plan").getString();
    // System.out.println("plan: " + plan);
    // should not use the index on "jcr:uuid"
    assertEquals("[nt:base] as [a] /* property notNull IS NOT NULL " + "where ([a].[notNull] is not null) " + "and ([a].[equals] = 1) " + "and (isdescendantnode([a], [/])) */", plan);
}
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) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 67 with RowIterator

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

the class QueryTest method getValuesOnMvp.

/**
     * OAK-1093
     */
@Test
public void getValuesOnMvp() throws RepositoryException {
    Session session = getAdminSession();
    Node hello = session.getRootNode().addNode("hello");
    hello.setProperty("id", "1");
    hello.setProperty("properties", new String[] { "p1", "p2" });
    session.save();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery("select properties from [nt:base] where id = 1", Query.JCR_SQL2);
    QueryResult r = q.execute();
    RowIterator it = r.getRows();
    assertTrue(it.hasNext());
    Row row = it.nextRow();
    assertEquals("p1 p2", row.getValues()[0].getString());
}
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) 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 68 with RowIterator

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

the class QueryTest method firstSelector.

@Test
public void firstSelector() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    Node a = root.addNode("a");
    a.setProperty("test", true);
    Node b = a.addNode("b");
    b.setProperty("test", true);
    session.save();
    QueryResult r = session.getWorkspace().getQueryManager().createQuery("//a[@test]/b[@test]", "xpath").execute();
    String firstSelector = r.getSelectorNames()[0];
    RowIterator rows = r.getRows();
    Row row = rows.nextRow();
    String path = row.getPath(firstSelector);
    assertEquals("/a/b", path);
}
Also used : QueryResult(javax.jcr.query.QueryResult) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) 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 69 with RowIterator

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

the class QueryTest method getPaths.

private static String getPaths(Query q) throws RepositoryException {
    QueryResult r = q.execute();
    RowIterator it = r.getRows();
    StringBuilder buff = new StringBuilder();
    if (it.hasNext()) {
        Row row = it.nextRow();
        if (buff.length() > 0) {
            buff.append(", ");
        }
        buff.append(row.getPath());
    }
    return buff.toString();
}
Also used : QueryResult(javax.jcr.query.QueryResult) RowIterator(javax.jcr.query.RowIterator) Row(javax.jcr.query.Row)

Example 70 with RowIterator

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

the class QueryTest method nodeTypeConstraint.

@SuppressWarnings("deprecation")
@Test
public void nodeTypeConstraint() throws Exception {
    Session session = getAdminSession();
    Node root = session.getRootNode();
    Node folder1 = root.addNode("folder1", "nt:folder");
    Node folder2 = root.addNode("folder2", "nt:folder");
    JcrUtils.putFile(folder1, "file", "text/plain", new ByteArrayInputStream("foo bar".getBytes("UTF-8")));
    folder2.addNode("folder3", "nt:folder");
    session.save();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q = qm.createQuery("//element(*, nt:folder)", Query.XPATH);
    Set<String> paths = new HashSet<String>();
    for (RowIterator it = q.execute().getRows(); it.hasNext(); ) {
        paths.add(it.nextRow().getPath());
    }
    assertEquals(new HashSet<String>(Arrays.asList("/folder1", "/folder2", "/folder2/folder3")), paths);
}
Also used : Query(javax.jcr.query.Query) ByteArrayInputStream(java.io.ByteArrayInputStream) Node(javax.jcr.Node) RowIterator(javax.jcr.query.RowIterator) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) HashSet(java.util.HashSet) Sets.newHashSet(com.google.common.collect.Sets.newHashSet) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Aggregations

RowIterator (javax.jcr.query.RowIterator)86 Node (javax.jcr.Node)48 QueryResult (javax.jcr.query.QueryResult)45 QueryManager (javax.jcr.query.QueryManager)27 Row (javax.jcr.query.Row)27 Query (javax.jcr.query.Query)25 Test (org.junit.Test)20 Session (javax.jcr.Session)17 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)15 RepositoryException (javax.jcr.RepositoryException)12 Value (javax.jcr.Value)11 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)8 ValueFactory (javax.jcr.ValueFactory)7 ArrayList (java.util.ArrayList)5 NodeIterator (javax.jcr.NodeIterator)5 NoSuchElementException (java.util.NoSuchElementException)4 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)4 RowIteratorAdapter (org.apache.jackrabbit.commons.iterator.RowIteratorAdapter)4 HashMap (java.util.HashMap)3 Iterator (java.util.Iterator)3