Search in sources :

Example 1 with QueryObjectModelFactory

use of javax.jcr.query.qom.QueryObjectModelFactory in project jackrabbit by apache.

the class SQL2PathEscapingTest method testGetChildrenApiDirect.

/**
     * will build a query directly via the api using a spaced path
     * 
     * @throws Exception
     */
public void testGetChildrenApiDirect() 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(qom.execute(), 2);
}
Also used : Column(javax.jcr.query.qom.Column) Constraint(javax.jcr.query.qom.Constraint) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel) QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) Source(javax.jcr.query.qom.Source)

Example 2 with QueryObjectModelFactory

use of javax.jcr.query.qom.QueryObjectModelFactory in project jackrabbit by apache.

the class QueryManagerImpl method getQuery.

/**
     * @see QueryManager#getQuery(Node)
     */
public Query getQuery(Node node) throws InvalidQueryException, RepositoryException {
    checkIsAlive();
    NamePathResolver resolver = mgrProvider.getNamePathResolver();
    if (!node.isNodeType(resolver.getJCRName(NameConstants.NT_QUERY))) {
        throw new InvalidQueryException("Node is not of type nt:query");
    }
    if (node.getSession() != session) {
        throw new InvalidQueryException("Node belongs to a different session.");
    }
    String statement = node.getProperty(resolver.getJCRName(NameConstants.JCR_STATEMENT)).getString();
    String language = node.getProperty(resolver.getJCRName(NameConstants.JCR_LANGUAGE)).getString();
    if (Query.JCR_JQOM.equals(language)) {
        QueryObjectModelFactory qomFactory = new QOMFactory(node, resolver);
        QueryObjectModelBuilder builder = QueryObjectModelBuilderRegistry.getQueryObjectModelBuilder(language);
        return builder.createQueryObjectModel(statement, qomFactory, valueFactory);
    } else {
        return new QueryImpl(session, mgrProvider, itemMgr, wspManager, statement, language, node);
    }
}
Also used : NamePathResolver(org.apache.jackrabbit.spi.commons.conversion.NamePathResolver) QueryObjectModelBuilder(org.apache.jackrabbit.commons.query.QueryObjectModelBuilder) QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 3 with QueryObjectModelFactory

use of javax.jcr.query.qom.QueryObjectModelFactory in project jackrabbit by apache.

the class QueryImpl method columnForName.

/**
     * Returns a column for the given property name and the default selector
     * name.
     *
     * @param propertyName the name of the property as well as the column.
     * @return a column.
     * @throws RepositoryException if an error occurs while creating the column.
     */
protected ColumnImpl columnForName(Name propertyName) throws RepositoryException {
    Workspace workspace = sessionContext.getSessionImpl().getWorkspace();
    QueryObjectModelFactory qomFactory = workspace.getQueryManager().getQOMFactory();
    String name = sessionContext.getJCRName(propertyName);
    return (ColumnImpl) qomFactory.column(sessionContext.getJCRName(DEFAULT_SELECTOR_NAME), name, name);
}
Also used : QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) ColumnImpl(org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl) Workspace(javax.jcr.Workspace)

Example 4 with QueryObjectModelFactory

use of javax.jcr.query.qom.QueryObjectModelFactory in project jackrabbit by apache.

the class QueryImpl method getColumns.

/**
     * Returns the columns for this query.
     *
     * @return array of columns.
     * @throws RepositoryException if an error occurs.
     */
