Skip to content

Commit

Permalink
Distinct reworked #2: selectDistinct replaced with withDistinct()
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Oct 19, 2016
1 parent 9206d19 commit a2ac981
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 0 additions & 3 deletions src/main/kotlin/org/jetbrains/exposed/sql/Queries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import java.util.*

inline fun FieldSet.select(where: SqlExpressionBuilder.()->Op<Boolean>) : Query = select(SqlExpressionBuilder.where())

fun FieldSet.selectDistinct(where: (SqlExpressionBuilder.()->Op<Boolean>)? = null) : Query =
Query(TransactionManager.current(), this, where?.let { SqlExpressionBuilder.let(it)}).apply { distinct = true }

fun FieldSet.select(where: Op<Boolean>) : Query = Query(TransactionManager.current(), this, where)

fun FieldSet.selectAll() : Query = Query(TransactionManager.current(), this, null)
Expand Down
7 changes: 6 additions & 1 deletion src/main/kotlin/org/jetbrains/exposed/sql/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ open class Query(val transaction: Transaction, val set: FieldSet, val where: Op<
private var having: Op<Boolean>? = 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

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand Down

0 comments on commit a2ac981

Please sign in to comment.