Files
GRDB.swift/GRDB/Core/FetchRequest.swift
T

187 lines
6.8 KiB
Swift

// MARK: - PreparedRequest
/// A PreparedRequest is a request that is ready to be executed.
/// :nodoc:
public struct PreparedRequest {
/// A prepared statement
public var statement: SelectStatement
/// An eventual adapter for rows fetched by the select statement
public var adapter: RowAdapter?
/// Support for eager loading of hasMany associations.
var supplementaryFetch: (([Row]) throws -> Void)?
}
extension PreparedRequest: Refinable { }
// MARK: - FetchRequest
/// The protocol for all requests that fetch database rows, and tell how those
/// rows should be interpreted.
///
/// This protocol is "closed": you can not define custom types that conform
/// to it.
public protocol FetchRequest: SQLRequestProtocol, DatabaseRegionConvertible {
/// The type that tells how fetched database rows should be interpreted.
associatedtype RowDecoder
/// Returns a PreparedRequest that is ready to be executed.
///
/// - parameter db: A database connection.
/// - parameter singleResult: A hint that a single result row will be
/// consumed. Implementations can optionally use this to optimize the
/// prepared statement.
/// - returns: A prepared request.
func makePreparedRequest(_ db: Database, forSingleResult singleResult: Bool) throws -> PreparedRequest
/// Returns the number of rows fetched by the request.
///
/// - parameter db: A database connection.
func fetchCount(_ db: Database) throws -> Int
}
extension FetchRequest {
/// Returns the number of rows fetched by the request.
///
/// The default implementation builds a naive SQL query based on the
/// statement returned by the `requestSQL(_:forSingleResult:)` method:
/// `SELECT COUNT(*) FROM (...)`.
///
/// - parameter db: A database connection.
public func fetchCount(_ db: Database) throws -> Int {
let context = SQLGenerationContext(db)
let sql = try requestSQL(context, forSingleResult: false)
let countSQL = "SELECT COUNT(*) FROM (\(sql))"
return try Int.fetchOne(db, sql: countSQL, arguments: context.arguments)!
}
/// Returns the database region that the request looks into.
///
/// This default implementation returns a region built from the statement
/// returned by the `requestSQL(_:forSingleResult)` method.
///
/// - parameter db: A database connection.
public func databaseRegion(_ db: Database) throws -> DatabaseRegion {
let context = SQLGenerationContext(db)
let sql = try requestSQL(context, forSingleResult: false)
let statement = try db.makeSelectStatement(sql: sql)
return statement.selectedRegion
}
/// Returns a PreparedRequest that is ready to be executed.
///
/// This default implementation returns a request built from the
/// `requestSQL(_:forSingleResult)` method.
///
/// - parameter db: A database connection.
/// - parameter singleResult: A hint that a single result row will be
/// consumed. Implementations can optionally use this to optimize the
/// prepared statement.
/// - returns: A prepared request.
public func makePreparedRequest(_ db: Database, forSingleResult singleResult: Bool) throws -> PreparedRequest {
let context = SQLGenerationContext(db)
let sql = try requestSQL(context, forSingleResult: singleResult)
let statement = try db.makeSelectStatement(sql: sql)
statement.arguments = context.arguments
return PreparedRequest(statement: statement)
}
}
// MARK: - AdaptedFetchRequest
extension FetchRequest {
/// Returns an adapted request.
public func adapted(_ adapter: @escaping (Database) throws -> RowAdapter) -> AdaptedFetchRequest<Self> {
AdaptedFetchRequest(self, adapter)
}
}
/// An adapted request.
public struct AdaptedFetchRequest<Base: FetchRequest>: FetchRequest {
public typealias RowDecoder = Base.RowDecoder
private let base: Base
private let adapter: (Database) throws -> RowAdapter
/// Creates an adapted request from a base request and a closure that builds
/// a row adapter from a database connection.
init(_ base: Base, _ adapter: @escaping (Database) throws -> RowAdapter) {
self.base = base
self.adapter = adapter
}
/// :nodoc:
public func requestSQL(_ context: SQLGenerationContext, forSingleResult singleResult: Bool) throws -> String {
try base.requestSQL(context, forSingleResult: singleResult)
}
/// :nodoc:
public func makePreparedRequest(_ db: Database, forSingleResult singleResult: Bool) throws -> PreparedRequest {
var request = try base.makePreparedRequest(db, forSingleResult: singleResult)
if let baseAdapter = request.adapter {
request.adapter = try ChainedAdapter(first: baseAdapter, second: adapter(db))
} else {
request.adapter = try adapter(db)
}
return request
}
/// :nodoc:
public func fetchCount(_ db: Database) throws -> Int {
try base.fetchCount(db)
}
/// :nodoc:
public func databaseRegion(_ db: Database) throws -> DatabaseRegion {
try base.databaseRegion(db)
}
}
// MARK: - AnyFetchRequest
/// A type-erased FetchRequest.
///
/// An AnyFetchRequest forwards its operations to an underlying request,
/// hiding its specifics.
public struct AnyFetchRequest<RowDecoder>: FetchRequest {
// DatabaseRegionConvertible
private let _databaseRegion: (Database) throws -> DatabaseRegion
// SQLRequestProtocol
private let _requestSQL: (SQLGenerationContext, _ singleResult: Bool) throws -> String
// FetchRequest
private let _preparedRequest: (Database, _ singleResult: Bool) throws -> PreparedRequest
private let _fetchCount: (Database) throws -> Int
#warning("TODO: force same RowDecoder, and expose asRequest(of:)")
/// Creates a request that wraps and forwards operations to `request`.
public init<Request: FetchRequest>(_ request: Request) {
_databaseRegion = request.databaseRegion
_requestSQL = request.requestSQL
_preparedRequest = request.makePreparedRequest
_fetchCount = request.fetchCount
}
/// :nodoc:
public func databaseRegion(_ db: Database) throws -> DatabaseRegion {
try _databaseRegion(db)
}
/// :nodoc:
public func requestSQL(_ context: SQLGenerationContext, forSingleResult singleResult: Bool) throws -> String {
try _requestSQL(context, singleResult)
}
/// :nodoc:
public func makePreparedRequest(_ db: Database, forSingleResult singleResult: Bool) throws -> PreparedRequest {
try _preparedRequest(db, singleResult)
}
/// :nodoc:
public func fetchCount(_ db: Database) throws -> Int {
try _fetchCount(db)
}
}