Search in sources :

Example 1 with JoinStatement

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

the class QueryBuilder method buildQuery.

private static final <T extends Model> SelectStatement buildQuery(Class<T> clazz, List<Rule> rules, int depth) {
    String tableName = DatabaseBuilder.getTableName(clazz);
    JoinStatement selfJoin = new JoinStatement();
    selfJoin.left(tableName, "self" + depth);
    SelectStatement subSelect = new SelectStatement();
    Rule rule = rules.get(0);
    List<String> fields = Arrays.asList(rule.getKey().split("__"));
    if (fields.size() == 1) {
        String fieldName = fields.get(0);
        T instance = Model.getInstace(clazz);
        if (instance != null) {
            Object o = getFieldInstance(clazz, instance, fieldName);
            if (DatabaseBuilder.isRelationalField(o)) {
                // gather ids for fields
                SelectStatement s = buildJoin(clazz, fields, rule, depth);
                JoinStatement join = new JoinStatement();
                join.left(tableName, "a").right(s, "b").on(Model.PK, tableName);
                subSelect.from(join).select("a.*");
            } else {
                Where where = new Where();
                where.setStatement(rule.getStatement());
                subSelect.from(tableName).where(where);
            }
        }
    } else {
        int left = depth + 1;
        int right = depth + 2;
        JoinStatement join = new JoinStatement();
        join.left(tableName, "outer" + left).right(buildJoin(clazz, fields, rule, depth), "outer" + right).on(Model.PK, tableName);
        subSelect.from(join).select("outer" + left + ".*");
    }
    if (rules.size() == 1) {
        return subSelect;
    }
    selfJoin.right(subSelect, "self" + (depth + 1)).on(Model.PK, Model.PK);
    JoinStatement outerSelfJoin = new JoinStatement();
    outerSelfJoin.left(subSelect, "outerSelf" + depth).right(buildQuery(clazz, rules.subList(1, rules.size()), (depth + 2)), "outerSelf" + (depth + 1)).on(Model.PK, Model.PK);
    SelectStatement select = new SelectStatement();
    select.from(outerSelfJoin).select("outerSelf" + depth + ".*");
    return select;
}
Also used : SelectStatement(com.orm.androrm.statement.SelectStatement) JoinStatement(com.orm.androrm.statement.JoinStatement)

Example 2 with JoinStatement

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

the class JoinStatementTest method testOnSubselect.

public void testOnSubselect() {
    SelectStatement left = new SelectStatement();
    left.from("left_table").select("foo");
    SelectStatement right = new SelectStatement();
    right.from("right_table").select("bar");
    JoinStatement join = new JoinStatement();
    join.left(left, "a").right(right, "b").on("foo", "bar");
    assertEquals("(SELECT foo FROM `left_table`) AS a JOIN (SELECT bar FROM `right_table`) AS b ON a.foo=b.bar", join.toString());
}
Also used : SelectStatement(com.orm.androrm.statement.SelectStatement) JoinStatement(com.orm.androrm.statement.JoinStatement)

Example 3 with JoinStatement

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

the class JoinStatementTest method testPlainTable.

public void testPlainTable() {
    JoinStatement join = new JoinStatement();
    join.left("left_table", "a").right("right_table", "b").on("field1", "field2");
    assertEquals("(SELECT * FROM `left_table`) AS a JOIN (SELECT * FROM `right_table`) AS b ON a.field1=b.field2", join.toString());
}
Also used : JoinStatement(com.orm.androrm.statement.JoinStatement)

Example 4 with JoinStatement

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

the class SelectStatementTest method testFromJoin.

public void testFromJoin() {
    JoinStatement join = new JoinStatement();
    join.left("left_table", "a").right("right_table", "b").on("field1", "field2");
    mSelect.from(join);
    assertEquals("SELECT * FROM (SELECT * FROM `left_table`) AS a JOIN (SELECT * FROM `right_table`) AS b ON a.field1=b.field2", mSelect.toString());
}
Also used : JoinStatement(com.orm.androrm.statement.JoinStatement)

Example 5 with JoinStatement

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

the class ManyToManyField method getRightJoinSide.

private SelectStatement getRightJoinSide(int id) {
    String leftTable = DatabaseBuilder.getTableName(mOriginClass);
    String rightTable = DatabaseBuilder.getTableName(mTargetClass);
    Where where = new Where();
    where.setStatement(new Statement(leftTable, id));
    SelectStatement relation = new SelectStatement();
    relation.from(mTableName).select(leftTable, rightTable).where(where);
    JoinStatement join = new JoinStatement();
    join.left(relation, "left").right(rightTable, "right").on(rightTable, Model.PK);
    SelectStatement select = new SelectStatement();
    select.from(join).select("left." + rightTable + " AS " + rightTable);
    return select;
}
Also used : SelectStatement(com.orm.androrm.statement.SelectStatement) JoinStatement(com.orm.androrm.statement.JoinStatement) SelectStatement(com.orm.androrm.statement.SelectStatement) Statement(com.orm.androrm.statement.Statement) Where(com.orm.androrm.Where) 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