Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@ecopoesis
Copy link
Contributor

@ecopoesis ecopoesis commented Sep 10, 2021

Changes whereWithConditions and whereWithOrConditions so they don't cause stack overflows.

This is done by introducing a new expression type: VarargsExpression. VarargsExpression wraps a collection of ColumnDeclaring<Boolean>. On output it wraps each ColumnDeclaring<Boolean> in parenthesis and joins them together with the operator. For code like a list of (t.id eq 1) and (t.departmentId eq 100), the resulting SQL looks like:

((("t"."id" = ?) AND ("t"."department_id" = ?)) OR (("t"."id" = ?) AND ("t"."department_id" = ?)) OR (("t"."id" = ?) AND ("t"."department_id" = ?)))

This SQL is different than what was built before. Previously whereWithConditions/whereWithOrConditions treated and and or as purely binary operators, nesting subsequent expressions in increasingly deep parenthesis. SQL allows us to skip the nesting which this takes advantage of.

Fixes #327

@vincentlauvlwj
Copy link
Member

Actually we don't need another expression type, simply refactoring the whereWithConditions function can solve your problem:

public inline fun Query.whereWithConditions(block: (MutableList<ColumnDeclaring<Boolean>>) -> Unit): Query {
    var conditions: List<ColumnDeclaring<Boolean>> = ArrayList<ColumnDeclaring<Boolean>>().apply(block)

    if (conditions.isEmpty()) {
        return this
    } else {
        while (conditions.size > 1) {
            conditions = conditions.chunked(2) { chunk ->
                if (chunk.size == 2) chunk[0] and chunk[1] else chunk[0]
            }
        }

        return this.where { conditions[0] }
    }
}

@ecopoesis
Copy link
Contributor Author

I've updated the PR with your version.

@vincentlauvlwj vincentlauvlwj changed the base branch from master to v3.5.x September 11, 2021 15:35
@vincentlauvlwj vincentlauvlwj merged commit 7d00f87 into kotlin-orm:v3.5.x Sep 11, 2021
@ecopoesis ecopoesis deleted the feature/where-with-conditions-stack-overflow branch July 12, 2022 21:07
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

Successfully merging this pull request may close these issues.

StackOverflowError when using too many whereWithOrConditions

2 participants