Skip to content

Commit df360b2

Browse files
authored
Don't execute @Fetch{All,One} queries for selections (#350)
It's currently possible to execute these queries if you specify a parameter like `animation`. These parameters aren't actually useful for a selection query, so let's deprecate those overloads.
1 parent 45a7a19 commit df360b2

4 files changed

Lines changed: 140 additions & 12 deletions

File tree

Sources/SQLiteData/FetchAll.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,16 @@ public struct FetchAll<Element: Sendable>: Sendable {
219219
}
220220

221221
extension FetchAll {
222+
@available(*, deprecated, message: "Remove unused parameters: 'database', 'scheduler'.")
223+
public init(
224+
wrappedValue: [Element] = [],
225+
database: (any DatabaseReader)? = nil,
226+
scheduler: some ValueObservationScheduler & Hashable
227+
)
228+
where Element: StructuredQueriesCore._Selection, Element.QueryOutput == Element {
229+
sharedReader = SharedReader(value: wrappedValue)
230+
}
231+
222232
/// Initializes this property with a query that fetches every row from a table.
223233
///
224234
/// - Parameters:
@@ -393,6 +403,16 @@ extension FetchAll: Equatable where Element: Equatable {
393403
sharedReader.update()
394404
}
395405

406+
@available(*, deprecated, message: "Remove unused parameters: 'database', 'animation'.")
407+
public init(
408+
wrappedValue: [Element] = [],
409+
database: (any DatabaseReader)? = nil,
410+
animation: Animation
411+
)
412+
where Element: StructuredQueriesCore._Selection, Element.QueryOutput == Element {
413+
sharedReader = SharedReader(value: wrappedValue)
414+
}
415+
396416
/// Initializes this property with a query that fetches every row from a table.
397417
///
398418
/// - Parameters:

Sources/SQLiteData/FetchOne.swift

Lines changed: 65 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ public struct FetchOne<Value: Sendable>: Sendable {
7979
sharedReader = SharedReader(value: wrappedValue)
8080
}
8181

82+
/// Initializes this property with a wrapped value.
83+
///
84+
/// - Parameter wrappedValue: A default value to associate with this property.
85+
public init(wrappedValue: sending Value)
86+
where
87+
Value: _Selection,
88+
Value.QueryOutput == Value
89+
{
90+
sharedReader = SharedReader(value: wrappedValue)
91+
}
92+
8293
/// Initializes this property with a query that fetches the first row from a table.
8394
///
8495
/// - Parameters:
@@ -121,18 +132,6 @@ public struct FetchOne<Value: Sendable>: Sendable {
121132
)
122133
}
123134

124-
/// Initializes this property with a wrapped value.
125-
///
126-
/// - Parameter wrappedValue: A default value to associate with this property.
127-
public init(wrappedValue: sending Value)
128-
where
129-
Value: _OptionalProtocol,
130-
Value: _Selection,
131-
Value.QueryOutput == Value
132-
{
133-
sharedReader = SharedReader(value: wrappedValue)
134-
}
135-
136135
/// Initializes this property with a query associated with the wrapped value.
137136
///
138137
/// - Parameters:
@@ -430,6 +429,33 @@ public struct FetchOne<Value: Sendable>: Sendable {
430429
}
431430

