Skip to content

Commit 494e697

Browse files
committed
Warn against selectID() when the ID type is an optional.
1 parent 87b236d commit 494e697

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

GRDB/QueryInterface/Request/QueryInterfaceRequest.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,26 @@ extension QueryInterfaceRequest: SelectionRequest {
290290
/// // SELECT id FROM player WHERE ...
291291
/// let request = try Player.filter(...).selectID()
292292
/// ```
293+
///
294+
/// **Important**: if the record type has an `ID` type that is an
295+
/// optional, such as `Int64?`, it is recommended to prefer
296+
/// ``selectPrimaryKey(as:)`` instead:
297+
///
298+
/// ```swift
299+
/// struct Player: Identifiable {
300+
/// var id: Int64?
301+
/// }
302+
///
303+
/// // NOT RECOMMENDED: Set<Int64?>
304+
/// let ids = try Player.filter(...)
305+
/// .selectID()
306+
/// .fetchSet(db)
307+
///
308+
/// // BETTER: Set<Int64>
309+
/// let ids = try Player.filter(...)
310+
/// .selectPrimaryKey(as: Int64.self)
311+
/// .fetchSet(db)
312+
/// ```
293313
public func selectID() -> QueryInterfaceRequest<RowDecoder.ID>
294314
where RowDecoder: Identifiable
295315
{

GRDB/QueryInterface/SQL/Table.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,24 @@ extension Table {
435435
/// // SELECT id FROM player
436436
/// let request = try table.selectID()
437437
/// ```
438+
///
439+
/// **Important**: if the record type has an `ID` type that is an
440+
/// optional, such as `Int64?`, it is recommended to prefer
441+
/// ``selectPrimaryKey(as:)`` instead:
442+
///
443+
/// ```swift
444+
/// struct Player: Identifiable {
445+
/// var id: Int64?
446+
/// }
447+
///
448+
/// let table = Table<Player>("player")
449+
///
450+
/// // NOT RECOMMENDED: Set<Int64?>
451+
/// let ids = try table.selectID().fetchSet(db)
452+
///
453+
/// // BETTER: Set<Int64>
454+
/// let ids = try table.selectPrimaryKey(as: Int64.self).fetchSet(db)
455+
/// ```
438456
public func selectID() -> QueryInterfaceRequest<RowDecoder.ID>
439457
where RowDecoder: Identifiable
440458
{

GRDB/QueryInterface/TableRecord+QueryInterfaceRequest.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,22 @@ extension TableRecord {
259259
/// // SELECT id FROM player
260260
/// let request = try Player.selectID()
261261
/// ```
262+
///
263+
/// **Important**: if the record type has an `ID` type that is an
264+
/// optional, such as `Int64?`, it is recommended to prefer
265+
/// ``selectPrimaryKey(as:)`` instead:
266+
///
267+
/// ```swift
268+
/// struct Player: Identifiable {
269+
/// var id: Int64?
270+
/// }
271+
///
272+
/// // NOT RECOMMENDED: Set<Int64?>
273+
/// let ids = try Player.selectID().fetchSet(db)
274+
///
275+
/// // BETTER: Set<Int64>
276+
/// let ids = try Player.selectPrimaryKey(as: Int64.self).fetchSet(db)
277+
/// ```
262278
public static func selectID() -> QueryInterfaceRequest<Self.ID>
263279
where Self: Identifiable
264280
{

0 commit comments

Comments
 (0)