Search in sources :

Example 1 with Comparison

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

the class LuceneQueryFactory method mapConstraintToQueryAndFilter.

protected Predicate mapConstraintToQueryAndFilter(QueryPair query, Constraint constraint, Map<String, NodeType> selectorMap, JackrabbitIndexSearcher searcher, IndexReader reader) throws RepositoryException, IOException {
    Predicate filter = Predicate.TRUE;
    if (constraint instanceof And) {
        And and = (And) constraint;
        filter = mapConstraintToQueryAndFilter(query, and.getConstraint1(), selectorMap, searcher, reader);
        Predicate other = mapConstraintToQueryAndFilter(query, and.getConstraint2(), selectorMap, searcher, reader);
        if (filter == Predicate.TRUE) {
            filter = other;
        } else if (other != Predicate.TRUE) {
            filter = Predicates.and(filter, other);
        }
    } else if (constraint instanceof Comparison) {
        Comparison c = (Comparison) constraint;
        Transform transform = new Transform(c.getOperand1());
        DynamicOperand left = transform.operand;
        final String operator = c.getOperator();
        StaticOperand right = c.getOperand2();
        if (left instanceof Length || left instanceof FullTextSearchScore || (((!JCR_OPERATOR_EQUAL_TO.equals(operator) && !JCR_OPERATOR_LIKE.equals(operator)) || transform.transform != TRANSFORM_NONE) && (left instanceof NodeName || left instanceof NodeLocalName))) {
            try {
                int type = PropertyType.UNDEFINED;
                if (left instanceof Length) {
                    type = PropertyType.LONG;
                } else if (left instanceof FullTextSearchScore) {
                    type = PropertyType.DOUBLE;
                }
                final DynamicOperand operand = c.getOperand1();
                final Value value = evaluator.getValue(right, type);
                filter = new RowPredicate() {

                    @Override
                    protected boolean evaluate(Row row) throws RepositoryException {
                        return new ValueComparator().evaluate(operator, evaluator.getValue(operand, row), value);
                    }
                };
            } catch (ValueFormatException e) {
                throw new InvalidQueryException(e);
            }
        } else {
            Query cq = getComparisonQuery(left, transform.transform, operator, right, selectorMap);
            query.subQuery.add(cq, MUST);
        }
    } else if (constraint instanceof DescendantNode) {
        final DescendantNode descendantNode = (DescendantNode) constraint;
        Query context = getNodeIdQuery(UUID, descendantNode.getAncestorPath());
        query.mainQuery = new DescendantSelfAxisQuery(context, query.subQuery, false);
    } else {
        query.subQuery.add(create(constraint, selectorMap, searcher), MUST);
    }
    return filter;
}
Also used : Query(org.apache.lucene.search.Query) BooleanQuery(org.apache.lucene.search.BooleanQuery) DynamicOperand(javax.jcr.query.qom.DynamicOperand) StaticOperand(javax.jcr.query.qom.StaticOperand) NodeName(javax.jcr.query.qom.NodeName) ValueComparator(org.apache.jackrabbit.core.query.lucene.join.ValueComparator) Predicate(org.apache.jackrabbit.commons.predicate.Predicate) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) NodeLocalName(javax.jcr.query.qom.NodeLocalName) RowPredicate(org.apache.jackrabbit.commons.predicate.RowPredicate) Comparison(javax.jcr.query.qom.Comparison) Length(javax.jcr.query.qom.Length) And(javax.jcr.query.qom.And) DescendantNode(javax.jcr.query.qom.DescendantNode) Value(javax.jcr.Value) PropertyValue(javax.jcr.query.qom.PropertyValue) ValueFormatException(javax.jcr.ValueFormatException) FullTextSearchScore(javax.jcr.query.qom.FullTextSearchScore) SelectorRow(org.apache.jackrabbit.core.query.lucene.join.SelectorRow) Row(javax.jcr.query.Row) InvalidQueryException(javax.jcr.query.InvalidQueryException)

Example 2 with Comparison

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

the class QueryObjectModelFactoryTest method testComparison.

/**
 * Test case for {@link QueryObjectModelFactory#comparison(DynamicOperand, String, StaticOperand)}
 */