protected ColumnImpl[] getColumns() throws RepositoryException {
    SessionImpl session = sessionContext.getSessionImpl();
    QueryObjectModelFactory qomFactory = session.getWorkspace().getQueryManager().getQOMFactory();
    // get columns
    Map<Name, ColumnImpl> columns = new LinkedHashMap<Name, ColumnImpl>();
    for (Name name : root.getSelectProperties()) {
        String pn = sessionContext.getJCRName(name);
        ColumnImpl col = (ColumnImpl) qomFactory.column(sessionContext.getJCRName(DEFAULT_SELECTOR_NAME), pn, pn);
        columns.put(name, col);
    }
    if (columns.size() == 0) {
        // use node type constraint
        LocationStepQueryNode[] steps = root.getLocationNode().getPathSteps();
        final Name[] ntName = new Name[1];
        steps[steps.length - 1].acceptOperands(new DefaultQueryNodeVisitor() {

            public Object visit(AndQueryNode node, Object data) throws RepositoryException {
                return node.acceptOperands(this, data);
            }

            public Object visit(NodeTypeQueryNode node, Object data) {
                ntName[0] = node.getValue();
                return data;
            }
        }, null);
        if (ntName[0] == null) {
            ntName[0] = NameConstants.NT_BASE;
        }
        NodeTypeImpl nt = session.getNodeTypeManager().getNodeType(ntName[0]);
        PropertyDefinition[] propDefs = nt.getPropertyDefinitions();
        for (PropertyDefinition pd : propDefs) {
            QPropertyDefinition propDef = ((PropertyDefinitionImpl) pd).unwrap();
            if (!propDef.definesResidual() && !propDef.isMultiple()) {
                columns.put(propDef.getName(), columnForName(propDef.getName()));
            }
        }
    }
    // add jcr:path and jcr:score if not selected already
    if (!columns.containsKey(NameConstants.JCR_PATH)) {
        columns.put(NameConstants.JCR_PATH, columnForName(NameConstants.JCR_PATH));
    }
    if (!columns.containsKey(NameConstants.JCR_SCORE)) {
        columns.put(NameConstants.JCR_SCORE, columnForName(NameConstants.JCR_SCORE));
    }
    return columns.values().toArray(new ColumnImpl[columns.size()]);
}
Also used : NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) LocationStepQueryNode(org.apache.jackrabbit.spi.commons.query.LocationStepQueryNode) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) LinkedHashMap(java.util.LinkedHashMap) Name(org.apache.jackrabbit.spi.Name) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) SessionImpl(org.apache.jackrabbit.core.SessionImpl) QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) ColumnImpl(org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl) DefaultQueryNodeVisitor(org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor) NodeTypeQueryNode(org.apache.jackrabbit.spi.commons.query.NodeTypeQueryNode) AndQueryNode(org.apache.jackrabbit.spi.commons.query.AndQueryNode)

Example 5 with QueryObjectModelFactory

use of javax.jcr.query.qom.QueryObjectModelFactory in project jackrabbit by apache.

the class SQL2PathEscapingTest method testGetChildrenApiStatement.

/**
     * 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);
}
Also used : Column(javax.jcr.query.qom.Column) Constraint(javax.jcr.query.qom.Constraint) QueryObjectModel(javax.jcr.query.qom.QueryObjectModel) QueryObjectModelFactory(javax.jcr.query.qom.QueryObjectModelFactory) Source(javax.jcr.query.qom.Source)

Aggregations

QueryObjectModelFactory (javax.jcr.query.qom.QueryObjectModelFactory)5 Column (javax.jcr.query.qom.Column)2 Constraint (javax.jcr.query.qom.Constraint)2 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)2 Source (javax.jcr.query.qom.Source)2 ColumnImpl (org.apache.jackrabbit.spi.commons.query.qom.ColumnImpl)2 LinkedHashMap (java.util.LinkedHashMap)1 RepositoryException (javax.jcr.RepositoryException)1 Workspace (javax.jcr.Workspace)1 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)1 InvalidQueryException (javax.jcr.query.InvalidQueryException)1 QueryObjectModelBuilder (org.apache.jackrabbit.commons.query.QueryObjectModelBuilder)1 SessionImpl (org.apache.jackrabbit.core.SessionImpl)1 NodeTypeImpl (org.apache.jackrabbit.core.nodetype.NodeTypeImpl)1 Name (org.apache.jackrabbit.spi.Name)1 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)1 NamePathResolver (org.apache.jackrabbit.spi.commons.conversion.NamePathResolver)1 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)1 AndQueryNode (org.apache.jackrabbit.spi.commons.query.AndQueryNode)1 DefaultQueryNodeVisitor (org.apache.jackrabbit.spi.commons.query.DefaultQueryNodeVisitor)1