From 96d04716c22c1745152ed07593a02eab637ceb65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 11 May 2025 10:57:20 +0200 Subject: [PATCH] Update more documentation with the closure syntax --- .../DatabaseSchemaRecommendations.md | 4 ++-- .../Extension/DatabaseRegionObservation.md | 2 +- .../Extension/ValueObservation.md | 2 +- .../RecordRecommendedPractices.md | 18 ++++++++---------- GRDB/Documentation.docc/SwiftConcurrency.md | 10 ++++++++-- GRDB/Record/TableRecord.swift | 14 ++++++++++++-- GRDB/ValueObservation/ValueObservation.swift | 6 +++--- 7 files changed, 35 insertions(+), 21 deletions(-) diff --git a/GRDB/Documentation.docc/DatabaseSchemaRecommendations.md b/GRDB/Documentation.docc/DatabaseSchemaRecommendations.md index cb92143bb..7aabe6429 100644 --- a/GRDB/Documentation.docc/DatabaseSchemaRecommendations.md +++ b/GRDB/Documentation.docc/DatabaseSchemaRecommendations.md @@ -262,11 +262,11 @@ try dbQueue.write { db in // Use instead: let player = try Player - .filter(Column("teamId") == 42 && Column("position") == 1) + .filter { $0.teamId == 42 && $0.position == 1 } .fetchOne(db) try Player - .filter(Column("name") == "Arthur") + .filter { $0.name == "Arthur" } .deleteAll(db) } ``` diff --git a/GRDB/Documentation.docc/Extension/DatabaseRegionObservation.md b/GRDB/Documentation.docc/Extension/DatabaseRegionObservation.md index 9bfee067f..172cc169b 100644 --- a/GRDB/Documentation.docc/Extension/DatabaseRegionObservation.md +++ b/GRDB/Documentation.docc/Extension/DatabaseRegionObservation.md @@ -59,7 +59,7 @@ You can feed `DatabaseRegionObservation` with any type that conforms to the ``Da ```swift // Observe the score column of the 'player' table let observation = DatabaseRegionObservation( - tracking: Player.select(Column("score"))) + tracking: Player.select(\.score)) // Observe the 'score' column of the 'player' table let observation = DatabaseRegionObservation( diff --git a/GRDB/Documentation.docc/Extension/ValueObservation.md b/GRDB/Documentation.docc/Extension/ValueObservation.md index e0abef0ce..26215e931 100644 --- a/GRDB/Documentation.docc/Extension/ValueObservation.md +++ b/GRDB/Documentation.docc/Extension/ValueObservation.md @@ -214,7 +214,7 @@ Consider a scenario where you'd like to get a specific Player's row, but only wh let observation = ValueObservation.tracking( // Define the tracked database region // (the score column of the player with id 1) - region: Player.select(Column("score")).filter(id: 1), + region: Player.select(\.score).filter(id: 1), // Define what to fetch upon such change to the tracked region // (the player with id 1) fetch: { db in try Player.fetchOne(db, id: 1) } diff --git a/GRDB/Documentation.docc/RecordRecommendedPractices.md b/GRDB/Documentation.docc/RecordRecommendedPractices.md index 88549016f..30033cf2f 100644 --- a/GRDB/Documentation.docc/RecordRecommendedPractices.md +++ b/GRDB/Documentation.docc/RecordRecommendedPractices.md @@ -214,7 +214,7 @@ extension Book.Kind: DatabaseValueConvertible { } // Fetch all novels let novels = try dbQueue.read { db in - try Book.filter(Column("kind") == Book.Kind.novel).fetchAll(db) + try Book.filter { $0.kind == Book.Kind.novel }.fetchAll(db) } ``` @@ -330,12 +330,12 @@ try dbQueue.read { db in // Fetch all authors, ordered by name, // in a localized case-insensitive fashion let sortedAuthors: [Author] = try Author.all() - .order(Author.Columns.name.collating(.localizedCaseInsensitiveCompare)) + .order { $0.name.collating(.localizedCaseInsensitiveCompare) } .fetchAll(db) // Count French authors let frenchAuthorCount: Int = try Author.all() - .filter(Author.Columns.countryCode == "FR") + .filter { $0.countryCode == "FR" } .fetchCount(db) } ``` @@ -351,13 +351,12 @@ Define those methods in extensions of the ``DerivableRequest`` protocol, as belo extension DerivableRequest { /// Order authors by name, in a localized case-insensitive fashion func orderByName() -> Self { - let name = Author.Columns.name - return order(name.collating(.localizedCaseInsensitiveCompare)) + order { $0.name.collating(.localizedCaseInsensitiveCompare) } } /// Filters authors from a country func filter(countryCode: String) -> Self { - filter(Author.Columns.countryCode == countryCode) + filter { $0.countryCode == countryCode } } } @@ -365,13 +364,12 @@ extension DerivableRequest { extension DerivableRequest { /// Order books by title, in a localized case-insensitive fashion func orderByTitle() -> Self { - let title = Book.Columns.title - return order(title.collating(.localizedCaseInsensitiveCompare)) + order { $0.title.collating(.localizedCaseInsensitiveCompare) } } /// Filters books by kind func filter(kind: Book.Kind) -> Self { - filter(Book.Columns.kind == kind) + filter { $0.kind == kind } } } ``` @@ -397,7 +395,7 @@ Extensions to the `DerivableRequest` protocol can not change the type of request extension QueryInterfaceRequest { // Selects authors' name func selectName() -> QueryInterfaceRequest { - select(Author.Columns.name) + select { $0.name } } } diff --git a/GRDB/Documentation.docc/SwiftConcurrency.md b/GRDB/Documentation.docc/SwiftConcurrency.md index f16a00ef8..4fcfc15fa 100644 --- a/GRDB/Documentation.docc/SwiftConcurrency.md +++ b/GRDB/Documentation.docc/SwiftConcurrency.md @@ -91,8 +91,14 @@ extension Player: FetchableRecord, PersistableRecord { // because non-'Sendable' type '[any SQLSelectable]' // may have shared mutable state static let databaseSelection: [any SQLSelectable] = [ - Column("id"), Column("name"), Column("score") + Columns.id, Columns.name, Columns.score ] + + enum Columns { + static let id = Column("id") + static let name = Column("name") + static let score = Column("score") + } } ``` @@ -101,7 +107,7 @@ extension Player: FetchableRecord, PersistableRecord { ```swift extension Player: FetchableRecord, PersistableRecord { static var databaseSelection: [any SQLSelectable] { - [Column("id"), Column("name"), Column("score")] + [Columns.id, Columns.name, Columns.score] } } ``` diff --git a/GRDB/Record/TableRecord.swift b/GRDB/Record/TableRecord.swift index 988d04cc9..47d2d7323 100644 --- a/GRDB/Record/TableRecord.swift +++ b/GRDB/Record/TableRecord.swift @@ -217,7 +217,12 @@ public protocol TableRecord { /// struct PartialPlayer: TableRecord { /// static let databaseTableName = "player" /// static var databaseSelection: [any SQLSelectable] { - /// [Column("id"), Column("name")] + /// [Columns.id, Columns.name] + /// } + /// + /// enum Columns { + /// static let id = Column("id") + /// static let name = Column("name") /// } /// } /// @@ -372,7 +377,12 @@ extension TableRecord { /// struct PartialPlayer: TableRecord { /// static let databaseTableName = "player" /// static var databaseSelection: [any SQLSelectable] { - /// [Column("id"), Column("name")] + /// [Columns.id, Columns.name] + /// } + /// + /// enum Columns { + /// static let id = Column("id") + /// static let name = Column("name") /// } /// } /// diff --git a/GRDB/ValueObservation/ValueObservation.swift b/GRDB/ValueObservation/ValueObservation.swift index 16467c6dd..92cfe9864 100644 --- a/GRDB/ValueObservation/ValueObservation.swift +++ b/GRDB/ValueObservation/ValueObservation.swift @@ -820,7 +820,7 @@ extension ValueObservation { /// /// // Tracks the 'score' column in the 'player' table /// let observation = ValueObservation.tracking( - /// region: Player.select(Column("score"), + /// region: Player.select(\.score), /// fetch: { db in ... }) /// /// // Tracks the 'score' column in the 'player' table @@ -892,7 +892,7 @@ extension ValueObservation { /// /// // Tracks the 'score' column in the 'player' table /// let observation = ValueObservation.tracking( - /// regions: [Player.select(Column("score")], + /// regions: [Player.select(\.score)], /// fetch: { db in ... }) /// /// // Tracks the 'score' column in the 'player' table @@ -963,7 +963,7 @@ extension ValueObservation { /// let totalPlayerCount = try Player.fetchCount(db) /// /// let bestPlayers = try Player - /// .order(Column("score").desc) + /// .order(\.score.desc) /// .limit(10) /// .fetchAll(db) ///