From 42d29b305ea1e6d28ab6ecd7ecfa2da8ac664a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 16 Jan 2022 13:54:03 +0100 Subject: [PATCH] Cleanup --- GRDB/Core/Statement.swift | 69 ++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 34 deletions(-) diff --git a/GRDB/Core/Statement.swift b/GRDB/Core/Statement.swift index e43b78fed..255bc21de 100644 --- a/GRDB/Core/Statement.swift +++ b/GRDB/Core/Statement.swift @@ -289,6 +289,29 @@ public final class Statement { } } + /// Executes the prepared statement. + /// + /// - parameter arguments: Optional statement arguments. + /// - throws: A DatabaseError whenever an SQLite error occurs. + public func execute(arguments: StatementArguments? = nil) throws { + SchedulingWatchdog.preconditionValidQueue(database) + reset(withArguments: arguments) + try database.withAuthorizer(database.statementWillExecute(self)) { + // Iterate all rows, since they may execute side effects. + while true { + switch sqlite3_step(sqliteStatement) { + case SQLITE_DONE: + try database.statementDidExecute(self) + return + case SQLITE_ROW: + break + case let code: + try database.statementDidFail(self, withResultCode: code) + } + } + } + } + /// Calls the given closure after one successful call to `sqlite3_step()`. /// /// This method is unable to deal with statements that need a specific @@ -331,32 +354,22 @@ public final class Statement { /// 4. `Statement.forEachStep(...)` deals with the eventual authorizer. @usableFromInline func forEachStep(_ body: (SQLiteStatement) throws -> Void) throws { + SchedulingWatchdog.preconditionValidQueue(database) guard sqlite3_stmt_busy(sqliteStatement) == 0 else { // We can't deal with possible authorizer fatalError("Statement is busy") } - if let authorizer = try database.statementWillExecute(self) { - try database.withAuthorizer(authorizer) { - try uncheckedForEach(body) - } - } else { - try uncheckedForEach(body) - } - } - - /// Implementation detail of `forEach(_:)`. - /// Does not check for sqlite3_stmt_busy and eventual authorizer. - @usableFromInline - func uncheckedForEach(_ body: (SQLiteStatement) throws -> Void) throws { - while true { - switch sqlite3_step(sqliteStatement) { - case SQLITE_DONE: - try database.statementDidExecute(self) - return - case SQLITE_ROW: - try body(sqliteStatement) - case let code: - try database.statementDidFail(self, withResultCode: code) + try database.withAuthorizer(database.statementWillExecute(self)) { + while true { + switch sqlite3_step(sqliteStatement) { + case SQLITE_DONE: + try database.statementDidExecute(self) + return + case SQLITE_ROW: + try body(sqliteStatement) + case let code: + try database.statementDidFail(self, withResultCode: code) + } } } } @@ -493,18 +506,6 @@ extension Statement { return false } } - - /// Executes the prepared statement. - /// - /// - parameter arguments: Optional statement arguments. - /// - throws: A DatabaseError whenever an SQLite error occurs. - public func execute(arguments: StatementArguments? = nil) throws { - SchedulingWatchdog.preconditionValidQueue(database) - reset(withArguments: arguments) - - // Iterate all rows, since they may execute side effects. - try forEachStep { _ in } - } } // MARK: - StatementBinding