mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
Base DatabaseSequence on DatabaseCursor (addresses #147)
This commit is contained in:
1 parent
d97360ef52
commit
2eb190dbbb
6 files changed
+42
-116
No files matched your search
@@ -100,9 +100,7 @@ public extension DatabaseValueConvertible {
|
||||
let row = try Row(statement: statement).adaptedRow(adapter: adapter, statement: statement)
|
||||
return try statement.fetchCursor(arguments: arguments) { () -> Self in
|
||||
let dbv: DatabaseValue = row.value(atIndex: 0)
|
||||
if dbv.isNull {
|
||||
throw DatabaseError(code: SQLITE_ERROR, message: "could not convert database NULL value to \(Self.self)", sql: statement.sql, arguments: arguments)
|
||||
} else if let value = Self.fromDatabaseValue(dbv) {
|
||||
if let value = Self.fromDatabaseValue(dbv) {
|
||||
return value
|
||||
} else {
|
||||
throw DatabaseError(code: SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Self.self)", sql: statement.sql, arguments: arguments)
|
||||
@@ -133,10 +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> {
|
||||
let row = try! Row(statement: statement).adaptedRow(adapter: adapter, statement: statement)
|
||||
return statement.fetchSequence(arguments: arguments) {
|
||||
row.value(atIndex: 0)
|
||||
}
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of values fetched from a prepared statement.
|
||||
@@ -167,14 +162,8 @@ public extension DatabaseValueConvertible {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: An optional value.
|
||||
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> Self? {
|
||||
let row = try! Row(statement: statement).adaptedRow(adapter: adapter, statement: statement)
|
||||
let sequence = statement.fetchSequence(arguments: arguments) {
|
||||
row.value(atIndex: 0) as Self?
|
||||
}
|
||||
if let value = sequence.makeIterator().next() {
|
||||
return value
|
||||
}
|
||||
return nil
|
||||
let cursor = try! DatabaseValue.fetchCursor(statement, arguments: arguments, adapter: adapter)
|
||||
return try! cursor.step().flatMap { Self.fromDatabaseValue($0) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -328,10 +317,10 @@ public extension Optional where Wrapped: DatabaseValueConvertible {
|
||||
let row = try Row(statement: statement).adaptedRow(adapter: adapter, statement: statement)
|
||||
return try statement.fetchCursor(arguments: arguments) { () -> Wrapped? in
|
||||
let dbv: DatabaseValue = row.value(atIndex: 0)
|
||||
if dbv.isNull {
|
||||
return nil
|
||||
} else if let value = Wrapped.fromDatabaseValue(dbv) {
|
||||
if let value = Wrapped.fromDatabaseValue(dbv) {
|
||||
return value
|
||||
} else if dbv.isNull {
|
||||
return nil
|
||||
} else {
|
||||
throw DatabaseError(code: SQLITE_ERROR, message: "could not convert database value \(dbv) to \(Wrapped.self)", sql: statement.sql, arguments: arguments)
|
||||
}
|
||||
@@ -361,10 +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?> {
|
||||
let row = try! Row(statement: statement).adaptedRow(adapter: adapter, statement: statement)
|
||||
return statement.fetchSequence(arguments: arguments) {
|
||||
row.value(atIndex: 0) as Wrapped?
|
||||
}
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of optional values fetched from a prepared statement.
|
||||
|
||||
+4
-28
@@ -544,11 +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> {
|
||||
// Metal rows can be reused. And reusing them yields better performance.
|
||||
let row = try! Row(statement: statement).adaptedRow(adapter: adapter, statement: statement)
|
||||
return statement.fetchSequence(arguments: arguments) {
|
||||
row
|
||||
}
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of rows fetched from a prepared statement.
|
||||
@@ -562,20 +558,7 @@ extension Row {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: An array of rows.
|
||||
public static func fetchAll(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> [Row] {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
let columnNames = statement.columnNames
|
||||
let sequence: DatabaseSequence<Row>
|
||||
if let adapter = adapter {
|
||||
let concreteRowAdapter = try! adapter.concreteRowAdapter(with: statement)
|
||||
sequence = statement.fetchSequence(arguments: arguments) {
|
||||
Row(baseRow: Row(copiedFromSQLiteStatement: sqliteStatement, columnNames: columnNames), concreteRowAdapter: concreteRowAdapter)
|
||||
}
|
||||
} else {
|
||||
sequence = statement.fetchSequence(arguments: arguments) {
|
||||
Row(copiedFromSQLiteStatement: sqliteStatement, columnNames: columnNames)
|
||||
}
|
||||
}
|
||||
return Array(sequence)
|
||||
return try! fetchCursor(statement, arguments: arguments, adapter: adapter).map { $0.copy() }
|
||||
}
|
||||
|
||||
/// Returns a single row fetched from a prepared statement.
|
||||
@@ -589,15 +572,8 @@ extension Row {
|
||||
/// - adapter: Optional RowAdapter
|
||||
/// - returns: An optional row.
|
||||
public static func fetchOne(_ statement: SelectStatement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) -> Row? {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
let columnNames = statement.columnNames
|
||||
let sequence = statement.fetchSequence(arguments: arguments) {
|
||||
Row(copiedFromSQLiteStatement: sqliteStatement, columnNames: columnNames)
|
||||
}
|
||||
guard let row = sequence.makeIterator().next() else {
|
||||
return nil
|
||||
}
|
||||
return try! row.adaptedRow(adapter: adapter, statement: statement)
|
||||
let cursor = try! fetchCursor(statement, arguments: arguments, adapter: adapter)
|
||||
return try! cursor.step().flatMap { $0.copy() }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+13
-24
@@ -243,9 +243,10 @@ public final class SelectStatement : Statement {
|
||||
return columnIndexes[name.lowercased()]
|
||||
}
|
||||
|
||||
/// Creates a DatabaseCursor
|
||||
func fetchCursor<Element>(arguments: StatementArguments? = nil, element: @escaping () throws -> Element) throws -> DatabaseCursor<Element> {
|
||||
// Check that cursor is built on a valid queue.
|
||||
SchedulingWatchdog.preconditionValidQueue(database, "Database was not used on the correct thread. Create cursors in a protected dispatch queue.")
|
||||
SchedulingWatchdog.preconditionValidQueue(database, "Database was not used on the correct thread.")
|
||||
|
||||
// Force arguments validity. See UpdateStatement.execute(), and Database.execute()
|
||||
try! prepare(withArguments: arguments)
|
||||
@@ -255,48 +256,37 @@ public final class SelectStatement : Statement {
|
||||
}
|
||||
|
||||
/// Creates a DatabaseSequence
|
||||
func fetchSequence<Element>(arguments: StatementArguments? = nil, element: @escaping () -> Element) -> DatabaseSequence<Element> {
|
||||
// Force arguments validity. See UpdateStatement.execute(), and Database.execute()
|
||||
try! prepare(withArguments: arguments)
|
||||
return DatabaseSequence(statement: self, element: element)
|
||||
func fetch<Element>(cursor: @escaping () throws -> DatabaseCursor<Element>) -> DatabaseSequence<Element> {
|
||||
return DatabaseSequence(cursor: cursor)
|
||||
}
|
||||
}
|
||||
|
||||
/// A sequence of elements fetched from the database.
|
||||
public struct DatabaseSequence<Element>: Sequence {
|
||||
private let makeIteratorImpl: () throws -> DatabaseIterator<Element>
|
||||
private let iterator: () -> DatabaseIterator<Element>
|
||||
|
||||
// Statement sequence
|
||||
fileprivate init(statement: SelectStatement, element: @escaping () -> Element) {
|
||||
self.makeIteratorImpl = {
|
||||
// Check that iterator is built on a valid queue.
|
||||
SchedulingWatchdog.preconditionValidQueue(statement.database, "Database was not used on the correct thread. Iterate sequences in a protected dispatch queue, or consider using an array returned by fetchAll() instead.")
|
||||
|
||||
// Support multiple sequence iterations
|
||||
try statement.reset()
|
||||
|
||||
return DatabaseIterator(cursor: DatabaseCursor(statement: statement, element: element))
|
||||
}
|
||||
init(cursor: @escaping () throws -> DatabaseCursor<Element>) {
|
||||
self.init(iterator: { try! DatabaseIterator(cursor: cursor()) })
|
||||
}
|
||||
|
||||
// Empty sequence
|
||||
static func makeEmptySequence(inDatabase database: Database) -> DatabaseSequence {
|
||||
// Empty sequence is just as strict as statement sequence, and requires
|
||||
// to be used on the database queue.
|
||||
return DatabaseSequence() {
|
||||
// Check that iterator is built on a valid queue.
|
||||
SchedulingWatchdog.preconditionValidQueue(database, "Database was not used on the correct thread. Iterate sequences in a protected dispatch queue, or consider using an array returned by fetchAll() instead.")
|
||||
return DatabaseSequence(iterator: {
|
||||
SchedulingWatchdog.preconditionValidQueue(database, "Database was not used on the correct thread.")
|
||||
return DatabaseIterator(cursor: nil)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
private init(_ makeIteratorImpl: @escaping () throws -> DatabaseIterator<Element>) {
|
||||
self.makeIteratorImpl = makeIteratorImpl
|
||||
private init(iterator: @escaping () -> DatabaseIterator<Element>) {
|
||||
self.iterator = iterator
|
||||
}
|
||||
|
||||
/// Return a *iterator* over the elements of this *sequence*.
|
||||
public func makeIterator() -> DatabaseIterator<Element> {
|
||||
return try! makeIteratorImpl()
|
||||
return iterator()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -392,7 +382,6 @@ extension DatabaseCursor {
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ public extension DatabaseValueConvertible where Self: StatementColumnConvertible
|
||||
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
return try statement.fetchCursor(arguments: arguments) {
|
||||
guard sqlite3_column_type(sqliteStatement, columnIndex) != SQLITE_NULL else {
|
||||
if sqlite3_column_type(sqliteStatement, columnIndex) == SQLITE_NULL {
|
||||
throw DatabaseError(code: SQLITE_ERROR, message: "could not convert database NULL value to \(Self.self)", sql: statement.sql, arguments: arguments)
|
||||
}
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
@@ -95,22 +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> {
|
||||
// We'll read from leftmost column at index 0, unless adapter mangle columns
|
||||
let columnIndex: Int32
|
||||
if let adapter = adapter {
|
||||
let concreteAdapter = try! adapter.concreteRowAdapter(with: statement)
|
||||
columnIndex = Int32(concreteAdapter.concreteColumnMapping.baseColumIndex(adaptedIndex: 0))
|
||||
} else {
|
||||
columnIndex = 0
|
||||
}
|
||||
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
return statement.fetchSequence(arguments: arguments) {
|
||||
guard sqlite3_column_type(sqliteStatement, columnIndex) != SQLITE_NULL else {
|
||||
fatalError("could not convert database NULL value to \(Self.self)")
|
||||
}
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
}
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of values fetched from a prepared statement.
|
||||
@@ -138,27 +123,23 @@ 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
|
||||
let columnIndex: Int32
|
||||
if let adapter = adapter {
|
||||
let row = try! Row(statement: statement).adaptedRow(adapter: adapter, statement: statement)
|
||||
let sequence = statement.fetchSequence(arguments: arguments) {
|
||||
row.value(atIndex: 0) as Self?
|
||||
}
|
||||
if let value = sequence.makeIterator().next() {
|
||||
return value
|
||||
}
|
||||
return nil
|
||||
let concreteAdapter = try! adapter.concreteRowAdapter(with: statement)
|
||||
columnIndex = Int32(concreteAdapter.concreteColumnMapping.baseColumIndex(adaptedIndex: 0))
|
||||
} else {
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
let sequence = statement.fetchSequence(arguments: arguments) {
|
||||
(sqlite3_column_type(sqliteStatement, 0) == SQLITE_NULL) ?
|
||||
(nil as Self?) :
|
||||
Self.init(sqliteStatement: sqliteStatement, index: 0)
|
||||
}
|
||||
if let value = sequence.makeIterator().next() {
|
||||
return value
|
||||
}
|
||||
return nil
|
||||
columnIndex = 0
|
||||
}
|
||||
|
||||
let sqliteStatement = statement.sqliteStatement
|
||||
let cursor = try! statement.fetchCursor(arguments: arguments) { () -> Self? in
|
||||
if sqlite3_column_type(sqliteStatement, columnIndex) == SQLITE_NULL {
|
||||
return nil
|
||||
}
|
||||
return Self.init(sqliteStatement: sqliteStatement, index: columnIndex)
|
||||
}
|
||||
return try! cursor.step() ?? nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -77,12 +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> {
|
||||
let row = try! Row(statement: statement).adaptedRow(adapter: adapter, statement: statement)
|
||||
return statement.fetchSequence(arguments: arguments) {
|
||||
var value = self.init(row: row)
|
||||
value.awakeFromFetch(row: row)
|
||||
return value
|
||||
}
|
||||
return statement.fetch { try fetchCursor(statement, arguments: arguments, adapter: adapter) }
|
||||
}
|
||||
|
||||
/// Returns an array of records fetched from a prepared statement.
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
- [ ] DatabaseCursor: we need to be able to make fetch() reuse fetchCursor(). It's glaring in `public extension DatabaseValueConvertible where Self: StatementColumnConvertible {`
|
||||
- [ ] Swift 3.0.2 (Xcode 8.2): "Type inference will properly unwrap optionals when used with generics and implicitly-unwrapped optionals." Maybe this fixes `row.value(named: "foo") as? Int`?
|
||||
- [ ] Refactor Database notion of transaction/savepoints into a single type. Support INSERT OR ROLLBACK.
|
||||
- Since some statements may implicitly rollback transactions, we can not rely on explicit rollback statements to infer the transaction state. We can only rely on sqlite3_rollback_hook, assuming it is called even for implicit rollbacks (test with an INSERT OR ROLLBACK statement).
|
||||
|
||||
Reference in new issue
Block a user