Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Examples/CaseStudies/DynamicQuery.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ struct DynamicQueryDemo: SwiftUICaseStudy {
return try Value(
facts: search.fetchAll(db),
searchCount: search.fetchCount(db),
totalCount: Fact.all.fetchCount(db)
totalCount: Fact.fetchCount(db)
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/CaseStudies/TransactionDemo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ struct TransactionDemo: SwiftUICaseStudy {
func fetch(_ db: Database) throws -> Value {
try Value(
facts: Fact.order { $0.id.desc() }.fetchAll(db),
count: Fact.all.fetchCount(db)
count: Fact.fetchCount(db)
)
}
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Examples/Reminders/ReminderForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ struct ReminderFormPreview: PreviewProvider {
let (remindersList, reminder) = try! prepareDependencies {
$0.defaultDatabase = try Reminders.appDatabase()
return try $0.defaultDatabase.write { db in
let remindersList = try RemindersList.all.fetchOne(db)!
let remindersList = try RemindersList.fetchOne(db)!
return (
remindersList,
try Reminder.where { $0.remindersListID.eq(remindersList.id) }.fetchOne(db)!
Expand Down
4 changes: 2 additions & 2 deletions Examples/Reminders/ReminderRow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ struct ReminderRowPreview: PreviewProvider {
let _ = try! prepareDependencies {
$0.defaultDatabase = try Reminders.appDatabase()
try $0.defaultDatabase.read { db in
reminder = try Reminder.all.fetchOne(db)
remindersList = try RemindersList.all.fetchOne(db)!
reminder = try Reminder.fetchOne(db)
remindersList = try RemindersList.fetchOne(db)!
}
}

Expand Down
4 changes: 2 additions & 2 deletions Examples/Reminders/RemindersDetail.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ struct RemindersDetailPreview: PreviewProvider {
$0.defaultDatabase = try Reminders.appDatabase()
return try $0.defaultDatabase.read { db in
(
try RemindersList.all.fetchOne(db)!,
try Tag.all.fetchOne(db)!
try RemindersList.fetchOne(db)!,
try Tag.fetchOne(db)!
)
}
}
Expand Down
10 changes: 5 additions & 5 deletions Examples/RemindersTests/RemindersDetailsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ extension BaseTestSuite {
@Dependency(\.defaultDatabase) var database

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

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

try await model.$reminderRows.load()
Expand Down Expand Up @@ -164,7 +164,7 @@ extension BaseTestSuite {
}

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

try await model.$reminderRows.load()
Expand Down Expand Up @@ -211,7 +211,7 @@ extension BaseTestSuite {
}

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

try await model.$reminderRows.load()
Expand Down Expand Up @@ -319,7 +319,7 @@ extension BaseTestSuite {
}

@Test func tagged() async throws {
let tag = try await database.read { try Tag.find("someday").fetchOne($0)! }
let tag = try await database.read { try Tag.find($0, key: "someday") }
let model = RemindersDetailModel(detailType: .tags([tag]))
try await model.$reminderRows.load()
assertInlineSnapshot(of: model.reminderRows.map(\.reminder.title), as: .customDump) {
Expand Down
2 changes: 1 addition & 1 deletion Examples/SyncUpTests/SyncUpFormTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct SyncUpFormTests {

@Test func updateExisting() async throws {
let existingSyncUp = try await database.read { db in
try #require(try SyncUp.all.fetchOne(db))
try #require(try SyncUp.fetchOne(db))
}
let draft = SyncUp.Draft(existingSyncUp)
let model = SyncUpFormModel(syncUp: draft)
Expand Down
2 changes: 2 additions & 0 deletions Sources/SQLiteData/Documentation.docc/SQLiteData.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,8 @@ with SQLite to take full advantage of GRDB and SQLiteData.

- ``StructuredQueriesCore/Statement``
- ``StructuredQueriesCore/SelectStatement``
- ``StructuredQueriesCore/Table``
- ``StructuredQueriesCore/PrimaryKeyedTable``
- ``QueryCursor``

### Observing model data
Expand Down
19 changes: 19 additions & 0 deletions Sources/SQLiteData/StructuredQueries+GRDB/Statement+GRDB.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,25 @@ extension SelectStatement where QueryValue == (), Joins == () {
}
}

extension SelectStatement where QueryValue == (), From: PrimaryKeyedTable, Joins == () {
/// Returns a single value fetched from the database for a given primary key.
///
/// - Parameters
/// - db: A database connection.
/// - primaryKey: A primary key identifying a table row.
/// - Returns: A single value decoded from the database.
@inlinable
public func find(
_ db: Database,
key primaryKey: some QueryExpression<From.PrimaryKey>
) throws -> From.QueryOutput {
guard let record = try asSelect().find(primaryKey).fetchOne(db) else {
throw NotFound()
}
return record
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
extension SelectStatement where QueryValue == () {
/// Returns an array of all values fetched from the database.
Expand Down
55 changes: 55 additions & 0 deletions Sources/SQLiteData/StructuredQueries+GRDB/Table+GRDB.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import StructuredQueriesCore

extension StructuredQueriesCore.Table {
/// Returns an array of all values fetched from the database.
///
/// - Parameter db: A database connection.
/// - Returns: An array of all values decoded from the database.
@inlinable
public static func fetchAll(_ db: Database) throws -> [QueryOutput] {
try all.fetchAll(db)
}

/// Returns a single value fetched from the database.
///
/// - Parameter db: A database connection.
/// - Returns: A single value decoded from the database.
@inlinable
public static func fetchOne(_ db: Database) throws -> QueryOutput? {
try all.fetchOne(db)
}

/// Returns the number of rows fetched by the query.
///
/// - Parameter db: A database connection.
/// - Returns: The number of rows fetched by the query.
@inlinable
public static func fetchCount(_ db: Database) throws -> Int {
try all.fetchCount(db)
}

/// Returns a cursor to all values fetched from the database.
///
/// - Parameter db: A database connection.
/// - Returns: A cursor to all values decoded from the database.
@inlinable
public static func fetchCursor(_ db: Database) throws -> QueryCursor<QueryOutput> {
try all.fetchCursor(db)
}
}

extension StructuredQueriesCore.PrimaryKeyedTable {
/// Returns a single value fetched from the database for a given primary key.
///
/// - Parameters
/// - db: A database connection.
/// - primaryKey: A primary key identifying a table row.
/// - Returns: A single value decoded from the database.
@inlinable
public static func find(
_ db: Database,
key primaryKey: some QueryExpression<PrimaryKey>
) throws -> QueryOutput {
try all.find(db, key: primaryKey)
}
}