diff --git a/Documentation/CommonTableExpressions.md b/Documentation/CommonTableExpressions.md index 6a025315f..16373db74 100644 --- a/Documentation/CommonTableExpressions.md +++ b/Documentation/CommonTableExpressions.md @@ -362,7 +362,9 @@ And we can fetch the data that feeds our application screen: ```swift -let chatInfos: [ChatInfos] = try dbQueue.read(request.fetchAll) +let chatInfos: [ChatInfos] = try dbQueue.read { db in + try request.fetchAll(db) +} ``` > :bulb: **Tip**: the joining methods are generally type-safe: they won't allow you to join apples to oranges. This works when associations have a *precise* type. In this context, anonymous `CommonTableExpression` CTEs can work against type safety. When you want to define associations between several CTEs, and make sure the compiler will notice wrong uses of those associations, tag your common table expressions with an explicit type: `CommonTableExpression`. diff --git a/Documentation/GRDB5MigrationGuide.md b/Documentation/GRDB5MigrationGuide.md index eca1c7ca7..797d1099f 100644 --- a/Documentation/GRDB5MigrationGuide.md +++ b/Documentation/GRDB5MigrationGuide.md @@ -397,7 +397,9 @@ let publisher = observation // NEW: GRDB 5 let query: SQL = "UPDATE player SET name = \(name) WHERE id = \(id)" - let (sql, arguments) = try dbQueue.read(query.build) + let (sql, arguments) = try dbQueue.read { db in + try query.build(db) + } print(sql) // prints "UPDATE player SET name = ? WHERE id = ?" print(arguments) // prints ["O'Brien", 42] ``` diff --git a/Documentation/SQLInterpolation.md b/Documentation/SQLInterpolation.md index 3a00cce83..2af59d2fb 100644 --- a/Documentation/SQLInterpolation.md +++ b/Documentation/SQLInterpolation.md @@ -112,13 +112,15 @@ let components: [SQL] = [ let query = components.joined(separator: " ") ``` -Extract the plain SQL string from a literal: +To extract the plain SQL string from a literal, you need a `Database` connection such as the one provided by the `read` and `write` methods: ```swift -let query: SQL = "UPDATE player SET name = \(name) WHERE id = \(id)" -let (sql, arguments) = try dbQueue.read(query.build) -print(sql) // prints "UPDATE player SET name = ? WHERE id = ?" -print(arguments) // prints ["O'Brien", 42] +try dbQueue.read { db in + let query: SQL = "UPDATE player SET name = \(name) WHERE id = \(id)" + let (sql, arguments) = try query.build(db) + print(sql) // prints "UPDATE player SET name = ? WHERE id = ?" + print(arguments) // prints ["O'Brien", 42] +} ``` Build a literal from a plain SQL string: diff --git a/GRDB/Core/DatabaseSnapshot.swift b/GRDB/Core/DatabaseSnapshot.swift index 984ab5c63..1381c1871 100644 --- a/GRDB/Core/DatabaseSnapshot.swift +++ b/GRDB/Core/DatabaseSnapshot.swift @@ -59,10 +59,14 @@ import Dispatch /// let snapshot2 = try dbPool.makeSnapshot() /// /// // Guaranteed to be zero -/// let count1 = try snapshot1.read(Player.fetchCount) +/// let count1 = try snapshot1.read { db in +/// try Player.fetchCount(db) +/// } /// /// // Could be anything -/// let count2 = try snapshot2.read(Player.fetchCount) +/// let count2 = try snapshot2.read { db in +/// try Player.fetchCount(db) +/// } /// ``` /// /// `DatabaseSnapshot` inherits its database access methods from the diff --git a/GRDB/Core/DatabaseSnapshotPool.swift b/GRDB/Core/DatabaseSnapshotPool.swift index 8535072b1..c32b0b1a2 100644 --- a/GRDB/Core/DatabaseSnapshotPool.swift +++ b/GRDB/Core/DatabaseSnapshotPool.swift @@ -104,7 +104,9 @@ public final class DatabaseSnapshotPool { /// /// // Later... Maybe some players have been created. /// // The snapshot is guaranteed to see an empty table of players, though: - /// let count = try snapshot.read(Player.fetchCount) + /// let count = try snapshot.read { db in + /// try Player.fetchCount(db) + /// } /// assert(count == 0) /// ``` ///