From 991edffc7317f415f49c2cf82aaa3673f332820b Mon Sep 17 00:00:00 2001 From: Jan Sigrist Date: Fri, 22 Nov 2024 14:47:02 +0100 Subject: [PATCH 1/2] DOPE-260: add support for ordering by OrderExpression directly --- .../dope/resolvable/clause/ISelectClause.kt | 6 ++--- .../resolvable/clause/model/OrderByClause.kt | 7 ++++-- .../resolvable/clause/OrderByClauseTest.kt | 25 +++++++++++++++++++ 3 files changed, 32 insertions(+), 6 deletions(-) diff --git a/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/ISelectClause.kt b/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/ISelectClause.kt index a0bc0808..006290f2 100644 --- a/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/ISelectClause.kt +++ b/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/ISelectClause.kt @@ -45,10 +45,8 @@ interface ISelectOrderByClause : ISelectLimitClause { } interface ISelectGroupByClause : ISelectOrderByClause { - fun orderBy(expression: TypeExpression, orderByType: OrderByType? = null) = SelectOrderByClause( - OrderExpression(expression, orderByType), - parentClause = this, - ) + fun orderBy(orderExpression: OrderExpression) = SelectOrderByClause(orderExpression, parentClause = this) + fun orderBy(expression: TypeExpression, orderByType: OrderByType? = null) = orderBy(OrderExpression(expression, orderByType)) } interface ISelectWhereClause : ISelectGroupByClause { diff --git a/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/model/OrderByClause.kt b/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/model/OrderByClause.kt index 69869cbf..0e8b9f02 100644 --- a/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/model/OrderByClause.kt +++ b/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/model/OrderByClause.kt @@ -39,13 +39,16 @@ class SelectOrderByClause( ) } - fun thenOrderBy(typeExpression: TypeExpression, orderByType: OrderByType? = null) = + fun thenOrderBy(orderExpression: OrderExpression) = SelectOrderByClause( this.orderExpression, *additionalOrderExpressions, - OrderExpression(typeExpression, orderByType), + orderExpression, parentClause = this.parentClause, ) + + fun thenOrderBy(typeExpression: TypeExpression, orderByType: OrderByType? = null) = + thenOrderBy(OrderExpression(typeExpression, orderByType)) } class OrderExpression(private val expression: TypeExpression, private val orderByType: OrderByType? = null) : Resolvable { diff --git a/core/src/test/kotlin/ch/ergon/dope/resolvable/clause/OrderByClauseTest.kt b/core/src/test/kotlin/ch/ergon/dope/resolvable/clause/OrderByClauseTest.kt index c48327bd..53d01d12 100644 --- a/core/src/test/kotlin/ch/ergon/dope/resolvable/clause/OrderByClauseTest.kt +++ b/core/src/test/kotlin/ch/ergon/dope/resolvable/clause/OrderByClauseTest.kt @@ -245,6 +245,31 @@ 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 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() From 7edb81142c0f778b25f62d45f498ea7bd6da6642 Mon Sep 17 00:00:00 2001 From: Jan Sigrist Date: Fri, 22 Nov 2024 16:09:55 +0100 Subject: [PATCH 2/2] DOPE-260: add support for ordering by multiple OrderExpressions --- .../ch/ergon/dope/resolvable/clause/ISelectClause.kt | 3 ++- .../dope/resolvable/clause/OrderByClauseTest.kt | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/ISelectClause.kt b/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/ISelectClause.kt index 006290f2..ba374e5d 100644 --- a/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/ISelectClause.kt +++ b/core/src/main/kotlin/ch/ergon/dope/resolvable/clause/ISelectClause.kt @@ -45,7 +45,8 @@ interface ISelectOrderByClause : ISelectLimitClause { } interface ISelectGroupByClause : ISelectOrderByClause { - fun orderBy(orderExpression: OrderExpression) = SelectOrderByClause(orderExpression, parentClause = this) + fun orderBy(orderExpression: OrderExpression, vararg additionalOrderExpressions: OrderExpression) = + SelectOrderByClause(orderExpression, *additionalOrderExpressions, parentClause = this) fun orderBy(expression: TypeExpression, orderByType: OrderByType? = null) = orderBy(OrderExpression(expression, orderByType)) } diff --git a/core/src/test/kotlin/ch/ergon/dope/resolvable/clause/OrderByClauseTest.kt b/core/src/test/kotlin/ch/ergon/dope/resolvable/clause/OrderByClauseTest.kt index 53d01d12..41c2df71 100644 --- a/core/src/test/kotlin/ch/ergon/dope/resolvable/clause/OrderByClauseTest.kt +++ b/core/src/test/kotlin/ch/ergon/dope/resolvable/clause/OrderByClauseTest.kt @@ -257,6 +257,18 @@ class OrderByClauseTest : ManagerDependentTest { 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()