Search code examples
androidgreendao

Why GreenDAO doesn't support LIKE operator completely?


GreenDAO just supported one position of LIKE operator. It was " LIKE ?"

I wanna fetch records with variety of this operator. such as, " LIKE %?", " LIKE ?%" and " LIKE %?%". but it doesn't supported by GreenDAO.

Also I've used queryRaw() and queryRawCreate(), unfortunately it didn't work, too. For example:

libDocSeriesDao.queryRawCreate( " Where T.Title Like '%?%' Or T.ViewTitle Like '%?%'", aKeyword, aKeyword).listLazy();

Any help would be greatly appreciated.


Solution

  • The '%' character must not be part of the query string when you use the '?' character. You can use any combination of % when binding the parameter.

    Here is an example how to use LIKE queries (taken from greenDAO's unit tests at https://github.com/greenrobot/greenDAO/commit/788313904fa58a0c8628f6b2e016a4a385f344c6):

    Query<TestEntity> query = dao.queryBuilder().where(Properties.SimpleString.like("%robot")).build();
    TestEntity entity2 = query.uniqueOrThrow();
    assertEquals(entity.getId(), entity2.getId());
    
    query.setParameter(0, "green%");
    entity2 = query.uniqueOrThrow();
    assertEquals(entity.getId(), entity2.getId());
    
    query.setParameter(0, "%enrob%");
    entity2 = query.uniqueOrThrow();
    assertEquals(entity.getId(), entity2.getId());
    

    The same principle is valid for raw queries. For your example, you should do this:

    libDocSeriesDao.queryRawCreate(" Where T.Title Like ? Or T.ViewTitle Like ?", aKeyword, aKeyword).listLazy();
    

    Also, aKeyword must have the % character.