yahoo / squidb

SquiDB is a SQLite database library for Android and iOS
https://github.com/yahoo/squidb/wiki
Apache License 2.0
1.31k stars 132 forks source link

Using the Query.fork() function with the 'OR' operator #95

Closed dotWee closed 8 years ago

dotWee commented 8 years ago

I'm looking for a way to apply multiple criterions (from a List) to a single query, seperated by the 'OR' operator.

I tried this using the Query.fork() function and appending another criterion in a for-loop. But those criterions get seperated using the operator 'AND'.

Is there any simple way to achieve this, except compiling every query and appending them to plain SQL?

sbosley commented 8 years ago

Does Criterion.or meet your needs?

Criterion orConcatenatedCriterion = Criterion.or(criterion1, criterion2, ..., criterionN);
query.where(orConcatenatedCriterion);
dotWee commented 8 years ago

Yes! That was quick. Thanks for the fast answer!

sbosley commented 8 years ago

Great! Glad that worked, I wasn't sure if the varargs would be a feasible approach for you or if it HAD to be a List :) If you had needed to iterate through the list, you can also do the following:

Criterion orCriterion = criterionList.get(0);
for (int i = 1; i < criterionList.size(); i++) {
    orCriterion = orCriterion.or(criterionList.get(i));
}
query.where(orCriterion);

I think creating a conjunction criterion like this from a List rather than the varargs array Criterion.or and Criterion.and is a valid use case, so we can introduce functions to make this easier in a future release. Glad that the varargs version works for you though!