Last active
May 6, 2018 15:48
-
-
Save groue/f7634ffc9829d472867c65e8a01c2e8f to your computer and use it in GitHub Desktop.
ContextFetchableRecord: a new realm of records for GRDB.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A replication of the convenience fetching methods of FetchableRecord, | |
// built for another record protocol: ContextFetchableRecord | |
protocol ContextFetchableRecord { | |
associatedtype Context | |
init(row: Row, context: Context) | |
} | |
extension ContextFetchableRecord { | |
// MARK: Fetching From SelectStatement | |
static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil, context: Context) throws -> MapCursor<RowCursor, Self> { | |
return try Row.fetchCursor(statement, arguments: arguments, adapter: adapter).map { | |
Self(row: $0, context: context) | |
} | |
} | |
static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil, context: Context) throws -> [Self] { | |
return try Array(fetchCursor(statement, arguments: arguments, adapter: adapter, context: context)) | |
} | |
static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil, context: Context) throws -> Self? { | |
return try fetchCursor(statement, arguments: arguments, adapter: adapter, context: context).next() | |
} | |
} | |
extension ContextFetchableRecord { | |
// MARK: Fetching From SQL | |
static func fetchCursor(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil, context: Context) throws -> MapCursor<RowCursor, Self> { | |
return try SQLRequest<Self>(sql, arguments: arguments, adapter: adapter).fetchCursor(db, context: context) | |
} | |
static func fetchAll(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil, context: Context) throws -> [Self] { | |
return try SQLRequest<Self>(sql, arguments: arguments, adapter: adapter).fetchAll(db, context: context) | |
} | |
static func fetchOne(_ db: Database, _ sql: String, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil, context: Context) throws -> Self? { | |
return try SQLRequest<Self>(sql, arguments: arguments, adapter: adapter).fetchOne(db, context: context) | |
} | |
} | |
extension ContextFetchableRecord { | |
// MARK: Fetching From FetchRequest | |
static func fetchCursor<R: FetchRequest>(_ db: Database, _ request: R, context: Context) throws -> MapCursor<RowCursor, Self> { | |
let (statement, adapter) = try request.prepare(db) | |
return try fetchCursor(statement, adapter: adapter, context: context) | |
} | |
static func fetchAll<R: FetchRequest>(_ db: Database, _ request: R, context: Context) throws -> [Self] { | |
let (statement, adapter) = try request.prepare(db) | |
return try fetchAll(statement, adapter: adapter, context: context) | |
} | |
static func fetchOne<R: FetchRequest>(_ db: Database, _ request: R, context: Context) throws -> Self? { | |
let (statement, adapter) = try request.prepare(db) | |
return try fetchOne(statement, adapter: adapter, context: context) | |
} | |
} | |
extension FetchRequest where RowDecoder: ContextFetchableRecord { | |
// MARK: Fetching Records | |
func fetchCursor(_ db: Database, context: RowDecoder.Context) throws -> MapCursor<RowCursor, RowDecoder> { | |
return try RowDecoder.fetchCursor(db, self, context: context) | |
} | |
func fetchAll(_ db: Database, context: RowDecoder.Context) throws -> [RowDecoder] { | |
return try RowDecoder.fetchAll(db, self, context: context) | |
} | |
func fetchOne(_ db: Database, context: RowDecoder.Context) throws -> RowDecoder? { | |
return try RowDecoder.fetchOne(db, self, context: context) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment