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;
}
}
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));
}
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));
}
Aggregations