diff --git a/CHANGELOG.md b/CHANGELOG.md index 5266bc6c4..d7acac60e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -123,6 +123,8 @@ New features have been added in order to plug a few holes and support the [RxGRD let strings = try Set(String.fetchCursor(...)) ``` +- The new `AnyDatabaseReader` and `AnyDatabaseWriter` type erasers help dealing with the `DatabaseReader` and `DatabaseWriter` protocols. + ### Breaking Changes @@ -305,6 +307,13 @@ New features have been added in order to plug a few holes and support the [RxGRD extension TableMapping { - @available(*, deprecated) static func primaryKeyRowComparator(_ db: Database) throws -> (Row, Row) -> Bool } + ++final class AnyDatabaseReader : DatabaseReader { ++ init(_ base: DatabaseReader) ++} ++final class AnyDatabaseWriter : DatabaseWriter { ++ init(_ base: DatabaseWriter) ++} ``` ### Experimental API diff diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 0d793a24c..84ee9c46e 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -208,3 +208,44 @@ extension DatabaseReader { } } } + +/// A type-erased DatabaseReader +/// +/// Instances of AnyDatabaseReader forward their methods to an arbitrary +/// underlying database reader. +public final class AnyDatabaseReader : DatabaseReader { + private let base: DatabaseReader + + /// Creates a database reader that wraps a base database reader. + public init(_ base: DatabaseReader) { + self.base = base + } + + public func read(_ block: (Database) throws -> T) throws -> T { + return try base.read(block) + } + + public func unsafeRead(_ block: (Database) throws -> T) throws -> T { + return try base.unsafeRead(block) + } + + public func unsafeReentrantRead(_ block: (Database) throws -> T) throws -> T { + return try base.unsafeReentrantRead(block) + } + + public func add(function: DatabaseFunction) { + base.add(function: function) + } + + public func remove(function: DatabaseFunction) { + base.remove(function: function) + } + + public func add(collation: DatabaseCollation) { + base.add(collation: collation) + } + + public func remove(collation: DatabaseCollation) { + base.remove(collation: collation) + } +} diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index d395917a8..7c8f5b264 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -123,3 +123,56 @@ extension DatabaseWriter { write { $0.remove(transactionObserver: transactionObserver) } } } + +/// A type-erased DatabaseWriter +/// +/// Instances of AnyDatabaseWriter forward their methods to an arbitrary +/// underlying database writer. +public final class AnyDatabaseWriter : DatabaseWriter { + private let base: DatabaseWriter + + /// Creates a database writer that wraps a base database writer. + public init(_ base: DatabaseWriter) { + self.base = base + } + + public func read(_ block: (Database) throws -> T) throws -> T { + return try base.read(block) + } + + public func unsafeRead(_ block: (Database) throws -> T) throws -> T { + return try base.unsafeRead(block) + } + + public func unsafeReentrantRead(_ block: (Database) throws -> T) throws -> T { + return try base.unsafeReentrantRead(block) + } + + public func add(function: DatabaseFunction) { + base.add(function: function) + } + + public func remove(function: DatabaseFunction) { + base.remove(function: function) + } + + public func add(collation: DatabaseCollation) { + base.add(collation: collation) + } + + public func remove(collation: DatabaseCollation) { + base.remove(collation: collation) + } + + public func write(_ block: (Database) throws -> T) rethrows -> T { + return try base.write(block) + } + + public func unsafeReentrantWrite(_ block: (Database) throws -> T) rethrows -> T { + return try base.unsafeReentrantWrite(block) + } + + public func readFromCurrentState(_ block: @escaping (Database) -> Void) throws { + try base.readFromCurrentState(block) + } +} diff --git a/README.md b/README.md index 62c37f290..c7cbbf14e 100644 --- a/README.md +++ b/README.md @@ -5706,7 +5706,13 @@ However, database queues are not database pools, and DatabaseReader and Database - DatabaseWriter.readFromCurrentState is synchronous, or asynchronous, depending on whether it is run by a queue or a pool (see [advanced DatabasePool](#advanced-databasepool)). It thus requires higher libDispatch skills, and more complex synchronization code. - The definition of "current state" in DatabaseWriter.readFromCurrentState is [delicate](http://groue.github.io/GRDB.swift/docs/1.3/Protocols/DatabaseWriter.html#/s:FP4GRDB14DatabaseWriter20readFromCurrentStateFzFCS_8DatabaseT_T_). -DatabaseReader and DatabaseWriter are not a tool for applications that hesitate between DatabaseQueue and DatabasePool, and look for a common API. As seen above, the protocols actually make applications harder to write correctly. Instead, they target reusable agnostic code that has *both* queues and pools in mind. For example, GRDB uses those protocols for [migrations](#migrations) and [FetchedRecordsController](#fetchedrecordscontroller), two tools that accept both queues and pools. +DatabaseReader and DatabaseWriter are not a tool for applications that hesitate between DatabaseQueue and DatabasePool, and look for a common API. As seen above, the protocols actually make applications harder to write correctly. Instead, they target generic code that has *both* queues and pools in mind. The built-in AnyDatabaseReader and AnyDatabaseWriter type erasers serve the same purpose. + +DatabaseWriter and DatabaseReader fuel, for example: + +- [Migrations](#migrations) +- [FetchedRecordsController](#fetchedrecordscontroller) +- [RxGRDB](http://github.com/RxSwiftCommunity/RxGRDB) ### Unsafe Concurrency APIs diff --git a/Tests/GRDBTests/DatabaseReaderTests.swift b/Tests/GRDBTests/DatabaseReaderTests.swift index 3bbd89671..a2db2af09 100644 --- a/Tests/GRDBTests/DatabaseReaderTests.swift +++ b/Tests/GRDBTests/DatabaseReaderTests.swift @@ -68,4 +68,10 @@ class DatabaseReaderTests : GRDBTestCase { } } } + + func testAnyDatabaseReader() { + // This test passes if this code compiles. + let reader: DatabaseReader = DatabaseQueue() + let _: DatabaseReader = AnyDatabaseReader(reader) + } } diff --git a/Tests/GRDBTests/DatabaseWriterTests.swift b/Tests/GRDBTests/DatabaseWriterTests.swift index 6fb191f0a..ee3188fc8 100644 --- a/Tests/GRDBTests/DatabaseWriterTests.swift +++ b/Tests/GRDBTests/DatabaseWriterTests.swift @@ -44,4 +44,10 @@ class DatabaseWriterTests : GRDBTestCase { } } } + + func testAnyDatabaseWriter() { + // This test passes if this code compiles. + let writer: DatabaseWriter = DatabaseQueue() + let _: DatabaseWriter = AnyDatabaseWriter(writer) + } }