Breaking: AnyFetchRequest.asRequest(of:)

This commit is contained in:
Gwendal Roué committed 2020-05-30 10:30:07 +02:00
1 parent e0266bdde3
commit a5de2b9288
5 files changed
+37 -18

No files matched your search

+25 -9
View File
@@ -216,16 +216,19 @@ public struct AnyFetchRequest<RowDecoder>: FetchRequest {
private let _requestSQL: (SQLGenerationContext, _ singleResult: Bool) throws -> String
// FetchRequest
private let _preparedRequest: (Database, _ singleResult: Bool) throws -> PreparedRequest
private let _makePreparedRequest: (Database, _ singleResult: Bool) throws -> PreparedRequest
private let _fetchCount: (Database) throws -> Int
#warning("TODO: force same RowDecoder, and expose asRequest(of:)")
/// Creates a request that wraps and forwards operations to `request`.
public init<Request: FetchRequest>(_ request: Request) {
_databaseRegion = request.databaseRegion
_requestSQL = request.requestSQL
_preparedRequest = request.makePreparedRequest
_fetchCount = request.fetchCount
/// Creates a request bound to type RowDecoder.
///
/// - parameter type: The fetched type RowDecoder
/// - returns: A request bound to type RowDecoder.
public func asRequest<RowDecoder>(of type: RowDecoder.Type) -> AnyFetchRequest<RowDecoder> {
AnyFetchRequest<RowDecoder>(
_databaseRegion: _databaseRegion,
_requestSQL: _requestSQL,
_makePreparedRequest: _makePreparedRequest,
_fetchCount: _fetchCount)
}
/// :nodoc:
@@ -241,7 +244,7 @@ public struct AnyFetchRequest<RowDecoder>: FetchRequest {
/// :nodoc:
public func makePreparedRequest(_ db: Database, forSingleResult singleResult: Bool) throws -> PreparedRequest {
try _preparedRequest(db, singleResult)
try _makePreparedRequest(db, singleResult)
}
/// :nodoc:
@@ -249,3 +252,16 @@ public struct AnyFetchRequest<RowDecoder>: FetchRequest {
try _fetchCount(db)
}
}
extension AnyFetchRequest {
/// Creates a request that wraps and forwards operations to `request`.
public init<Request: FetchRequest>(_ request: Request)
where Request.RowDecoder == RowDecoder
{
self.init(
_databaseRegion: request.databaseRegion,
_requestSQL: request.requestSQL,
_makePreparedRequest: request.makePreparedRequest,
_fetchCount: request.fetchCount)
}
}
+4
View File
@@ -10,6 +10,10 @@ extension AnyFetchRequest {
@available(*, unavailable, renamed: "RowDecoder")
typealias T = RowDecoder
@available(*, unavailable, message: "Use AnyFetchRequest(request).asRequest(of: SomeType.self) instead.")
public init<Request: FetchRequest>(_ request: Request)
{ preconditionFailure() }
@available(*, unavailable, message: "Define your own FetchRequest type instead.")
public init(_ prepare: @escaping (Database, _ singleResult: Bool) throws -> (SelectStatement, RowAdapter?))
{ preconditionFailure() }
@@ -366,9 +366,9 @@ extension QueryInterfaceRequest {
map(\.query) { $0.limit(limit, offset: offset) }
}
/// Creates a request bound to type Target.
/// Creates a request bound to type RowDecoder.
///
/// The returned request can fetch if the type Target is fetchable (Row,
/// The returned request can fetch if the type RowDecoder is fetchable (Row,
/// value, record).
///
/// // Int?
@@ -377,8 +377,8 @@ extension QueryInterfaceRequest {
/// .asRequest(of: Int.self) // <--
/// .fetchOne(db)
///
/// - parameter type: The fetched type Target
/// - returns: A typed request bound to type Target.
/// - parameter type: The fetched type RowDecoder
/// - returns: A request bound to type RowDecoder.
public func asRequest<RowDecoder>(of type: RowDecoder.Type) -> QueryInterfaceRequest<RowDecoder> {
QueryInterfaceRequest<RowDecoder>(query: query)
}
+3 -4
View File
@@ -103,7 +103,7 @@ public final class FetchedRecordsController<Record: FetchableRecord> {
where Request: FetchRequest, Request.RowDecoder == Record
{
self.itemsAreIdenticalFactory = itemsAreIdenticalFactory
self.request = ItemRequest(request)
self.request = AnyFetchRequest(request).asRequest(of: Item<Record>.self)
(self.region, self.itemsAreIdentical) = try databaseWriter.unsafeRead { db in
let region = try request.databaseRegion(db)
let itemsAreIdentical = try itemsAreIdenticalFactory(db)
@@ -166,7 +166,7 @@ public final class FetchedRecordsController<Record: FetchableRecord> {
throws
where Request: FetchRequest, Request.RowDecoder == Record
{
self.request = ItemRequest(request)
self.request = AnyFetchRequest(request).asRequest(of: Item<Record>.self)
(self.region, self.itemsAreIdentical) = try databaseWriter.unsafeRead { db in
let region = try request.databaseRegion(db)
let itemsAreIdentical = try itemsAreIdenticalFactory(db)
@@ -389,8 +389,7 @@ public final class FetchedRecordsController<Record: FetchableRecord> {
private let itemsAreIdenticalFactory: ItemComparatorFactory<Record>
/// The request
fileprivate typealias ItemRequest = AnyFetchRequest<Item<Record>>
fileprivate var request: ItemRequest
fileprivate var request: AnyFetchRequest<Item<Record>>
/// The observed database region
private var region: DatabaseRegion
+1 -1
View File
@@ -446,7 +446,7 @@ class DatabaseRegionTests : GRDBTestCase {
try XCTAssertEqual(request.databaseRegion(db).description, "foo(a,id)[1,2,3]")
do {
let derivedRequest: AnyFetchRequest<Row> = AnyFetchRequest(request)
let derivedRequest = AnyFetchRequest(request)
try XCTAssertEqual(derivedRequest.databaseRegion(db).description, "foo(a,id)[1,2,3]")
}
do {