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 · 16 comments
Open

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

gartesk opened this issue Oct 6, 2017 · 16 comments
Assignees
Labels
feature
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
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
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
Copy link
Member

@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 the feature label Feb 25, 2019
@greenrobot-team greenrobot-team added this to the 2.4.0 milestone Feb 25, 2019
@jrcacd
Copy link

@jrcacd jrcacd commented May 12, 2019

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

@greenrobot-team
Copy link
Member

@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
Copy link
Member

@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
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
Copy link
Member

@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
Copy link
Member

@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()

@IdeasMX01
Copy link

@IdeasMX01 IdeasMX01 commented Oct 29, 2020

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?

@Queatz
Copy link

@Queatz Queatz commented Nov 22, 2020

Question: Am I missing something or has querying on links not been ported over to the new API yet?

@greenrobot-team
Copy link
Member

@greenrobot-team greenrobot-team commented Nov 23, 2020

@Queatz It should work. Have an example where it doesn't?

@Queatz
Copy link

@Queatz Queatz commented Nov 23, 2020

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

@greenrobot-team
Copy link
Member

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

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

@greenrobot-team
Copy link
Member

@greenrobot-team greenrobot-team commented Oct 18, 2021

Note: This is also available with 2.9.2-RC4 which has more recent changes than the last 3.0 alpha release.

@greenrobot-team greenrobot-team removed this from the 3.0 milestone Oct 18, 2021
@greenrobot-team greenrobot-team added this to the 2.9.2 milestone Oct 18, 2021
@greenrobot-team greenrobot-team removed this from the 2.9.2 milestone Oct 19, 2021
@greenrobot-team greenrobot-team added this to the 3.0 milestone Oct 19, 2021
@greenrobot-team
Copy link
Member

@greenrobot-team greenrobot-team commented Oct 19, 2021

This is now available with the 3.0.1 release. We welcome additional feedback (or create an issue if you found a bug).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature
Projects
None yet
Development

No branches or pull requests

8 participants