mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
Fix #1124 for more cursors
This commit is contained in:
1 parent
3fff6b2f51
commit
bd67153095
11 files changed
+256
-134
No files matched your search
@@ -768,6 +768,13 @@ public final class EnumeratedCursor<Base: Cursor>: Cursor {
|
||||
defer { index += 1 }
|
||||
return (index, element)
|
||||
}
|
||||
|
||||
public func forEach(_ body: ((Int, Base.Element)) throws -> Void) throws {
|
||||
try base.forEach { element in
|
||||
defer { index += 1 }
|
||||
try body((index, element))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A cursor whose elements consist of the elements of some base cursor that
|
||||
@@ -791,6 +798,14 @@ public final class FilterCursor<Base: Cursor>: Cursor {
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
public func forEach(_ body: (Base.Element) throws -> Void) throws {
|
||||
try base.forEach { element in
|
||||
if try isIncluded(element) {
|
||||
try body(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A cursor consisting of all the elements contained in each segment contained
|
||||
@@ -839,6 +854,12 @@ public final class MapCursor<Base: Cursor, Element>: Cursor {
|
||||
guard let element = try base.next() else { return nil }
|
||||
return try transform(element)
|
||||
}
|
||||
|
||||
public func forEach(_ body: (Element) throws -> Void) throws {
|
||||
try base.forEach { element in
|
||||
try body(transform(element))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A cursor that only consumes up to `n` elements from an underlying
|
||||
|
||||
@@ -126,18 +126,15 @@ extension DatabaseValueConvertible {
|
||||
/// print(url)
|
||||
/// }
|
||||
/// }
|
||||
public final class DatabaseValueCursor<Value: DatabaseValueConvertible>: Cursor {
|
||||
private enum _State {
|
||||
case idle, busy, done, failed
|
||||
}
|
||||
|
||||
private let _statement: Statement
|
||||
public final class DatabaseValueCursor<Value: DatabaseValueConvertible>: DatabaseCursor {
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public var _state = _DatabaseCursorState.idle
|
||||
private let _sqliteStatement: SQLiteStatement
|
||||
private let _columnIndex: Int32
|
||||
private var _state = _State.idle
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
_statement = statement
|
||||
self.statement = statement
|
||||
_sqliteStatement = statement.sqliteStatement
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
@@ -152,12 +149,12 @@ public final class DatabaseValueCursor<Value: DatabaseValueConvertible>: Cursor
|
||||
|
||||
deinit {
|
||||
if _state == .busy {
|
||||
try? _statement.database.statementDidExecute(_statement)
|
||||
try? statement.database.statementDidExecute(statement)
|
||||
}
|
||||
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? _statement.reset()
|
||||
try? statement.reset()
|
||||
}
|
||||
|
||||
public func next() throws -> Value? {
|
||||
@@ -167,12 +164,12 @@ public final class DatabaseValueCursor<Value: DatabaseValueConvertible>: Cursor
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
case .idle:
|
||||
guard try _statement.database.statementWillExecute(_statement) == nil else {
|
||||
guard try statement.database.statementWillExecute(statement) == nil else {
|
||||
throw DatabaseError(
|
||||
resultCode: SQLITE_MISUSE,
|
||||
message: "Can't run statement that requires a customized authorizer from a cursor",
|
||||
sql: _statement.sql,
|
||||
arguments: _statement.arguments)
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
_state = .busy
|
||||
default:
|
||||
@@ -182,17 +179,17 @@ public final class DatabaseValueCursor<Value: DatabaseValueConvertible>: Cursor
|
||||
switch sqlite3_step(_sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
_state = .done
|
||||
try _statement.database.statementDidExecute(_statement)
|
||||
try statement.database.statementDidExecute(statement)
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
// TODO GRDB6: don't crash on decoding errors
|
||||
return try! Value.decode(
|
||||
fromStatement: _sqliteStatement,
|
||||
atUncheckedIndex: _columnIndex,
|
||||
context: RowDecodingContext(statement: _statement, index: Int(_columnIndex)))
|
||||
context: RowDecodingContext(statement: statement, index: Int(_columnIndex)))
|
||||
case let code:
|
||||
_state = .failed
|
||||
try _statement.database.statementDidFail(_statement, withResultCode: code)
|
||||
try statement.database.statementDidFail(statement, withResultCode: code)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -206,18 +203,15 @@ public final class DatabaseValueCursor<Value: DatabaseValueConvertible>: Cursor
|
||||
/// print(url)
|
||||
/// }
|
||||
/// }
|
||||
public final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible>: Cursor {
|
||||
private enum _State {
|
||||
case idle, busy, done, failed
|
||||
}
|
||||
|
||||
private let _statement: Statement
|
||||
public final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible>: DatabaseCursor {
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public var _state = _DatabaseCursorState.idle
|
||||
private let _sqliteStatement: SQLiteStatement
|
||||
private let _columnIndex: Int32
|
||||
private var _state = _State.idle
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
_statement = statement
|
||||
self.statement = statement
|
||||
_sqliteStatement = statement.sqliteStatement
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
@@ -232,12 +226,12 @@ public final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible>:
|
||||
|
||||
deinit {
|
||||
if _state == .busy {
|
||||
try? _statement.database.statementDidExecute(_statement)
|
||||
try? statement.database.statementDidExecute(statement)
|
||||
}
|
||||
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? _statement.reset()
|
||||
try? statement.reset()
|
||||
}
|
||||
|
||||
public func next() throws -> Value?? {
|
||||
@@ -247,12 +241,12 @@ public final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible>:
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
case .idle:
|
||||
guard try _statement.database.statementWillExecute(_statement) == nil else {
|
||||
guard try statement.database.statementWillExecute(statement) == nil else {
|
||||
throw DatabaseError(
|
||||
resultCode: SQLITE_MISUSE,
|
||||
message: "Can't run statement that requires a customized authorizer from a cursor",
|
||||
sql: _statement.sql,
|
||||
arguments: _statement.arguments)
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
_state = .busy
|
||||
default:
|
||||
@@ -262,17 +256,17 @@ public final class NullableDatabaseValueCursor<Value: DatabaseValueConvertible>:
|
||||
switch sqlite3_step(_sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
_state = .done
|
||||
try _statement.database.statementDidExecute(_statement)
|
||||
try statement.database.statementDidExecute(statement)
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
// TODO GRDB6: don't crash on decoding errors
|
||||
return try! Value.decodeIfPresent(
|
||||
fromStatement: _sqliteStatement,
|
||||
atUncheckedIndex: _columnIndex,
|
||||
context: RowDecodingContext(statement: _statement, index: Int(_columnIndex)))
|
||||
context: RowDecodingContext(statement: statement, index: Int(_columnIndex)))
|
||||
case let code:
|
||||
_state = .failed
|
||||
try _statement.database.statementDidFail(_statement, withResultCode: code)
|
||||
try statement.database.statementDidFail(statement, withResultCode: code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-7
@@ -1199,16 +1199,12 @@ extension Row {
|
||||
/// try dbQueue.read { db in
|
||||
/// let rows: RowCursor = try Row.fetchCursor(db, sql: "SELECT * FROM player")
|
||||
/// }
|
||||
public final class RowCursor: Cursor {
|
||||
private enum _State {
|
||||
case idle, busy, done, failed
|
||||
}
|
||||
|
||||
/// The statement iterated by this cursor
|
||||
public final class RowCursor: DatabaseCursor {
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public var _state = _DatabaseCursorState.idle
|
||||
private let _sqliteStatement: SQLiteStatement
|
||||
private let _row: Row // Reused for performance
|
||||
private var _state = _State.idle
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
self.statement = statement
|
||||
|
||||
+76
-15
@@ -324,6 +324,71 @@ extension Statement {
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Cursors
|
||||
|
||||
// TODO: we should move this state to `Statement`. Otherwise, two cursors
|
||||
// iterating the same statement may create an invalid state.
|
||||
/// The state of a statement cursor.
|
||||
///
|
||||
/// :nodoc:
|
||||
@frozen
|
||||
public enum _DatabaseCursorState {
|
||||
/// `sqlite3_step` was not called yet.
|
||||
case idle
|
||||
|
||||
/// `sqlite3_step` has not failed or returned `SQLITE_DONE` yet.
|
||||
case busy
|
||||
|
||||
/// `sqlite3_step` has returned `SQLITE_DONE`.
|
||||
case done
|
||||
|
||||
/// `sqlite3_step` has returned `SQLITE_DONE` an error.
|
||||
case failed
|
||||
}
|
||||
|
||||
/// Implementation details of `DatabaseCursor`.
|
||||
///
|
||||
/// :nodoc:
|
||||
public protocol _DatabaseCursor: AnyObject {
|
||||
var _state: _DatabaseCursorState { get set }
|
||||
}
|
||||
|
||||
/// A protocol for cursors that iterate a database statement.
|
||||
public protocol DatabaseCursor: _DatabaseCursor, Cursor {
|
||||
/// The statement iterated by the cursor
|
||||
var statement: Statement { get }
|
||||
}
|
||||
|
||||
extension DatabaseCursor {
|
||||
// Specific implementation of `forEach` in order to deal with
|
||||
// <https://github.com/groue/GRDB.swift/issues/1124>
|
||||
public func forEach(_ body: (Element) throws -> Void) throws {
|
||||
switch _state {
|
||||
case .busy:
|
||||
// We can't deal with possible authorizer
|
||||
fatalError("Not implemented")
|
||||
case .idle:
|
||||
if let authorizer = try statement.database.statementWillExecute(statement) {
|
||||
_state = .busy
|
||||
try statement.database.withAuthorizer(authorizer) {
|
||||
while let element = try next() {
|
||||
try body(element)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_state = .busy
|
||||
while let element = try next() {
|
||||
try body(element)
|
||||
}
|
||||
}
|
||||
default:
|
||||
while let element = try next() {
|
||||
try body(element)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A cursor that iterates a database statement without producing any value.
|
||||
/// Each call to the next() cursor method calls the sqlite3_step() C function.
|
||||
///
|
||||
@@ -334,18 +399,14 @@ extension Statement {
|
||||
/// let cursor = statement.makeCursor()
|
||||
/// try cursor.next()
|
||||
/// }
|
||||
final class StatementCursor: Cursor {
|
||||
private enum _State {
|
||||
case idle, busy, done, failed
|
||||
}
|
||||
|
||||
/* private, internal for testability */ let _statement: Statement
|
||||
final class StatementCursor: DatabaseCursor {
|
||||
let statement: Statement
|
||||
var _state = _DatabaseCursorState.idle
|
||||
private let _sqliteStatement: SQLiteStatement
|
||||
private var _state = _State.idle
|
||||
|
||||
// Use Statement.makeCursor() instead
|
||||
init(statement: Statement, arguments: StatementArguments? = nil) throws {
|
||||
_statement = statement
|
||||
self.statement = statement
|
||||
_sqliteStatement = statement.sqliteStatement
|
||||
|
||||
// Assume cursor is created for immediate iteration: reset and set arguments
|
||||
@@ -354,12 +415,12 @@ final class StatementCursor: Cursor {
|
||||
|
||||
deinit {
|
||||
if _state == .busy {
|
||||
try? _statement.database.statementDidExecute(_statement)
|
||||
try? statement.database.statementDidExecute(statement)
|
||||
}
|
||||
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? _statement.reset()
|
||||
try? statement.reset()
|
||||
}
|
||||
|
||||
/// :nodoc:
|
||||
@@ -370,12 +431,12 @@ final class StatementCursor: Cursor {
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
case .idle:
|
||||
guard try _statement.database.statementWillExecute(_statement) == nil else {
|
||||
guard try statement.database.statementWillExecute(statement) == nil else {
|
||||
throw DatabaseError(
|
||||
resultCode: SQLITE_MISUSE,
|
||||
message: "Can't run statement that requires a customized authorizer from a cursor",
|
||||
sql: _statement.sql,
|
||||
arguments: _statement.arguments)
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
_state = .busy
|
||||
default:
|
||||
@@ -385,13 +446,13 @@ final class StatementCursor: Cursor {
|
||||
switch sqlite3_step(_sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
_state = .done
|
||||
try _statement.database.statementDidExecute(_statement)
|
||||
try statement.database.statementDidExecute(statement)
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
return .some(())
|
||||
case let code:
|
||||
_state = .failed
|
||||
try _statement.database.statementDidFail(_statement, withResultCode: code)
|
||||
try statement.database.statementDidFail(statement, withResultCode: code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,19 +138,17 @@ extension DatabaseValueConvertible where Self: StatementColumnConvertible {
|
||||
/// print(name)
|
||||
/// }
|
||||
/// }
|
||||
public final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & StatementColumnConvertible> : Cursor {
|
||||
@usableFromInline
|
||||
enum _State {
|
||||
case idle, busy, done, failed
|
||||
}
|
||||
|
||||
@usableFromInline let _statement: Statement
|
||||
public final class FastDatabaseValueCursor<Value>: DatabaseCursor
|
||||
where Value: DatabaseValueConvertible & StatementColumnConvertible
|
||||
{
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public var _state = _DatabaseCursorState.idle
|
||||
@usableFromInline let _columnIndex: Int32
|
||||
@usableFromInline let _sqliteStatement: SQLiteStatement
|
||||
@usableFromInline var _state = _State.idle
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
_statement = statement
|
||||
self.statement = statement
|
||||
_sqliteStatement = statement.sqliteStatement
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
@@ -165,12 +163,12 @@ public final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & Sta
|
||||
|
||||
deinit {
|
||||
if _state == .busy {
|
||||
try? _statement.database.statementDidExecute(_statement)
|
||||
try? statement.database.statementDidExecute(statement)
|
||||
}
|
||||
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? _statement.reset()
|
||||
try? statement.reset()
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@@ -181,12 +179,12 @@ public final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & Sta
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
case .idle:
|
||||
guard try _statement.database.statementWillExecute(_statement) == nil else {
|
||||
guard try statement.database.statementWillExecute(statement) == nil else {
|
||||
throw DatabaseError(
|
||||
resultCode: SQLITE_MISUSE,
|
||||
message: "Can't run statement that requires a customized authorizer from a cursor",
|
||||
sql: _statement.sql,
|
||||
arguments: _statement.arguments)
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
_state = .busy
|
||||
default:
|
||||
@@ -196,17 +194,17 @@ public final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & Sta
|
||||
switch sqlite3_step(_sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
_state = .done
|
||||
try _statement.database.statementDidExecute(_statement)
|
||||
try statement.database.statementDidExecute(statement)
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
// TODO GRDB6: don't crash on decoding errors
|
||||
return try! Value.fastDecode(
|
||||
fromStatement: _sqliteStatement,
|
||||
atUncheckedIndex: _columnIndex,
|
||||
context: RowDecodingContext(statement: _statement, index: Int(_columnIndex)))
|
||||
context: RowDecodingContext(statement: statement, index: Int(_columnIndex)))
|
||||
case let code:
|
||||
_state = .failed
|
||||
try _statement.database.statementDidFail(_statement, withResultCode: code)
|
||||
try statement.database.statementDidFail(statement, withResultCode: code)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -221,21 +219,17 @@ public final class FastDatabaseValueCursor<Value: DatabaseValueConvertible & Sta
|
||||
/// print(email ?? "<NULL>")
|
||||
/// }
|
||||
/// }
|
||||
public final class FastNullableDatabaseValueCursor<Value>: Cursor
|
||||
public final class FastNullableDatabaseValueCursor<Value>: DatabaseCursor
|
||||
where Value: DatabaseValueConvertible & StatementColumnConvertible
|
||||
{
|
||||
@usableFromInline
|
||||
enum _State {
|
||||
case idle, busy, done, failed
|
||||
}
|
||||
|
||||
@usableFromInline let _statement: Statement
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public var _state = _DatabaseCursorState.idle
|
||||
@usableFromInline let _columnIndex: Int32
|
||||
@usableFromInline let _sqliteStatement: SQLiteStatement
|
||||
@usableFromInline var _state = _State.idle
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
_statement = statement
|
||||
self.statement = statement
|
||||
_sqliteStatement = statement.sqliteStatement
|
||||
if let adapter = adapter {
|
||||
// adapter may redefine the index of the leftmost column
|
||||
@@ -250,12 +244,12 @@ where Value: DatabaseValueConvertible & StatementColumnConvertible
|
||||
|
||||
deinit {
|
||||
if _state == .busy {
|
||||
try? _statement.database.statementDidExecute(_statement)
|
||||
try? statement.database.statementDidExecute(statement)
|
||||
}
|
||||
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? _statement.reset()
|
||||
try? statement.reset()
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@@ -266,12 +260,12 @@ where Value: DatabaseValueConvertible & StatementColumnConvertible
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
case .idle:
|
||||
guard try _statement.database.statementWillExecute(_statement) == nil else {
|
||||
guard try statement.database.statementWillExecute(statement) == nil else {
|
||||
throw DatabaseError(
|
||||
resultCode: SQLITE_MISUSE,
|
||||
message: "Can't run statement that requires a customized authorizer from a cursor",
|
||||
sql: _statement.sql,
|
||||
arguments: _statement.arguments)
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
_state = .busy
|
||||
default:
|
||||
@@ -281,17 +275,17 @@ where Value: DatabaseValueConvertible & StatementColumnConvertible
|
||||
switch sqlite3_step(_sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
_state = .done
|
||||
try _statement.database.statementDidExecute(_statement)
|
||||
try statement.database.statementDidExecute(statement)
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
// TODO GRDB6: don't crash on decoding errors
|
||||
return try! Value.fastDecodeIfPresent(
|
||||
fromStatement: _sqliteStatement,
|
||||
atUncheckedIndex: _columnIndex,
|
||||
context: RowDecodingContext(statement: _statement, index: Int(_columnIndex)))
|
||||
context: RowDecodingContext(statement: statement, index: Int(_columnIndex)))
|
||||
case let code:
|
||||
_state = .failed
|
||||
try _statement.database.statementDidFail(_statement, withResultCode: code)
|
||||
try statement.database.statementDidFail(statement, withResultCode: code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -511,18 +511,15 @@ extension FetchRequest where RowDecoder: FetchableRecord & Hashable {
|
||||
/// try dbQueue.read { db in
|
||||
/// let players: RecordCursor<Player> = try Player.fetchCursor(db, sql: "SELECT * FROM player")
|
||||
/// }
|
||||
public final class RecordCursor<Record: FetchableRecord>: Cursor {
|
||||
private enum _State {
|
||||
case idle, busy, done, failed
|
||||
}
|
||||
|
||||
private let _statement: Statement
|
||||
private let _row: Row // Reused for performance
|
||||
public final class RecordCursor<Record: FetchableRecord>: DatabaseCursor {
|
||||
public let statement: Statement
|
||||
/// :nodoc:
|
||||
public var _state = _DatabaseCursorState.idle
|
||||
private let _row: Row // Instanciated once, reused for performance
|
||||
private let _sqliteStatement: SQLiteStatement
|
||||
private var _state = _State.idle
|
||||
|
||||
init(statement: Statement, arguments: StatementArguments? = nil, adapter: RowAdapter? = nil) throws {
|
||||
_statement = statement
|
||||
self.statement = statement
|
||||
_row = try Row(statement: statement).adapted(with: adapter, layout: statement)
|
||||
_sqliteStatement = statement.sqliteStatement
|
||||
|
||||
@@ -532,12 +529,12 @@ public final class RecordCursor<Record: FetchableRecord>: Cursor {
|
||||
|
||||
deinit {
|
||||
if _state == .busy {
|
||||
try? _statement.database.statementDidExecute(_statement)
|
||||
try? statement.database.statementDidExecute(statement)
|
||||
}
|
||||
|
||||
// Statement reset fails when sqlite3_step has previously failed.
|
||||
// Just ignore reset error.
|
||||
try? _statement.reset()
|
||||
try? statement.reset()
|
||||
}
|
||||
|
||||
public func next() throws -> Record? {
|
||||
@@ -547,12 +544,12 @@ public final class RecordCursor<Record: FetchableRecord>: Cursor {
|
||||
// statement is reset by another cursor.
|
||||
return nil
|
||||
case .idle:
|
||||
guard try _statement.database.statementWillExecute(_statement) == nil else {
|
||||
guard try statement.database.statementWillExecute(statement) == nil else {
|
||||
throw DatabaseError(
|
||||
resultCode: SQLITE_MISUSE,
|
||||
message: "Can't run statement that requires a customized authorizer from a cursor",
|
||||
sql: _statement.sql,
|
||||
arguments: _statement.arguments)
|
||||
sql: statement.sql,
|
||||
arguments: statement.arguments)
|
||||
}
|
||||
_state = .busy
|
||||
default:
|
||||
@@ -562,41 +559,13 @@ public final class RecordCursor<Record: FetchableRecord>: Cursor {
|
||||
switch sqlite3_step(_sqliteStatement) {
|
||||
case SQLITE_DONE:
|
||||
_state = .done
|
||||
try _statement.database.statementDidExecute(_statement)
|
||||
try statement.database.statementDidExecute(statement)
|
||||
return nil
|
||||
case SQLITE_ROW:
|
||||
return Record(row: _row)
|
||||
case let code:
|
||||
_state = .failed
|
||||
try _statement.database.statementDidFail(_statement, withResultCode: code)
|
||||
}
|
||||
}
|
||||
|
||||
// Specific override in order to deal with https://github.com/groue/GRDB.swift/issues/1124
|
||||
/// :nodoc:
|
||||
public func forEach(_ body: (Record) throws -> Void) throws {
|
||||
switch _state {
|
||||
case .busy:
|
||||
// We can't deal with possible authorizer
|
||||
fatalError("Not implemented")
|
||||
case .idle:
|
||||
if let authorizer = try _statement.database.statementWillExecute(_statement) {
|
||||
_state = .busy
|
||||
try _statement.database.withAuthorizer(authorizer) {
|
||||
while let element = try next() {
|
||||
try body(element)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
_state = .busy
|
||||
while let element = try next() {
|
||||
try body(element)
|
||||
}
|
||||
}
|
||||
default:
|
||||
while let element = try next() {
|
||||
try body(element)
|
||||
}
|
||||
try statement.database.statementDidFail(statement, withResultCode: code)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -246,4 +246,24 @@ class CursorTests: GRDBTestCase {
|
||||
XCTAssertEqual(dictionary, [1: "a", 2: "b", 3: "a"])
|
||||
}
|
||||
}
|
||||
|
||||
func testArrayInitializer() throws {
|
||||
// Test that Array initializer calls `forEach`.
|
||||
// This is important in order to prevent
|
||||
// <https://github.com/groue/GRDB.swift/issues/1124>
|
||||
class TestCursor: Cursor {
|
||||
func next() -> Int? {
|
||||
fatalError("Must not be called during Array creation")
|
||||
}
|
||||
|
||||
func forEach(_ body: (Int) throws -> Void) throws {
|
||||
try body(0)
|
||||
try body(1)
|
||||
}
|
||||
}
|
||||
|
||||
let cursor = TestCursor()
|
||||
let elements = try Array(cursor)
|
||||
XCTAssertEqual(elements, [0, 1])
|
||||
}
|
||||
}
|
||||
@@ -18,6 +18,31 @@ class EnumeratedCursorTests: GRDBTestCase {
|
||||
XCTAssertTrue(try cursor.next() == nil) // past the end
|
||||
}
|
||||
|
||||
func testEnumeratedCursorForEach() throws {
|
||||
// Test that enumerated().forEach() calls base.forEach().
|
||||
// This is important in order to prevent
|
||||
// <https://github.com/groue/GRDB.swift/issues/1124>
|
||||
class TestCursor: Cursor {
|
||||
func next() -> String? {
|
||||
fatalError("Must not be called during forEach")
|
||||
}
|
||||
|
||||
func forEach(_ body: (String) throws -> Void) throws {
|
||||
try body("foo")
|
||||
try body("bar")
|
||||
}
|
||||
}
|
||||
|
||||
let base = TestCursor()
|
||||
let cursor = base.enumerated()
|
||||
let elements = try Array(cursor)
|
||||
XCTAssertEqual(elements.count, 2)
|
||||
XCTAssertEqual(elements[0].0, 0)
|
||||
XCTAssertEqual(elements[0].1, "foo")
|
||||
XCTAssertEqual(elements[1].0, 1)
|
||||
XCTAssertEqual(elements[1].1, "bar")
|
||||
}
|
||||
|
||||
func testEnumeratedCursorFromThrowingCursor() throws {
|
||||
var i = 0
|
||||
let strings = ["foo", "bar"]
|
||||
|
||||
@@ -14,6 +14,28 @@ class FilterCursorTests: GRDBTestCase {
|
||||
XCTAssertTrue(try cursor.next() == nil) // past the end
|
||||
}
|
||||
|
||||
func testFilterCursorForEach() throws {
|
||||
// Test that filter().forEach() calls base.forEach().
|
||||
// This is important in order to prevent
|
||||
// <https://github.com/groue/GRDB.swift/issues/1124>
|
||||
class TestCursor: Cursor {
|
||||
func next() -> Int? {
|
||||
fatalError("Must not be called during forEach")
|
||||
}
|
||||
|
||||
func forEach(_ body: (Int) throws -> Void) throws {
|
||||
try body(1)
|
||||
try body(2)
|
||||
try body(3)
|
||||
try body(4)
|
||||
}
|
||||
}
|
||||
|
||||
let base = TestCursor()
|
||||
let cursor = base.filter { $0 % 2 == 0 }
|
||||
try XCTAssertEqual(Array(cursor), [2, 4])
|
||||
}
|
||||
|
||||
func testFilterCursorFromThrowingCursor() {
|
||||
var i = 0
|
||||
let base: AnyCursor<Int> = AnyCursor {
|
||||
|
||||
@@ -13,6 +13,26 @@ class MapCursorTests: GRDBTestCase {
|
||||
XCTAssertTrue(try cursor.next() == nil)
|
||||
}
|
||||
|
||||
func testMapCursorForEach() throws {
|
||||
// Test that map().forEach() calls base.forEach().
|
||||
// This is important in order to prevent
|
||||
// <https://github.com/groue/GRDB.swift/issues/1124>
|
||||
class TestCursor: Cursor {
|
||||
func next() -> Int? {
|
||||
fatalError("Must not be called during forEach")
|
||||
}
|
||||
|
||||
func forEach(_ body: (Int) throws -> Void) throws {
|
||||
try body(1)
|
||||
try body(2)
|
||||
}
|
||||
}
|
||||
|
||||
let base = TestCursor()
|
||||
let cursor = base.map { $0 * $0 }
|
||||
try XCTAssertEqual(Array(cursor), [1, 4])
|
||||
}
|
||||
|
||||
func testMapThrowingCursor() {
|
||||
var i = 0
|
||||
let base: AnyCursor<Int> = AnyCursor {
|
||||
|
||||
@@ -29,7 +29,7 @@ class SelectStatementTests : GRDBTestCase {
|
||||
let cursor = try statement.makeCursor()
|
||||
|
||||
// Check that StatementCursor gives access to the raw SQLite API
|
||||
XCTAssertEqual(String(cString: sqlite3_column_name(cursor._statement.sqliteStatement, 0)), "firstName")
|
||||
XCTAssertEqual(String(cString: sqlite3_column_name(cursor.statement.sqliteStatement, 0)), "firstName")
|
||||
|
||||
XCTAssertFalse(try cursor.next() == nil)
|
||||
XCTAssertFalse(try cursor.next() == nil)
|
||||
@@ -44,7 +44,7 @@ class SelectStatementTests : GRDBTestCase {
|
||||
let customError = NSError(domain: "Custom", code: 0xDEAD)
|
||||
db.add(function: DatabaseFunction("throw", argumentCount: 0, pure: true) { _ in throw customError })
|
||||
func test(_ cursor: StatementCursor) throws {
|
||||
let sql = cursor._statement.sql
|
||||
let sql = cursor.statement.sql
|
||||
do {
|
||||
_ = try cursor.next()
|
||||
XCTFail()
|
||||
|
||||
Reference in new issue
Block a user