SE-0299: enhance access to Column.rowID

This commit is contained in:
Gwendal Roué committed 2021-08-22 12:42:53 +02:00
1 parent 14b5eea277
commit 0511fec001
11 files changed
+63 -5

No files matched your search

+1
View File
@@ -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.
@@ -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) }
+7
View File
@@ -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.
@@ -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`
+2 -2
View File
@@ -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
}
+1 -2
View File
@@ -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
@@ -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<Author> { request(for: Book.author) }
}
+8
View File
@@ -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
+8
View File
@@ -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
+9 -1
View File
@@ -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
@@ -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