Search code examples
androidsugarorm

Android: SugarORM Or condition


I am using SugarORM.

The selection code:

String action = "date_int ASC";

record_list = Select.from(Records_records.class).where(Condition.prop("category").eq("A")).orderBy(action).list(); 
record_list = Select.from(Records_records.class).where(Condition.prop("category").eq("B")).orderBy(action).list();

Question:

The list obtained above is either category being A only or B only.

From the official website it can only generate AND condition for A and B as follows:

record_list = Select.from(Records_records.class).where(Condition.prop("category").eq("A"), Condition.prop("category").eq("B")).orderBy(action).list();

How could I get a list a combined list for category with (A or B) and in the order of "date_int ASC"?

Thanks!


Solution

  • You can use whereOr() for this:

    record_list = Select.from(Records_records.class).whereOr(Condition.prop("category").eq("A"), Condition.prop("category").eq("B")).orderBy(action).list();
    

    It is the OR counterpart to the default AND that where() uses.

    It is not documented, but you can find it in the source:

    @Test
    public void testWhereOr(){
        Select where = Select.from(TestRecord.class).whereOr(Condition.prop("test").eq("satya"));
        assertEquals("(test = ? )", where.getWhereCond());
        assertEquals(1, where.getArgs().length);
        assertEquals("satya", where.getArgs()[0]);
    
        where = Select.from(TestRecord.class).whereOr(Condition.prop("test").eq("satya"), Condition.prop("prop").eq(2));
        assertEquals("(test = ?  OR prop = ? )", where.getWhereCond());
        assertEquals(2, where.getArgs().length);
        assertEquals("satya", where.getArgs()[0]);
        assertEquals("2", where.getArgs()[1]);
    }