diff --git a/Documentation/AssociationsBasics.md b/Documentation/AssociationsBasics.md index 15cf3657d..6d38c60c9 100644 --- a/Documentation/AssociationsBasics.md +++ b/Documentation/AssociationsBasics.md @@ -751,8 +751,9 @@ Foreign keys can also be defined from query interface columns: ```swift struct Book: TableRecord { - enum Columns: String, ColumnExpression { - case id, title, authorId, translatorId + enum Columns { + static let authorId = Column("authorId") + static let translatorId = Column("translatorId") } static let authorForeignKey = ForeignKey([Columns.authorId]) diff --git a/GRDB/Documentation.docc/RecordRecommendedPractices.md b/GRDB/Documentation.docc/RecordRecommendedPractices.md index 44a0d345e..88549016f 100644 --- a/GRDB/Documentation.docc/RecordRecommendedPractices.md +++ b/GRDB/Documentation.docc/RecordRecommendedPractices.md @@ -297,7 +297,7 @@ Once we have record types that are able to read and write in the database, we'd ### Columns -Requests that filter or sort records are defined with **columns**, defined in a dedicated enumeration. When the record type conforms to [`Codable`], columns can be derived from the `CodingKeys` enum: +Requests that filter or sort records are defined with **columns**, defined in a dedicated enumeration, with the name `Columns`, nested inside the record type. When the record type conforms to [`Codable`], columns can be derived from the `CodingKeys` enum: ```swift // HOW TO define columns for a Codable record @@ -310,13 +310,15 @@ extension Author { } ``` -For other record types, declare a plain `String` enum that conforms to the ``ColumnExpression`` protocol: +For non-Codable record types, declare columns with their names: ```swift // HOW TO define columns for a non-Codable record extension Author { - enum Columns: String, ColumnExpression { - case id, name, countryCode + enum Columns { + static let id = Column("id") + static let name = Column("name") + static let countryCode = Column("countryCode") } } ``` diff --git a/GRDB/JSON/JSONColumn.swift b/GRDB/JSON/JSONColumn.swift index 8f778d2b5..5a9b86080 100644 --- a/GRDB/JSON/JSONColumn.swift +++ b/GRDB/JSON/JSONColumn.swift @@ -59,8 +59,10 @@ /// > extension Player: FetchableRecord, PersistableRecord { /// > // That's another valid way to define columns. /// > // But we don't have any JSONColumn this time. -/// > enum Columns: String, ColumnExpression { -/// > case id, name, address +/// > enum Columns { +/// > static let id = Column("id") +/// > static let name = Column("name") +/// > static let address = Column("address") /// > } /// > } /// > diff --git a/GRDB/QueryInterface/ForeignKey.swift b/GRDB/QueryInterface/ForeignKey.swift index fbe27461d..5ac9e3a52 100644 --- a/GRDB/QueryInterface/ForeignKey.swift +++ b/GRDB/QueryInterface/ForeignKey.swift @@ -45,8 +45,9 @@ /// /// ```swift /// struct Book: TableRecord { -/// enum Columns: String, ColumnExpression { -/// case id, title, authorId, translatorId +/// enum Columns { +/// static let authorId = Column("authorId") +/// static let translatorId = Column("translatorId") /// } /// /// static let authorForeignKey = ForeignKey([Columns.authorId])) diff --git a/GRDB/QueryInterface/SQL/Column.swift b/GRDB/QueryInterface/SQL/Column.swift index cef9bcbe6..766e2d7c5 100644 --- a/GRDB/QueryInterface/SQL/Column.swift +++ b/GRDB/QueryInterface/SQL/Column.swift @@ -123,11 +123,13 @@ extension Column: ColumnExpression { } /// Support for column enums: /// -/// struct Player { -/// enum Columns: String, ColumnExpression { -/// case id, name, score -/// } +/// ```swift +/// struct Player { +/// enum Columns: String, ColumnExpression { +/// case id, name, score /// } +/// } +/// ``` extension ColumnExpression where Self: RawRepresentable, Self.RawValue == String { public var name: String { rawValue } } diff --git a/README.md b/README.md index 4ab3180ce..c04d8acfc 100644 --- a/README.md +++ b/README.md @@ -1904,8 +1904,11 @@ Rows also accept column enums: ```swift extension Place : FetchableRecord { - enum Columns: String, ColumnExpression { - case id, title, latitude, longitude + enum Columns { + static let id = Column("id") + static let title = Column("title") + static let latitude = Column("latitude") + static let longitude = Column("longitude") } init(row: Row) { @@ -2070,8 +2073,11 @@ Persistence containers also accept column enums: ```swift extension Place : MutablePersistableRecord { - enum Columns: String, ColumnExpression { - case id, title, latitude, longitude + enum Columns { + static let id = Column("id") + static let title = Column("title") + static let latitude = Column("latitude") + static let longitude = Column("longitude") } func encode(to container: inout PersistenceContainer) { @@ -3070,8 +3076,12 @@ struct Place { // SQL generation extension Place: TableRecord { /// The table columns - enum Columns: String, ColumnExpression { - case id, title, isFavorite, latitude, longitude + enum Columns { + static let id = Column("id") + static let title = Column("title") + static let isFavorite = Column("isFavorite") + static let latitude = Column("latitude") + static let longitude = Column("longitude") } } @@ -3305,21 +3315,25 @@ let players = try request.fetchAll(db) // [Player] > **Note**: all examples in the documentation below use a record type, but you can always substitute a `Table` instead. -Next, declare the table **columns** that you want to use for filtering, or sorting: +Next, declare the table **columns** that you want to use for filtering, or sorting, in a nested type named `Columns`: ```swift -let idColumn = Column("id") -let nameColumn = Column("name") +extension Player { + enum Columns { + static let id = Column("id") + static let name = Column("name") + } +} ``` -You can also declare column enums, if you prefer: +If `Player` is `Codable`, you'll prefer defining columns from coding keys: ```swift -// Columns.id and Columns.name can be used just as -// idColumn and nameColumn declared above. -enum Columns: String, ColumnExpression { - case id - case name +extension Player { + enum Columns { + static let id = Column(CodingKeys.id) + static let name = Column(CodingKeys.name) + } } ```