couchbase / couchbase-lite-android-ce

The community edition of couchbase lite for android
Apache License 2.0
9 stars 1 forks source link

Enhance query builder : create complex query #18

Closed seb-bisson closed 5 years ago

seb-bisson commented 5 years ago

We want to build a query given different parameters (with/without where expression, with/without orderby expression, with/without limit expression, ...).

Actually, it's not possible to build a complex query with builder. The only solution we found is to create a query for each situation (more than 10) wich is not optimal and very complex to maintain.

Example: today we handle where expression this way:

       Query query = null;
        Expression expression = getFilterExpression(filter);
        if (expression != null) {
            query = from.where(expression);
        } else {
            query = from;
        }

Now we want to add or not other conditions.

Do you have any clue to resolve our problems?

seb-bisson commented 5 years ago

It should be usefull to be close to builder pattern:

https://en.wikipedia.org/wiki/Builder_pattern

bmeike commented 5 years ago

It is true that you cannot build a complex expression using the QueryBuilder. You can, however, build a complex expression using the Expression builder, and then add it to the query. Is that not sufficient?

seb-bisson commented 5 years ago

The expression builder permit to add multiple fields/expression. And I'me using it this way.

As I'm writing a cordova plugin, i would like to allow adding sortbyexpression and/or a group by expression and/or a limitExpression as a plugin user could use it.

Does it make sense for you?

bmeike commented 5 years ago

Is it not the case that, were we to implement this as you describe, it would necessarily be possible to do this:

QueryBuilder
  .select("foo")
  .from("bar")
  .where(Expression.blahblah())
  .select("baz")
  .where(Expression.mumble())

We would have to catch this kind of misuse at runtime...

bmeike commented 5 years ago

Hey @seb-bisson : Do you have more thoughts on this? I'll close this, if you are good.