Introduce AnyDatabaseReader and AnyDatabaseWriter

This commit is contained in:
Gwendal Roué committed 2017-09-10 13:37:48 +02:00
1 parent 2f2add1c49
commit 3fce1e00c9
6 files changed
+122 -1

No files matched your search

+9
View File
@@ -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
+41
View File
@@ -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<T>(_ block: (Database) throws -> T) throws -> T {
return try base.read(block)
}
public func unsafeRead<T>(_ block: (Database) throws -> T) throws -> T {
return try base.unsafeRead(block)
}
public func unsafeReentrantRead<T>(_ 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)
}
}
+53
View File
@@ -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<T>(_ block: (Database) throws -> T) throws -> T {
return try base.read(block)
}
public func unsafeRead<T>(_ block: (Database) throws -> T) throws -> T {
return try base.unsafeRead(block)
}
public func unsafeReentrantRead<T>(_ 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<T>(_ block: (Database) throws -> T) rethrows -> T {
return try base.write(block)
}
public func unsafeReentrantWrite<T>(_ block: (Database) throws -> T) rethrows -> T {
return try base.unsafeReentrantWrite(block)
}
public func readFromCurrentState(_ block: @escaping (Database) -> Void) throws {
try base.readFromCurrentState(block)
}
}
+7 -1
View File
@@ -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
@@ -68,4 +68,10 @@ class DatabaseReaderTests : GRDBTestCase {
}
}
}
func testAnyDatabaseReader() {
// This test passes if this code compiles.
let reader: DatabaseReader = DatabaseQueue()
let _: DatabaseReader = AnyDatabaseReader(reader)
}
}
@@ -44,4 +44,10 @@ class DatabaseWriterTests : GRDBTestCase {
}
}
}
func testAnyDatabaseWriter() {
// This test passes if this code compiles.
let writer: DatabaseWriter = DatabaseQueue()
let _: DatabaseWriter = AnyDatabaseWriter(writer)
}
}