mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
async DatabaseReader.read
This commit is contained in:
1 parent
20a1c8666e
commit
64be4763f2
2 files changed
+89
No files matched your search
@@ -120,6 +120,7 @@ public protocol DatabaseReader: AnyObject {
|
||||
/// - parameter value: A function that accesses the database.
|
||||
/// - throws: The error thrown by `value`, or any `DatabaseError` that would
|
||||
/// happen while establishing the read access to the database.
|
||||
@_disfavoredOverload // SR-15150 Async overloading in protocol implementation fails
|
||||
func read<T>(_ value: (Database) throws -> T) throws -> T
|
||||
|
||||
/// Asynchronously executes a read-only function that accepts a
|
||||
@@ -265,6 +266,54 @@ extension DatabaseReader {
|
||||
}
|
||||
}
|
||||
|
||||
#if swift(>=5.5)
|
||||
extension DatabaseReader {
|
||||
// MARK: - Asynchronous Database Access
|
||||
|
||||
/// Asynchronously executes a read-only function that accepts a database
|
||||
/// connection, and returns its result.
|
||||
///
|
||||
/// let count = try await reader.read { db in
|
||||
/// try Player.fetchCount(db)
|
||||
/// }
|
||||
///
|
||||
/// The `value` function runs in an isolated fashion: eventual concurrent
|
||||
/// database updates are not visible from the function:
|
||||
///
|
||||
/// try await reader.read { db in
|
||||
/// // Those two values are guaranteed to be equal, even if the
|
||||
/// // `player` table is modified, between the two requests, by
|
||||
/// // some other database connection or some other thread.
|
||||
/// let count1 = try Player.fetchCount(db)
|
||||
/// let count2 = try Player.fetchCount(db)
|
||||
/// }
|
||||
///
|
||||
/// try await reader.read { db in
|
||||
/// // Now this value may be different:
|
||||
/// let count = try Player.fetchCount(db)
|
||||
/// }
|
||||
///
|
||||
/// Attempts to write in the database throw a DatabaseError with
|
||||
/// resultCode `SQLITE_READONLY`.
|
||||
///
|
||||
/// - parameter value: A function that accesses the database.
|
||||
/// - throws: The error thrown by `value`, or any `DatabaseError` that would
|
||||
/// happen while establishing the read access to the database.
|
||||
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
|
||||
public func read<T>(_ value: @escaping (Database) throws -> T) async throws -> T {
|
||||
try await withUnsafeThrowingContinuation { continuation in
|
||||
asyncRead { result in
|
||||
do {
|
||||
try continuation.resume(returning: value(result.get()))
|
||||
} catch {
|
||||
continuation.resume(throwing: error)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if canImport(Combine)
|
||||
extension DatabaseReader {
|
||||
// MARK: - Publishing Database Values
|
||||
|
||||
@@ -30,6 +30,28 @@ class DatabaseReaderTests : GRDBTestCase {
|
||||
try test(setup(makeDatabasePool()).makeSnapshot())
|
||||
}
|
||||
|
||||
#if swift(>=5.5)
|
||||
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
|
||||
func testAsyncAwait_ReadCanRead() async throws {
|
||||
func setup<T: DatabaseWriter>(_ dbWriter: T) throws -> T {
|
||||
try dbWriter.write { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id INTEGER PRIMARY KEY)")
|
||||
}
|
||||
return dbWriter
|
||||
}
|
||||
func test(_ dbReader: DatabaseReader) async throws {
|
||||
let count = try await dbReader.read { db in
|
||||
try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM t")
|
||||
}
|
||||
XCTAssertEqual(count, 0)
|
||||
}
|
||||
|
||||
try await test(setup(makeDatabaseQueue()))
|
||||
try await test(setup(makeDatabasePool()))
|
||||
try await test(setup(makeDatabasePool()).makeSnapshot())
|
||||
}
|
||||
#endif
|
||||
|
||||
func testReadPreventsDatabaseModification() throws {
|
||||
func test(_ dbReader: DatabaseReader) throws {
|
||||
do {
|
||||
@@ -46,6 +68,24 @@ class DatabaseReaderTests : GRDBTestCase {
|
||||
try test(makeDatabasePool().makeSnapshot())
|
||||
}
|
||||
|
||||
#if swift(>=5.5)
|
||||
@available(macOS 12, iOS 15, tvOS 15, watchOS 8, *)
|
||||
func testAsyncAwait_ReadPreventsDatabaseModification() async throws {
|
||||
func test(_ dbReader: DatabaseReader) async throws {
|
||||
do {
|
||||
try await dbReader.read { db in
|
||||
try db.execute(sql: "CREATE TABLE t (id INTEGER PRIMARY KEY)")
|
||||
}
|
||||
XCTFail("Expected error")
|
||||
} catch DatabaseError.SQLITE_READONLY {
|
||||
}
|
||||
}
|
||||
|
||||
try await test(makeDatabaseQueue())
|
||||
try await test(makeDatabasePool())
|
||||
try await test(makeDatabasePool().makeSnapshot())
|
||||
}
|
||||
#endif
|
||||
// MARK: - UnsafeRead
|
||||
|
||||
func testUnsafeReadCanRead() throws {
|
||||
|
||||
Reference in new issue
Block a user