public void testComparison() throws RepositoryException {
    PropertyValue op1 = qf.propertyValue(SELECTOR_NAME1, propertyName1);
    BindVariableValue op2 = qf.bindVariable(VARIABLE_NAME);
    for (Iterator<String> it = OPERATORS.iterator(); it.hasNext(); ) {
        String operator = it.next();
        Comparison comp = qf.comparison(op1, operator, op2);
        assertTrue("Not a PropertyValue operand", comp.getOperand1() instanceof PropertyValue);
        assertTrue("Not a BindVariableValue operand", comp.getOperand2() instanceof BindVariableValue);
        assertEquals("Wrong operator", operator, comp.getOperator());
    }
}
Also used : BindVariableValue(javax.jcr.query.qom.BindVariableValue) Comparison(javax.jcr.query.qom.Comparison) PropertyValue(javax.jcr.query.qom.PropertyValue)

Example 3 with Comparison

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

the class QomTest method comparison.

@Test
public void comparison() throws RepositoryException {
    PropertyValue p = f.propertyValue("selectorName", "propertyName");
    Literal l = f.literal(vf.createValue(1));
    Comparison c = f.comparison(p, QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, l);
    assertEquals(p, c.getOperand1());
    assertEquals(QueryObjectModelConstants.JCR_OPERATOR_EQUAL_TO, c.getOperator());
    assertEquals(l, c.getOperand2());
    assertEquals("[selectorName].[propertyName] = 1", c.toString());
}
Also used : Comparison(javax.jcr.query.qom.Comparison) Literal(javax.jcr.query.qom.Literal) PropertyValue(javax.jcr.query.qom.PropertyValue) Test(org.junit.Test)

Example 4 with Comparison

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

the class LuceneQueryFactory method create.

protected Query create(Constraint constraint, Map<String, NodeType> selectorMap, JackrabbitIndexSearcher searcher) throws RepositoryException, IOException {
    if (constraint instanceof And) {
        return getAndQuery((And) constraint, selectorMap, searcher);
    } else if (constraint instanceof Or) {
        return getOrQuery((Or) constraint, selectorMap, searcher);
    } else if (constraint instanceof Not) {
        return getNotQuery((Not) constraint, selectorMap, searcher);
    } else if (constraint instanceof PropertyExistence) {
        return getPropertyExistenceQuery((PropertyExistence) constraint);
    } else if (constraint instanceof Comparison) {
        Comparison c = (Comparison) constraint;
        Transform left = new Transform(c.getOperand1());
        return getComparisonQuery(left.operand, left.transform, c.getOperator(), c.getOperand2(), selectorMap);
    } else if (constraint instanceof FullTextSearch) {
        return getFullTextSearchQuery((FullTextSearch) constraint);
    } else if (constraint instanceof SameNode) {
        SameNode sn = (SameNode) constraint;
        return getNodeIdQuery(UUID, sn.getPath());
    } else if (constraint instanceof ChildNode) {
        ChildNode cn = (ChildNode) constraint;
        return getNodeIdQuery(PARENT, cn.getParentPath());
    } else if (constraint instanceof DescendantNode) {
        DescendantNode dn = (DescendantNode) constraint;
        return getDescendantNodeQuery(dn, searcher);
    } else {
        throw new UnsupportedRepositoryOperationException("Unknown constraint type: " + constraint);
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) Not(javax.jcr.query.qom.Not) Or(javax.jcr.query.qom.Or) Comparison(javax.jcr.query.qom.Comparison) And(javax.jcr.query.qom.And) DescendantNode(javax.jcr.query.qom.DescendantNode) FullTextSearch(javax.jcr.query.qom.FullTextSearch) SameNode(javax.jcr.query.qom.SameNode) PropertyExistence(javax.jcr.query.qom.PropertyExistence) ChildNode(javax.jcr.query.qom.ChildNode)

Aggregations

Comparison (javax.jcr.query.qom.Comparison)4 PropertyValue (javax.jcr.query.qom.PropertyValue)3 And (javax.jcr.query.qom.And)2 DescendantNode (javax.jcr.query.qom.DescendantNode)2 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)1 Value (javax.jcr.Value)1 ValueFormatException (javax.jcr.ValueFormatException)1 InvalidQueryException (javax.jcr.query.InvalidQueryException)1 Row (javax.jcr.query.Row)1 BindVariableValue (javax.jcr.query.qom.BindVariableValue)1 ChildNode (javax.jcr.query.qom.ChildNode)1 DynamicOperand (javax.jcr.query.qom.DynamicOperand)1 FullTextSearch (javax.jcr.query.qom.FullTextSearch)1 FullTextSearchScore (javax.jcr.query.qom.FullTextSearchScore)1 Length (javax.jcr.query.qom.Length)1 Literal (javax.jcr.query.qom.Literal)1 NodeLocalName (javax.jcr.query.qom.NodeLocalName)1 NodeName (javax.jcr.query.qom.NodeName)1 Not (javax.jcr.query.qom.Not)1 Or (javax.jcr.query.qom.Or)1