+
Skip to content

don't cause stackoverflow in whereWithConditions/whereWithOrConditions #328

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions ktorm-core/src/main/kotlin/org/ktorm/dsl/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,18 @@ public inline fun Query.where(condition: () -> ColumnDeclaring<Boolean>): Query
* Note that if we don't add any conditions to the list, the `where` clause would not be set.
*/
public inline fun Query.whereWithConditions(block: (MutableList<ColumnDeclaring<Boolean>>) -> Unit): Query {
val conditions = ArrayList<ColumnDeclaring<Boolean>>().apply(block)
var conditions: List<ColumnDeclaring<Boolean>> = ArrayList<ColumnDeclaring<Boolean>>().apply(block)

if (conditions.isEmpty()) {
return this
} else {
return this.where { conditions.reduce { a, b -> a and b } }
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] }
}
}

Expand All @@ -237,12 +243,18 @@ public inline fun Query.whereWithConditions(block: (MutableList<ColumnDeclaring<
* Note that if we don't add any conditions to the list, the `where` clause would not be set.
*/
public inline fun Query.whereWithOrConditions(block: (MutableList<ColumnDeclaring<Boolean>>) -> Unit): Query {
val conditions = ArrayList<ColumnDeclaring<Boolean>>().apply(block)
var conditions: List<ColumnDeclaring<Boolean>> = ArrayList<ColumnDeclaring<Boolean>>().apply(block)

if (conditions.isEmpty()) {
return this
} else {
return this.where { conditions.reduce { a, b -> a or b } }
while (conditions.size > 1) {
conditions = conditions.chunked(2) { chunk ->
if (chunk.size == 2) chunk[0] or chunk[1] else chunk[0]
}
}

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

Expand Down
21 changes: 20 additions & 1 deletion ktorm-core/src/test/kotlin/org/ktorm/dsl/QueryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import java.util.concurrent.ExecutionException
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeoutException
import kotlin.random.Random

/**
* Created by vince on Dec 07, 2018.
Expand Down Expand Up @@ -69,6 +70,24 @@ class QueryTest : BaseTest() {
assert(name == "vince")
}

@Test
fun testWhereWithOrConditionsNoStackOverflow() {
val t = Employees.aliased("t")

val sql = database
.from(t)
.select(t.name)
.whereWithOrConditions { where ->
repeat(100_000) {
where += (t.id eq Random.nextInt()) and (t.departmentId eq Random.nextInt())
}
}
.sql

// very large SQL doesn't cause stackoverflow
assert(true)
}

@Test
fun testCombineConditions() {
val t = Employees.aliased("t")
Expand Down Expand Up @@ -300,4 +319,4 @@ class QueryTest : BaseTest() {
assert(names[0] == "0:vince")
assert(names[1] == "1:marry")
}
}
}
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载