@Override public org.modeshape.jcr.api.query.Query createQuery( String statement, String language ) throws InvalidQueryException, RepositoryException { CheckArg.isNotNull(statement, "statement"); CheckArg.isNotNull(language, "language"); return createQuery(statement, language, null, null); }
@Override public JcrQueryManager getQueryManager() throws RepositoryException { session.checkLive(); if (this.queryManager == null) { try { lock.lock(); if (queryManager == null) queryManager = new JcrQueryManager(session); } finally { lock.unlock(); } } return queryManager; }
hints.hasFullTextSearch = true; // always include the score hints.qualifyExpandedColumnNames = true; // always qualify expanded names with the selector name in JCR-SQL2 return resultWith(expression, QueryLanguage.JCR_SQL2, command, hints, null, null); } catch (org.modeshape.jcr.query.parse.InvalidQueryException e) {
@Test @FixFor( "MODE-2329" ) public void shouldAllowUsingExpandedSelectorNameInQOM() throws Exception { QueryObjectModelFactory qomFactory = session.getWorkspace().getQueryManager().getQOMFactory(); Selector selector = qomFactory.selector(NodeType.NT_BASE, "category"); // Build and execute the query ... Query query = qomFactory.createQuery(selector, qomFactory.childNode("category", "/Cars"), null, new Column[0]); assertThat(query.getStatement(), is("SELECT * FROM [{http://www.jcp.org/jcr/nt/1.0}base] AS category WHERE ISCHILDNODE(category,'/Cars')")); QueryResult result = query.execute(); validateQuery().rowCount(4).hasColumns(allColumnNames("category")).validate(query, result); }
@FixFor( "MODE-1611" ) @Test public void shouldAllowQomUseOfIsChildNodeInWhereClause() throws RepositoryException { QueryObjectModelFactory qomFactory = session.getWorkspace().getQueryManager().getQOMFactory(); Selector selector = qomFactory.selector("nt:base", "category"); ChildNode childNodeConstraint = qomFactory.childNode("category", "/Cars"); Constraint constraint = childNodeConstraint; Column[] columns = new Column[0]; Ordering[] orderings = null; // Build and execute the query ... Query query = qomFactory.createQuery(selector, constraint, orderings, columns); assertThat(query.getStatement(), is("SELECT * FROM [nt:base] AS category WHERE ISCHILDNODE(category,'/Cars')")); QueryResult result = query.execute(); validateQuery().rowCount(4).hasColumns(allColumnNames("category")).validate(query, result); }
@Override public org.modeshape.jcr.api.query.Query createQuery( String statement, String language, Locale locale ) throws InvalidQueryException, RepositoryException { CheckArg.isNotNull(statement, "statement"); CheckArg.isNotNull(language, "language"); CheckArg.isNotNull(locale, "locale"); return createQuery(statement, language, null, locale); }
hints.hasFullTextSearch = true; // always include the score hints.qualifyExpandedColumnNames = true; // always qualify expanded names with the selector name in JCR-SQL2 return resultWith(expression, QueryLanguage.JCR_SQL2, command, hints, null, null); } catch (org.modeshape.jcr.query.parse.InvalidQueryException e) {
@Override public JcrQueryManager getQueryManager() throws RepositoryException { session.checkLive(); if (this.queryManager == null) { try { lock.lock(); if (queryManager == null) queryManager = new JcrQueryManager(session); } finally { lock.unlock(); } } return queryManager; }
@Test @FixFor( { "MODE-1055", "MODE-2347" } ) public void shouldAllowMissingSelectorColumnsInQOM() throws Exception { Node node = session.getRootNode().addNode("test", "modetest:simpleType"); node.setProperty("fieldA", "A_value"); session.save(); // query for a property present in a subtype which doesn't have any residuals, using a super-type selector String sql = "SELECT * FROM [nt:base] AS node WHERE node.fieldA = 'A_value'"; try { QueryObjectModelFactory qomFactory = session.getWorkspace().getQueryManager().getQOMFactory(); Selector selector = qomFactory.selector("nt:base", "node"); PropertyValue propValue = qomFactory.propertyValue("node", "fieldA"); Literal literal = qomFactory.literal(session.getValueFactory().createValue("A_value")); Constraint constraint = qomFactory.comparison(propValue, QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, literal); Query query = qomFactory.createQuery(selector, constraint, null, new Column[0]); assertThat(query.getStatement(), is(sql)); QueryResult result = query.execute(); validateQuery().rowCount(1).validate(query, result); } finally { node.remove(); session.save(); } }
@FixFor("MODE-2499") @Test public void shouldThrowExceptionWhenXPathSyntaxNotValid() throws RepositoryException { String sql = "select node.[jcr:name] from [nt:unstructured] as node"; try { Query query = session.getWorkspace().getQueryManager().createQuery(sql, Query.XPATH); fail("Should throw InvalidQueryException"); } catch (InvalidQueryException e) { } }
hints.qualifyExpandedColumnNames = true; return resultWith(expression, parser.getLanguage(), command, hints, storedAtPath, locale); } catch (ParsingException e) {
protected void assertQomQueryWithBooleanValue( int numResults, String propertyName, String operator, boolean propertyValue ) throws RepositoryException { QueryObjectModelFactory qomFactory = session.getWorkspace().getQueryManager().getQOMFactory(); ValueFactory valueFactory = session.getValueFactory(); Value propertyValueObj = valueFactory.createValue(propertyValue); Selector selector = qomFactory.selector("notion:typed", "node"); Constraint constraint = null; if (propertyName != null && operator != null) { PropertyValue propValue = qomFactory.propertyValue("node", propertyName); Literal literal = qomFactory.literal(propertyValueObj); constraint = qomFactory.comparison(propValue, operator, literal); } Column[] columns = new Column[2]; columns[0] = qomFactory.column("node", "notion:booleanProperty", "notion:booleanProperty"); columns[1] = qomFactory.column("node", "notion:booleanProperty2", "notion:booleanProperty2"); Ordering[] orderings = null; // Build and execute the query ... Query query = qomFactory.createQuery(selector, constraint, orderings, columns); assertThat(query, is(notNullValue())); QueryResult result = query.execute(); String[] columnNames = {"notion:booleanProperty", "notion:booleanProperty2"}; validateQuery().rowCount(numResults).hasColumns(columnNames).validate(query, result); }
private void queryContentInWorkspace( JcrRepository newRepository, String workspaceName ) throws RepositoryException { JcrSession session = newRepository.login(); try { String statement = "SELECT [car:model], [car:year], [car:msrp] FROM [car:Car] AS car"; Query query = session.getWorkspace().getQueryManager().createQuery(statement, Query.JCR_SQL2); QueryResult results = query.execute(); assertThat(results.getRows().getSize(), is(13L)); } finally { session.logout(); } }
hints.qualifyExpandedColumnNames = true; return resultWith(expression, parser.getLanguage(), command, hints, storedAtPath, locale); } catch (ParsingException e) {
private void executeQueryWithSingleResult( String sql ) throws RepositoryException { Query query = session.getWorkspace().getQueryManager().createQuery(sql, JcrRepository.QueryLanguage.JCR_SQL2); QueryResult result = query.execute(); validateQuery().rowCount(1).hasColumns("jcr:path").validate(query, result); }
@SuppressWarnings( "deprecation" ) @Test public void shouldBeAbleToExecuteXPathQueryWithComplexContainsCriteriaWithNoHyphenAndNoWildcard() throws RepositoryException { String xpath = "/jcr:root//*[jcr:contains(., '\"heavy duty\"')]"; Query query = session.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH); QueryResult result = query.execute(); // by default there is no stemming or punctuation replacement assertFalse(result.getRows().hasNext()); }
@SuppressWarnings( "deprecation" ) @Test public void shouldBeAbleToExecuteXPathQueryWithContainsCriteria() throws RepositoryException { String xpath = "/jcr:root//*[jcr:contains(., 'liter')]"; Query query = session.getWorkspace().getQueryManager().createQuery(xpath, Query.XPATH); assertThat(query, is(notNullValue())); QueryResult result = query.execute(); validateQuery().rowCount(2).hasColumns("jcr:primaryType", "jcr:path", "jcr:score").validate(query, result); }
@Test public void shouldGetNodesOrderedByPath() throws RepositoryException { String sql = "SELECT * FROM [nt:unstructured] WHERE ISCHILDNODE('/Cars') ORDER BY [jcr:path]"; Query query = session.getWorkspace().getQueryManager().createQuery(sql, Query.JCR_SQL2); validateQuery().rowCount(4).validate(query, query.execute()); }
@SuppressWarnings( "deprecation" ) @Test public void shouldBeAbleToExecuteXPathQueryToFindChildOfRootNode() throws RepositoryException { Query query = session.getWorkspace().getQueryManager().createQuery("/jcr:root/Cars", Query.XPATH); assertThat(query, is(notNullValue())); QueryResult result = query.execute(); validateQuery().rowCount(1).hasColumns("jcr:primaryType", "jcr:path", "jcr:score").validate(query, result); }
@Test public void shouldBeAbleToCreateAndExecuteJcrSql2QueryWithChildNodeJoin() throws RepositoryException { String sql = "SELECT car.* from [car:Car] as car JOIN [nt:unstructured] as category ON ISCHILDNODE(car,category) WHERE NAME(category) LIKE 'Utility'"; Query query = session.getWorkspace().getQueryManager().createQuery(sql, Query.JCR_SQL2); assertThat(query, is(notNullValue())); QueryResult result = query.execute(); validateQuery().rowCount(5).hasColumns(carColumnNames("car")).validate(query, result); }