From 07aecd3a959fe28b3636e4f77668e7248bda62cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 8 Dec 2016 08:43:58 +0100 Subject: [PATCH] Fix failing tests of TypedFetchRequest --- GRDB/Core/FetchRequest.swift | 58 ++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/GRDB/Core/FetchRequest.swift b/GRDB/Core/FetchRequest.swift index e3fc5e961..e8992e81d 100644 --- a/GRDB/Core/FetchRequest.swift +++ b/GRDB/Core/FetchRequest.swift @@ -213,6 +213,64 @@ extension TypedFetchRequest where FetchedType: DatabaseValueConvertible { } } +extension TypedFetchRequest where FetchedType: DatabaseValueConvertible & StatementColumnConvertible { + + // MARK: Fetching From FetchRequest + + /// TODO + /// Returns a cursor over values fetched from a fetch request. + /// + /// let nameColumn = Column("name") + /// let request = Person.select(nameColumn) + /// let names = try String.fetchCursor(db, request) // DatabaseCursor + /// while let name = try names.next() { // String + /// ... + /// } + /// + /// If the database is modified during the cursor iteration, the remaining + /// elements are undefined. + /// + /// The cursor must be iterated in a protected dispath queue. + /// + /// - parameters: + /// - db: A database connection. + /// - request: A fetch request. + /// - returns: A cursor over fetched values. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + public func fetchCursor(_ db: Database) throws -> DatabaseCursor { + return try FetchedType.fetchCursor(db, self) + } + + /// TODO + /// Returns an array of values fetched from a fetch request. + /// + /// let nameColumn = Column("name") + /// let request = Person.select(nameColumn) + /// let names = try String.fetchAll(db, request) // [String] + /// + /// - parameter db: A database connection. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + public func fetchAll(_ db: Database) throws -> [FetchedType] { + return try FetchedType.fetchAll(db, self) + } + + /// TODO + /// Returns a single value fetched from a fetch request. + /// + /// The result is nil if the query returns no row, or if no value can be + /// extracted from the first row. + /// + /// let nameColumn = Column("name") + /// let request = Person.select(nameColumn) + /// let name = try String.fetchOne(db, request) // String? + /// + /// - parameter db: A database connection. + /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. + public func fetchOne(_ db: Database) throws -> FetchedType? { + return try FetchedType.fetchOne(db, self) + } +} + /// TODO public protocol OptionalProtocol { associatedtype Value