From 4bcdb9189527a9307083fe5f1105c1ef5b4c6534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 16 Jan 2018 14:11:05 +0100 Subject: [PATCH] AnyRequest and all derived requests preserve database region, and counting method --- GRDB/Core/Request.swift | 80 ++++++++++++++++++++++------------------- TODO.md | 2 -- 2 files changed, 43 insertions(+), 39 deletions(-) diff --git a/GRDB/Core/Request.swift b/GRDB/Core/Request.swift index 4ef64ec86..6d9f13b77 100644 --- a/GRDB/Core/Request.swift +++ b/GRDB/Core/Request.swift @@ -93,6 +93,9 @@ extension Request { /// An adapted request. public struct AdaptedRequest : Request { + private let base: Base + private let adapter: (Database) throws -> RowAdapter + /// Creates an adapted request from a base request and a closure that builds /// a row adapter from a database connection. init(_ base: Base, _ adapter: @escaping (Database) throws -> RowAdapter) { @@ -100,10 +103,6 @@ public struct AdaptedRequest : Request { self.adapter = adapter } - /// A tuple that contains a prepared statement that is ready to be - /// executed, and an eventual row adapter. - /// - /// - parameter db: A database connection. public func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) { let (statement, baseAdapter) = try base.prepare(db) if let baseAdapter = baseAdapter { @@ -113,15 +112,13 @@ public struct AdaptedRequest : Request { } } - /// The number of rows fetched by the request. - /// - /// - parameter db: A database connection. public func fetchCount(_ db: Database) throws -> Int { return try base.fetchCount(db) } - private let base: Base - private let adapter: (Database) throws -> RowAdapter + public func region(_ db: Database) throws -> DatabaseRegion { + return try base.region(db) + } } /// A type-erased Request. @@ -129,26 +126,36 @@ public struct AdaptedRequest : Request { /// An instance of AnyRequest forwards its operations to an underlying request, /// hiding its specifics. public struct AnyRequest : Request { + private let base: Request + /// Creates a new request that wraps and forwards operations to `request`. public init(_ request: Request) { - self._prepare = { try request.prepare($0) } + base = request } /// Creates a new request whose `prepare()` method wraps and forwards /// operations the argument closure. public init(_ prepare: @escaping (Database) throws -> (SelectStatement, RowAdapter?)) { - _prepare = prepare + struct PrepareRequest: Request { + let base: (Database) throws -> (SelectStatement, RowAdapter?) + func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) { + return try base(db) + } + } + base = PrepareRequest(base: prepare) } - /// A tuple that contains a prepared statement that is ready to be - /// executed, and an eventual row adapter. - /// - /// - parameter db: A database connection. public func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) { - return try _prepare(db) + return try base.prepare(db) } - private let _prepare: (Database) throws -> (SelectStatement, RowAdapter?) + public func fetchCount(_ db: Database) throws -> Int { + return try base.fetchCount(db) + } + + public func region(_ db: Database) throws -> DatabaseRegion { + return try base.region(db) + } } /// A Request built from raw SQL. @@ -254,32 +261,28 @@ extension TypedRequest { /// An adapted typed request. public struct AdaptedTypedRequest : TypedRequest { - /// The type that can convert raw database rows to fetched values public typealias RowDecoder = Base.RowDecoder + private let base: AdaptedRequest + /// Creates an adapted request from a base request and a closure that builds /// a row adapter from a database connection. init(_ base: Base, _ adapter: @escaping (Database) throws -> RowAdapter) { - adaptedRequest = AdaptedRequest(base, adapter) + self.base = AdaptedRequest(base, adapter) } - /// A tuple that contains a prepared statement that is ready to be - /// executed, and an eventual row adapter. - /// - /// - parameter db: A database connection. public func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) { - return try adaptedRequest.prepare(db) + return try base.prepare(db) } - /// The number of rows fetched by the request. - /// - /// - parameter db: A database connection. public func fetchCount(_ db: Database) throws -> Int { - return try adaptedRequest.fetchCount(db) + return try base.fetchCount(db) } - private let adaptedRequest: AdaptedRequest + public func region(_ db: Database) throws -> DatabaseRegion { + return try base.region(db) + } } /// A type-erased TypedRequest. @@ -289,27 +292,30 @@ public struct AdaptedTypedRequest : TypedRequest { public struct AnyTypedRequest : TypedRequest { /// The type that can convert raw database rows to fetched values public typealias RowDecoder = T + private let base: Request /// Creates a new request that wraps and forwards operations to `request`. public init(_ request: Request) where Request: TypedRequest, Request.RowDecoder == RowDecoder { - self._prepare = { try request.prepare($0) } + base = AnyRequest(request) } /// Creates a new request whose `prepare()` method wraps and forwards /// operations the argument closure. public init(_ prepare: @escaping (Database) throws -> (SelectStatement, RowAdapter?)) { - _prepare = prepare + base = AnyRequest(prepare) } - /// A tuple that contains a prepared statement that is ready to be - /// executed, and an eventual row adapter. - /// - /// - parameter db: A database connection. public func prepare(_ db: Database) throws -> (SelectStatement, RowAdapter?) { - return try _prepare(db) + return try base.prepare(db) } - private let _prepare: (Database) throws -> (SelectStatement, RowAdapter?) + public func fetchCount(_ db: Database) throws -> Int { + return try base.fetchCount(db) + } + + public func region(_ db: Database) throws -> DatabaseRegion { + return try base.region(db) + } } extension TypedRequest where RowDecoder: RowConvertible { diff --git a/TODO.md b/TODO.md index f00b96bb5..9303d9c93 100644 --- a/TODO.md +++ b/TODO.md @@ -11,8 +11,6 @@ - [ ] CASE x WHEN w1 THEN r1 WHEN w2 THEN r2 ELSE r3 END https://www.sqlite.org/lang_expr.html - [ ] Write regression tests for #156 and #157 - [ ] Improve Database.region(rowIds:in:) -- [ ] Replace Region.intersection with Region.setRowIds(for:). It is not really an intersection. It just gives the tracked rowIds for a table, without modifying other tables. -- [ ] Have Any(Typed)Request preserve region GRDB 3.0