Search in sources :

Example 81 with QueryManager

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

the class QueryFulltextTest method fulltextOrWithinText.

@Test
public void fulltextOrWithinText() throws Exception {
    Session session = getAdminSession();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Node testRootNode = session.getRootNode().addNode("testroot");
    Node n1 = testRootNode.addNode("node1");
    n1.setProperty("text", "hello");
    Node n2 = testRootNode.addNode("node2");
    n2.setProperty("text", "hallo");
    Node n3 = testRootNode.addNode("node3");
    n3.setProperty("text", "hello hallo");
    session.save();
    String sql2 = "select [jcr:path] as [path] from [nt:base] " + "where contains([text], 'hello OR hallo') order by [jcr:path]";
    Query q;
    q = qm.createQuery("explain " + sql2, Query.JCR_SQL2);
    assertEquals("[nt:base] as [nt:base] /* traverse \"*\" " + "where contains([nt:base].[text], 'hello OR hallo') */", getResult(q.execute(), "plan"));
    // verify the result
    // uppercase "OR" mean logical "or"
    q = qm.createQuery(sql2, Query.JCR_SQL2);
    assertEquals("/testroot/node1, /testroot/node2, /testroot/node3", getResult(q.execute(), "path"));
    // lowercase "or" mean search for the term "or"
    sql2 = "select [jcr:path] as [path] from [nt:base] " + "where contains([text], 'hello or hallo') order by [jcr:path]";
    q = qm.createQuery(sql2, Query.JCR_SQL2);
    assertEquals("", getResult(q.execute(), "path"));
}
Also used : Query(javax.jcr.query.Query) Node(javax.jcr.Node) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 82 with QueryManager

use of javax.jcr.query.QueryManager 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 83 with QueryManager

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

the class QueryTest method fnNameEncoding.

@SuppressWarnings("deprecation")
@Test
public void fnNameEncoding() throws Exception {
    Session session = getAdminSession();
    session.getRootNode().addNode("123456_test_name");
    session.save();
    QueryManager qm = session.getWorkspace().getQueryManager();
    Query q;
    q = qm.createQuery("//*[jcr:like(fn:name(), '%123456%')]", Query.XPATH);
    assertEquals("/123456_test_name", getPaths(q));
    q = qm.createQuery("//*[fn:name() = '123456_test_name']", Query.XPATH);
    assertEquals("", getPaths(q));
}
Also used : Query(javax.jcr.query.Query) QueryManager(javax.jcr.query.QueryManager) Session(javax.jcr.Session) JackrabbitSession(org.apache.jackrabbit.api.JackrabbitSession) Test(org.junit.Test) AbstractRepositoryTest(org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)

Example 84 with QueryManager

use of javax.jcr.query.QueryManager 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 85 with QueryManager

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

the class RemappingTest method testQuery1.

public void testQuery1() throws Exception {
    String statement = createStatement("my:property", "stringValue");
    QueryManager qm = session.getWorkspace().getQueryManager();
    QueryResult qr = qm.createQuery(statement, "xpath").execute();
    // xpath: 
    // /jcr:root/testroot/my:node//element(*)[@my:property='stringValue']
    // select [jcr:path], [jcr:score], * from [nt:base] as a 
    // where [my:property] = 'stringValue' 
    // and isdescendantnode(a, '/testroot/my:node') 
    NodeIterator ni = qr.getNodes();
    assertTrue(ni.hasNext());
    assertEquals(resultPath, ni.nextNode().getPath());
}
Also used : NodeIterator(javax.jcr.NodeIterator) QueryResult(javax.jcr.query.QueryResult) QueryManager(javax.jcr.query.QueryManager)

Aggregations

QueryManager (javax.jcr.query.QueryManager)103 Query (javax.jcr.query.Query)69 Node (javax.jcr.Node)61 QueryResult (javax.jcr.query.QueryResult)55 Session (javax.jcr.Session)54 NodeIterator (javax.jcr.NodeIterator)34 RowIterator (javax.jcr.query.RowIterator)27 Test (org.junit.Test)27 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)22 RepositoryException (javax.jcr.RepositoryException)18 Row (javax.jcr.query.Row)14 JackrabbitSession (org.apache.jackrabbit.api.JackrabbitSession)14 FacetResult (org.apache.jackrabbit.oak.query.facet.FacetResult)9 ValueFactory (javax.jcr.ValueFactory)7 NoSuchElementException (java.util.NoSuchElementException)6 InvalidItemStateException (javax.jcr.InvalidItemStateException)3 Value (javax.jcr.Value)3 Authorizable (org.apache.jackrabbit.api.security.user.Authorizable)3 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2