diff --git a/GRDB/SQLiteError.swift b/GRDB/SQLiteError.swift index 1fe089c17..ca95887c1 100644 --- a/GRDB/SQLiteError.swift +++ b/GRDB/SQLiteError.swift @@ -12,13 +12,15 @@ public struct SQLiteError : ErrorType { public var code: Int { return _code } public let message: String? + public let sql: String? - init(code: Int32, message: String? = nil) { + init(code: Int32, message: String? = nil, sql: String? = nil) { self._code = Int(code) self.message = message + self.sql = sql } - init(code: Int32, cConnection: CConnection) { + init(code: Int32, cConnection: CConnection, sql: String? = nil) { let message: String? let cString = sqlite3_errmsg(cConnection) if cString == nil { @@ -26,12 +28,12 @@ public struct SQLiteError : ErrorType { } else { message = String.fromCString(cString) } - self.init(code: code, message: message) + self.init(code: code, message: message, sql: sql) } - static func checkCResultCode(code: Int32, cConnection: CConnection) throws { + static func checkCResultCode(code: Int32, cConnection: CConnection, sql: String? = nil) throws { if code != SQLITE_OK { - throw SQLiteError(code: code, cConnection: cConnection) + throw SQLiteError(code: code, cConnection: cConnection, sql: sql) } } } diff --git a/GRDB/SelectStatement.swift b/GRDB/SelectStatement.swift index ee333f947..11ecee453 100644 --- a/GRDB/SelectStatement.swift +++ b/GRDB/SelectStatement.swift @@ -93,7 +93,7 @@ public class SelectStatement : Statement { case SQLITE_ROW: return Row(statement: self, unsafe: unsafe) default: - try! SQLiteError.checkCResultCode(code, cConnection: self.database.cConnection) + try! SQLiteError.checkCResultCode(code, cConnection: self.database.cConnection, sql: self.sql) return nil } } diff --git a/GRDB/Statement.swift b/GRDB/Statement.swift index 634551951..cc4bebaaf 100644 --- a/GRDB/Statement.swift +++ b/GRDB/Statement.swift @@ -19,7 +19,7 @@ public class Statement { self.database = database self.databaseQueueID = dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) let code = sqlite3_prepare_v2(database.cConnection, sql, -1, &cStatement, nil) - try SQLiteError.checkCResultCode(code, cConnection: database.cConnection) + try SQLiteError.checkCResultCode(code, cConnection: database.cConnection, sql: sql) } deinit { @@ -59,7 +59,7 @@ public class Statement { public func reset() throws { let code = sqlite3_reset(cStatement) - try SQLiteError.checkCResultCode(code, cConnection: database.cConnection) + try SQLiteError.checkCResultCode(code, cConnection: database.cConnection, sql: sql) } public func clearBindings() { diff --git a/GRDB/UpdateStatement.swift b/GRDB/UpdateStatement.swift index 52f497e5b..53ab65c9b 100644 --- a/GRDB/UpdateStatement.swift +++ b/GRDB/UpdateStatement.swift @@ -19,7 +19,7 @@ public class UpdateStatement : Statement { // the statement has finished executing successfully break default: - try SQLiteError.checkCResultCode(code, cConnection: database.cConnection) + try SQLiteError.checkCResultCode(code, cConnection: database.cConnection, sql: sql) } } }