objectbox / objectbox-java

Android Database - first and fast, lightweight on-device vector database

Home Page:https://objectbox.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

It's posible to build a query like `A and ((B and C) or (D and E) or (F and G))` with QueryBuilder?

how8570 opened this issue · comments

I saw the #123, #533, #201.
I got the answer of (A and B) or (C and D) on them.

I try to query with such condiction A and ((B and C) or (D and E) or (F and G)) with QueryBuilder,
and write the following code:

val targetList = someBoxStore.query {
    equal(Table_.A, true)

    equal(Table_.B, true)
    and()
    equal(Table_.C, true)

    or()

    equal(Table_.D, true)
    and()
    equal(Table_.E, true)
    
    or()

    equal(Table_.F, true)
    and()
    equal(Table_.G, true)
}.find()

and the result seem not correctly. 🤔

It posible make a query of that condition combination?

To nest conditions (e.g. wrap them in parentheses) pass them as arguments to and(conditions) or or(conditions). See https://docs.objectbox.io/queries.

Sure that I knew there is a QueryCondition<T>#or(QueryConditions<T>) method, but it cannot put in QueryBuilder#query{} block.

Since it diffrent style to build queries(?), and QueryBuilder I prefer so I haven't try it.
Maybe I am looking for a builder style to do this?

I will try the method chains style if it may solve my prob.

Yeah the following code make the query work as expected,

val targetList = someBox.query(
    someBox_.A.equal(true)
        .and(
            someBox_.B.equal(treu)
            .and(someBox_.C.equal(true))
            .or(
                someBox_.D.equal(true)
                .and(someBox_.E.equal(true))
            ).or(
                someBox_.F.equal(true)
                .and(someBox_.equal(true))
            )
        )
).find()

but it kinda gross 😢, and cannot fit in the query{ } style.

This is the way to do it. The query { } Kotlin helper function is part of the legacy query API, I would not use it in new code.