According to QueryBuilder documentation, we can define the order of logical operations using implicit and(). However, this implementation does not allow us to create complex conditions, such as (A and B) or (C and D). This comment provides a workaround, but having Realm-like chainable queries would be much nicer. Is changing existing QueryBuilder mechanics planned in future versions?
Yes, the query API subject to changes and this will be solved, of course. :-)
P.S.: if you don't have a very big amount of entities, a more convenient work around are query filters.
Hey! We recently started using this library and so far looks great, but this is a really good feature needed. Any update on this issue?
Copying from #533: A new query API is in testing internally, but currently on hold.
-ut
We really need this feature. Do we still nedd to wait for long?
We are currently spending most of our time on building sync. I can't say when the new query API will be released.
-Uwe
3.0.0-alpha1 has a preview of a new Query API that supports nested conditions. We welcome feedback!
https://docs.objectbox.io/#v-3-0-0-alpha1-2020-03-09
https://docs.objectbox.io/queries#new-query-api
An example for (A and B) or (C and D):
List<User> query = box.query(
User_.propA.equal(x).and(User_.propB.equal(y))
.or(User_.propC.equal(s).and(User_.propD.equal(t)))
).build().find();
Hi, I don't know if this is doable in Java or not, but I think it would be easier if we could type something like this:
List
user.propA.equal(x) && user.propB.equal(y) || user.propC.equal(s) && user.propD.equal(t)
).build().find();
It's very similar to what c# has into LinQ, but like I say, I don't know if it's doable in Java or in ObjectBox or how much effort would it take.
@Zpecter Thanks for this suggestion. If you are using the Kotlin infix functions for and and or it's pretty close to what you suggested:
val query = box.query(
user.propA.equal(x) and user.propB.equal(y) or user.propC.equal(s) and user.propD.equal(t)
).build().find()
Edit: I'm not aware how to create infix functions like this or to e.g. overload operators in Java. Open to pointers.
3.0.0-alpha2 adds additional Kotlin infix functions for creating conditions.
https://docs.objectbox.io/#v-3-0-0-alpha2-2020-03-24
E.g. using above example:
val query = box.query(
user.propA equal x and (user.propB equal y) or (user.propC equal s) and (user.propD equal t)
).build().find()
What about this condition?
WHERE
user.Id = _y
AND (
( user.Visible = true AND user.Type = Normal )
OR ( user.Visible = true AND user.Type <> Normal AND user.Status = CLOSE )
)
ORDER DESC
It's a little bit confusing the use of "and" and "or" and mixing them.
Any suggestion?
Question: Am I missing something or has querying on links not been ported over to the new API yet?
@Queatz It should work. Have an example where it doesn't?
@greenrobot I think I commented on the wrong issue...my question was specific to the new query builder and support for querying on links. I couldn't find anything on the PropertyQueryCondition about links.
@Queatz This should work: box.query().apply(conditions).link(relation).apply(conditions).
Hint: as noted in the Javadoc box.query(conditions) is just a shortcut for box.query().apply(conditions). Where box.query() returns a builder that still has the link(relation) method, like before.
Most helpful comment
3.0.0-alpha1has a preview of a new Query API that supports nested conditions. We welcome feedback!https://docs.objectbox.io/#v-3-0-0-alpha1-2020-03-09
https://docs.objectbox.io/queries#new-query-api
An example for
(A and B) or (C and D):