Skip to content

Commit

Permalink
Refactor: EXPOSED-396 fetchBatchedResults logic inside override itera…
Browse files Browse the repository at this point in the history
…tor method
  • Loading branch information
roharon committed Jun 2, 2024
1 parent 5df30f1 commit 56d2dff
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -264,24 +264,21 @@ open class Query(override var set: FieldSet, where: Op<Boolean>?) : AbstractQuer
limit = batchSize
(orderByExpressions as MutableList).add(autoIncColumn to sortOrder)
val whereOp = where ?: Op.TRUE
val fetchInAscendingOrder = sortOrder in listOf(SortOrder.ASC, SortOrder.ASC_NULLS_FIRST, SortOrder.ASC_NULLS_LAST)

return object : Iterable<Iterable<ResultRow>> {
override fun iterator(): Iterator<Iterable<ResultRow>> {
return iterator {
var lastOffset: Long? = null
var lastOffset = if (fetchInAscendingOrder) null else 0L
while (true) {
val query = this@Query.copy().adjustWhere {
return@adjustWhere lastOffset.let {
if (it == null) return@let whereOp

return@let if (listOf(SortOrder.ASC, SortOrder.ASC_NULLS_FIRST, SortOrder.ASC_NULLS_LAST)
.contains(sortOrder)
) {
whereOp and (autoIncColumn greater it)
lastOffset?.let {
whereOp and if (fetchInAscendingOrder) {
(autoIncColumn greater it)
} else {
whereOp and (autoIncColumn less it)
(autoIncColumn less it)
}
}
} ?: whereOp
}

val results = query.iterator().asSequence().toList()
Expand Down

0 comments on commit 56d2dff

Please sign in to comment.