OrderedRequest.withStableOrder()

This commit is contained in:
Gwendal Roué committed 2023-10-12 09:17:50 +02:00
1 parent aadb887b34
commit 7a7ee554c7
14 files changed
+162

No files matched your search

@@ -222,6 +222,12 @@ extension Association {
relation = relation.unordered()
}
}
public func withStableOrder() -> Self {
withDestinationRelation { relation in
relation = relation.withStableOrder()
}
}
}
// TableRequest conformance
@@ -344,6 +344,12 @@ extension QueryInterfaceRequest: OrderedRequest {
$0.relation = $0.relation.unordered()
}
}
public func withStableOrder() -> QueryInterfaceRequest<RowDecoder> {
with {
$0.relation = $0.relation.withStableOrder()
}
}
}
extension QueryInterfaceRequest: AggregatingRequest {
@@ -905,6 +905,7 @@ extension AggregatingRequest {
/// - ``orderWhenConnected(_:)``
/// - ``reversed()``
/// - ``unordered()``
/// - ``withStableOrder()``
public protocol OrderedRequest {
/// Sorts the fetched rows according to the given SQL ordering terms.
///
@@ -965,6 +966,14 @@ public protocol OrderedRequest {
/// .unordered()
/// ```
func unordered() -> Self
/// Returns a request with a stable order.
///
/// The returned request lifts ordering ambiguities and always return
/// its results in the same order.
///
/// The purpose of this method is to make requests testable.
func withStableOrder() -> Self
}
extension OrderedRequest {
@@ -1378,6 +1387,7 @@ extension JoinableRequest where Self: SelectionRequest {
/// - ``TableRequest/orderByPrimaryKey()``
/// - ``OrderedRequest/reversed()``
/// - ``OrderedRequest/unordered()``
/// - ``OrderedRequest/withStableOrder()``
///
/// ### Associations
///
+15
View File
@@ -282,6 +282,21 @@ extension SQLRelation: Refinable {
}
}
func withStableOrder() -> Self {
with {
// Order by primary key. Don't order by rowid because those are
// not stable: rowids can change after a vacuum.
$0.ordering = $0.ordering.appending(Ordering(orderings: { db in
try db.primaryKey(source.tableName).columns.map { SQLExpression.column($0).sqlOrdering }
}))
$0.children = children.mapValues { child in
child.with {
$0.relation = $0.relation.withStableOrder()
}
}
}
}
// Remove ordering iff relation has no LIMIT clause
func unorderedUnlessLimited() -> Self {
if limit != nil {