use of siena.QueryJoin in project siena by mandubian.
the class JdbcDBUtils method appendSqlOrder.
public static <T> void appendSqlOrder(Query<T> query, StringBuilder sql) {
Class<T> clazz = query.getQueriedClass();
JdbcClassInfo info = JdbcClassInfo.getClassInfo(clazz);
List<QueryOrder> orders = query.getOrders();
List<QueryJoin> joins = query.getJoins();
if (orders.isEmpty() && joins.isEmpty()) {
return;
}
sql.append(" ORDER BY ");
boolean first = true;
for (QueryOrder order : orders) {
if (!first) {
sql.append(", ");
}
first = false;
if (order.parentField == null) {
String[] columns = ClassInfo.getColumnNames(order.field, info.tableName);
for (String column : columns) {
sql.append(column + (order.ascending ? "" : " DESC"));
}
} else {
try {
JdbcClassInfo parentCi = JdbcClassInfo.getClassInfo(order.parentField.getType());
Field subField = order.parentField.getType().getField(order.field.getName());
// get columns using join field alias
//String[] columns = ClassInfo.getColumnNames(subField, parentCi.tableName);
String[] columns = ClassInfo.getColumnNames(subField, parentCi.joinFieldAliases.get(order.parentField.getName()));
for (String column : columns) {
sql.append(column + (order.ascending ? "" : " DESC"));
}
} catch (NoSuchFieldException ex) {
throw new SienaException("Order not possible: join sort field " + order.field.getName() + " is not a known field of " + order.parentField.getName(), ex);
}
}
}
}
Aggregations