Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating queries with complex 'and' and 'or' combinations #201

Open
gartesk opened this issue Oct 6, 2017 · 9 comments
Open

Creating queries with complex 'and' and 'or' combinations #201

gartesk opened this issue Oct 6, 2017 · 9 comments
Assignees
Labels
Milestone

Comments

@gartesk
Copy link

@gartesk gartesk commented Oct 6, 2017

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?

@greenrobot

This comment has been minimized.

Copy link
Member

@greenrobot greenrobot commented Oct 7, 2017

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.

@chiara-jm

This comment has been minimized.

Copy link

@chiara-jm chiara-jm commented Jan 17, 2018

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?

@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

@greenrobot-team greenrobot-team commented Feb 25, 2019

Copying from #533: A new query API is in testing internally, but currently on hold.
-ut

@greenrobot-team greenrobot-team added this to the 2.4.0 milestone Feb 25, 2019
@jrcacd

This comment has been minimized.

Copy link

@jrcacd jrcacd commented May 12, 2019

We really need this feature. Do we still nedd to wait for long?

@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

@greenrobot-team greenrobot-team commented May 14, 2019

We are currently spending most of our time on building sync. I can't say when the new query API will be released.
-Uwe

@greenrobot greenrobot removed this from the 2.4.0 milestone Oct 3, 2019
@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

@greenrobot-team greenrobot-team commented Mar 9, 2020

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();
@greenrobot-team greenrobot-team added this to the 3.0 milestone Mar 9, 2020
@greenrobot-team greenrobot-team self-assigned this Mar 9, 2020
@Zpecter

This comment has been minimized.

Copy link

@Zpecter Zpecter commented Mar 9, 2020

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 query = box.query(user ->
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.

@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

@greenrobot-team greenrobot-team commented Mar 9, 2020

@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.

@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

@greenrobot-team greenrobot-team commented Mar 24, 2020

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
6 participants
You can’t perform that action at this time.