mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Add unordered() to OrderedRequest
This commit is contained in:
1 parent
f98502a136
commit
3786482368
5 files changed
+104
-8
No files matched your search
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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")
|
||||
""")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user