mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-23 04:04:22 +00:00
70 lines
2.4 KiB
Swift
70 lines
2.4 KiB
Swift
extension Row {
|
|
|
|
// MARK: - Fetching From QueryInterfaceRequest
|
|
|
|
/// Returns an array of rows fetched from a fetch request.
|
|
///
|
|
/// let request = Player.all()
|
|
/// let rows = try Row.fetchAll(db, request)
|
|
///
|
|
/// - parameters:
|
|
/// - db: A database connection.
|
|
/// - request: A FetchRequest.
|
|
/// - returns: An array of rows.
|
|
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
|
@inlinable
|
|
public static func fetchAll<T>(_ db: Database, _ request: QueryInterfaceRequest<T>) throws -> [Row] {
|
|
// TODO: prefetch if needed
|
|
let (statement, adapter) = try request.prepare(db, forSingleResult: false)
|
|
return try fetchAll(statement, adapter: adapter)
|
|
}
|
|
|
|
/// Returns a single row fetched from a fetch request.
|
|
///
|
|
/// let request = Player.filter(key: 1)
|
|
/// let row = try Row.fetchOne(db, request)
|
|
///
|
|
/// - parameters:
|
|
/// - db: A database connection.
|
|
/// - request: A FetchRequest.
|
|
/// - returns: An optional row.
|
|
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
|
@inlinable
|
|
public static func fetchOne<T>(_ db: Database, _ request: QueryInterfaceRequest<T>) throws -> Row? {
|
|
// TODO: prefetch if needed
|
|
let (statement, adapter) = try request.prepare(db, forSingleResult: true)
|
|
return try fetchOne(statement, adapter: adapter)
|
|
}
|
|
}
|
|
|
|
extension QueryInterfaceRequest where RowDecoder: Row {
|
|
|
|
// MARK: Fetching Rows
|
|
|
|
/// An array of fetched rows.
|
|
///
|
|
/// let request: QueryInterfaceRequest<Row> = ...
|
|
/// let rows = try request.fetchAll(db)
|
|
///
|
|
/// - parameter db: A database connection.
|
|
/// - returns: An array of fetched rows.
|
|
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
|
@inlinable
|
|
public func fetchAll(_ db: Database) throws -> [Row] {
|
|
return try Row.fetchAll(db, self)
|
|
}
|
|
|
|
/// The first fetched row.
|
|
///
|
|
/// let request: QueryInterfaceRequest<Row> = ...
|
|
/// let row = try request.fetchOne(db)
|
|
///
|
|
/// - parameter db: A database connection.
|
|
/// - returns: An optional row.
|
|
/// - throws: A DatabaseError is thrown whenever an SQLite error occurs.
|
|
@inlinable
|
|
public func fetchOne(_ db: Database) throws -> Row? {
|
|
return try Row.fetchOne(db, self)
|
|
}
|
|
}
|