diff --git a/CHANGELOG.md b/CHANGELOG.md index e1cbbea66..9850633f2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -82,6 +82,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - **New**: [#1036](https://github.com/groue/GRDB.swift/pull/1036) by [@groue](https://github.com/groue): Restore minimal CI tests - **New**: [#1037](https://github.com/groue/GRDB.swift/pull/1037) by [@groue](https://github.com/groue): Support BUILD_LIBRARY_FOR_DISTRIBUTION - **New**: [#1038](https://github.com/groue/GRDB.swift/pull/1038) by [@GetToSet](https://github.com/GetToSet): Add match operator in ColumnExpression for FTS5 +- **New**: With Swift 5.5+, enhance access to `Column.rowID`. Row and PersistenceContainer subscripts now accept `row[.rowID]` and `container[.rowID]` ([SE-0299](https://github.com/apple/swift-evolution/blob/main/proposals/0299-extend-generic-static-member-lookup.md)) - **Documentation Update**: A new [Combine and Data Consistency](Documentation/Combine.md#combine-and-data-consistency) chapter explains how to make sure database publishers do not break important database invariants. diff --git a/GRDB/QueryInterface/Request/RequestProtocols.swift b/GRDB/QueryInterface/Request/RequestProtocols.swift index bbfe97130..39bafad3e 100644 --- a/GRDB/QueryInterface/Request/RequestProtocols.swift +++ b/GRDB/QueryInterface/Request/RequestProtocols.swift @@ -405,7 +405,11 @@ extension TableRequest where Self: AggregatingRequest { return [Column(rowIDColumn)] } else if primaryKey.tableHasRowID { // Prefer the rowid + #if compiler(>=5.5) + return [.rowID] + #else return [Column.rowID] + #endif } else { // WITHOUT ROWID table: group by primary key columns return primaryKey.columns.map { Column($0) } diff --git a/GRDB/QueryInterface/SQL/Column.swift b/GRDB/QueryInterface/SQL/Column.swift index 2a658eb44..d6d1299fb 100644 --- a/GRDB/QueryInterface/SQL/Column.swift +++ b/GRDB/QueryInterface/SQL/Column.swift @@ -30,6 +30,13 @@ extension ColumnExpression { } } +#if compiler(>=5.5) +extension ColumnExpression where Self == Column { + /// The hidden rowID column + public static var rowID: Self { Column.rowID } +} +#endif + /// A column in a database table. /// /// When you need to introduce your own column type, don't wrap a Column. diff --git a/GRDB/QueryInterface/SQL/SQLExpression.swift b/GRDB/QueryInterface/SQL/SQLExpression.swift index e76711e5f..99d0632b2 100644 --- a/GRDB/QueryInterface/SQL/SQLExpression.swift +++ b/GRDB/QueryInterface/SQL/SQLExpression.swift @@ -1565,6 +1565,13 @@ public protocol SQLExpressible { var sqlExpression: SQLExpression { get } } +#if compiler(>=5.5) +extension SQLExpressible where Self == Column { + /// The hidden rowID column + public static var rowID: Self { Column.rowID } +} +#endif + /// `SQLSpecificExpressible` is a protocol for all database-specific types that /// can be turned into an SQL expression. Types whose existence is not purely /// dedicated to the database should adopt the `SQLExpressible` diff --git a/README.md b/README.md index 5f55af51d..c3627c5d1 100644 --- a/README.md +++ b/README.md @@ -3581,7 +3581,7 @@ When SQLite won't let you provide an explicit primary key (as in [full-text](Doc var id: Int64? init(row: Row) { - id = row[Column.rowID] + id = row[Column.rowID] // or `row[.rowID]` with Swift 5.5+ } } ``` @@ -3600,7 +3600,7 @@ When SQLite won't let you provide an explicit primary key (as in [full-text](Doc var id: Int64? func encode(to container: inout PersistenceContainer) { - container[Column.rowID] = id + container[Column.rowID] = id // or `container[.rowID]` with Swift 5.5+ container["message"] = message container["date"] = date } diff --git a/Support/GRDBDeploymentTarget.xcconfig b/Support/GRDBDeploymentTarget.xcconfig index a88f9e56d..2247751b2 100644 --- a/Support/GRDBDeploymentTarget.xcconfig +++ b/Support/GRDBDeploymentTarget.xcconfig @@ -4,5 +4,4 @@ TVOS_DEPLOYMENT_TARGET = 9.0 WATCHOS_DEPLOYMENT_TARGET = 2.0 //// Compile with all opt-in APIs -//GCC_PREPROCESSOR_DEFINITIONS = $(inherited) GRDB_SQLITE_ENABLE_PREUPDATE_HOOK=1 -//OTHER_SWIFT_FLAGS = $(inherited) -D SQLITE_ENABLE_FTS5 -D SQLITE_ENABLE_PREUPDATE_HOOK +OTHER_SWIFT_FLAGS = $(inherited) -D SQLITE_ENABLE_FTS5 diff --git a/Tests/GRDBTests/DerivableRequestTests.swift b/Tests/GRDBTests/DerivableRequestTests.swift index b23ef6f3a..7d9c96cc2 100644 --- a/Tests/GRDBTests/DerivableRequestTests.swift +++ b/Tests/GRDBTests/DerivableRequestTests.swift @@ -25,10 +25,18 @@ private struct Book: FetchableRecord, PersistableRecord, Codable { static let databaseTableName = "book" static let author = belongsTo(Author.self) + #if compiler(>=5.5) + static let bookFts4 = hasOne(BookFts4.self, using: ForeignKey([.rowID])) + #else static let bookFts4 = hasOne(BookFts4.self, using: ForeignKey([Column.rowID])) + #endif #if SQLITE_ENABLE_FTS5 + #if compiler(>=5.5) + static let bookFts5 = hasOne(BookFts5.self, using: ForeignKey([.rowID])) + #else static let bookFts5 = hasOne(BookFts5.self, using: ForeignKey([Column.rowID])) #endif + #endif var author: QueryInterfaceRequest { request(for: Book.author) } } diff --git a/Tests/GRDBTests/FTS3RecordTests.swift b/Tests/GRDBTests/FTS3RecordTests.swift index 21aade5a6..d7f3ff400 100644 --- a/Tests/GRDBTests/FTS3RecordTests.swift +++ b/Tests/GRDBTests/FTS3RecordTests.swift @@ -10,7 +10,11 @@ private struct Book { extension Book : FetchableRecord { init(row: Row) { + #if compiler(>=5.5) + id = row[.rowID] + #else id = row[Column.rowID] + #endif title = row["title"] author = row["author"] body = row["body"] @@ -22,7 +26,11 @@ extension Book : MutablePersistableRecord { static let databaseSelection: [SQLSelectable] = [AllColumns(), Column.rowID] func encode(to container: inout PersistenceContainer) { + #if compiler(>=5.5) + container[.rowID] = id + #else container[Column.rowID] = id + #endif container["title"] = title container["author"] = author container["body"] = body diff --git a/Tests/GRDBTests/FTS4RecordTests.swift b/Tests/GRDBTests/FTS4RecordTests.swift index d6b00cda1..8f322063b 100644 --- a/Tests/GRDBTests/FTS4RecordTests.swift +++ b/Tests/GRDBTests/FTS4RecordTests.swift @@ -10,7 +10,11 @@ private struct Book { extension Book : FetchableRecord { init(row: Row) { + #if compiler(>=5.5) + id = row[.rowID] + #else id = row[Column.rowID] + #endif title = row["title"] author = row["author"] body = row["body"] @@ -22,7 +26,11 @@ extension Book : MutablePersistableRecord { static let databaseSelection: [SQLSelectable] = [AllColumns(), Column.rowID] func encode(to container: inout PersistenceContainer) { + #if compiler(>=5.5) + container[.rowID] = id + #else container[Column.rowID] = id + #endif container["title"] = title container["author"] = author container["body"] = body diff --git a/Tests/GRDBTests/FTS5RecordTests.swift b/Tests/GRDBTests/FTS5RecordTests.swift index fd1fcd26c..2e23f03f4 100644 --- a/Tests/GRDBTests/FTS5RecordTests.swift +++ b/Tests/GRDBTests/FTS5RecordTests.swift @@ -11,7 +11,11 @@ private struct Book { extension Book : FetchableRecord { init(row: Row) { + #if compiler(>=5.5) + id = row[.rowID] + #else id = row[Column.rowID] + #endif title = row["title"] author = row["author"] body = row["body"] @@ -21,9 +25,13 @@ extension Book : FetchableRecord { extension Book : MutablePersistableRecord { static let databaseTableName = "books" static let databaseSelection: [SQLSelectable] = [AllColumns(), Column.rowID] - + func encode(to container: inout PersistenceContainer) { + #if compiler(>=5.5) + container[.rowID] = id + #else container[Column.rowID] = id + #endif container["title"] = title container["author"] = author container["body"] = body diff --git a/Tests/GRDBTests/RecordPrimaryKeyHiddenRowIDTests.swift b/Tests/GRDBTests/RecordPrimaryKeyHiddenRowIDTests.swift index 3dbd97bca..a17f5b45a 100644 --- a/Tests/GRDBTests/RecordPrimaryKeyHiddenRowIDTests.swift +++ b/Tests/GRDBTests/RecordPrimaryKeyHiddenRowIDTests.swift @@ -36,7 +36,11 @@ private class Person : Record, Hashable { } required init(row: Row) { + #if compiler(>=5.5) + id = row[.rowID] + #else id = row[Column.rowID] + #endif age = row["age"] name = row["name"] creationDate = row["creationDate"] @@ -44,7 +48,11 @@ private class Person : Record, Hashable { } override func encode(to container: inout PersistenceContainer) { + #if compiler(>=5.5) + container[.rowID] = id + #else container[Column.rowID] = id + #endif container["name"] = name container["age"] = age container["creationDate"] = creationDate