mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
StrictConcurrency: TableRecord.databaseSelection must be declared as a computed property
An alternative would have been to make SQLSelectable conform to Sendable. But this makes it harder for users to provide SQLSelectable types.
This commit is contained in:
1 parent
1291bd7dea
commit
24d232aafd
18 files changed
+90
-49
No files matched your search
@@ -0,0 +1,6 @@
|
|||||||
|
`TableRecord.databaseSelection` should be declared as a computed static property:
|
||||||
|
|
||||||
|
```diff
|
||||||
|
-static met databaseSelection: [any SQLSelectable] = [AllColumns(), Column.rowID]
|
||||||
|
+static var databaseSelection: [any SQLSelectable] { [AllColumns(), Column.rowID] }
|
||||||
|
```
|
||||||
@@ -395,7 +395,9 @@ This chapter lists all kinds of supported interpolations.
|
|||||||
|
|
||||||
struct AltPlayer: TableRecord {
|
struct AltPlayer: TableRecord {
|
||||||
static let databaseTableName = "player"
|
static let databaseTableName = "player"
|
||||||
static let databaseSelection: [any SQLSelectable] = [Column("id"), Column("name")]
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
|
[Column("id"), Column("name")]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// SELECT player.id, player.name FROM player
|
// SELECT player.id, player.name FROM player
|
||||||
|
|||||||
@@ -170,9 +170,9 @@ struct Player: Codable {
|
|||||||
extension Player: FetchableRecord, MutablePersistableRecord {
|
extension Player: FetchableRecord, MutablePersistableRecord {
|
||||||
// Required because the primary key
|
// Required because the primary key
|
||||||
// is the hidden rowid column.
|
// is the hidden rowid column.
|
||||||
static let databaseSelection: [any SQLSelectable] = [
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
AllColumns(),
|
[AllColumns(), Column.rowID]
|
||||||
Column.rowID]
|
}
|
||||||
|
|
||||||
// Update id upon successful insertion
|
// Update id upon successful insertion
|
||||||
mutating func didInsert(_ inserted: InsertionSuccess) {
|
mutating func didInsert(_ inserted: InsertionSuccess) {
|
||||||
|
|||||||
@@ -134,15 +134,16 @@ public protocol TableRecord {
|
|||||||
///
|
///
|
||||||
/// ```swift
|
/// ```swift
|
||||||
/// struct Player: TableRecord {
|
/// struct Player: TableRecord {
|
||||||
/// static let databaseSelection: [any SQLSelectable] = [AllColumns()]
|
/// static var databaseSelection: [any SQLSelectable] {
|
||||||
|
/// [AllColumns()]
|
||||||
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// struct PartialPlayer: TableRecord {
|
/// struct PartialPlayer: TableRecord {
|
||||||
/// static let databaseTableName = "player"
|
/// static let databaseTableName = "player"
|
||||||
/// static let databaseSelection: [any SQLSelectable] = [
|
/// static var databaseSelection: [any SQLSelectable] {
|
||||||
/// Column("id"),
|
/// [Column("id"), Column("name")]
|
||||||
/// Column("name"),
|
/// }
|
||||||
/// ]
|
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// // SELECT * FROM player
|
/// // SELECT * FROM player
|
||||||
@@ -156,6 +157,19 @@ public protocol TableRecord {
|
|||||||
/// > explicitly declared as `[any SQLSelectable]`. If it is not, the
|
/// > explicitly declared as `[any SQLSelectable]`. If it is not, the
|
||||||
/// > Swift compiler may silently miss the protocol requirement,
|
/// > Swift compiler may silently miss the protocol requirement,
|
||||||
/// > resulting in sticky `SELECT *` requests.
|
/// > resulting in sticky `SELECT *` requests.
|
||||||
|
///
|
||||||
|
/// > Important: Make sure the property is declared as a computed
|
||||||
|
/// > property (`static var`), instead of a stored property
|
||||||
|
/// > (`static let`). Computed properties avoid a compiler diagnostic
|
||||||
|
/// > with stored properties:
|
||||||
|
/// >
|
||||||
|
/// > ```swift
|
||||||
|
/// > // static property 'databaseSelection' is not
|
||||||
|
/// > // concurrency-safe because non-'Sendable' type
|
||||||
|
/// > // '[any SQLSelectable]' may have shared
|
||||||
|
/// > // mutable state.
|
||||||
|
/// > static let databaseSelection: [any SQLSelectable] = [AllColumns()]
|
||||||
|
/// > ```
|
||||||
static var databaseSelection: [any SQLSelectable] { get }
|
static var databaseSelection: [any SQLSelectable] { get }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -243,7 +257,9 @@ extension TableRecord {
|
|||||||
///
|
///
|
||||||
/// struct PartialPlayer: TableRecord {
|
/// struct PartialPlayer: TableRecord {
|
||||||
/// static let databaseTableName = "player"
|
/// static let databaseTableName = "player"
|
||||||
/// static let databaseSelection = [Column("id"), Column("name")]
|
/// static var databaseSelection: [any SQLSelectable] {
|
||||||
|
/// [Column("id"), Column("name")]
|
||||||
|
/// }
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// try dbQueue.write { db in
|
/// try dbQueue.write { db in
|
||||||
|
|||||||
@@ -3280,12 +3280,15 @@ extension Place: TableRecord {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Arrange the selected columns and lock their order
|
/// Arrange the selected columns and lock their order
|
||||||
static let databaseSelection: [any SQLSelectable] = [
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
Columns.id,
|
[
|
||||||
Columns.title,
|
Columns.id,
|
||||||
Columns.favorite,
|
Columns.title,
|
||||||
Columns.latitude,
|
Columns.favorite,
|
||||||
Columns.longitude]
|
Columns.latitude,
|
||||||
|
Columns.longitude,
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fetching methods
|
// Fetching methods
|
||||||
@@ -3973,12 +3976,12 @@ The default selection for a record type is controlled by the `databaseSelection`
|
|||||||
```swift
|
```swift
|
||||||
struct RestrictedPlayer : TableRecord {
|
struct RestrictedPlayer : TableRecord {
|
||||||
static let databaseTableName = "player"
|
static let databaseTableName = "player"
|
||||||
static let databaseSelection: [any SQLSelectable] = [Column("id"), Column("name")]
|
static var databaseSelection: [any SQLSelectable] { [Column("id"), Column("name")] }
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ExtendedPlayer : TableRecord {
|
struct ExtendedPlayer : TableRecord {
|
||||||
static let databaseTableName = "player"
|
static let databaseTableName = "player"
|
||||||
static let databaseSelection: [any SQLSelectable] = [AllColumns(), Column.rowID]
|
static var databaseSelection: [any SQLSelectable] { [AllColumns(), Column.rowID] }
|
||||||
}
|
}
|
||||||
|
|
||||||
// SELECT id, name FROM player
|
// SELECT id, name FROM player
|
||||||
|
|||||||
@@ -16,12 +16,12 @@ private struct B : TableRecord {
|
|||||||
|
|
||||||
private struct RestrictedB : TableRecord {
|
private struct RestrictedB : TableRecord {
|
||||||
static let databaseTableName = "b"
|
static let databaseTableName = "b"
|
||||||
static let databaseSelection: [any SQLSelectable] = [Column("name")]
|
static var databaseSelection: [any SQLSelectable] { [Column("name")] }
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct ExtendedB : TableRecord {
|
private struct ExtendedB : TableRecord {
|
||||||
static let databaseTableName = "b"
|
static let databaseTableName = "b"
|
||||||
static let databaseSelection: [any SQLSelectable] = [AllColumns(), Column.rowID]
|
static var databaseSelection: [any SQLSelectable] { [AllColumns(), Column.rowID] }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test SQL generation
|
/// Test SQL generation
|
||||||
|
|||||||
@@ -16,12 +16,12 @@ private struct B : TableRecord {
|
|||||||
|
|
||||||
private struct RestrictedB : TableRecord {
|
private struct RestrictedB : TableRecord {
|
||||||
static let databaseTableName = "b"
|
static let databaseTableName = "b"
|
||||||
static let databaseSelection: [any SQLSelectable] = [Column("name")]
|
static var databaseSelection: [any SQLSelectable] { [Column("name")] }
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct ExtendedB : TableRecord {
|
private struct ExtendedB : TableRecord {
|
||||||
static let databaseTableName = "b"
|
static let databaseTableName = "b"
|
||||||
static let databaseSelection: [any SQLSelectable] = [AllColumns(), Column.rowID]
|
static var databaseSelection: [any SQLSelectable] { [AllColumns(), Column.rowID] }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test SQL generation
|
/// Test SQL generation
|
||||||
|
|||||||
@@ -19,12 +19,12 @@ private struct C: TableRecord {
|
|||||||
|
|
||||||
private struct RestrictedC : TableRecord {
|
private struct RestrictedC : TableRecord {
|
||||||
static let databaseTableName = "c"
|
static let databaseTableName = "c"
|
||||||
static let databaseSelection: [any SQLSelectable] = [Column("name")]
|
static var databaseSelection: [any SQLSelectable] { [Column("name")] }
|
||||||
}
|
}
|
||||||
|
|
||||||
private struct ExtendedC : TableRecord {
|
private struct ExtendedC : TableRecord {
|
||||||
static let databaseTableName = "c"
|
static let databaseTableName = "c"
|
||||||
static let databaseSelection: [any SQLSelectable] = [AllColumns(), Column.rowID]
|
static var databaseSelection: [any SQLSelectable] { [AllColumns(), Column.rowID] }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test SQL generation
|
/// Test SQL generation
|
||||||
|
|||||||
@@ -20,7 +20,9 @@ class ColumnExpressionTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test databaseSelection
|
// Test databaseSelection
|
||||||
static let databaseSelection: [any SQLSelectable] = [Columns.id, Columns.name, Columns.score]
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
|
[Columns.id, Columns.name, Columns.score]
|
||||||
|
}
|
||||||
|
|
||||||
init(row: Row) {
|
init(row: Row) {
|
||||||
// Test row subscript
|
// Test row subscript
|
||||||
@@ -80,7 +82,9 @@ class ColumnExpressionTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test databaseSelection
|
// Test databaseSelection
|
||||||
static let databaseSelection: [any SQLSelectable] = [Columns.id, Columns.name, Columns.score]
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
|
[Columns.id, Columns.name, Columns.score]
|
||||||
|
}
|
||||||
|
|
||||||
init(row: Row) {
|
init(row: Row) {
|
||||||
// Test row subscript
|
// Test row subscript
|
||||||
@@ -148,7 +152,9 @@ class ColumnExpressionTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test databaseSelection
|
// Test databaseSelection
|
||||||
static let databaseSelection: [any SQLSelectable] = [Columns.id, Columns.name, Columns.score]
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
|
[Columns.id, Columns.name, Columns.score]
|
||||||
|
}
|
||||||
|
|
||||||
static var testRequest: QueryInterfaceRequest<Player> {
|
static var testRequest: QueryInterfaceRequest<Player> {
|
||||||
// Test expression derivation
|
// Test expression derivation
|
||||||
@@ -196,7 +202,9 @@ class ColumnExpressionTests: GRDBTestCase {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test databaseSelection
|
// Test databaseSelection
|
||||||
static let databaseSelection: [any SQLSelectable] = [CodingKeys.id, CodingKeys.name, CodingKeys.score]
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
|
[CodingKeys.id, CodingKeys.name, CodingKeys.score]
|
||||||
|
}
|
||||||
|
|
||||||
static var testRequest: QueryInterfaceRequest<Player> {
|
static var testRequest: QueryInterfaceRequest<Player> {
|
||||||
// Test expression derivation
|
// Test expression derivation
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ extension Book : FetchableRecord {
|
|||||||
|
|
||||||
extension Book : MutablePersistableRecord {
|
extension Book : MutablePersistableRecord {
|
||||||
static let databaseTableName = "books"
|
static let databaseTableName = "books"
|
||||||
static let databaseSelection: [any SQLSelectable] = [AllColumns(), Column.rowID]
|
static var databaseSelection: [any SQLSelectable] { [AllColumns(), Column.rowID] }
|
||||||
|
|
||||||
func encode(to container: inout PersistenceContainer) {
|
func encode(to container: inout PersistenceContainer) {
|
||||||
container[.rowID] = id
|
container[.rowID] = id
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ extension Book : FetchableRecord {
|
|||||||
|
|
||||||
extension Book : MutablePersistableRecord {
|
extension Book : MutablePersistableRecord {
|
||||||
static let databaseTableName = "books"
|
static let databaseTableName = "books"
|
||||||
static let databaseSelection: [any SQLSelectable] = [AllColumns(), Column.rowID]
|
static var databaseSelection: [any SQLSelectable] { [AllColumns(), Column.rowID] }
|
||||||
|
|
||||||
func encode(to container: inout PersistenceContainer) {
|
func encode(to container: inout PersistenceContainer) {
|
||||||
container[.rowID] = id
|
container[.rowID] = id
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ extension Book : FetchableRecord {
|
|||||||
|
|
||||||
extension Book : MutablePersistableRecord {
|
extension Book : MutablePersistableRecord {
|
||||||
static let databaseTableName = "books"
|
static let databaseTableName = "books"
|
||||||
static let databaseSelection: [any SQLSelectable] = [AllColumns(), Column.rowID]
|
static var databaseSelection: [any SQLSelectable] { [AllColumns(), Column.rowID] }
|
||||||
|
|
||||||
func encode(to container: inout PersistenceContainer) {
|
func encode(to container: inout PersistenceContainer) {
|
||||||
container[.rowID] = id
|
container[.rowID] = id
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ final class JSONColumnTests: GRDBTestCase {
|
|||||||
static let info = JSONColumn(CodingKeys.info)
|
static let info = JSONColumn(CodingKeys.info)
|
||||||
}
|
}
|
||||||
|
|
||||||
static let databaseSelection: [any SQLSelectable] = [Columns.id, Columns.info]
|
static var databaseSelection: [any SQLSelectable] { [Columns.id, Columns.info] }
|
||||||
}
|
}
|
||||||
|
|
||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
|
|||||||
@@ -50,7 +50,7 @@ private struct T2: Codable, FetchableRecord, TableRecord {
|
|||||||
|
|
||||||
private struct T3: Codable, FetchableRecord, TableRecord {
|
private struct T3: Codable, FetchableRecord, TableRecord {
|
||||||
static let databaseTableName = "t3"
|
static let databaseTableName = "t3"
|
||||||
static let databaseSelection: [any SQLSelectable] = [Column("t1id"), Column("name")]
|
static var databaseSelection: [any SQLSelectable] { [Column("t1id"), Column("name")] }
|
||||||
var t1id: Int64
|
var t1id: Int64
|
||||||
var name: String
|
var name: String
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -309,7 +309,9 @@ extension SQLLiteralTests {
|
|||||||
try makeDatabaseQueue().inDatabase { db in
|
try makeDatabaseQueue().inDatabase { db in
|
||||||
struct Player: TableRecord { }
|
struct Player: TableRecord { }
|
||||||
struct AltPlayer: TableRecord {
|
struct AltPlayer: TableRecord {
|
||||||
static let databaseSelection: [any SQLSelectable] = [Column("id"), Column("name")]
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
|
[Column("id"), Column("name")]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
do {
|
do {
|
||||||
let query: SQL = """
|
let query: SQL = """
|
||||||
|
|||||||
@@ -468,7 +468,7 @@ class TableRecordDeleteTests: GRDBTestCase {
|
|||||||
|
|
||||||
struct Team: MutablePersistableRecord, FetchableRecord {
|
struct Team: MutablePersistableRecord, FetchableRecord {
|
||||||
// Test RETURNING
|
// Test RETURNING
|
||||||
static let databaseSelection: [any SQLSelectable] = [Column("id"), Column("name")]
|
static var databaseSelection: [any SQLSelectable] { [Column("id"), Column("name")] }
|
||||||
static let players = hasMany(Player.self)
|
static let players = hasMany(Player.self)
|
||||||
func encode(to container: inout PersistenceContainer) { preconditionFailure("should not be called") }
|
func encode(to container: inout PersistenceContainer) { preconditionFailure("should not be called") }
|
||||||
init(row: Row) { preconditionFailure("should not be called") }
|
init(row: Row) { preconditionFailure("should not be called") }
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ class TableRecordTests: GRDBTestCase {
|
|||||||
func testExtendedDatabaseSelection() throws {
|
func testExtendedDatabaseSelection() throws {
|
||||||
struct Record: TableRecord {
|
struct Record: TableRecord {
|
||||||
static let databaseTableName = "t1"
|
static let databaseTableName = "t1"
|
||||||
static let databaseSelection: [any SQLSelectable] = [AllColumns(), Column.rowID]
|
static var databaseSelection: [any SQLSelectable] { [AllColumns(), Column.rowID] }
|
||||||
}
|
}
|
||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
try dbQueue.inDatabase { db in
|
try dbQueue.inDatabase { db in
|
||||||
@@ -116,7 +116,9 @@ class TableRecordTests: GRDBTestCase {
|
|||||||
func testRestrictedDatabaseSelection() throws {
|
func testRestrictedDatabaseSelection() throws {
|
||||||
struct Record: TableRecord {
|
struct Record: TableRecord {
|
||||||
static let databaseTableName = "t1"
|
static let databaseTableName = "t1"
|
||||||
static let databaseSelection: [any SQLSelectable] = [Column("a"), Column("b")]
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
|
[Column("a"), Column("b")]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
let dbQueue = try makeDatabaseQueue()
|
let dbQueue = try makeDatabaseQueue()
|
||||||
try dbQueue.inDatabase { db in
|
try dbQueue.inDatabase { db in
|
||||||
|
|||||||
@@ -29,18 +29,20 @@ private struct Item: Codable, FetchableRecord, PersistableRecord {
|
|||||||
i9 = row[9]
|
i9 = row[9]
|
||||||
}
|
}
|
||||||
|
|
||||||
static let databaseSelection: [any SQLSelectable] = [
|
static var databaseSelection: [any SQLSelectable] {
|
||||||
Column("i0"),
|
[
|
||||||
Column("i1"),
|
Column("i0"),
|
||||||
Column("i2"),
|
Column("i1"),
|
||||||
Column("i3"),
|
Column("i2"),
|
||||||
Column("i4"),
|
Column("i3"),
|
||||||
Column("i5"),
|
Column("i4"),
|
||||||
Column("i6"),
|
Column("i5"),
|
||||||
Column("i7"),
|
Column("i6"),
|
||||||
Column("i8"),
|
Column("i7"),
|
||||||
Column("i9"),
|
Column("i8"),
|
||||||
]
|
Column("i9"),
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Here we test the extraction of a plain Swift struct
|
/// Here we test the extraction of a plain Swift struct
|
||||||
|
|||||||
Reference in new issue
Block a user