Skip to content

Commit a2d5453

Browse files
authored
Improve type-inference for #sql. (#37)
1 parent 8368982 commit a2d5453

File tree

4 files changed

+189
-68
lines changed

4 files changed

+189
-68
lines changed

Sources/SharingGRDBCore/Documentation.docc/Articles/Fetching.md

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,8 @@ learn more, be sure to check out the [documentation][sq-getting-started] of Stru
7373
You can even execute a SQL string to populate the data in your features:
7474

7575
```swift
76-
@FetchAll(
77-
#sql(
78-
"SELECT * FROM reminders where isCompleted ORDER BY title DESC",
79-
as: Reminder.self
80-
)
81-
)
82-
var completedReminders
76+
@FetchAll(#sql("SELECT * FROM reminders where isCompleted ORDER BY title DESC"))
77+
var completedReminders: [Reminder]
8378
```
8479

8580
This uses the `#sql` macro for constructing [safe SQL strings][sq-safe-sql-strings]. You are
@@ -94,11 +89,10 @@ description of your schema to prevent accidental typos:
9489
FROM \(Reminder.self)
9590
WHERE \(Reminder.isCompleted)
9691
ORDER BY \(Reminder.title) DESC
97-
""",
98-
as: Reminder.self
92+
"""
9993
)
10094
)
101-
var completedReminders
95+
var completedReminders: [Reminder]
10296
```
10397

10498
These interpolations are completely safe to do because they are statically known at compile time,
@@ -186,7 +180,7 @@ var completedRemindersCount = 0
186180
You can use the `#sql` macro with `@FetchOne` to execute a safe SQL string:
187181

188182
```swift
189-
@FetchOne(#sql("SELECT count(*) FROM reminders WHERE isCompleted", as: Int.self))
183+
@FetchOne(#sql("SELECT count(*) FROM reminders WHERE isCompleted"))
190184
var completedRemindersCount = 0
191185
```
192186

Sources/SharingGRDBCore/FetchAll.swift

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,30 @@ public struct FetchAll<Element: Sendable>: Sendable {
158158
)
159159
}
160160

161+
/// Initializes this property with a query associated with the wrapped value.
162+
///
163+
/// - Parameters:
164+
/// - statement: A query associated with the wrapped value.
165+
/// - database: The database to read from. A value of `nil` will use the default database
166+
/// (`@Dependency(\.defaultDatabase)`).
167+
public init<S: StructuredQueriesCore.Statement<Element>>(
168+
wrappedValue: [Element] = [],
169+
_ statement: S,
170+
database: (any DatabaseReader)? = nil
171+
)
172+
where
173+
Element: QueryRepresentable,
174+
Element == S.QueryValue.QueryOutput
175+
{
176+
sharedReader = SharedReader(
177+
wrappedValue: wrappedValue,
178+
.fetch(
179+
FetchAllStatementValueRequest(statement: statement),
180+
database: database
181+
)
182+
)
183+
}
184+
161185
/// Initializes this property with a query associated with the wrapped value.
162186
///
163187
/// - Parameters:
@@ -397,6 +421,34 @@ extension FetchAll {
397421
)
398422
}
399423

424+
/// Initializes this property with a query associated with the wrapped value.
425+
///
426+
/// - Parameters:
427+
/// - statement: A query associated with the wrapped value.
428+
/// - database: The database to read from. A value of `nil` will use the default database
429+
/// (`@Dependency(\.defaultDatabase)`).
430+
/// - scheduler: The scheduler to observe from. By default, database observation is performed
431+
/// asynchronously on the main queue.
432+
public init<S: StructuredQueriesCore.Statement<Element>>(
433+
wrappedValue: [Element] = [],
434+
_ statement: S,
435+
database: (any DatabaseReader)? = nil,
436+
scheduler: some ValueObservationScheduler & Hashable
437+
)
438+
where
439+
Element: QueryRepresentable,
440+
Element == S.QueryValue.QueryOutput
441+
{
442+
sharedReader = SharedReader(
443+
wrappedValue: wrappedValue,
444+
.fetch(
445+
FetchAllStatementValueRequest(statement: statement),
446+
database: database,
447+
scheduler: scheduler
448+
)
449+
)
450+
}
451+
400452
/// Initializes this property with a query associated with the wrapped value.
401453
///
402454
/// - Parameters:
@@ -666,6 +718,34 @@ extension FetchAll: Equatable where Element: Equatable {
666718
)
667719
}
668720

721+
/// Initializes this property with a query associated with the wrapped value.
722+
///
723+
/// - Parameters:
724+
/// - statement: A query associated with the wrapped value.
725+
/// - database: The database to read from. A value of `nil` will use the default database
726+
/// (`@Dependency(\.defaultDatabase)`).
727+
/// - animation: The animation to use for user interface changes that result from changes to
728+
/// the fetched results.
729+
public init<S: StructuredQueriesCore.Statement<Element>>(
730+
wrappedValue: [Element] = [],
731+
_ statement: S,
732+
database: (any DatabaseReader)? = nil,
733+
animation: Animation
734+
)
735+
where
736+
Element: QueryRepresentable,
737+
Element == S.QueryValue.QueryOutput
738+
{
739+
sharedReader = SharedReader(
740+
wrappedValue: wrappedValue,
741+
.fetch(
742+
FetchAllStatementValueRequest(statement: statement),
743+
database: database,
744+
animation: animation
745+
)
746+
)
747+
}
748+
669749
/// Initializes this property with a query associated with the wrapped value.
670750
///
671751
/// - Parameters:

0 commit comments

Comments
 (0)