Search in sources :

Example 6 with QueryJoin

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);
            }
        }
    }
}
Also used : QueryJoin(siena.QueryJoin) QueryOrder(siena.QueryOrder) Field(java.lang.reflect.Field) JdbcClassInfo(siena.jdbc.JdbcPersistenceManager.JdbcClassInfo) SienaException(siena.SienaException)

Aggregations

QueryJoin (siena.QueryJoin)6 Field (java.lang.reflect.Field)5 HashMap (java.util.HashMap)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 JdbcClassInfo (siena.jdbc.JdbcPersistenceManager.JdbcClassInfo)2 Key (com.google.appengine.api.datastore.Key)1 ClassInfo (siena.ClassInfo)1 QueryOrder (siena.QueryOrder)1 SienaException (siena.SienaException)1 SienaRestrictedApiException (siena.SienaRestrictedApiException)1 Discovery (siena.base.test.model.Discovery)1