From 64be4763f2f938ab8a2be29d5d0556c49b98fcbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 11 Oct 2021 17:39:51 +0200 Subject: [PATCH] async DatabaseReader.read --- GRDB/Core/DatabaseReader.swift | 49 +++++++++++++++++++++++ Tests/GRDBTests/DatabaseReaderTests.swift | 40 ++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/GRDB/Core/DatabaseReader.swift b/GRDB/Core/DatabaseReader.swift index 1e0d68a31..f96764eb8 100644 --- a/GRDB/Core/DatabaseReader.swift +++ b/GRDB/Core/DatabaseReader.swift @@ -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(_ 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(_ 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 diff --git a/Tests/GRDBTests/DatabaseReaderTests.swift b/Tests/GRDBTests/DatabaseReaderTests.swift index 00d941c29..edfbdde99 100644 --- a/Tests/GRDBTests/DatabaseReaderTests.swift +++ b/Tests/GRDBTests/DatabaseReaderTests.swift @@ -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(_ 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 {