public void testCreateQuery() throws RepositoryException { Selector selector = qf.selector(testNodeType, SELECTOR_NAME1); QueryObjectModel qom = qf.createQuery(selector, null, null, null); assertTrue("Not a selector source", qom.getSource() instanceof Selector); assertNull("Constraint must be null", qom.getConstraint()); assertEquals("Wrong size of orderings", 0, qom.getOrderings().length); assertEquals("Wrong size of columns", 0, qom.getColumns().length); }
/** * Executes the query based on QOM and then pops-up in the node hierarchy until returnItemType is found. If the result * is not returnItemType or none of its parents are then next node in result is checked. * Duplicate nodes are removed from result. * For date/time expressions use <code>DateUtil.create*Expression()</code> methods. * * @return Result as NodeIterator */ public static NodeIterator search(QueryObjectModel model, String returnItemType) throws InvalidQueryException, RepositoryException { return NodeUtil.filterDuplicates(NodeUtil.filterParentNodeType(model.execute().getNodes(), returnItemType)); }
/** * Tests if a query object model returns {@link Query#JCR_JQOM} when calling * {@link Query#getLanguage()}. */ public void testJCRQOM() throws RepositoryException { QueryObjectModel qom = qf.createQuery( qf.selector(testNodeType, "s"), null, null, null ); assertEquals("Query returns wrong language.", Query.JCR_JQOM, qom.getLanguage()); } }
protected QueryObjectModel createQuery(Ordering[] orderings) throws RepositoryException { JoinCondition c = qf.equiJoinCondition( LEFT, propertyName1, RIGHT, propertyName2); QueryObjectModel qom = createQuery( QueryObjectModelConstants.JCR_JOIN_TYPE_INNER, c); return qf.createQuery(qom.getSource(), qom.getConstraint(), orderings, qom.getColumns()); } }
protected void checkResultOrder(QueryObjectModel qom, String[] selectorNames, Node[][] nodes) throws RepositoryException { checkResultOrder(qom.execute(), selectorNames, nodes); checkResultOrder(qm.createQuery(qom.getStatement(), Query.JCR_SQL2).execute(), selectorNames, nodes); }
@Test public void createQuery() throws RepositoryException { Selector s = f.selector("nt:file", "x"); BindVariableValue b = f.bindVariable("var"); Constraint c = f.propertyExistence("x", "c"); PropertyValue p = f.propertyValue("x", "propertyName"); c = f.and(f.comparison(p, QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, b), c); Ordering o = f.ascending(p); Column col = f.column("x", "propertyName", "columnName"); Ordering[] ords = new Ordering[]{o}; Column[] cols = new Column[]{col}; QueryObjectModel q = f.createQuery(s, c, ords, cols); assertEquals(Query.JCR_JQOM, q.getLanguage()); String[] bv = q.getBindVariableNames(); assertEquals(1, bv.length); assertEquals("var", bv[0]); assertEquals(s, q.getSource()); assertEquals(c, q.getConstraint()); assertEquals(o, q.getOrderings()[0]); assertEquals(col, q.getColumns()[0]); }
/** * Calls back the <code>callable</code> first with the <code>qom</code> and * then a JCR_SQL2 query created from {@link QueryObjectModel#getStatement()}. * * @param qom a query object model. * @param callable the callback. * @throws RepositoryException if an error occurs. */ protected void forQOMandSQL2(QueryObjectModel qom, Callable callable) throws RepositoryException { List<Query> queries = new ArrayList<Query>(); queries.add(qom); queries.add(qm.createQuery(qom.getStatement(), Query.JCR_SQL2)); for (Iterator<Query> it = queries.iterator(); it.hasNext();) { callable.call(it.next()); } }
/** * Checks the query object model by executing it directly and matching the * result against the given <code>nodes</code>. Then the QOM is executed * again using {@link QueryObjectModel#getStatement()} with {@link * Query#JCR_SQL2}. * * @param qom the query object model to check. * @param nodes the result nodes. * @throws RepositoryException if an error occurs while executing the * query. */ protected void checkQOM(QueryObjectModel qom, Node[] nodes) throws RepositoryException { checkResult(qom.execute(), nodes); checkResult(qm.createQuery(qom.getStatement(), Query.JCR_SQL2).execute(), nodes); }
); queries.add(qom); queries.add(qm.createQuery(qom.getStatement(), Query.JCR_SQL2)); if (isSupportedLanguage(qsXPATH)) { String xpath = testPath + "/element(*, " + testNodeType + ")";
public void testCreateQueryFromSource() throws RepositoryException { Source selector = qf.selector(testNodeType, SELECTOR_NAME1); QueryObjectModel qom = qf.createQuery(selector, null, null, null); assertTrue("Not a selector source", qom.getSource() instanceof Selector); assertNull("Constraint must be null", qom.getConstraint()); assertEquals("Wrong size of orderings", 0, qom.getOrderings().length); assertEquals("Wrong size of columns", 0, qom.getColumns().length); }
public void testURILiteral() throws RepositoryException { Value literal = superuser.getValueFactory().createValue("http://example.com", PropertyType.URI); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with URI that cannot be converted to NAME must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
/** * Checks the query object model by executing it directly and matching the * result against the given <code>nodes</code>. Then the QOM is executed * again using {@link QueryObjectModel#getStatement()} with * {@link Query#JCR_SQL2}. * * @param qom the query object model to check. * @param selectorNames the selector names of the qom. * @param nodes the result nodes. * @throws RepositoryException if an error occurs while executing the * query. */ protected void checkQOM(QueryObjectModel qom, String[] selectorNames, Node[][] nodes) throws RepositoryException { checkResult(qom.execute(), selectorNames, nodes); checkResult(qm.createQuery(qom.getStatement(), Query.JCR_SQL2).execute(), selectorNames, nodes); }
/** * the statement behind the api should be consistent, and return a similar * query. in our case it should escape the paths that have spaces in them by * enclosing them in single quotes * * @throws Exception */ public void testGetChildrenApiStatement() throws Exception { QueryObjectModelFactory qomf = qm.getQOMFactory(); Source source1 = qomf.selector(NodeType.NT_BASE, "selector"); Column[] columns = new Column[] { qomf.column("selector", null, null) }; Constraint constraint2 = qomf.childNode("selector", n1.getPath()); QueryObjectModel qom = qomf.createQuery(source1, constraint2, null, columns); checkResult(executeSQL2Query(qom.getStatement()), 2); }
public void testCreateQueryFromSourceWithConstraint() throws RepositoryException { Source selector = qf.selector(testNodeType, SELECTOR_NAME1); PropertyExistence propExist = qf.propertyExistence(SELECTOR_NAME1, propertyName1); QueryObjectModel qom = qf.createQuery( selector, propExist, null, null); assertTrue("Not a selector source", qom.getSource() instanceof Selector); assertTrue("Not a property existence constraint", qom.getConstraint() instanceof PropertyExistence); assertEquals("Wrong size of orderings", 0, qom.getOrderings().length); assertEquals("Wrong size of columns", 0, qom.getColumns().length); }
public void testStringLiteralInvalidName() throws RepositoryException { Value literal = superuser.getValueFactory().createValue("[" + nodeLocalName); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with STRING that cannot be converted to NAME must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
private QueryResult[] executeQueries(String propertyName, String operator, Value length) throws RepositoryException { QueryObjectModel qom = qf.createQuery( qf.selector(testNodeType, "s"), qf.and( qf.childNode("s", testRoot), qf.comparison( qf.length( qf.propertyValue( "s", propertyName)), operator, qf.literal(length)) ), null, null); QueryResult[] results = new QueryResult[2]; results[0] = qom.execute(); results[1] = qm.createQuery(qom.getStatement(), Query.JCR_SQL2).execute(); return results; } }
public void testCreateQueryWithConstraint() throws RepositoryException { Selector selector = qf.selector(testNodeType, SELECTOR_NAME1); PropertyExistence propExist = qf.propertyExistence(SELECTOR_NAME1, propertyName1); QueryObjectModel qom = qf.createQuery( selector, propExist, null, null); assertTrue("Not a selector source", qom.getSource() instanceof Selector); assertTrue("Not a property existence constraint", qom.getConstraint() instanceof PropertyExistence); assertEquals("Wrong size of orderings", 0, qom.getOrderings().length); assertEquals("Wrong size of columns", 0, qom.getColumns().length); }
public void testDateLiteral() throws RepositoryException { Value literal = vf.createValue(Calendar.getInstance()); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with DATE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST('" + literal.getString() + "' AS DATE)"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with DATE must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }
public void testCreateQueryFromSourceWithConstraintAndOrdering() throws RepositoryException { Source selector = qf.selector(testNodeType, SELECTOR_NAME1); PropertyExistence propExist = qf.propertyExistence(SELECTOR_NAME1, propertyName1); PropertyValue propValue = qf.propertyValue(SELECTOR_NAME1, propertyName1); Ordering ordering = qf.ascending(propValue); QueryObjectModel qom = qf.createQuery(selector, propExist, new Ordering[]{ordering}, null); assertTrue("Not a selector source", qom.getSource() instanceof Selector); assertTrue("Not a property existence constraint", qom.getConstraint() instanceof PropertyExistence); assertEquals("Wrong size of orderings", 1, qom.getOrderings().length); assertEquals("Wrong size of columns", 0, qom.getColumns().length); }
public void testDecimalLiteral() throws RepositoryException { Value literal = vf.createValue(new BigDecimal(283)); try { createQuery(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal).execute(); fail("NodeName comparison with DECIMAL must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } try { String stmt = "SELECT * FROM [" + testNodeType + "] AS s " + "WHERE NAME(s) = CAST('" + literal.getString() + "' AS DECIMAL)"; qm.createQuery(stmt, Query.JCR_SQL2).execute(); fail("NAME() comparison with DECIMAL must fail with InvalidQueryException"); } catch (InvalidQueryException e) { // expected } }