Skip to content

Commit

Permalink
Merge pull request #70 from ergon/bugfix/dope-260-fix-order-by
Browse files Browse the repository at this point in the history
DOPE-260: add support for ordering by OrderExpression directly
  • Loading branch information
jansigi authored Nov 26, 2024
2 parents c710fcd + 7edb811 commit a246309
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ interface ISelectOrderByClause<T : ValidType> : ISelectLimitClause<T> {
}

interface ISelectGroupByClause<T : ValidType> : ISelectOrderByClause<T> {
fun orderBy(expression: TypeExpression<out ValidType>, orderByType: OrderByType? = null) = SelectOrderByClause(
OrderExpression(expression, orderByType),
parentClause = this,
)
fun orderBy(orderExpression: OrderExpression, vararg additionalOrderExpressions: OrderExpression) =
SelectOrderByClause(orderExpression, *additionalOrderExpressions, parentClause = this)
fun orderBy(expression: TypeExpression<out ValidType>, orderByType: OrderByType? = null) = orderBy(OrderExpression(expression, orderByType))
}

interface ISelectWhereClause<T : ValidType> : ISelectGroupByClause<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ class SelectOrderByClause<T : ValidType>(
)
}

fun thenOrderBy(typeExpression: TypeExpression<out ValidType>, orderByType: OrderByType? = null) =
fun thenOrderBy(orderExpression: OrderExpression) =
SelectOrderByClause(
this.orderExpression,
*additionalOrderExpressions,
OrderExpression(typeExpression, orderByType),
orderExpression,
parentClause = this.parentClause,
)

fun thenOrderBy(typeExpression: TypeExpression<out ValidType>, orderByType: OrderByType? = null) =
thenOrderBy(OrderExpression(typeExpression, orderByType))
}

class OrderExpression(private val expression: TypeExpression<out ValidType>, private val orderByType: OrderByType? = null) : Resolvable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,43 @@ class OrderByClauseTest : ManagerDependentTest {
assertEquals(expected.toDopeQuery(manager), actual.toDopeQuery(manager))
}

@Test
fun `should support order by function with orderExpression`() {
val stringField = someStringField()
val parentClause = someSelectClause()
val orderExpression = OrderExpression(stringField)
val expected = SelectOrderByClause(orderExpression, parentClause = parentClause)

val actual = parentClause.orderBy(orderExpression)

assertEquals(expected.toDopeQuery(manager), actual.toDopeQuery(manager))
}

@Test
fun `should support order by function with multiple orderExpressions`() {
val parentClause = someSelectClause()
val orderExpression1 = OrderExpression(someStringField())
val orderExpression2 = OrderExpression(someNumberField(), ASC)
val expected = SelectOrderByClause(orderExpression1, orderExpression2, parentClause = parentClause)

val actual = parentClause.orderBy(orderExpression1, orderExpression2)

assertEquals(expected.toDopeQuery(manager), actual.toDopeQuery(manager))
}

@Test
fun `should support order by function with then order by with orderExpression`() {
val stringField = someStringField()
val parentClause = someSelectClause()
val orderType = ASC
val orderExpression = OrderExpression(someNumberField())
val expected = SelectOrderByClause(OrderExpression(stringField, orderType), orderExpression, parentClause = parentClause)

val actual = parentClause.orderBy(stringField, orderType).thenOrderBy(orderExpression)

assertEquals(expected.toDopeQuery(manager), actual.toDopeQuery(manager))
}

@Test
fun `should support order by function with then order by`() {
val stringField = someStringField()
Expand Down

0 comments on commit a246309

Please sign in to comment.