Skip to content

Commit

Permalink
#476 No way to ORDER BY in DAO
Browse files Browse the repository at this point in the history
  • Loading branch information
Tapac committed Jan 31, 2019
1 parent 6b61977 commit 31cbebc
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
8 changes: 1 addition & 7 deletions src/main/kotlin/org/jetbrains/exposed/dao/Entity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ class View<out Target: Entity<*>> (val op : Op<Boolean>, val factory: EntityClas
override operator fun iterator(): Iterator<Target> = factory.find(op).iterator()
operator fun getValue(o: Any?, desc: KProperty<*>): SizedIterable<Target> = factory.find(op)
override fun copy(): SizedIterable<Target> = View(op, factory)
override fun orderBy(vararg order: Pair<Expression<*>, SortOrder>): SizedIterable<Target> = factory.find(op).orderBy(*order)
}

@Suppress("UNCHECKED_CAST")
Expand Down Expand Up @@ -278,13 +279,6 @@ open class Entity<ID:Comparable<ID>>(val id: EntityID<ID>) {
}
}

operator fun <TColumn, TReal> ColumnWithTransform<TColumn, TReal>.getValue(o: Entity<ID>, desc: KProperty<*>): TReal =
toReal(column.getValue(o, desc))

operator fun <TColumn, TReal> ColumnWithTransform<TColumn, TReal>.setValue(o: Entity<ID>, desc: KProperty<*>, value: TReal) {
column.setValue(o, desc, toColumn(value))
}

infix fun <TID:Comparable<TID>, Target:Entity<TID>> EntityClass<TID, Target>.via(table: Table): InnerTableLink<Entity<ID>, TID, Target> =
InnerTableLink(table, this@via)

Expand Down
8 changes: 8 additions & 0 deletions src/main/kotlin/org/jetbrains/exposed/sql/IterableEx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ interface SizedIterable<out T>: Iterable<T> {
fun forUpdate(): SizedIterable<T> = this
fun notForUpdate(): SizedIterable<T> = this
fun copy() : SizedIterable<T>
fun orderBy(vararg order: Pair<Expression<*>, SortOrder>) : SizedIterable<T>
}

fun <T> emptySized() : SizedIterable<T> = EmptySizedIterable()
Expand All @@ -29,6 +30,8 @@ class EmptySizedIterable<out T> : SizedIterable<T>, Iterator<T> {
override fun hasNext(): Boolean = false

override fun copy(): SizedIterable<T> = this

override fun orderBy(vararg order: Pair<Expression<*>, SortOrder>): SizedIterable<T> = this
}

class SizedCollection<out T>(val delegate: Collection<T>): SizedIterable<T> {
Expand All @@ -38,6 +41,7 @@ class SizedCollection<out T>(val delegate: Collection<T>): SizedIterable<T> {
override fun count() = delegate.size
override fun empty() = delegate.isEmpty()
override fun copy(): SizedIterable<T> = SizedCollection(delegate)
override fun orderBy(vararg order: Pair<Expression<*>, SortOrder>): SizedIterable<T> = this
}

class LazySizedCollection<out T>(val delegate: SizedIterable<T>): SizedIterable<T> {
Expand Down Expand Up @@ -87,6 +91,9 @@ class LazySizedCollection<out T>(val delegate: SizedIterable<T>): SizedIterable<
}

override fun copy(): SizedIterable<T> = LazySizedCollection(delegate.copy())

override fun orderBy(vararg order: Pair<Expression<*>, SortOrder>): SizedIterable<T>
= LazySizedCollection(delegate.orderBy(*order))
}

infix fun <T, R> SizedIterable<T>.mapLazy(f:(T)->R):SizedIterable<R> {
Expand All @@ -98,6 +105,7 @@ infix fun <T, R> SizedIterable<T>.mapLazy(f:(T)->R):SizedIterable<R> {
override fun count(): Int = source.count()
override fun empty(): Boolean = source.empty()
override fun copy(): SizedIterable<R> = source.copy().mapLazy(f)
override fun orderBy(vararg order: Pair<Expression<*>, SortOrder>) = source.orderBy(*order).mapLazy(f)

override operator fun iterator(): Iterator<R> {
val sourceIterator = source.iterator()
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/org/jetbrains/exposed/sql/Query.kt
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ open class Query(set: FieldSet, where: Op<Boolean>?): SizedIterable<ResultRow>,
return this
}

fun orderBy(vararg columns: Pair<Expression<*>, SortOrder>) : Query {
override fun orderBy(vararg columns: Pair<Expression<*>, SortOrder>) : Query {
(orderByExpressions as MutableList).addAll(columns)
return this
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,9 @@ class EntityTests: DatabaseTestsBase() {
val uniqueId by Categories.uniqueId
var title by Categories.title
val posts by Post optionalReferrersOn Posts.category

override fun equals(other: Any?) = (other as? Category)?.id?.equals(id) == true
override fun hashCode() = id.value.hashCode()
}

@Test
Expand Down Expand Up @@ -502,6 +505,18 @@ class EntityTests: DatabaseTestsBase() {
}
}

@Test fun testOrderByOnEntities() {
withTables(Categories) {
val category1 = Category.new { title = "Test1" }
val category3 = Category.new { title = "Test3" }
val category2 = Category.new { title = "Test2" }

assertEqualLists(listOf(category1, category3, category2), Category.all().toList())
assertEqualLists(listOf(category1, category2, category3), Category.all().orderBy(Categories.title to SortOrder.ASC).toList())
assertEqualLists(listOf(category3, category2, category1), Category.all().orderBy(Categories.title to SortOrder.DESC).toList())
}
}

private fun <T> newTransaction(statement: Transaction.() -> T) =
inTopLevelTransaction(TransactionManager.current().db.metadata.defaultTransactionIsolation, 1, null, statement)

Expand Down

0 comments on commit 31cbebc

Please sign in to comment.