mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Tests for consumption of prefetched rows into Decodable records
This commit is contained in:
1 parent
8786a60d31
commit
17e8df2d25
4 files changed
+244
-55
No files matched your search
@@ -1,3 +1,31 @@
|
||||
extension QueryInterfaceRequest where RowDecoder: FetchableRecord {
|
||||
/// An array of fetched records.
|
||||
///
|
||||
/// let request: QueryInterfaceRequest<Player> = ...
|
||||
/// let players = try request.fetchAll(db) // [Player]
|
||||
///
|
||||
/// - parameter db: A database connection.
|
||||
/// - returns: An array of records.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
@inlinable // TODO: should not be inlinable
|
||||
public func fetchAll(_ db: Database) throws -> [RowDecoder] {
|
||||
return try RowDecoder.fetchAll(db, self)
|
||||
}
|
||||
|
||||
/// The first fetched record.
|
||||
///
|
||||
/// let request: QueryInterfaceRequest<Player> = ...
|
||||
/// let player = try request.fetchOne(db) // Player?
|
||||
///
|
||||
/// - parameter db: A database connection.
|
||||
/// - returns: An optional record.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
@inlinable // TODO: should not be inlinable
|
||||
public func fetchOne(_ db: Database) throws -> RowDecoder? {
|
||||
return try RowDecoder.fetchOne(db, self)
|
||||
}
|
||||
}
|
||||
|
||||
extension FetchableRecord {
|
||||
|
||||
// MARK: Fetching From QueryInterfaceRequest
|
||||
@@ -12,11 +40,17 @@ extension FetchableRecord {
|
||||
/// - sql: a FetchRequest.
|
||||
/// - returns: An array of records.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
@inlinable
|
||||
@inlinable // TODO: should not be inlinable
|
||||
public static func fetchAll<T>(_ db: Database, _ request: QueryInterfaceRequest<T>) throws -> [Self] {
|
||||
// TODO: prefetch if needed
|
||||
let (statement, adapter) = try request.prepare(db, forSingleResult: false)
|
||||
return try fetchAll(statement, adapter: adapter)
|
||||
let associations = request.prefetchedAssociations
|
||||
if associations.isEmpty {
|
||||
return try fetchAll(statement, adapter: adapter)
|
||||
} else {
|
||||
let rows = try Row.fetchAll(statement, adapter: adapter)
|
||||
try Row.prefetch(db, associations: associations, in: rows)
|
||||
return rows.map(Self.init(row:))
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a single record fetched from a query interface request.
|
||||
@@ -29,38 +63,18 @@ extension FetchableRecord {
|
||||
/// - sql: a FetchRequest.
|
||||
/// - returns: An optional record.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
@inlinable
|
||||
@inlinable // TODO: should not be inlinable
|
||||
public static func fetchOne<T>(_ db: Database, _ request: QueryInterfaceRequest<T>) throws -> Self? {
|
||||
// TODO: prefetch if needed
|
||||
let (statement, adapter) = try request.prepare(db, forSingleResult: true)
|
||||
return try fetchOne(statement, adapter: adapter)
|
||||
}
|
||||
}
|
||||
|
||||
extension QueryInterfaceRequest where RowDecoder: FetchableRecord {
|
||||
/// An array of fetched records.
|
||||
///
|
||||
/// let request: QueryInterfaceRequest<Player> = ...
|
||||
/// let players = try request.fetchAll(db) // [Player]
|
||||
///
|
||||
/// - parameter db: A database connection.
|
||||
/// - returns: An array of records.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
@inlinable
|
||||
public func fetchAll(_ db: Database) throws -> [RowDecoder] {
|
||||
return try RowDecoder.fetchAll(db, self)
|
||||
}
|
||||
|
||||
/// The first fetched record.
|
||||
///
|
||||
/// let request: QueryInterfaceRequest<Player> = ...
|
||||
/// let player = try request.fetchOne(db) // Player?
|
||||
///
|
||||
/// - parameter db: A database connection.
|
||||
/// - returns: An optional record.
|
||||
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
||||
@inlinable
|
||||
public func fetchOne(_ db: Database) throws -> RowDecoder? {
|
||||
return try RowDecoder.fetchOne(db, self)
|
||||
let associations = request.prefetchedAssociations
|
||||
if associations.isEmpty {
|
||||
return try fetchOne(statement, adapter: adapter)
|
||||
} else {
|
||||
guard let row = try Row.fetchOne(statement, adapter: adapter) else {
|
||||
return nil
|
||||
}
|
||||
try Row.prefetch(db, associations: associations, in: [row])
|
||||
return Self.init(row: row)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user