Search in sources :

Example 6 with JoinStatement

use of com.orm.androrm.statement.JoinStatement in project androrm by androrm.

the class ManyToManyField method getJoin.

private JoinStatement getJoin(String leftAlias, String rightAlias, int id) {
    JoinStatement join = new JoinStatement();
    join.left(DatabaseBuilder.getTableName(mTargetClass), leftAlias).right(getRightJoinSide(id), rightAlias).on(Model.PK, DatabaseBuilder.getTableName(mTargetClass));
    return join;
}
Also used : JoinStatement(com.orm.androrm.statement.JoinStatement)

Example 7 with JoinStatement

use of com.orm.androrm.statement.JoinStatement 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)

Example 8 with JoinStatement

use of com.orm.androrm.statement.JoinStatement in project androrm by androrm.

the class QuerySet method filter.

public QuerySet<T> filter(Filter filter) throws NoSuchFieldException {
    SelectStatement query = QueryBuilder.buildQuery(mClass, filter.getRules());
    if (mQuery == null) {
        mQuery = query;
    } else {
        JoinStatement join = new JoinStatement();
        join.left(mQuery, "left").right(query, "right").on(Model.PK, Model.PK);
        SelectStatement select = new SelectStatement();
        select.from(join);
        mQuery = select;
    }
    return this;
}
Also used : SelectStatement(com.orm.androrm.statement.SelectStatement) JoinStatement(com.orm.androrm.statement.JoinStatement)

Aggregations

JoinStatement (com.orm.androrm.statement.JoinStatement)8 SelectStatement (com.orm.androrm.statement.SelectStatement)5 Where (com.orm.androrm.Where)1 Relation (com.orm.androrm.field.Relation)1 Statement (com.orm.androrm.statement.Statement)1