diff --git a/GRDB.xcodeproj/project.pbxproj b/GRDB.xcodeproj/project.pbxproj index eebcb87e1..1e6c97890 100644 --- a/GRDB.xcodeproj/project.pbxproj +++ b/GRDB.xcodeproj/project.pbxproj @@ -327,6 +327,10 @@ 5672DE6B1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; }; 5672DE6C1CDB751D0022BA81 /* DatabasePoolBackupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */; }; 5683C2691B4D445E00296494 /* libsqlite3.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = 5683C2681B4D445E00296494 /* libsqlite3.dylib */; }; + 5687358C1CEDC334009B9116 /* FetchRequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5687358B1CEDC334009B9116 /* FetchRequestType.swift */; }; + 5687358D1CEDC334009B9116 /* FetchRequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5687358B1CEDC334009B9116 /* FetchRequestType.swift */; }; + 5687358E1CEDC334009B9116 /* FetchRequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5687358B1CEDC334009B9116 /* FetchRequestType.swift */; }; + 5687358F1CEDC334009B9116 /* FetchRequestType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5687358B1CEDC334009B9116 /* FetchRequestType.swift */; }; 568BE5231CB0371D00270F93 /* GRDBCipher.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 560FC54D1CB003810014AA8E /* GRDBCipher.framework */; }; 568E1CB21CB037BA008D97A6 /* sqlite3.h in Headers */ = {isa = PBXBuildFile; fileRef = 568BE5191CB035A900270F93 /* sqlite3.h */; settings = {ATTRIBUTES = (Public, ); }; }; 568E1CB91CB03847008D97A6 /* GRDBCipher-Bridging.h in Headers */ = {isa = PBXBuildFile; fileRef = 568E1CB71CB03847008D97A6 /* GRDBCipher-Bridging.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -892,6 +896,7 @@ 5672DE581CDB72520022BA81 /* DatabaseQueueBackupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabaseQueueBackupTests.swift; sourceTree = ""; }; 5672DE661CDB751D0022BA81 /* DatabasePoolBackupTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DatabasePoolBackupTests.swift; sourceTree = ""; }; 5683C2681B4D445E00296494 /* libsqlite3.dylib */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libsqlite3.dylib; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS8.4.sdk/usr/lib/libsqlite3.dylib; sourceTree = DEVELOPER_DIR; }; + 5687358B1CEDC334009B9116 /* FetchRequestType.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = FetchRequestType.swift; sourceTree = ""; }; 568BE5191CB035A900270F93 /* sqlite3.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = sqlite3.h; path = SQLCipher/src/sqlite3.h; sourceTree = ""; }; 568BE51F1CB0360500270F93 /* GRDB.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = GRDB.xcconfig; path = SQLCipher/GRDB.xcconfig; sourceTree = ""; }; 568BE5201CB0360500270F93 /* module.modulemap */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.module-map"; name = module.modulemap; path = SQLCipher/module.modulemap; sourceTree = ""; }; @@ -1248,6 +1253,7 @@ isa = PBXGroup; children = ( 56300B6F1C53F592005A543B /* FetchRequest.swift */, + 5687358B1CEDC334009B9116 /* FetchRequestType.swift */, 5605F1891C6B1A8700235C62 /* SQLCollation.swift */, 5605F18A1C6B1A8700235C62 /* SQLFunction.swift */, 5605F18B1C6B1A8700235C62 /* SQLOperator.swift */, @@ -2229,6 +2235,7 @@ 560FC5381CB003810014AA8E /* DatabaseDateComponents.swift in Sources */, 56193E961CD8A3E300F95862 /* FetchedRecordsController.swift in Sources */, 560FC5391CB003810014AA8E /* DatabaseReader.swift in Sources */, + 5687358D1CEDC334009B9116 /* FetchRequestType.swift in Sources */, 560FC53A1CB003810014AA8E /* NSNull.swift in Sources */, 560FC53B1CB003810014AA8E /* Database.swift in Sources */, 560FC53C1CB003810014AA8E /* DatabaseQueue.swift in Sources */, @@ -2455,6 +2462,7 @@ 56AFCA0D1CB1A8BB00F48B96 /* DatabaseDateComponents.swift in Sources */, 56AFCA0E1CB1A8BB00F48B96 /* DatabaseReader.swift in Sources */, 56AFCA0F1CB1A8BB00F48B96 /* NSNull.swift in Sources */, + 5687358F1CEDC334009B9116 /* FetchRequestType.swift in Sources */, 56AFCA101CB1A8BB00F48B96 /* DatabaseQueue.swift in Sources */, 56AFCA111CB1A8BB00F48B96 /* DatabaseError.swift in Sources */, 56AFCA121CB1A8BB00F48B96 /* NSNumber.swift in Sources */, @@ -2665,6 +2673,7 @@ 5605F15E1C672E4000235C62 /* DatabaseDateComponents.swift in Sources */, 563363C11C942C04000BE133 /* DatabaseReader.swift in Sources */, 5605F1661C672E4000235C62 /* NSNull.swift in Sources */, + 5687358E1CEDC334009B9116 /* FetchRequestType.swift in Sources */, 56A238841B9C75030082EB20 /* DatabaseQueue.swift in Sources */, 56A238821B9C75030082EB20 /* DatabaseError.swift in Sources */, 5605F1681C672E4000235C62 /* NSNumber.swift in Sources */, @@ -2874,6 +2883,7 @@ 5605F15D1C672E4000235C62 /* DatabaseDateComponents.swift in Sources */, 56193E8E1CD8A3E200F95862 /* FetchedRecordsController.swift in Sources */, 563363C01C942C04000BE133 /* DatabaseReader.swift in Sources */, + 5687358C1CEDC334009B9116 /* FetchRequestType.swift in Sources */, 5605F1651C672E4000235C62 /* NSNull.swift in Sources */, 56A2387D1B9C75030082EB20 /* Database.swift in Sources */, 56A238831B9C75030082EB20 /* DatabaseQueue.swift in Sources */, diff --git a/GRDB/FetchRequest/FetchRequest.swift b/GRDB/FetchRequest/FetchRequest.swift index 7f5fa68fe..47bbe5d1a 100644 --- a/GRDB/FetchRequest/FetchRequest.swift +++ b/GRDB/FetchRequest/FetchRequest.swift @@ -9,6 +9,15 @@ public struct FetchRequest { self.init(query: _SQLSelectQuery(select: [_SQLResultColumn.Star(nil)], from: .Table(name: tableName, alias: nil))) } + init(query: _SQLSelectQuery) { + self.query = query + } +} + +extension FetchRequest : FetchRequestType { + + public typealias FetchedType = T + /// Returns a prepared statement that is ready to be executed. /// /// - throws: A DatabaseError whenever SQLite could not parse the sql query. @@ -21,10 +30,6 @@ public struct FetchRequest { try statement.setArgumentsWithValidation(StatementArguments(bindings)) return statement } - - init(query: _SQLSelectQuery) { - self.query = query - } } @@ -193,58 +198,6 @@ extension FetchRequest { } -extension FetchRequest where T: RowConvertible { - - // MARK: Fetching Record and RowConvertible - - /// Returns a sequence of values. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.order(nameColumn) - /// let persons = request.fetch(db) // DatabaseSequence - /// - /// The returned sequence can be consumed several times, but it may yield - /// different results, should database changes have occurred between two - /// generations: - /// - /// let persons = request.fetch(db) - /// Array(persons).count // 3 - /// db.execute("DELETE ...") - /// Array(persons).count // 2 - /// - /// If the database is modified while the sequence is iterating, the - /// remaining elements are undefined. - @warn_unused_result - public func fetch(db: Database) -> DatabaseSequence { - return try! T.fetch(selectStatement(db)) - } - - /// Returns an array of values fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.order(nameColumn) - /// let persons = request.fetchAll(db) // [Person] - /// - /// - parameter db: A database connection. - @warn_unused_result - public func fetchAll(db: Database) -> [T] { - return Array(fetch(db)) - } - - /// Returns a single value fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.order(nameColumn) - /// let person = request.fetchOne(db) // Person? - /// - /// - parameter db: A database connection. - @warn_unused_result - public func fetchOne(db: Database) -> T? { - return fetch(db).generate().next() - } -} - - extension TableMapping { // MARK: Request Derivation @@ -326,316 +279,3 @@ extension TableMapping { return all().fetchCount(db) } } - - -extension DatabaseValueConvertible { - - // MARK: Fetching From FetchRequest - - /// Returns a sequence of values fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn) - /// let names = String.fetch(db, request) // DatabaseSequence - /// - /// The returned sequence can be consumed several times, but it may yield - /// different results, should database changes have occurred between two - /// generations: - /// - /// let names = String.fetch(db, request) - /// Array(names) // Arthur, Barbara - /// db.execute("DELETE ...") - /// Array(names) // Arthur - /// - /// If the database is modified while the sequence is iterating, the - /// remaining elements are undefined. - @warn_unused_result - public static func fetch(db: Database, _ request: FetchRequest) -> DatabaseSequence { - return try! fetch(request.selectStatement(db)) - } - - /// Returns an array of values fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn) - /// let names = String.fetchAll(db, request) // [String] - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchAll(db: Database, _ request: FetchRequest) -> [Self] { - return try! fetchAll(request.selectStatement(db)) - } - - /// 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 = SQLColumn("name") - /// let request = Person.select(nameColumn) - /// let name = String.fetchOne(db, request) // String? - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchOne(db: Database, _ request: FetchRequest) -> Self? { - return try! fetchOne(request.selectStatement(db)) - } -} - - -extension Optional where Wrapped: DatabaseValueConvertible { - - // MARK: Fetching From FetchRequest - - /// Returns a sequence of optional values fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn) - /// let names = Optional.fetch(db, request) // DatabaseSequence - /// - /// The returned sequence can be consumed several times, but it may yield - /// different results, should database changes have occurred between two - /// generations: - /// - /// let names = Optional.fetch(db, request) - /// Array(names) // Arthur, Barbara - /// db.execute("DELETE ...") - /// Array(names) // Arthur - /// - /// If the database is modified while the sequence is iterating, the - /// remaining elements are undefined. - @warn_unused_result - public static func fetch(db: Database, _ request: FetchRequest) -> DatabaseSequence { - return try! fetch(request.selectStatement(db)) - } - - /// Returns an array of optional values fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn) - /// let names = Optional.fetchAll(db, request) // [String?] - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchAll(db: Database, _ request: FetchRequest) -> [Wrapped?] { - return try! fetchAll(request.selectStatement(db)) - } -} - - -extension DatabaseValueConvertible where Self: StatementColumnConvertible { - - // MARK: Fetching From FetchRequest - - /// Returns a sequence of values fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn) - /// let names = String.fetch(db, request) // DatabaseSequence - /// - /// The returned sequence can be consumed several times, but it may yield - /// different results, should database changes have occurred between two - /// generations: - /// - /// let names = String.fetch(db, request) - /// Array(names) // Arthur, Barbara - /// db.execute("DELETE ...") - /// Array(names) // Arthur - /// - /// If the database is modified while the sequence is iterating, the - /// remaining elements are undefined. - @warn_unused_result - public static func fetch(db: Database, _ request: FetchRequest) -> DatabaseSequence { - return try! fetch(request.selectStatement(db)) - } - - /// Returns an array of values fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(nameColumn) - /// let names = String.fetchAll(db, request) // [String] - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchAll(db: Database, _ request: FetchRequest) -> [Self] { - return try! fetchAll(request.selectStatement(db)) - } - - /// 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 = SQLColumn("name") - /// let request = Person.select(nameColumn) - /// let name = String.fetchOne(db, request) // String? - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchOne(db: Database, _ request: FetchRequest) -> Self? { - return try! fetchOne(request.selectStatement(db)) - } -} - - -extension RowConvertible { - - // MARK: Fetching From FetchRequest - - /// Returns a sequence of records fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("firstName") - /// let request = Person.order(nameColumn) - /// let identities = Identity.fetch(db, request) // DatabaseSequence - /// - /// The returned sequence can be consumed several times, but it may yield - /// different results, should database changes have occurred between two - /// generations: - /// - /// let identities = Identity.fetch(db, request) - /// Array(identities).count // 3 - /// db.execute("DELETE ...") - /// Array(identities).count // 2 - /// - /// If the database is modified while the sequence is iterating, the - /// remaining elements are undefined. - @warn_unused_result - public static func fetch(db: Database, _ request: FetchRequest) -> DatabaseSequence { - return try! fetch(request.selectStatement(db)) - } - - /// Returns an array of records fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.order(nameColumn) - /// let identities = Identity.fetchAll(db, request) // [Identity] - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchAll(db: Database, _ request: FetchRequest) -> [Self] { - return try! fetchAll(request.selectStatement(db)) - } - - /// Returns a single record fetched from a fetch request. - /// - /// let nameColumn = SQLColumn("name") - /// let request = Person.order(nameColumn) - /// let identity = Identity.fetchOne(db, request) // Identity? - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchOne(db: Database, _ request: FetchRequest) -> Self? { - return try! fetchOne(request.selectStatement(db)) - } -} - -extension RowConvertible where Self: TableMapping { - - // MARK: Fetching All - - /// Returns a sequence of all records fetched from the database. - /// - /// let persons = Person.fetch(db) // DatabaseSequence - /// - /// The returned sequence can be consumed several times, but it may yield - /// different results, should database changes have occurred between two - /// generations: - /// - /// let persons = Person.fetch(db) - /// Array(persons).count // 3 - /// db.execute("DELETE ...") - /// Array(persons).count // 2 - /// - /// If the database is modified while the sequence is iterating, the - /// remaining elements are undefined. - @warn_unused_result - public static func fetch(db: Database) -> DatabaseSequence { - return all().fetch(db) - } - - /// Returns an array of all records fetched from the database. - /// - /// let persons = Person.fetchAll(db) // [Person] - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchAll(db: Database) -> [Self] { - return all().fetchAll(db) - } - - /// Returns the first record fetched from a fetch request. - /// - /// let person = Person.fetchOne(db) // Person? - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchOne(db: Database) -> Self? { - return all().fetchOne(db) - } -} - - -extension Row { - - // MARK: Fetching From FetchRequest - - /// Returns a sequence of rows fetched from a fetch request. - /// - /// let idColumn = SQLColumn("id") - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(idColumn, nameColumn) - /// for row in Row.fetch(db, request) { - /// let id: Int64 = row.value(atIndex: 0) - /// let name: String = row.value(atIndex: 1) - /// } - /// - /// Fetched rows are reused during the sequence iteration: don't wrap a row - /// sequence in an array with `Array(rows)` or `rows.filter { ... }` since - /// you would not get the distinct rows you expect. Use `Row.fetchAll(...)` - /// instead. - /// - /// For the same reason, make sure you make a copy whenever you extract a - /// row for later use: `row.copy()`. - /// - /// The returned sequence can be consumed several times, but it may yield - /// different results, should database changes have occurred between two - /// generations: - /// - /// let rows = Row.fetch(statement) - /// for row in rows { ... } // 3 steps - /// db.execute("DELETE ...") - /// for row in rows { ... } // 2 steps - /// - /// If the database is modified while the sequence is iterating, the - /// remaining elements of the sequence are undefined. - @warn_unused_result - public static func fetch(db: Database, _ request: FetchRequest) -> DatabaseSequence { - return try! fetch(request.selectStatement(db)) - } - - /// Returns an array of rows fetched from a fetch request. - /// - /// let idColumn = SQLColumn("id") - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(idColumn, nameColumn) - /// let rows = Row.fetchAll(db, request) - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchAll(db: Database, _ request: FetchRequest) -> [Row] { - return try! fetchAll(request.selectStatement(db)) - } - - /// Returns a single row fetched from a fetch request. - /// - /// let idColumn = SQLColumn("id") - /// let nameColumn = SQLColumn("name") - /// let request = Person.select(idColumn, nameColumn) - /// let row = Row.fetchOne(db, request) - /// - /// - parameter db: A database connection. - @warn_unused_result - public static func fetchOne(db: Database, _ request: FetchRequest) -> Row? { - return try! fetchOne(request.selectStatement(db)) - } -} diff --git a/GRDB/FetchRequest/FetchRequestType.swift b/GRDB/FetchRequest/FetchRequestType.swift new file mode 100644 index 000000000..52e91f239 --- /dev/null +++ b/GRDB/FetchRequest/FetchRequestType.swift @@ -0,0 +1,370 @@ +public protocol FetchRequestType { + associatedtype FetchedType + func selectStatement(db: Database) throws -> SelectStatement +} + + +extension FetchRequestType where FetchedType: RowConvertible { + + // MARK: Fetching Record and RowConvertible + + /// Returns a sequence of values. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.order(nameColumn) + /// let persons = request.fetch(db) // DatabaseSequence + /// + /// The returned sequence can be consumed several times, but it may yield + /// different results, should database changes have occurred between two + /// generations: + /// + /// let persons = request.fetch(db) + /// Array(persons).count // 3 + /// db.execute("DELETE ...") + /// Array(persons).count // 2 + /// + /// If the database is modified while the sequence is iterating, the + /// remaining elements are undefined. + @warn_unused_result + public func fetch(db: Database) -> DatabaseSequence { + return try! FetchedType.fetch(selectStatement(db)) + } + + /// Returns an array of values fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.order(nameColumn) + /// let persons = request.fetchAll(db) // [Person] + /// + /// - parameter db: A database connection. + @warn_unused_result + public func fetchAll(db: Database) -> [FetchedType] { + return Array(fetch(db)) + } + + /// Returns a single value fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.order(nameColumn) + /// let person = request.fetchOne(db) // Person? + /// + /// - parameter db: A database connection. + @warn_unused_result + public func fetchOne(db: Database) -> FetchedType? { + return fetch(db).generate().next() + } +} + + +extension DatabaseValueConvertible { + + // MARK: Fetching From FetchRequest + + /// Returns a sequence of values fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn) + /// let names = String.fetch(db, request) // DatabaseSequence + /// + /// The returned sequence can be consumed several times, but it may yield + /// different results, should database changes have occurred between two + /// generations: + /// + /// let names = String.fetch(db, request) + /// Array(names) // Arthur, Barbara + /// db.execute("DELETE ...") + /// Array(names) // Arthur + /// + /// If the database is modified while the sequence is iterating, the + /// remaining elements are undefined. + @warn_unused_result + public static func fetch(db: Database, _ request: Request) -> DatabaseSequence { + return try! fetch(request.selectStatement(db)) + } + + /// Returns an array of values fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn) + /// let names = String.fetchAll(db, request) // [String] + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchAll(db: Database, _ request: Request) -> [Self] { + return try! fetchAll(request.selectStatement(db)) + } + + /// 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 = SQLColumn("name") + /// let request = Person.select(nameColumn) + /// let name = String.fetchOne(db, request) // String? + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchOne(db: Database, _ request: Request) -> Self? { + return try! fetchOne(request.selectStatement(db)) + } +} + + +extension Optional where Wrapped: DatabaseValueConvertible { + + // MARK: Fetching From FetchRequest + + /// Returns a sequence of optional values fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn) + /// let names = Optional.fetch(db, request) // DatabaseSequence + /// + /// The returned sequence can be consumed several times, but it may yield + /// different results, should database changes have occurred between two + /// generations: + /// + /// let names = Optional.fetch(db, request) + /// Array(names) // Arthur, Barbara + /// db.execute("DELETE ...") + /// Array(names) // Arthur + /// + /// If the database is modified while the sequence is iterating, the + /// remaining elements are undefined. + @warn_unused_result + public static func fetch(db: Database, _ request: Request) -> DatabaseSequence { + return try! fetch(request.selectStatement(db)) + } + + /// Returns an array of optional values fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn) + /// let names = Optional.fetchAll(db, request) // [String?] + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchAll(db: Database, _ request: Request) -> [Wrapped?] { + return try! fetchAll(request.selectStatement(db)) + } +} + + +extension DatabaseValueConvertible where Self: StatementColumnConvertible { + + // MARK: Fetching From FetchRequest + + /// Returns a sequence of values fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn) + /// let names = String.fetch(db, request) // DatabaseSequence + /// + /// The returned sequence can be consumed several times, but it may yield + /// different results, should database changes have occurred between two + /// generations: + /// + /// let names = String.fetch(db, request) + /// Array(names) // Arthur, Barbara + /// db.execute("DELETE ...") + /// Array(names) // Arthur + /// + /// If the database is modified while the sequence is iterating, the + /// remaining elements are undefined. + @warn_unused_result + public static func fetch(db: Database, _ request: Request) -> DatabaseSequence { + return try! fetch(request.selectStatement(db)) + } + + /// Returns an array of values fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(nameColumn) + /// let names = String.fetchAll(db, request) // [String] + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchAll(db: Database, _ request: Request) -> [Self] { + return try! fetchAll(request.selectStatement(db)) + } + + /// 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 = SQLColumn("name") + /// let request = Person.select(nameColumn) + /// let name = String.fetchOne(db, request) // String? + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchOne(db: Database, _ request: Request) -> Self? { + return try! fetchOne(request.selectStatement(db)) + } +} + + +extension RowConvertible { + + // MARK: Fetching From FetchRequest + + /// Returns a sequence of records fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("firstName") + /// let request = Person.order(nameColumn) + /// let identities = Identity.fetch(db, request) // DatabaseSequence + /// + /// The returned sequence can be consumed several times, but it may yield + /// different results, should database changes have occurred between two + /// generations: + /// + /// let identities = Identity.fetch(db, request) + /// Array(identities).count // 3 + /// db.execute("DELETE ...") + /// Array(identities).count // 2 + /// + /// If the database is modified while the sequence is iterating, the + /// remaining elements are undefined. + @warn_unused_result + public static func fetch(db: Database, _ request: Request) -> DatabaseSequence { + return try! fetch(request.selectStatement(db)) + } + + /// Returns an array of records fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.order(nameColumn) + /// let identities = Identity.fetchAll(db, request) // [Identity] + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchAll(db: Database, _ request: Request) -> [Self] { + return try! fetchAll(request.selectStatement(db)) + } + + /// Returns a single record fetched from a fetch request. + /// + /// let nameColumn = SQLColumn("name") + /// let request = Person.order(nameColumn) + /// let identity = Identity.fetchOne(db, request) // Identity? + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchOne(db: Database, _ request: Request) -> Self? { + return try! fetchOne(request.selectStatement(db)) + } +} + + +extension RowConvertible where Self: TableMapping { + + // MARK: Fetching All + + /// Returns a sequence of all records fetched from the database. + /// + /// let persons = Person.fetch(db) // DatabaseSequence + /// + /// The returned sequence can be consumed several times, but it may yield + /// different results, should database changes have occurred between two + /// generations: + /// + /// let persons = Person.fetch(db) + /// Array(persons).count // 3 + /// db.execute("DELETE ...") + /// Array(persons).count // 2 + /// + /// If the database is modified while the sequence is iterating, the + /// remaining elements are undefined. + @warn_unused_result + public static func fetch(db: Database) -> DatabaseSequence { + return all().fetch(db) + } + + /// Returns an array of all records fetched from the database. + /// + /// let persons = Person.fetchAll(db) // [Person] + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchAll(db: Database) -> [Self] { + return all().fetchAll(db) + } + + /// Returns the first record fetched from a fetch request. + /// + /// let person = Person.fetchOne(db) // Person? + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchOne(db: Database) -> Self? { + return all().fetchOne(db) + } +} + + +extension Row { + + // MARK: Fetching From FetchRequest + + /// Returns a sequence of rows fetched from a fetch request. + /// + /// let idColumn = SQLColumn("id") + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(idColumn, nameColumn) + /// for row in Row.fetch(db, request) { + /// let id: Int64 = row.value(atIndex: 0) + /// let name: String = row.value(atIndex: 1) + /// } + /// + /// Fetched rows are reused during the sequence iteration: don't wrap a row + /// sequence in an array with `Array(rows)` or `rows.filter { ... }` since + /// you would not get the distinct rows you expect. Use `Row.fetchAll(...)` + /// instead. + /// + /// For the same reason, make sure you make a copy whenever you extract a + /// row for later use: `row.copy()`. + /// + /// The returned sequence can be consumed several times, but it may yield + /// different results, should database changes have occurred between two + /// generations: + /// + /// let rows = Row.fetch(statement) + /// for row in rows { ... } // 3 steps + /// db.execute("DELETE ...") + /// for row in rows { ... } // 2 steps + /// + /// If the database is modified while the sequence is iterating, the + /// remaining elements of the sequence are undefined. + @warn_unused_result + public static func fetch(db: Database, _ request: Request) -> DatabaseSequence { + return try! fetch(request.selectStatement(db)) + } + + /// Returns an array of rows fetched from a fetch request. + /// + /// let idColumn = SQLColumn("id") + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(idColumn, nameColumn) + /// let rows = Row.fetchAll(db, request) + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchAll(db: Database, _ request: Request) -> [Row] { + return try! fetchAll(request.selectStatement(db)) + } + + /// Returns a single row fetched from a fetch request. + /// + /// let idColumn = SQLColumn("id") + /// let nameColumn = SQLColumn("name") + /// let request = Person.select(idColumn, nameColumn) + /// let row = Row.fetchOne(db, request) + /// + /// - parameter db: A database connection. + @warn_unused_result + public static func fetchOne(db: Database, _ request: Request) -> Row? { + return try! fetchOne(request.selectStatement(db)) + } +}