Search in sources :

Example 1 with Condition

use of com.raizlabs.android.dbflow.sql.language.Condition in project pictureapp by EyeSeeTea.

the class Question method findRootQuestion.

/**
     * Find the first root question in the given tab
     *
     * This cannot be done due to a dbflow join bug
     * select q.*
     * from question q
     * left join header h on q.id_header=h.id_header
     * left join questionrelation qr on q.id_question=qr.id_question
     * where h.id_tab=1 and qr.id_question is null
     * order by q.order_pos
     */
public static Question findRootQuestion(Tab tab) {
    //Take every child question
    List<QuestionRelation> questionRelations = QuestionRelation.listAllParentChildRelations();
    if (questionRelations == null || questionRelations.size() == 0) {
        //flow without relations
        return new Select().from(Question.class).as(AppDatabase.questionName).join(Header.class, Join.JoinType.LEFT_OUTER).as(headerName).on(Question_Table.id_header_fk.withTable(questionAlias).eq(Header_Table.id_header.withTable(headerAlias))).join(Tab.class, Join.JoinType.LEFT_OUTER).as(tabName).on(Header_Table.id_tab_fk.withTable(headerAlias).eq(Tab_Table.id_tab.withTable(tabAlias))).where(Header_Table.id_tab_fk.withTable(headerAlias).eq(tab.getId_tab())).and(Tab_Table.type.withTable(tabAlias).eq(Constants.TAB_MULTI_QUESTION)).orderBy(Question_Table.order_pos.withTable(questionAlias), true).querySingle();
    }
    //Build a not in condition
    Iterator<QuestionRelation> questionRelationsIterator = questionRelations.iterator();
    Condition.In in = Question_Table.id_question.withTable(questionAlias).notIn(questionRelationsIterator.next().getQuestion().getId_question());
    while (questionRelationsIterator.hasNext()) {
        in.and(Long.toString(questionRelationsIterator.next().getQuestion().getId_question()));
    }
    return new Select().from(Question.class).as(questionName).join(Header.class, Join.JoinType.LEFT_OUTER).as(headerName).on(Question_Table.id_header_fk.withTable(questionAlias).eq(Header_Table.id_header.withTable(headerAlias))).where(Header_Table.id_tab_fk.withTable(headerAlias).eq(tab.getId_tab())).and(in).orderBy(Question_Table.order_pos, true).querySingle();
}
Also used : Condition(com.raizlabs.android.dbflow.sql.language.Condition) Select(com.raizlabs.android.dbflow.sql.language.Select)

Example 2 with Condition

use of com.raizlabs.android.dbflow.sql.language.Condition in project pictureapp by EyeSeeTea.

the class Question method getSiblingNoParent.

/**
     * Returns next question from same header considering the order.
     * This should not be a child question.
     */
private Question getSiblingNoParent() {
    //Take every child question
    List<QuestionRelation> questionRelations = QuestionRelation.listAllParentChildRelations();
    //Build a not in condition
    Condition.In in;
    if (questionRelations.size() == 0) {
        //Flow without children
        in = Question_Table.id_question.withTable(questionAlias).notIn(this.getId_question());
    } else {
        Iterator<QuestionRelation> questionRelationsIterator = questionRelations.iterator();
        in = Question_Table.id_question.withTable(questionAlias).notIn(questionRelationsIterator.next().getQuestion().getId_question());
        while (questionRelationsIterator.hasNext()) {
            in.and(Long.toString(questionRelationsIterator.next().getQuestion().getId_question()));
        }
    }
    //Siblings without parents relations
    this.sibling = new Select().from(Question.class).as(questionName).where(Question_Table.order_pos.withTable(questionAlias).greaterThan(this.getOrder_pos())).and(in).orderBy(OrderBy.fromProperty(Question_Table.order_pos).ascending()).querySingle();
    //no question behind this one -> build a null question to use cached value
    if (this.sibling == null) {
        this.sibling = buildNullQuestion();
    }
    return this.sibling;
}
Also used : Condition(com.raizlabs.android.dbflow.sql.language.Condition) Select(com.raizlabs.android.dbflow.sql.language.Select)

Aggregations

Condition (com.raizlabs.android.dbflow.sql.language.Condition)2 Select (com.raizlabs.android.dbflow.sql.language.Select)2