I need to verify some conditions to create a complete query:
QueryBuilder qb = getMyObjDao().queryBuilder();
if ( someCondition )
qb.where(MyObjDao.Properties.Prop1.eq(someValue));
else
qb.whereOr(MyObjDao.Properties.Prop2.eq(someValue),MyObjDao.Properties.Prop2.eq(someValue));if ( someOtherCondition )
qb.where(MyObjDao.Properties.Prop3.eq(someValue));
else
qb.whereOr(MyObjDao.Properties.Prop4.eq(someValue));
So is it possible to concatenate query builder conditions and create query builder dynamically? or anything so:
(a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') and
|(b = '%'+condition2 or b = '%'+condition2+'%' or b = condition2 + '%') and ....
How make that in greenDao?
QueryBuilder.and()
andQueryBuilder.or()
are used to combineWhereCondition
s. The resultingWhereCondition
s have to be used insideQueryBuilder.where()
(which will combine the conditions usingAND
) orQueryBuilder.whereOr()
.Please notice, that all your queries don't make much sense. As consequence the code I give you may not work as you expected, as I am just guessing what you expect. as example you can take
qb.whereOr(MyObjDao.Properties.Prop2.eq(someValue),MyObjDao.Properties.Prop2.eq(someValue))
This translates to
where (Prop2 = someValue OR Prop2 = someValue)
or maybe towhere (Prop2 = 'someValue' OR Prop2 = 'someValue')
. In each of them the OR and the second statement are obsolete.Another example is
(a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%') and (b = '%'+condition2 or b = '%'+condition2+'%' or b = condition2 + '%') and ....
If you are not searching for strings with%
in it, the query with a where-clause like this is going to return none or false results.For:
You can use something like this:
UPDATE
Of course you can use another approach without dynamic list as well:
For:
You can use this:
As you see there are a lot of possiblities how you can combine WhereConditions at runtime using greendao. But as long as you don't give a detailed example and description of what you really want to do, nobody can help you.
By the way:
where()
orwhereOr()
doesn't make any difference, if you are only using one WhereCondition.(a like '%'+condition1 or a like '%'+condition1+'%' or a like condition1 + '%')
instead of(a = '%'+condition1 or a = '%'+condition1+'%' or a = condition1 + '%')
.(a like '%'+condition1 or a like '%'+condition1+'%' or a like condition1 + '%')
is equal to(a like '%'+condition1+'%')
since every result that matchesa like '%'+condition1
ora like condition1+'%'
will also matcha like '%'+condition1+'%'
.