From 378648236809cd71b9deecfb5e55f7d18514857c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Fri, 17 May 2019 17:53:25 +0200 Subject: [PATCH] Add unordered() to OrderedRequest --- .../Association/Association.swift | 15 +++++ .../QueryInterfaceRequest.swift | 9 +++ GRDB/QueryInterface/RequestProtocols.swift | 7 ++ GRDB/QueryInterface/SQLRelation.swift | 15 +++-- Tests/GRDBTests/DerivableRequestTests.swift | 66 +++++++++++++++++-- 5 files changed, 104 insertions(+), 8 deletions(-) diff --git a/GRDB/QueryInterface/Association/Association.swift b/GRDB/QueryInterface/Association/Association.swift index 919946798..2f13597ed 100644 --- a/GRDB/QueryInterface/Association/Association.swift +++ b/GRDB/QueryInterface/Association/Association.swift @@ -166,6 +166,21 @@ extension Association { return mapDestinationRelation { $0.reversed() } } + /// Creates an association without any ordering. + /// + /// struct Player: TableRecord { + /// static let team = belongsTo(Team.self) + /// } + /// + /// // SELECT player.*, team.* + /// // FROM player + /// // JOIN team ON team.id = player.teamId + /// let association = Player.team.order(Column("name")).unordered() + /// var request = Player.including(required: association) + public func unordered() -> Self { + return mapDestinationRelation { $0.unordered() } + } + /// Creates an association with the given key. /// /// This new key impacts how rows fetched from the resulting association diff --git a/GRDB/QueryInterface/QueryInterfaceRequest.swift b/GRDB/QueryInterface/QueryInterfaceRequest.swift index b3c50c652..316890da5 100644 --- a/GRDB/QueryInterface/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/QueryInterfaceRequest.swift @@ -268,6 +268,15 @@ extension QueryInterfaceRequest : DerivableRequest, AggregatingRequest { return mapQuery { $0.reversed() } } + /// Creates a request without any ordering. + /// + /// // SELECT * FROM player + /// var request = Player.all().order(Column("name")) + /// request = request.unordered() + public func unordered() -> QueryInterfaceRequest { + return mapQuery { $0.unordered() } + } + /// Creates a request which fetches *limit* rows, starting at *offset*. /// /// // SELECT * FROM player LIMIT 1 diff --git a/GRDB/QueryInterface/RequestProtocols.swift b/GRDB/QueryInterface/RequestProtocols.swift index 13a408323..df70b30cc 100644 --- a/GRDB/QueryInterface/RequestProtocols.swift +++ b/GRDB/QueryInterface/RequestProtocols.swift @@ -405,6 +405,13 @@ public protocol OrderedRequest { /// var request = Player.all() /// request = request.reversed() func reversed() -> Self + + /// Creates a request without any ordering. + /// + /// // SELECT * FROM player + /// var request = Player.all().order(Column("name")) + /// request = request.unordered() + func unordered() -> Self } /// :nodoc: diff --git a/GRDB/QueryInterface/SQLRelation.swift b/GRDB/QueryInterface/SQLRelation.swift index 5d103b8b5..836d0e19d 100644 --- a/GRDB/QueryInterface/SQLRelation.swift +++ b/GRDB/QueryInterface/SQLRelation.swift @@ -131,6 +131,10 @@ struct SQLRelation { fileprivate func associationForKey(_ key: String) -> SQLAssociation { return SQLAssociation(key: key, condition: condition, relation: relation) } + + func mapRelation(_ transform: (SQLRelation) -> SQLRelation) -> Child { + return Child(kind: kind, condition: condition, relation: transform(relation)) + } } var source: SQLSource @@ -200,16 +204,19 @@ extension SQLRelation { return order(ordering.reversed) } + func unordered() -> SQLRelation { + var relation = self + relation.ordering = SQLRelation.Ordering() + relation.children = relation.children.mapValues { $0.mapRelation { $0.unordered() } } + return relation + } + private func order(_ ordering: SQLRelation.Ordering) -> SQLRelation { var relation = self relation.ordering = ordering return relation } - func unordered() -> SQLRelation { - return order(SQLRelation.Ordering()) - } - func appendingChild(_ child: SQLRelation.Child, forKey key: String) -> SQLRelation { var relation = self if let existingChild = relation.children.removeValue(forKey: key) { diff --git a/Tests/GRDBTests/DerivableRequestTests.swift b/Tests/GRDBTests/DerivableRequestTests.swift index 6ba87d3ba..c0f5e978f 100644 --- a/Tests/GRDBTests/DerivableRequestTests.swift +++ b/Tests/GRDBTests/DerivableRequestTests.swift @@ -58,7 +58,7 @@ private var libraryMigrator: DatabaseMigrator = { return migrator }() -// One extension... +// Define DerivableRequest extensions extension DerivableRequest where RowDecoder == Author { func filter(country: String) -> Self { return filter(Column("country") == country) @@ -71,6 +71,12 @@ extension DerivableRequest where RowDecoder == Author { } } +extension DerivableRequest where RowDecoder == Book { + func orderByTitle() -> Self { + return order(Column("title").collating(.localizedCaseInsensitiveCompare)) + } +} + class DerivableRequestTests: GRDBTestCase { func testFilter() throws { let dbQueue = try makeDatabaseQueue() @@ -98,32 +104,84 @@ class DerivableRequestTests: GRDBTestCase { try libraryMigrator.migrate(dbQueue) try dbQueue.inDatabase { db in // ... for two requests (1) + sqlQueries.removeAll() let authorNames = try Author.all() .orderByFullName() .fetchAll(db) .map { $0.fullName } XCTAssertEqual(authorNames, ["Herman Melville", "Marcel Proust"]) + XCTAssertEqual(lastSQLQuery, """ + SELECT * FROM "author" \ + ORDER BY "lastName" COLLATE swiftLocalizedCaseInsensitiveCompare, \ + "firstName" COLLATE swiftLocalizedCaseInsensitiveCompare + """) + sqlQueries.removeAll() let reversedAuthorNames = try Author.all() .orderByFullName() .reversed() .fetchAll(db) .map { $0.fullName } XCTAssertEqual(reversedAuthorNames, ["Marcel Proust", "Herman Melville"]) - + XCTAssertEqual(lastSQLQuery, """ + SELECT * FROM "author" \ + ORDER BY "lastName" COLLATE swiftLocalizedCaseInsensitiveCompare DESC, \ + "firstName" COLLATE swiftLocalizedCaseInsensitiveCompare DESC + """) + + sqlQueries.removeAll() + _ /* unorderedAuthors */ = try Author.all() + .orderByFullName() + .unordered() + .fetchAll(db) + XCTAssertEqual(lastSQLQuery, """ + SELECT * FROM "author" + """) + // ... for two requests (2) + sqlQueries.removeAll() let bookTitles = try Book .joining(required: Book.author.orderByFullName()) + .orderByTitle() .fetchAll(db) .map { $0.title } - XCTAssertEqual(bookTitles, ["Moby-Dick", "Du côté de chez Swann"]) + XCTAssertEqual(bookTitles, ["Du côté de chez Swann", "Moby-Dick"]) + XCTAssertEqual(lastSQLQuery, """ + SELECT "book".* FROM "book" \ + JOIN "author" ON ("author"."id" = "book"."authorId") \ + ORDER BY \ + "book"."title" COLLATE swiftLocalizedCaseInsensitiveCompare, \ + "author"."lastName" COLLATE swiftLocalizedCaseInsensitiveCompare, \ + "author"."firstName" COLLATE swiftLocalizedCaseInsensitiveCompare + """) + sqlQueries.removeAll() let reversedBookTitles = try Book .joining(required: Book.author.orderByFullName()) + .orderByTitle() .reversed() .fetchAll(db) .map { $0.title } - XCTAssertEqual(reversedBookTitles, ["Du côté de chez Swann", "Moby-Dick"]) + XCTAssertEqual(reversedBookTitles, ["Moby-Dick", "Du côté de chez Swann"]) + XCTAssertEqual(lastSQLQuery, """ + SELECT "book".* FROM "book" \ + JOIN "author" ON ("author"."id" = "book"."authorId") \ + ORDER BY \ + "book"."title" COLLATE swiftLocalizedCaseInsensitiveCompare DESC, \ + "author"."lastName" COLLATE swiftLocalizedCaseInsensitiveCompare DESC, \ + "author"."firstName" COLLATE swiftLocalizedCaseInsensitiveCompare DESC + """) + + sqlQueries.removeAll() + _ /* unorderedBooks */ = try Book + .joining(required: Book.author.orderByFullName()) + .orderByTitle() + .unordered() + .fetchAll(db) + XCTAssertEqual(lastSQLQuery, """ + SELECT "book".* FROM "book" \ + JOIN "author" ON ("author"."id" = "book"."authorId") + """) } } }