432431
extension FetchOne {
432+
@available(*, deprecated, message: "Remove unused parameters: 'database', 'scheduler'.")
433+
public init(
434+
wrappedValue: sending Value,
435+
database: (any DatabaseReader)? = nil,
436+
scheduler: some ValueObservationScheduler & Hashable
437+
)
438+
where
439+
Value: _Selection,
440+
Value.QueryOutput == Value
441+
{
442+
sharedReader = SharedReader(value: wrappedValue)
443+
}
444+
445+
@available(*, deprecated, message: "Remove unused parameters: 'database', 'scheduler'.")
446+
public init(
447+
wrappedValue: sending Value = Value._none,
448+
database: (any DatabaseReader)? = nil,
449+
scheduler: some ValueObservationScheduler & Hashable
450+
)
451+
where
452+
Value: _OptionalProtocol,
453+
Value: _Selection,
454+
Value.QueryOutput == Value
455+
{
456+
sharedReader = SharedReader(value: wrappedValue)
457+
}
458+
433459
/// Initializes this property with a query that fetches the first row from a table.
434460
///
435461
/// - Parameters:
@@ -880,6 +906,33 @@ extension FetchOne: Equatable where Value: Equatable {
880906
sharedReader.update()
881907
}
882908

909+
@available(*, deprecated, message: "Remove unused parameters: 'database', 'animation'.")
910+
public init(
911+
wrappedValue: sending Value,
912+
database: (any DatabaseReader)? = nil,
913+
animation: Animation
914+
)
915+
where
916+
Value: _Selection,
917+
Value.QueryOutput == Value
918+
{
919+
sharedReader = SharedReader(value: wrappedValue)
920+
}
921+
922+
@available(*, deprecated, message: "Remove unused parameters: 'database', 'animation'.")
923+
public init(
924+
wrappedValue: sending Value = Value._none,
925+
database: (any DatabaseReader)? = nil,
926+
animation: Animation
927+
)
928+
where
929+
Value: _OptionalProtocol,
930+
Value: _Selection,
931+
Value.QueryOutput == Value
932+
{
933+
sharedReader = SharedReader(value: wrappedValue)
934+
}
935+
883936
/// Initializes this property with a query that fetches the first row from a table.
884937
///
885938
/// - Parameters:

Tests/SQLiteDataTests/FetchAllTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import Foundation
33
import SQLiteData
44
import Testing
55

6+
#if canImport(SwiftUI)
7+
import SwiftUI
8+
#endif
9+
610
@Suite(.dependency(\.defaultDatabase, try .database()))
711
struct FetchAllTests {
812
@Dependency(\.defaultDatabase) var database
@@ -63,6 +67,16 @@ struct FetchAllTests {
6367
)
6468
}
6569
}
70+
71+
@available(*, deprecated)
72+
@Test func fetchAllSelection_Deprecated() async throws {
73+
#if canImport(SwiftUI)
74+
do {
75+
@FetchAll(animation: .default) var row: [Row]
76+
#expect($row.loadError == nil)
77+
}
78+
#endif
79+
}
6680
}
6781

6882
@Table
@@ -73,6 +87,12 @@ private struct Record: Equatable {
7387
@Column(as: Date?.UnixTimeRepresentation.self)
7488
var optionalDate: Date?
7589
}
90+
91+
@Selection
92+
private struct Row {
93+
let id: Int
94+
}
95+
7696
extension DatabaseWriter where Self == DatabaseQueue {
7797
fileprivate static func database() throws -> DatabaseQueue {
7898
let database = try DatabaseQueue()

Tests/SQLiteDataTests/FetchOneTests.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import Foundation
33
import SQLiteData
44
import Testing
55

6+
#if canImport(SwiftUI)
7+
import SwiftUI
8+
#endif
9+
610
@Suite(.dependency(\.defaultDatabase, try .database())) struct FetchOneTests {
711
@Dependency(\.defaultDatabase) var database
812

@@ -176,6 +180,31 @@ import Testing
176180
_record = FetchOne(wrappedValue: Record(id: 0), Record.all)
177181
#expect(record.id == 1)
178182
}
183+
184+
@Test func fetchOneSelection() async throws {
185+
do {
186+
@FetchOne var row: Row?
187+
#expect($row.loadError == nil)
188+
}
189+
do {
190+
@FetchOne var row = Row(id: 1)
191+
#expect($row.loadError == nil)
192+
}
193+
}
194+
195+
@available(*, deprecated)
196+
@Test func fetchOneSelection_Deprecated() async throws {
197+
#if canImport(SwiftUI)
198+
do {
199+
@FetchOne(animation: .default) var row: Row?
200+
#expect($row.loadError == nil)
201+
}
202+
do {
203+
@FetchOne(animation: .default) var row = Row(id: 1)
204+
#expect($row.loadError == nil)
205+
}
206+
#endif
207+
}
179208
}
180209

181210
@Table
@@ -187,6 +216,12 @@ private struct Record: Equatable {
187216
@Column(as: Date?.UnixTimeRepresentation.self)
188217
var optionalDate: Date?
189218
}
219+
220+
@Selection
221+
private struct Row {
222+
let id: Int
223+
}
224+
190225
extension DatabaseWriter where Self == DatabaseQueue {
191226
fileprivate static func database() throws -> DatabaseQueue {
192227
let database = try DatabaseQueue()

0 commit comments

Comments
 (0)