@inlinable StatementCursor

This commit is contained in:
Gwendal Roué committed 2019-02-23 17:19:56 +01:00
1 parent 9906cf799c
commit aa4f7c3d78
1 file changed
+23 -15
+23 -15
View File
@@ -348,9 +348,9 @@ public final class SelectStatement : Statement {
return columnIndexes[name.lowercased()]
}
/// Creates a cursor over the statement. This cursor does not produce any
/// value, and is only intended to give access to the sqlite3_step()
/// low-level function.
/// Creates a cursor over the statement which does not produce any
/// value. Each call to the next() cursor method calls the sqlite3_step()
/// C function.
func makeCursor(arguments: StatementArguments? = nil) -> StatementCursor {
return StatementCursor(statement: self, arguments: arguments)
}
@@ -362,23 +362,25 @@ public final class SelectStatement : Statement {
}
}
// TODO: remove public qualifier, or expose SelectStatement.makeCursor()
/// A cursor that iterates a database statement without producing any value.
/// Each call to the next() cursor method calls the sqlite3_step() C function.
///
/// For example:
///
/// try dbQueue.read { db in
/// let statement = db.makeSelectStatement(sql: "SELECT * FROM player")
/// let cursor: StatementCursor = statement.makeCursor()
/// let statement = db.makeSelectStatement(sql: "SELECT performSideEffect()")
/// let cursor = statement.makeCursor()
/// try cursor.next()
/// }
public final class StatementCursor: Cursor {
final class StatementCursor: Cursor {
public let statement: SelectStatement
private let sqliteStatement: SQLiteStatement
private var done = false
@usableFromInline let _sqliteStatement: SQLiteStatement
@usableFromInline var _done = false
// Use SelectStatement.cursor() instead
fileprivate init(statement: SelectStatement, arguments: StatementArguments? = nil) {
self.statement = statement
self.sqliteStatement = statement.sqliteStatement
self._sqliteStatement = statement.sqliteStatement
statement.reset(withArguments: arguments)
}
@@ -389,23 +391,29 @@ public final class StatementCursor: Cursor {
}
/// :nodoc:
@inlinable
public func next() throws -> Void? {
if done {
if _done {
// make sure this instance never yields a value again, even if the
// statement is reset by another cursor.
return nil
}
switch sqlite3_step(sqliteStatement) {
switch sqlite3_step(_sqliteStatement) {
case SQLITE_DONE:
done = true
_done = true
return nil
case SQLITE_ROW:
return .some(())
case let code:
statement.database.selectStatementDidFail(statement)
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
try handleErrorCode(code)
}
}
@usableFromInline
func handleErrorCode(_ code: Int32) throws -> Never {
statement.database.selectStatementDidFail(statement)
throw DatabaseError(resultCode: code, message: statement.database.lastErrorMessage, sql: statement.sql, arguments: statement.arguments)
}
}