diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt index 83d459c2e5..c3d33f43db 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt @@ -8,7 +8,6 @@ import org.jetbrains.exposed.sql.statements.api.PreparedStatementApi import org.jetbrains.exposed.sql.vendors.ForUpdateOption import org.jetbrains.exposed.sql.vendors.currentDialect import java.sql.ResultSet -import java.util.* enum class SortOrder(val code: String) { ASC(code = "ASC"), @@ -39,14 +38,15 @@ open class Query(override var set: FieldSet, where: Op?) : AbstractQuer var where: Op? = where private set - override val queryToExecute: Statement get() { - val distinctExpressions = set.fields.distinct() - return if (distinctExpressions.size < set.fields.size) { - copy().adjustSelect { select(distinctExpressions) } - } else { - this + override val queryToExecute: Statement + get() { + val distinctExpressions = set.fields.distinct() + return if (distinctExpressions.size < set.fields.size) { + copy().adjustSelect { select(distinctExpressions) } + } else { + this + } } - } /** Creates a new [Query] instance using all stored properties of this `SELECT` query. */ override fun copy(): Query = Query(set, where).also { copy -> @@ -272,11 +272,29 @@ open class Query(override var set: FieldSet, where: Op?) : AbstractQuer var lastOffset = if (fetchInAscendingOrder) 0L else null while (true) { val query = this@Query.copy().adjustWhere { - lastOffset?.let { + lastOffset?.let { lastOffset -> whereOp and if (fetchInAscendingOrder) { - (autoIncColumn greater it) + when (autoIncColumn.columnType) { + is EntityIDColumnType<*> -> { + (autoIncColumn as? Column>)?.let { + (it greater lastOffset) + } ?: (autoIncColumn as? Column>)?.let { + (it greater lastOffset.toInt()) + } ?: (autoIncColumn greater lastOffset) + } + else -> (autoIncColumn greater lastOffset) + } } else { - (autoIncColumn less it) + when (autoIncColumn.columnType) { + is EntityIDColumnType<*> -> { + (autoIncColumn as? Column>)?.let { + (it less lastOffset) + } ?: (autoIncColumn as? Column>)?.let { + (it less lastOffset.toInt()) + } ?: (autoIncColumn less lastOffset) + } + else -> (autoIncColumn less lastOffset) + } } } ?: whereOp } diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/FetchBatchedResultsTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/FetchBatchedResultsTests.kt index bad04bf06d..5937e6bd97 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/FetchBatchedResultsTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/FetchBatchedResultsTests.kt @@ -1,5 +1,6 @@ package org.jetbrains.exposed.sql.tests.shared.dml +import org.jetbrains.exposed.dao.id.IntIdTable import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.tests.DatabaseTestsBase import org.jetbrains.exposed.sql.tests.shared.assertEqualLists @@ -100,4 +101,20 @@ class FetchBatchedResultsTests : DatabaseTestsBase() { fun `when batch size is 0 or less, should throw an exception`() { DMLTestsData.Cities.selectAll().fetchBatchedResults(batchSize = -1) } + + @Test + fun testFetchBatchedResultsWithAutoIncrementEntityId() { + val tester1 = object : IntIdTable("table_1") { + val data = varchar("data", 100) + } + + val tester2 = object : IntIdTable("table_2") { + val moreData = varchar("more_data", 100) + val prevData = reference("prev_data", tester1, onUpdate = ReferenceOption.CASCADE) + } + + withTables(tester1, tester2) { + (tester2 innerJoin tester1).selectAll().fetchBatchedResults(10_000).flatten() + } + } }