Search in sources :

Example 16 with Ordering

use of org.apache.cayenne.query.Ordering in project cayenne by apache.

the class OrderingTranslator method doAppendPart.

/**
 * Translates query Ordering list to SQL ORDER BY clause. Ordering list is
 * obtained from <code>queryAssembler</code>'s query object. In a process of
 * building of ORDER BY clause, <code>queryAssembler</code> is notified when
 * a join needs to be added.
 *
 * @since 3.0
 */
@Override
protected void doAppendPart() {
    Query q = queryAssembler.getQuery();
    // only select queries can have ordering...
    if (q == null || !(q instanceof SelectQuery)) {
        return;
    }
    Iterator<Ordering> it = ((SelectQuery<?>) q).getOrderings().iterator();
    StringBuilder mainBuffer = this.out;
    try {
        while (it.hasNext()) {
            Ordering ord = it.next();
            // reset buffer to collect SQL for the single column, that we'll
            // be reusing
            this.out = new StringBuilder();
            if (ord.isCaseInsensitive()) {
                out.append("UPPER(");
            }
            Expression exp = ord.getSortSpec();
            if (exp.getType() == Expression.OBJ_PATH) {
                appendObjPath(exp);
            } else if (exp.getType() == Expression.DB_PATH) {
                appendDbPath(exp);
            } else if (exp.getType() == Expression.FUNCTION_CALL) {
                appendFunction(exp);
            } else {
                throw new CayenneRuntimeException("Unsupported ordering expression: %s", exp);
            }
            // Close UPPER() modifier
            if (ord.isCaseInsensitive()) {
                out.append(")");
            }
            String columnSQL = out.toString();
            mainBuffer.append(columnSQL);
            orderByColumnList.add(columnSQL);
            // "ASC" is a noop, omit it from the query
            if (!ord.isAscending()) {
                mainBuffer.append(" DESC");
            }
            if (it.hasNext()) {
                mainBuffer.append(", ");
            }
        }
    } finally {
        this.out = mainBuffer;
    }
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) SelectQuery(org.apache.cayenne.query.SelectQuery) Query(org.apache.cayenne.query.Query) Expression(org.apache.cayenne.exp.Expression) Ordering(org.apache.cayenne.query.Ordering) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 17 with Ordering

use of org.apache.cayenne.query.Ordering in project cayenne by apache.

the class SelectQueryOrderingTab method removeOrdering.

void removeOrdering() {
    int selection = table.getSelectedRow();
    if (selection < 0) {
        return;
    }
    OrderingModel model = (OrderingModel) table.getModel();
    Ordering ordering = model.getOrdering(selection);
    selectQuery.removeOrdering(ordering);
    model.fireTableRowsDeleted(selection, selection);
    mediator.fireQueryEvent(new QueryEvent(SelectQueryOrderingTab.this, selectQuery));
}
Also used : Ordering(org.apache.cayenne.query.Ordering) QueryEvent(org.apache.cayenne.configuration.event.QueryEvent)

Example 18 with Ordering

use of org.apache.cayenne.query.Ordering in project cayenne by apache.

the class SelectQueryOrderingTab method addOrdering.

void addOrdering() {
    String orderingPath = getSelectedPath();
    if (orderingPath == null) {
        return;
    }
    // check if such ordering already exists
    for (Ordering ord : selectQuery.getOrderings()) {
        if (orderingPath.equals(ord.getSortSpecString())) {
            return;
        }
    }
    selectQuery.addOrdering(new Ordering(orderingPath, SortOrder.ASCENDING));
    int index = selectQuery.getOrderings().size() - 1;
    OrderingModel model = (OrderingModel) table.getModel();
    model.fireTableRowsInserted(index, index);
    mediator.fireQueryEvent(new QueryEvent(SelectQueryOrderingTab.this, selectQuery));
}
Also used : Ordering(org.apache.cayenne.query.Ordering) QueryEvent(org.apache.cayenne.configuration.event.QueryEvent)

Aggregations

Ordering (org.apache.cayenne.query.Ordering)18 Test (org.junit.Test)10 QueryEvent (org.apache.cayenne.configuration.event.QueryEvent)2 SelectQuery (org.apache.cayenne.query.SelectQuery)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 Expression (org.apache.cayenne.exp.Expression)1 ExpressionException (org.apache.cayenne.exp.ExpressionException)1 DataMap (org.apache.cayenne.map.DataMap)1 Entity (org.apache.cayenne.map.Entity)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 SelectQueryDescriptor (org.apache.cayenne.map.SelectQueryDescriptor)1 Query (org.apache.cayenne.query.Query)1 SortOrder (org.apache.cayenne.query.SortOrder)1 SimpleLockingTestEntity (org.apache.cayenne.testdo.locking.SimpleLockingTestEntity)1 Artist (org.apache.cayenne.testdo.testmap.Artist)1 CayenneMapEntry (org.apache.cayenne.util.CayenneMapEntry)1