Search in sources :

Example 1 with Relation

use of com.orm.androrm.field.Relation in project androrm by androrm.

the class QueryBuilder method resolveLastField.

private static final <T extends Model> SelectStatement resolveLastField(Object field, Class<T> clazz, Rule rule) {
    SelectStatement select = new SelectStatement();
    if (DatabaseBuilder.isRelationalField(field) && !(field instanceof ForeignKeyField)) {
        Relation<?> r = (Relation<?>) field;
        return getRelationSelection(r, clazz, rule);
    }
    String tableName = DatabaseBuilder.getTableName(clazz);
    Where where = new Where();
    where.setStatement(rule.getStatement());
    select.from(tableName).distinct().select(Model.PK + " AS " + tableName).where(where);
    return select;
}
Also used : SelectStatement(com.orm.androrm.statement.SelectStatement) ForeignKeyField(com.orm.androrm.field.ForeignKeyField) Relation(com.orm.androrm.field.Relation)

Example 2 with Relation

use of com.orm.androrm.field.Relation in project androrm by androrm.

the class QueryBuilder method resolveRelationField.

private static final <T extends Model> SelectStatement resolveRelationField(Object field, Class<T> clazz, List<String> fields, Rule rule, int depth) {
    Relation<?> r = (Relation<?>) field;
    SelectStatement select = new SelectStatement();
    Class<? extends Model> target = r.getTarget();
    Map<String, String> joinParams = unwrapRelation(r, fields.get(0), clazz);
    String leftTable = joinParams.get("leftTable");
    String selectField = joinParams.get("selectField");
    String selectAs = joinParams.get("selectAs");
    String onLeft = joinParams.get("onLeft");
    String onRight = DatabaseBuilder.getTableName(target);
    /*
		 * After the steps above the left side of the join is always known. 
		 * What is currently unknown is, if there are any further sub-joins
		 * needed in order to accomplish the query. Therefore the right side
		 * of the join is provided with the result of this function. 
		 */
    JoinStatement join = new JoinStatement();
    join.left(leftTable, "table" + depth).right(buildJoin(target, fields.subList(1, fields.size()), rule, depth + 2), "table" + (depth + 1)).on(onLeft, onRight);
    /*
		 * The select will fetch the correct field from the previous join
		 * that will be needed in the next step. 
		 */
    select.from(join).distinct().select("table" + depth + "." + selectField + " AS " + selectAs);
    return select;
}
Also used : SelectStatement(com.orm.androrm.statement.SelectStatement) Relation(com.orm.androrm.field.Relation) JoinStatement(com.orm.androrm.statement.JoinStatement)

Aggregations

Relation (com.orm.androrm.field.Relation)2 SelectStatement (com.orm.androrm.statement.SelectStatement)2 ForeignKeyField (com.orm.androrm.field.ForeignKeyField)1 JoinStatement (com.orm.androrm.statement.JoinStatement)1