Skip to content

Commit b403572

Browse files
authored
Merge branch 'main' into fetch-task
2 parents 4027735 + 2d59cde commit b403572

10 files changed

Lines changed: 89 additions & 13 deletions

File tree

Examples/CaseStudies/DynamicQuery.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct DynamicQueryDemo: SwiftUICaseStudy {
9393
return try Value(
9494
facts: search.fetchAll(db),
9595
searchCount: search.fetchCount(db),
96-
totalCount: Fact.all.fetchCount(db)
96+
totalCount: Fact.fetchCount(db)
9797
)
9898
}
9999
}

Examples/CaseStudies/TransactionDemo.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct TransactionDemo: SwiftUICaseStudy {
6464
func fetch(_ db: Database) throws -> Value {
6565
try Value(
6666
facts: Fact.order { $0.id.desc() }.fetchAll(db),
67-
count: Fact.all.fetchCount(db)
67+
count: Fact.fetchCount(db)
6868
)
6969
}
7070
}

Examples/Reminders/ReminderForm.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ struct ReminderFormPreview: PreviewProvider {
208208
let (remindersList, reminder) = try! prepareDependencies {
209209
$0.defaultDatabase = try Reminders.appDatabase()
210210
return try $0.defaultDatabase.write { db in
211-
let remindersList = try RemindersList.all.fetchOne(db)!
211+
let remindersList = try RemindersList.fetchOne(db)!
212212
return (
213213
remindersList,
214214
try Reminder.where { $0.remindersListID.eq(remindersList.id) }.fetchOne(db)!

Examples/Reminders/ReminderRow.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ struct ReminderRowPreview: PreviewProvider {
162162
let _ = try! prepareDependencies {
163163
$0.defaultDatabase = try Reminders.appDatabase()
164164
try $0.defaultDatabase.read { db in
165-
reminder = try Reminder.all.fetchOne(db)
166-
remindersList = try RemindersList.all.fetchOne(db)!
165+
reminder = try Reminder.fetchOne(db)
166+
remindersList = try RemindersList.fetchOne(db)!
167167
}
168168
}
169169

Examples/Reminders/RemindersDetail.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,8 +367,8 @@ struct RemindersDetailPreview: PreviewProvider {
367367
$0.defaultDatabase = try Reminders.appDatabase()
368368
return try $0.defaultDatabase.read { db in
369369
(
370-
try RemindersList.all.fetchOne(db)!,
371-
try Tag.all.fetchOne(db)!
370+
try RemindersList.fetchOne(db)!,
371+
try Tag.fetchOne(db)!
372372
)
373373
}
374374
}

Examples/RemindersTests/RemindersDetailsTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension BaseTestSuite {
1212
@Dependency(\.defaultDatabase) var database
1313

1414
@Test func basics() async throws {
15-
let remindersList = try await database.read { try RemindersList.all.fetchOne($0)! }
15+
let remindersList = try await database.read { try RemindersList.fetchOne($0)! }
1616
let model = RemindersDetailModel(detailType: .remindersList(remindersList))
1717
try await model.$reminderRows.load()
1818
assertInlineSnapshot(of: model.reminderRows, as: .customDump) {
@@ -118,7 +118,7 @@ extension BaseTestSuite {
118118
}
119119

120120
@Test func ordering() async throws {
121-
let remindersList = try await database.read { try RemindersList.all.fetchOne($0)! }
121+
let remindersList = try await database.read { try RemindersList.fetchOne($0)! }
122122
let model = RemindersDetailModel(detailType: .remindersList(remindersList))
123123

124124
try await model.$reminderRows.load()
@@ -164,7 +164,7 @@ extension BaseTestSuite {
164164
}
165165

166166
@Test func showCompleted() async throws {
167-
let remindersList = try await database.read { try RemindersList.all.fetchOne($0)! }
167+
let remindersList = try await database.read { try RemindersList.fetchOne($0)! }
168168
let model = RemindersDetailModel(detailType: .remindersList(remindersList))
169169

170170
try await model.$reminderRows.load()
@@ -211,7 +211,7 @@ extension BaseTestSuite {
211211
}
212212

213213
@Test func move() async throws {
214-
let remindersList = try await database.read { try RemindersList.all.fetchOne($0)! }
214+
let remindersList = try await database.read { try RemindersList.fetchOne($0)! }
215215
let model = RemindersDetailModel(detailType: .remindersList(remindersList))
216216

217217
try await model.$reminderRows.load()
@@ -319,7 +319,7 @@ extension BaseTestSuite {
319319
}
320320

321321
@Test func tagged() async throws {
322-
let tag = try await database.read { try Tag.find("someday").fetchOne($0)! }
322+
let tag = try await database.read { try Tag.find($0, key: "someday") }
323323
let model = RemindersDetailModel(detailType: .tags([tag]))
324324
try await model.$reminderRows.load()
325325
assertInlineSnapshot(of: model.reminderRows.map(\.reminder.title), as: .customDump) {

Examples/SyncUpTests/SyncUpFormTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct SyncUpFormTests {
3939

4040
@Test func updateExisting() async throws {
4141
let existingSyncUp = try await database.read { db in
42-
try #require(try SyncUp.all.fetchOne(db))
42+
try #require(try SyncUp.fetchOne(db))
4343
}
4444
let draft = SyncUp.Draft(existingSyncUp)
4545
let model = SyncUpFormModel(syncUp: draft)

Sources/SQLiteData/Documentation.docc/SQLiteData.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,8 @@ with SQLite to take full advantage of GRDB and SQLiteData.
300300

301301
- ``StructuredQueriesCore/Statement``
302302
- ``StructuredQueriesCore/SelectStatement``
303+
- ``StructuredQueriesCore/Table``
304+
- ``StructuredQueriesCore/PrimaryKeyedTable``
303305
- ``QueryCursor``
304306

305307
### Observing model data

Sources/SQLiteData/StructuredQueries+GRDB/Statement+GRDB.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@ extension SelectStatement where QueryValue == (), Joins == () {
186186
}
187187
}
188188

189+
extension SelectStatement where QueryValue == (), From: PrimaryKeyedTable, Joins == () {
190+
/// Returns a single value fetched from the database for a given primary key.
191+
///
192+
/// - Parameters
193+
/// - db: A database connection.
194+
/// - primaryKey: A primary key identifying a table row.
195+
/// - Returns: A single value decoded from the database.
196+
@inlinable
197+
public func find(
198+
_ db: Database,
199+
key primaryKey: some QueryExpression<From.PrimaryKey>
200+
) throws -> From.QueryOutput {
201+
guard let record = try asSelect().find(primaryKey).fetchOne(db) else {
202+
throw NotFound()
203+
}
204+
return record
205+
}
206+
}
207+
189208
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
190209
extension SelectStatement where QueryValue == () {
191210
/// Returns an array of all values fetched from the database.
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import StructuredQueriesCore
2+
3+
extension StructuredQueriesCore.Table {
4+
/// Returns an array of all values fetched from the database.
5+
///
6+
/// - Parameter db: A database connection.
7+
/// - Returns: An array of all values decoded from the database.
8+
@inlinable
9+
public static func fetchAll(_ db: Database) throws -> [QueryOutput] {
10+
try all.fetchAll(db)
11+
}
12+
13+
/// Returns a single value fetched from the database.
14+
///
15+
/// - Parameter db: A database connection.
16+
/// - Returns: A single value decoded from the database.
17+
@inlinable
18+
public static func fetchOne(_ db: Database) throws -> QueryOutput? {
19+
try all.fetchOne(db)
20+
}
21+
22+
/// Returns the number of rows fetched by the query.
23+
///
24+
/// - Parameter db: A database connection.
25+
/// - Returns: The number of rows fetched by the query.
26+
@inlinable
27+
public static func fetchCount(_ db: Database) throws -> Int {
28+
try all.fetchCount(db)
29+
}
30+
31+
/// Returns a cursor to all values fetched from the database.
32+
///
33+
/// - Parameter db: A database connection.
34+
/// - Returns: A cursor to all values decoded from the database.
35+
@inlinable
36+
public static func fetchCursor(_ db: Database) throws -> QueryCursor<QueryOutput> {
37+
try all.fetchCursor(db)
38+
}
39+
}
40+
41+
extension StructuredQueriesCore.PrimaryKeyedTable {
42+
/// Returns a single value fetched from the database for a given primary key.
43+
///
44+
/// - Parameters
45+
/// - db: A database connection.
46+
/// - primaryKey: A primary key identifying a table row.
47+
/// - Returns: A single value decoded from the database.
48+
@inlinable
49+
public static func find(
50+
_ db: Database,
51+
key primaryKey: some QueryExpression<PrimaryKey>
52+
) throws -> QueryOutput {
53+
try all.find(db, key: primaryKey)
54+
}
55+
}

0 commit comments

Comments
 (0)