Search in sources :

Example 6 with Ordering

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

the class QueryEngine method createSortFields.

public SortField[] createSortFields(Ordering[] orderings, Session session) throws RepositoryException {
    if (orderings == null || orderings.length == 0) {
        return new SortField[] { SortField.FIELD_SCORE };
    }
    // orderings[] -> (property, ordering)
    Map<String, Ordering> orderByProperties = new HashMap<String, Ordering>();
    for (Ordering o : orderings) {
        final String p = o.toString();
        if (!orderByProperties.containsKey(p)) {
            orderByProperties.put(p, o);
        }
    }
    final DynamicOperandFieldComparatorSource dofcs = new DynamicOperandFieldComparatorSource(session, evaluator, orderByProperties);
    List<SortField> sortFields = new ArrayList<SortField>();
    // insertion order
    for (Ordering o : orderings) {
        final String p = o.toString();
        // order on jcr:score does not use the natural order as
        // implemented in lucene. score ascending in lucene means that
        // higher scores are first. JCR specs that lower score values
        // are first.
        boolean isAsc = QueryObjectModelConstants.JCR_ORDER_ASCENDING.equals(o.getOrder());
        if (JcrConstants.JCR_SCORE.equals(p)) {
            sortFields.add(new SortField(null, SortField.SCORE, !isAsc));
        } else {
            // TODO use native sort if available
            sortFields.add(new SortField(p, dofcs, !isAsc));
        }
    }
    return sortFields.toArray(new SortField[sortFields.size()]);
}
Also used : DynamicOperandFieldComparatorSource(org.apache.jackrabbit.core.query.lucene.sort.DynamicOperandFieldComparatorSource) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Ordering(javax.jcr.query.qom.Ordering) ArrayList(java.util.ArrayList) SortField(org.apache.lucene.search.SortField)

Example 7 with Ordering

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

the class RowComparator method compare.

public int compare(Row a, Row b) {
    try {
        for (Ordering ordering : orderings) {
            Operand operand = ordering.getOperand();
            Value[] va = evaluator.getValues(operand, a);
            Value[] vb = evaluator.getValues(operand, b);
            int d = comparator.compare(va, vb);
            if (d != 0) {
                if (JCR_ORDER_DESCENDING.equals(ordering.getOrder())) {
                    return -d;
                } else {
                    return d;
                }
            }
        }
        return 0;
    } catch (RepositoryException e) {
        throw new RuntimeException("Unable to compare rows " + a + " and " + b, e);
    }
}
Also used : Operand(javax.jcr.query.qom.Operand) Ordering(javax.jcr.query.qom.Ordering) Value(javax.jcr.Value) RepositoryException(javax.jcr.RepositoryException)

Example 8 with Ordering

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

the class Parser method createQueryObjectModel.

/**
     * Parse a JCR-SQL2 query and return the query object model
     *
     * @param query the query string
     * @return the query object model
     * @throws RepositoryException if parsing failed
     */
public QueryObjectModel createQueryObjectModel(String query) throws RepositoryException {
    initialize(query);
    selectors = new ArrayList<Selector>();
    expected = new ArrayList<String>();
    bindVariables = new HashMap<String, BindVariableValue>();
    read();
    read("SELECT");
    int columnParseIndex = parseIndex;
    ArrayList<ColumnOrWildcard> list = parseColumns();
    read("FROM");
    Source source = parseSource();
    Column[] columnArray = resolveColumns(columnParseIndex, list);
    Constraint constraint = null;
    if (readIf("WHERE")) {
        constraint = parseConstraint();
    }
    Ordering[] orderings = null;
    if (readIf("ORDER")) {
        read("BY");
        orderings = parseOrder();
    }
    if (currentToken.length() > 0) {
        throw getSyntaxError("<end>");
    }
    return factory.createQuery(source, constraint, orderings, columnArray);
}
Also used : BindVariableValue(javax.jcr.query.qom.BindVariableValue) Constraint(javax.jcr.query.qom.Constraint) Constraint(javax.jcr.query.qom.Constraint) Source(javax.jcr.query.qom.Source) Column(javax.jcr.query.qom.Column) Ordering(javax.jcr.query.qom.Ordering) Selector(javax.jcr.query.qom.Selector)

Example 9 with Ordering

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

the class Parser method parseOrder.

private Ordering[] parseOrder() throws RepositoryException {
    ArrayList<Ordering> orderList = new ArrayList<Ordering>();
    do {
        Ordering ordering;
        DynamicOperand op = parseDynamicOperand();
        if (readIf("DESC")) {
            ordering = factory.descending(op);
        } else {
            readIf("ASC");
            ordering = factory.ascending(op);
        }
        orderList.add(ordering);
    } while (readIf(","));
    Ordering[] orderings = new Ordering[orderList.size()];
    orderList.toArray(orderings);
    return orderings;
}
Also used : DynamicOperand(javax.jcr.query.qom.DynamicOperand) ArrayList(java.util.ArrayList) Ordering(javax.jcr.query.qom.Ordering)

Example 10 with Ordering

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

the class QOMFormatter method append.

private void append(Ordering[] orderings) {
    String comma = "";
    for (Ordering ordering : orderings) {
        append(comma);
        comma = ", ";
        append(ordering.getOperand());
        if (JCR_ORDER_DESCENDING.equals(ordering.getOrder())) {
            append(" DESC");
        }
    }
}
Also used : Ordering(javax.jcr.query.qom.Ordering)

Aggregations

Ordering (javax.jcr.query.qom.Ordering)18 PropertyValue (javax.jcr.query.qom.PropertyValue)9 QueryObjectModel (javax.jcr.query.qom.QueryObjectModel)6 Selector (javax.jcr.query.qom.Selector)6 Column (javax.jcr.query.qom.Column)5 Constraint (javax.jcr.query.qom.Constraint)4 PropertyExistence (javax.jcr.query.qom.PropertyExistence)4 Source (javax.jcr.query.qom.Source)3 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 BindVariableValue (javax.jcr.query.qom.BindVariableValue)2 DynamicOperand (javax.jcr.query.qom.DynamicOperand)2 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 RepositoryException (javax.jcr.RepositoryException)1 Value (javax.jcr.Value)1 Operand (javax.jcr.query.qom.Operand)1 DynamicOperandFieldComparatorSource (org.apache.jackrabbit.core.query.lucene.sort.DynamicOperandFieldComparatorSource)1 SortField (org.apache.lucene.search.SortField)1