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 {
@@ -208,6 +208,15 @@ class DerivableRequestTests: GRDBTestCase {
SELECT * FROM "author"
""")
sqlQueries.removeAll()
_ /* stableOrderAuthors */ = try Author.all()
.orderByFullName()
.withStableOrder()
.fetchAll(db)
XCTAssertEqual(lastSQLQuery, """
SELECT * FROM "author" ORDER BY "lastName" COLLATE swiftLocalizedCaseInsensitiveCompare, "firstName" COLLATE swiftLocalizedCaseInsensitiveCompare, "id"
""")
// ... for two requests (2)
sqlQueries.removeAll()
let bookTitles = try Book
@@ -252,6 +261,23 @@ class DerivableRequestTests: GRDBTestCase {
SELECT "book".* FROM "book" \
JOIN "author" ON "author"."id" = "book"."authorId"
""")
sqlQueries.removeAll()
_ /* stableOrderBooks */ = try Book
.joining(required: Book.author.orderByFullName())
.orderByTitle()
.withStableOrder()
.fetchAll(db)
XCTAssertEqual(lastSQLQuery, """
SELECT "book".* FROM "book" \
JOIN "author" ON "author"."id" = "book"."authorId" \
ORDER BY \
"book"."title" COLLATE swiftLocalizedCaseInsensitiveCompare, \
"book"."id", \
"author"."lastName" COLLATE swiftLocalizedCaseInsensitiveCompare, \
"author"."firstName" COLLATE swiftLocalizedCaseInsensitiveCompare, \
"author"."id"
""")
}
}
@@ -436,6 +436,17 @@ class RecordMinimalNonOptionalPrimaryKeySingleTests: GRDBTestCase {
}
// MARK: - Stable order
func testStableOrder() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = MinimalNonOptionalPrimaryKeySingle.all().withStableOrder()
try assertEqualSQL(db, request, "SELECT * FROM \"minimalSingles\" ORDER BY \"id\"")
}
}
// MARK: - Fetch With Primary Key
func testFetchCursorWithPrimaryKeys() throws {
@@ -470,6 +470,17 @@ class RecordMinimalPrimaryKeyRowIDTests : GRDBTestCase {
}
// MARK: - Stable order
func testStableOrder() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = MinimalRowID.all().withStableOrder()
try assertEqualSQL(db, request, "SELECT * FROM \"minimalRowIDs\" ORDER BY \"id\"")
}
}
// MARK: - Fetch With Primary Key
func testFetchCursorWithPrimaryKeys() throws {
@@ -492,6 +492,17 @@ class RecordMinimalPrimaryKeySingleTests: GRDBTestCase {
}
// MARK: - Stable order
func testStableOrder() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = MinimalSingle.all().withStableOrder()
try assertEqualSQL(db, request, "SELECT * FROM \"minimalSingles\" ORDER BY \"UUID\"")
}
}
// MARK: - Fetch With Primary Key
func testFetchCursorWithPrimaryKeys() throws {
@@ -562,6 +562,17 @@ class RecordPrimaryKeyHiddenRowIDTests : GRDBTestCase {
}
// MARK: - Stable order
func testStableOrder() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = Person.all().withStableOrder()
try assertEqualSQL(db, request, "SELECT *, \"rowid\" FROM \"persons\" ORDER BY \"rowid\"")
}
}
// MARK: - Fetch With Primary Key
func testFetchCursorWithPrimaryKeys() throws {
@@ -510,6 +510,17 @@ class RecordPrimaryKeyMultipleTests: GRDBTestCase {
}
// MARK: - Stable order
func testStableOrder() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = Citizenship.all().withStableOrder()
try assertEqualSQL(db, request, "SELECT * FROM \"citizenships\" ORDER BY \"personName\", \"countryName\"")
}
}
// MARK: - Exists
func testExistsWithNilPrimaryKeyReturnsFalse() throws {
@@ -147,6 +147,17 @@ class RecordPrimaryKeyNoneTests: GRDBTestCase {
}
// MARK: - Stable order
func testStableOrder() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = Item.all().withStableOrder()
try assertEqualSQL(db, request, "SELECT * FROM \"items\" ORDER BY \"rowid\"")
}
}
// MARK: - Fetch With Primary Key
func testFetchCursorWithPrimaryKeys() throws {
@@ -555,6 +555,17 @@ class RecordPrimaryKeyRowIDTests: GRDBTestCase {
}
// MARK: - Stable order
func testStableOrder() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = Person.all().withStableOrder()
try assertEqualSQL(db, request, "SELECT * FROM \"persons\" ORDER BY \"id\"")
}
}
// MARK: - Fetch With Primary Key
func testFetchCursorWithPrimaryKeys() throws {
@@ -497,6 +497,17 @@ class RecordPrimaryKeySingleTests: GRDBTestCase {
}
// MARK: - Stable order
func testStableOrder() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = Pet.all().withStableOrder()
try assertEqualSQL(db, request, "SELECT * FROM \"pets\" ORDER BY \"UUID\"")
}
}
// MARK: - Fetch With Primary Key
func testFetchCursorWithPrimaryKeys() throws {
@@ -494,6 +494,17 @@ class RecordPrimaryKeySingleWithReplaceConflictResolutionTests: GRDBTestCase {
}
// MARK: - Stable order
func testStableOrder() throws {
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
let request = Email.all().withStableOrder()
try assertEqualSQL(db, request, "SELECT * FROM \"emails\" ORDER BY \"email\"")
}
}
// MARK: - Fetch With Primary Key
func testFetchCursorWithPrimaryKeys() throws {