From a2ac981fc2208a578096e14a8d86c2de9ef1ee56 Mon Sep 17 00:00:00 2001 From: "andrey.tarashevskiy" Date: Wed, 19 Oct 2016 19:52:52 +0300 Subject: [PATCH] Distinct reworked #2: selectDistinct replaced with withDistinct() --- src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt | 3 --- src/main/kotlin/org/jetbrains/exposed/sql/Query.kt | 7 ++++++- .../org/jetbrains/exposed/sql/tests/shared/DMLTests.kt | 6 +++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt b/src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt index 3b56817170..f11acdcfb0 100644 --- a/src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt +++ b/src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt @@ -9,9 +9,6 @@ import java.util.* inline fun FieldSet.select(where: SqlExpressionBuilder.()->Op) : Query = select(SqlExpressionBuilder.where()) -fun FieldSet.selectDistinct(where: (SqlExpressionBuilder.()->Op)? = null) : Query = - Query(TransactionManager.current(), this, where?.let { SqlExpressionBuilder.let(it)}).apply { distinct = true } - fun FieldSet.select(where: Op) : Query = Query(TransactionManager.current(), this, where) fun FieldSet.selectAll() : Query = Query(TransactionManager.current(), this, null) diff --git a/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt b/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt index 82f8ccce75..466decd621 100644 --- a/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt +++ b/src/main/kotlin/org/jetbrains/exposed/sql/Query.kt @@ -82,7 +82,7 @@ open class Query(val transaction: Transaction, val set: FieldSet, val where: Op< private var having: Op? = null; private var limit: Int? = null private var offset: Int = 0 - internal var distinct: Boolean = false + private var distinct: Boolean = false private var count: Boolean = false private var forUpdate: Boolean? = null @@ -160,6 +160,11 @@ open class Query(val transaction: Transaction, val set: FieldSet, val where: Op< return this } + fun withDistinct() : Query { + distinct = true + return this + } + fun groupBy(vararg columns: Expression<*>): Query { for (column in columns) { groupedByColumns.add(column) diff --git a/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DMLTests.kt b/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DMLTests.kt index 6fc4933312..c3e212d077 100644 --- a/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DMLTests.kt +++ b/src/test/kotlin/org/jetbrains/exposed/sql/tests/shared/DMLTests.kt @@ -721,9 +721,9 @@ class DMLTests() : DatabaseTestsBase() { tbl.insert { it[tbl.name] = "test" } assertEquals(2, tbl.selectAll().count()) - assertEquals(2, tbl.selectDistinct().count()) - assertEquals(1, tbl.slice(tbl.name).selectDistinct().count()) - assertEquals("test", tbl.slice(tbl.name).selectDistinct().single()[tbl.name]) + assertEquals(2, tbl.selectAll().withDistinct().count()) + assertEquals(1, tbl.slice(tbl.name).selectAll().withDistinct().count()) + assertEquals("test", tbl.slice(tbl.name).selectAll().withDistinct().single()[tbl.name]) } }