Skip to content

Commit

Permalink
Make all read closures explicit in the documentation
Browse files Browse the repository at this point in the history
The short form dbQueue.read(fetch) can confuse the reader.
  • Loading branch information
groue committed Oct 22, 2023
1 parent fd2e552 commit 8fa4342
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 10 deletions.
4 changes: 3 additions & 1 deletion Documentation/CommonTableExpressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<SomeType>`.
Expand Down
4 changes: 3 additions & 1 deletion Documentation/GRDB5MigrationGuide.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
```
Expand Down
12 changes: 7 additions & 5 deletions Documentation/SQLInterpolation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions GRDB/Core/DatabaseSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion GRDB/Core/DatabaseSnapshotPool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/// ```
///
Expand Down

0 comments on commit 8fa4342

Please sign in to comment.