Skip to content

Commit 96d0471

Browse files
committed
Update more documentation with the closure syntax
1 parent 0049685 commit 96d0471

File tree

7 files changed

+35
-21
lines changed

7 files changed

+35
-21
lines changed

GRDB/Documentation.docc/DatabaseSchemaRecommendations.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@ try dbQueue.write { db in
262262
263263
// Use instead:
264264
let player = try Player
265-
.filter(Column("teamId") == 42 && Column("position") == 1)
265+
.filter { $0.teamId == 42 && $0.position == 1 }
266266
.fetchOne(db)
267267
268268
try Player
269-
.filter(Column("name") == "Arthur")
269+
.filter { $0.name == "Arthur" }
270270
.deleteAll(db)
271271
}
272272
```

GRDB/Documentation.docc/Extension/DatabaseRegionObservation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ You can feed `DatabaseRegionObservation` with any type that conforms to the ``Da
5959
```swift
6060
// Observe the score column of the 'player' table
6161
let observation = DatabaseRegionObservation(
62-
tracking: Player.select(Column("score")))
62+
tracking: Player.select(\.score))
6363

6464
// Observe the 'score' column of the 'player' table
6565
let observation = DatabaseRegionObservation(

GRDB/Documentation.docc/Extension/ValueObservation.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ Consider a scenario where you'd like to get a specific Player's row, but only wh
214214
let observation = ValueObservation.tracking(
215215
// Define the tracked database region
216216
// (the score column of the player with id 1)
217-
region: Player.select(Column("score")).filter(id: 1),
217+
region: Player.select(\.score).filter(id: 1),
218218
// Define what to fetch upon such change to the tracked region
219219
// (the player with id 1)
220220
fetch: { db in try Player.fetchOne(db, id: 1) }

GRDB/Documentation.docc/RecordRecommendedPractices.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ extension Book.Kind: DatabaseValueConvertible { }
214214
215215
// Fetch all novels
216216
let novels = try dbQueue.read { db in
217-
try Book.filter(Column("kind") == Book.Kind.novel).fetchAll(db)
217+
try Book.filter { $0.kind == Book.Kind.novel }.fetchAll(db)
218218
}
219219
```
220220
@@ -330,12 +330,12 @@ try dbQueue.read { db in
330330
// Fetch all authors, ordered by name,
331331
// in a localized case-insensitive fashion
332332
let sortedAuthors: [Author] = try Author.all()
333-
.order(Author.Columns.name.collating(.localizedCaseInsensitiveCompare))
333+
.order { $0.name.collating(.localizedCaseInsensitiveCompare) }
334334
.fetchAll(db)
335335
336336
// Count French authors
337337
let frenchAuthorCount: Int = try Author.all()
338-
.filter(Author.Columns.countryCode == "FR")
338+
.filter { $0.countryCode == "FR" }
339339
.fetchCount(db)
340340
}
341341
```
@@ -351,27 +351,25 @@ Define those methods in extensions of the ``DerivableRequest`` protocol, as belo
351351
extension DerivableRequest<Author> {
352352
/// Order authors by name, in a localized case-insensitive fashion
353353
func orderByName() -> Self {
354-
let name = Author.Columns.name
355-
return order(name.collating(.localizedCaseInsensitiveCompare))
354+
order { $0.name.collating(.localizedCaseInsensitiveCompare) }
356355
}
357356
358357
/// Filters authors from a country
359358
func filter(countryCode: String) -> Self {
360-
filter(Author.Columns.countryCode == countryCode)
359+
filter { $0.countryCode == countryCode }
361360
}
362361
}
363362
364363
// Book requests
365364
extension DerivableRequest<Book> {
366365
/// Order books by title, in a localized case-insensitive fashion
367366
func orderByTitle() -> Self {
368-
let title = Book.Columns.title
369-
return order(title.collating(.localizedCaseInsensitiveCompare))
367+
order { $0.title.collating(.localizedCaseInsensitiveCompare) }
370368
}
371369
372370
/// Filters books by kind
373371
func filter(kind: Book.Kind) -> Self {
374-
filter(Book.Columns.kind == kind)
372+
filter { $0.kind == kind }
375373
}
376374
}
377375
```
@@ -397,7 +395,7 @@ Extensions to the `DerivableRequest` protocol can not change the type of request
397395
extension QueryInterfaceRequest<Author> {
398396
// Selects authors' name
399397
func selectName() -> QueryInterfaceRequest<String> {
400-
select(Author.Columns.name)
398+
select { $0.name }
401399
}
402400
}
403401

GRDB/Documentation.docc/SwiftConcurrency.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,14 @@ extension Player: FetchableRecord, PersistableRecord {
9191
// because non-'Sendable' type '[any SQLSelectable]'
9292
// may have shared mutable state
9393
static let databaseSelection: [any SQLSelectable] = [
94-
Column("id"), Column("name"), Column("score")
94+
Columns.id, Columns.name, Columns.score
9595
]
96+
97+
enum Columns {
98+
static let id = Column("id")
99+
static let name = Column("name")
100+
static let score = Column("score")
101+
}
96102
}
97103
```
98104

@@ -101,7 +107,7 @@ extension Player: FetchableRecord, PersistableRecord {
101107
```swift
102108
extension Player: FetchableRecord, PersistableRecord {
103109
static var databaseSelection: [any SQLSelectable] {
104-
[Column("id"), Column("name"), Column("score")]
110+
[Columns.id, Columns.name, Columns.score]
105111
}
106112
}
107113
```

GRDB/Record/TableRecord.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,12 @@ public protocol TableRecord {
217217
/// struct PartialPlayer: TableRecord {
218218
/// static let databaseTableName = "player"
219219
/// static var databaseSelection: [any SQLSelectable] {
220-
/// [Column("id"), Column("name")]
220+
/// [Columns.id, Columns.name]
221+
/// }
222+
///
223+
/// enum Columns {
224+
/// static let id = Column("id")
225+
/// static let name = Column("name")
221226
/// }
222227
/// }
223228
///
@@ -372,7 +377,12 @@ extension TableRecord {
372377
/// struct PartialPlayer: TableRecord {
373378
/// static let databaseTableName = "player"
374379
/// static var databaseSelection: [any SQLSelectable] {
375-
/// [Column("id"), Column("name")]
380+
/// [Columns.id, Columns.name]
381+
/// }
382+
///
383+
/// enum Columns {
384+
/// static let id = Column("id")
385+
/// static let name = Column("name")
376386
/// }
377387
/// }
378388
///

GRDB/ValueObservation/ValueObservation.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ extension ValueObservation {
820820
///
821821
/// // Tracks the 'score' column in the 'player' table
822822
/// let observation = ValueObservation.tracking(
823-
/// region: Player.select(Column("score"),
823+
/// region: Player.select(\.score),
824824
/// fetch: { db in ... })
825825
///
826826
/// // Tracks the 'score' column in the 'player' table
@@ -892,7 +892,7 @@ extension ValueObservation {
892892
///
893893
/// // Tracks the 'score' column in the 'player' table
894894
/// let observation = ValueObservation.tracking(
895-
/// regions: [Player.select(Column("score")],
895+
/// regions: [Player.select(\.score)],
896896
/// fetch: { db in ... })
897897
///
898898
/// // Tracks the 'score' column in the 'player' table
@@ -963,7 +963,7 @@ extension ValueObservation {
963963
/// let totalPlayerCount = try Player.fetchCount(db)
964964
///
965965
/// let bestPlayers = try Player
966-
/// .order(Column("score").desc)
966+
/// .order(\.score.desc)
967967
/// .limit(10)
968968
/// .fetchAll(db)
969969
///

0 commit comments

Comments
 (0)