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 db65e20416..69f161259b 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 @@ -429,7 +429,7 @@ open class Query(override var set: FieldSet, where: Op?) : AbstractQuer * @sample org.jetbrains.exposed.sql.tests.shared.dml.InsertSelectTests.testInsertSelect02 */ override fun count(): Long { - return if (distinct || groupedByColumns.isNotEmpty() || limit != null) { + return if (distinct || groupedByColumns.isNotEmpty() || limit != null || offset > 0) { fun Column<*>.makeAlias() = alias(transaction.db.identifierManager.quoteIfNecessary("${table.tableNameWithoutSchemeSanitized}_$name")) diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/CountTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/CountTests.kt index 0a4b0c2472..8292f8f509 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/CountTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/CountTests.kt @@ -1,7 +1,9 @@ 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.TestDB import org.jetbrains.exposed.sql.tests.currentDialectTest import org.jetbrains.exposed.sql.tests.shared.* import org.jetbrains.exposed.sql.vendors.SQLServerDialect @@ -66,4 +68,26 @@ class CountTests : DatabaseTestsBase() { } } } + + @Test + fun testCountWithOffsetWithoutLimit() { + val tester = object : IntIdTable("users") { + val value = integer("value") + } + + // SQLite, MariaDB, Mysql do not support OFFSET clause without LIMIT + withTables(excludeSettings = TestDB.ALL_MYSQL_MARIADB + TestDB.SQLITE, tester) { + tester.batchInsert(listOf(1, 2, 3, 4, 5)) { + this[tester.value] = it + } + + assertEquals(5, tester.selectAll().count()) + + assertEquals(2, tester.selectAll().offset(1).limit(2).count()) + + assertEquals(2, tester.selectAll().limit(2).count()) + + assertEquals(3, tester.selectAll().offset(2).count()) + } + } } diff --git a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/DistinctOnTests.kt b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/DistinctOnTests.kt index 0b940b3bbd..b049eee695 100644 --- a/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/DistinctOnTests.kt +++ b/exposed-tests/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/dml/DistinctOnTests.kt @@ -87,7 +87,6 @@ class DistinctOnTests : DatabaseTestsBase() { } withTables(excludeSettings = TestDB.ALL - distinctOnSupportedDb, tester) { - addLogger(StdOutSqlLogger) // Empty list of columns should not cause exception tester.insert { it[value] = 1