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

Compound operations #469

Closed
Edvinas01 opened this issue Jan 18, 2019 · 4 comments
Closed

Compound operations #469

Edvinas01 opened this issue Jan 18, 2019 · 4 comments

Comments

@Edvinas01
Copy link
Contributor

I have a list of operations (they get generated dynamically based on user input and table columns) which need to be joined into a single operation using OR operator:

val ops : List<Op<Boolean>> = ...

In order to do so, I've created a helper utility:

abstract class CompoundOp<T>(
    private val exp: List<Expression<T>>,
    private val op: String
) : Op<T>() {

    override fun toSQL(queryBuilder: QueryBuilder) = exp
        .joinToString(separator = op) { exp ->
            "(${exp.toSQL(queryBuilder)})"
        }
}

class CompoundOrOp<T>(exp: List<Expression<T>>) : CompoundOp<T>(exp, " OR ")

then I can use it in the following way:

val ops : List<Op<Boolean>> = ...

val op: Op<Boolean> = CompoundOrOp(ops)

which results in:

(COLUMN_A = TRUE) OR (COLUMN_B = FALSE) OR (COLUMN_C = 'foo')

However I think I'm missing something. Is there a way to achieve this with current functions and classes present in the library? If not, could this be added as a new feature?

@Tapac
Copy link
Contributor

Tapac commented Jan 18, 2019

No, Exposed doesn't have such operations and I don't face a need of them in my practice, could you share use-cases where such compounds could be useful?

Also, you can replace a code above with simple:

fun List<Op<Boolean>>.compoundOr() = reduce { op, nextOp -> op or nextOp }

@Edvinas01
Copy link
Contributor Author

My use case is that I have quite a lot of "data tables" which depend on flat data structures (that is my entities do not contain joins).

In order to reduce the need of manually having to type out all specific filtering queries, I've created an utility class which takes dependsOnColumns and creates the filter query dynamically. The result of this is List<Op<Boolean>> which I need to pass to searchQuery method.

Previously I was using Spring Data, it had something similar where you can pass an array of Predicate to QueryBuilder.or. That is why I'm looking for similar functionality in Exposed.

@Edvinas01
Copy link
Contributor Author

Also the reduce operation which you've shown doesn't quite cut it, it will create such result:

(exp OR (exp OR))

while I'm looking for:

exp OR exp OR exp

@Tapac
Copy link
Contributor

Tapac commented Jan 30, 2019

Fixed in master: added compondAnd/compoundOr functions and optimized or operator to generate (exp) or (exp) or (exp)

@Tapac Tapac closed this as completed Jan 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants