Report failures from writing to the database.

This commit is contained in:
Brent Simmons committed 2026-09-16 13:18:57 -07:00
1 parent 8177f94428
commit 8a6a94f0fb
1 file changed
+20 -6
@@ -24,13 +24,20 @@ public extension FMDatabase {
return database
}
func executeUpdateInTransaction(_ sql: String, withArgumentsIn parameters: [Any]? = nil) {
/// Returns false if the update failed and the transaction was rolled back.
@discardableResult
func executeUpdateInTransaction(_ sql: String, withArgumentsIn parameters: [Any]? = nil) -> Bool {
beginTransaction()
guard executeUpdate(sql, withArgumentsIn: parameters) else {
// Capture the error before rollback() overwrites the last-error state.
let code = lastErrorCode()
let message = lastErrorMessage() ?? "unknown"
let path = databasePath() ?? "unknown"
rollback()
return
Self.logger.error("Update failed on \(path, privacy: .public) — SQLite \(code, privacy: .public): \(message, privacy: .public)")
return false
}
commit()
return commit()
}
private static let logger = Logger(subsystem: logSubsystem, category: "FMDatabase")
@@ -68,13 +75,20 @@ public extension FMDatabase {
}
}
func insertRows(_ dictionaries: [DatabaseDictionary], insertType: RSDatabaseInsertType, tableName: String) {
/// Returns false if any row failed to insert. Every row is attempted either way.
@discardableResult
func insertRows(_ dictionaries: [DatabaseDictionary], insertType: RSDatabaseInsertType, tableName: String) -> Bool {
var didInsertAllRows = true
for dictionary in dictionaries {
insertRow(dictionary, insertType: insertType, tableName: tableName)
if !insertRow(dictionary, insertType: insertType, tableName: tableName) {
didInsertAllRows = false
}
}
return didInsertAllRows
}
func insertRow(_ dictionary: DatabaseDictionary, insertType: RSDatabaseInsertType, tableName: String) {
@discardableResult
func insertRow(_ dictionary: DatabaseDictionary, insertType: RSDatabaseInsertType, tableName: String) -> Bool {
rs_insertRow(with: dictionary, insertType: insertType, tableName: tableName)
}