mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
#147: Array(cursor) initialiser; base all fetchAll and fetchOne methods on fetchCursor.
This commit is contained in:
1 parent
e5a9159282
commit
c80f0d317a
8 files changed
+30
-23
No files matched your search
@@ -1068,7 +1068,7 @@ extension Database {
|
||||
throw DatabaseError(message: "no such table: \(tableName)")
|
||||
}
|
||||
}
|
||||
let columns = try ColumnInfo.fetchCursor(self, "PRAGMA table_info(\(tableName.quotedDatabaseIdentifier))").map { $0 }
|
||||
let columns = try Array(ColumnInfo.fetchCursor(self, "PRAGMA table_info(\(tableName.quotedDatabaseIdentifier))"))
|
||||
guard columns.count > 0 else {
|
||||
throw DatabaseError(message: "no such table: \(tableName)")
|
||||
}
|
||||
|
||||
@@ -131,7 +131,7 @@ public extension DatabaseValueConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A sequence.
|
||||
public static func fetch(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> DatabaseSequence<Self> {
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
return DatabaseSequence { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of values fetched from a prepared statement.
|
||||
@@ -145,7 +145,7 @@ public extension DatabaseValueConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: An array.
|
||||
public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> [Self] {
|
||||
return Array(fetch(statement, arguments: arguments, adapter: adapter))
|
||||
return try! Array(fetchCursor(statement, arguments: arguments, adapter: adapter))
|
||||
}
|
||||
|
||||
/// Returns a single value fetched from a prepared statement.
|
||||
@@ -350,7 +350,7 @@ public extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A sequence of optional values.
|
||||
public static func fetch(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> DatabaseSequence<Wrapped?> {
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
return DatabaseSequence { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of optional values fetched from a prepared statement.
|
||||
@@ -364,7 +364,7 @@ public extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: An array of optional values.
|
||||
public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> [Wrapped?] {
|
||||
return Array(fetch(statement, arguments: arguments, adapter: adapter))
|
||||
return try! Array(fetchCursor(statement, arguments: arguments, adapter: adapter))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -544,7 +544,7 @@ extension Row {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A sequence of rows.
|
||||
public static func fetch(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> DatabaseSequence<Row> {
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
return DatabaseSequence { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of rows fetched from a prepared statement.
|
||||
|
||||
@@ -254,11 +254,6 @@ public final class SelectStatement : Statement {
|
||||
try reset()
|
||||
return DatabaseCursor(statement: self, element: element)
|
||||
}
|
||||
|
||||
/// Creates a DatabaseSequence
|
||||
func fetch<Element>(cursor: @escaping () throws -> DatabaseCursor<Element>) -> DatabaseSequence<Element> {
|
||||
return DatabaseSequence(cursor: cursor)
|
||||
}
|
||||
}
|
||||
|
||||
/// A sequence of elements fetched from the database.
|
||||
@@ -363,6 +358,7 @@ extension DatabaseCursor {
|
||||
|
||||
/// TODO
|
||||
public func map<T>(_ transform: (Element) throws -> T) throws -> [T] {
|
||||
// TODO: cursors should have an underestimatedCount
|
||||
var result: [T] = []
|
||||
while let element = try next() {
|
||||
try result.append(transform(element))
|
||||
@@ -380,6 +376,17 @@ extension DatabaseCursor {
|
||||
}
|
||||
}
|
||||
|
||||
extension Array {
|
||||
/// TODO
|
||||
public init<Cursor : DatabaseCursor<Element>>(_ cursor: Cursor) throws {
|
||||
// TODO: cursors should have an underestimatedCount
|
||||
self.init()
|
||||
while let element = try cursor.next() {
|
||||
append(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// A iterator of elements fetched from the database.
|
||||
public final class DatabaseIterator<Element>: IteratorProtocol {
|
||||
|
||||
@@ -54,10 +54,10 @@ public extension DatabaseValueConvertible where Self: StatementColumnConvertible
|
||||
|
||||
/// TODO
|
||||
public static func fetchCursor(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws -> DatabaseCursor<Self> {
|
||||
// We'll read from leftmost column at index 0, unless adapter mangle columns
|
||||
// We'll read from leftmost column at index 0, unless adapter mangles columns
|
||||
let columnIndex: Int32
|
||||
if let adapter = adapter {
|
||||
let concreteAdapter = try! adapter.concreteRowAdapter(with: statement)
|
||||
let concreteAdapter = try adapter.concreteRowAdapter(with: statement)
|
||||
columnIndex = Int32(concreteAdapter.concreteColumnMapping.baseColumIndex(adaptedIndex: 0))
|
||||
} else {
|
||||
columnIndex = 0
|
||||
@@ -95,7 +95,7 @@ public extension DatabaseValueConvertible where Self: StatementColumnConvertible
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A sequence of values.
|
||||
public static func fetch(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> DatabaseSequence<Self> {
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
return DatabaseSequence { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of values fetched from a prepared statement.
|
||||
@@ -109,7 +109,7 @@ public extension DatabaseValueConvertible where Self: StatementColumnConvertible
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: An array of values.
|
||||
public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> [Self] {
|
||||
return Array(fetch(statement, arguments: arguments, adapter: adapter))
|
||||
return try! Array(fetchCursor(statement, arguments: arguments, adapter: adapter))
|
||||
}
|
||||
|
||||
/// Returns a single value fetched from a prepared statement.
|
||||
@@ -123,7 +123,7 @@ public extension DatabaseValueConvertible where Self: StatementColumnConvertible
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: An optional value.
|
||||
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> Self? {
|
||||
// We'll read from leftmost column at index 0, unless adapter mangle columns
|
||||
// We'll read from leftmost column at index 0, unless adapter mangles columns
|
||||
let columnIndex: Int32
|
||||
if let adapter = adapter {
|
||||
let concreteAdapter = try! adapter.concreteRowAdapter(with: statement)
|
||||
|
||||
@@ -114,7 +114,7 @@ public struct DatabaseMigrator {
|
||||
}
|
||||
|
||||
private func runMigrations(_ db: Database) throws {
|
||||
let appliedIdentifiers = try String.fetchCursor(db, "SELECT identifier FROM grdb_migrations").map { $0 }
|
||||
let appliedIdentifiers = try Array(String.fetchCursor(db, "SELECT identifier FROM grdb_migrations"))
|
||||
for migration in migrations where !appliedIdentifiers.contains(migration.identifier) {
|
||||
try migration.run(db)
|
||||
}
|
||||
|
||||
@@ -147,7 +147,7 @@ public final class FetchedRecordsController<Record: RowConvertible> {
|
||||
databaseWriter.writeForIssue117 { db in
|
||||
// TODO: handle errors
|
||||
let (statement, adapter) = try! request.prepare(db)
|
||||
let initialItems = try! Item<Record>.fetchCursor(statement, adapter: adapter).map { $0 }
|
||||
let initialItems = try! Array(Item<Record>.fetchCursor(statement, adapter: adapter))
|
||||
fetchedItems = initialItems
|
||||
|
||||
#if os(iOS)
|
||||
@@ -612,7 +612,7 @@ private final class FetchedRecordsObserver<Record: RowConvertible> : Transaction
|
||||
|
||||
databaseWriter.readFromWrite { db in
|
||||
// TODO: handle errors
|
||||
fetchedItems = try! Item<Record>.fetchCursor(db, request).map { $0 }
|
||||
fetchedItems = try! Array(Item<Record>.fetchCursor(db, request))
|
||||
fetchedAlongside = fetchAlongside(db)
|
||||
|
||||
// Fetch is complete:
|
||||
@@ -859,7 +859,7 @@ private final class FetchedRecordsObserver<Record: RowConvertible> : Transaction
|
||||
|
||||
databaseWriter.readFromWrite { db in
|
||||
// TODO: handle errors
|
||||
fetchedItems = try Item<Record>.fetchCursor(db, request).map { $0 }
|
||||
fetchedItems = try Array(Item<Record>.fetchCursor(db, request))
|
||||
fetchedAlongside = fetchAlongside(db)
|
||||
|
||||
// Fetch is complete:
|
||||
|
||||
@@ -77,7 +77,7 @@ extension RowConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: A sequence of records.
|
||||
public static func fetch(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> DatabaseSequence<Self> {
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
return DatabaseSequence { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of records fetched from a prepared statement.
|
||||
@@ -91,7 +91,7 @@ extension RowConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: An array of records.
|
||||
public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> [Self] {
|
||||
return Array(fetch(statement, arguments: arguments, adapter: adapter))
|
||||
return try! Array(fetchCursor(statement, arguments: arguments, adapter: adapter))
|
||||
}
|
||||
|
||||
/// Returns a single record fetched from a prepared statement.
|
||||
@@ -105,7 +105,7 @@ extension RowConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: An optional record.
|
||||
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> Self? {
|
||||
return fetch(statement, arguments: arguments, adapter: adapter).makeIterator().next()
|
||||
return try! fetchCursor(statement, arguments: arguments, adapter: adapter).next()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user