mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
LogErrorFunction is Sendable
This commit is contained in:
1 parent
4d9d67dd67
commit
f362518dd6
5 files changed
+37
-14
No files matched your search
@@ -122,8 +122,14 @@ let SQLITE_TRANSIENT = unsafeBitCast(OpaquePointer(bitPattern: -1), to: sqlite3_
|
|||||||
/// - ``logError``
|
/// - ``logError``
|
||||||
/// - ``releaseMemory()``
|
/// - ``releaseMemory()``
|
||||||
/// - ``trace(options:_:)``
|
/// - ``trace(options:_:)``
|
||||||
|
///
|
||||||
|
/// ### Supporting Types
|
||||||
|
///
|
||||||
|
/// - ``BusyCallback``
|
||||||
|
/// - ``BusyMode``
|
||||||
/// - ``CheckpointMode``
|
/// - ``CheckpointMode``
|
||||||
/// - ``DatabaseBackupProgress``
|
/// - ``DatabaseBackupProgress``
|
||||||
|
/// - ``LogErrorFunction``
|
||||||
/// - ``StorageClass``
|
/// - ``StorageClass``
|
||||||
/// - ``TraceEvent``
|
/// - ``TraceEvent``
|
||||||
/// - ``TracingOptions``
|
/// - ``TracingOptions``
|
||||||
@@ -143,8 +149,26 @@ public final class Database: CustomStringConvertible, CustomDebugStringConvertib
|
|||||||
|
|
||||||
/// The error logging function.
|
/// The error logging function.
|
||||||
///
|
///
|
||||||
|
/// SQLite can be configured to invoke a callback function containing
|
||||||
|
/// an error code and a terse error message whenever anomalies occur.
|
||||||
|
///
|
||||||
|
/// This global error callback must be configured early in the lifetime
|
||||||
|
/// of your application:
|
||||||
|
///
|
||||||
|
/// ```swift
|
||||||
|
/// Database.logError = { (resultCode, message) in
|
||||||
|
/// NSLog("%@", "SQLite error \(resultCode): \(message)")
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// - warning: Database.logError must be set before any database
|
||||||
|
/// connection is opened. This includes the connections that your
|
||||||
|
/// application opens with GRDB, but also connections opened by
|
||||||
|
/// other tools, such as third-party libraries. Setting it after a
|
||||||
|
/// connection has been opened is an SQLite misuse, and has no effect.
|
||||||
|
///
|
||||||
/// Related SQLite documentation: <https://www.sqlite.org/errlog.html>
|
/// Related SQLite documentation: <https://www.sqlite.org/errlog.html>
|
||||||
public static var logError: LogErrorFunction? = nil {
|
nonisolated(unsafe) public static var logError: LogErrorFunction? = nil {
|
||||||
didSet {
|
didSet {
|
||||||
if logError != nil {
|
if logError != nil {
|
||||||
_registerErrorLogCallback { (_, code, message) in
|
_registerErrorLogCallback { (_, code, message) in
|
||||||
@@ -1830,7 +1854,7 @@ extension Database {
|
|||||||
|
|
||||||
// MARK: - Database-Related Types
|
// MARK: - Database-Related Types
|
||||||
|
|
||||||
/// See BusyMode and <https://www.sqlite.org/c3ref/busy_handler.html>
|
/// See ``BusyMode`` and <https://www.sqlite.org/c3ref/busy_handler.html>
|
||||||
public typealias BusyCallback = @Sendable (_ numberOfTries: Int) -> Bool
|
public typealias BusyCallback = @Sendable (_ numberOfTries: Int) -> Bool
|
||||||
|
|
||||||
/// When there are several connections to a database, a connection may try
|
/// When there are several connections to a database, a connection may try
|
||||||
@@ -2019,7 +2043,7 @@ extension Database {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// An error log function that takes an error code and message.
|
/// An error log function that takes an error code and message.
|
||||||
public typealias LogErrorFunction = (_ resultCode: ResultCode, _ message: String) -> Void
|
public typealias LogErrorFunction = @Sendable (_ resultCode: ResultCode, _ message: String) -> Void
|
||||||
|
|
||||||
/// An SQLite storage class.
|
/// An SQLite storage class.
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -397,7 +397,7 @@ class DatabasePoolTests: GRDBTestCase {
|
|||||||
XCTFail("Expected Error")
|
XCTFail("Expected Error")
|
||||||
} catch DatabaseError.SQLITE_BUSY { }
|
} catch DatabaseError.SQLITE_BUSY { }
|
||||||
}
|
}
|
||||||
XCTAssert(lastMessage!.contains("unfinalized statement: SELECT * FROM sqlite_master"))
|
XCTAssert(lastSQLiteDiagnostic.value!.message.contains("unfinalized statement: SELECT * FROM sqlite_master"))
|
||||||
|
|
||||||
// Database is not closed: no error
|
// Database is not closed: no error
|
||||||
try dbPool.write { db in
|
try dbPool.write { db in
|
||||||
@@ -427,7 +427,7 @@ class DatabasePoolTests: GRDBTestCase {
|
|||||||
//
|
//
|
||||||
// The first comes from GRDB, and the second, depending on the SQLite
|
// The first comes from GRDB, and the second, depending on the SQLite
|
||||||
// version, from `sqlite3_close_v2()`. Write the test so that it always pass:
|
// version, from `sqlite3_close_v2()`. Write the test so that it always pass:
|
||||||
XCTAssert(lastMessage!.contains("unfinalized statement"))
|
XCTAssert(lastSQLiteDiagnostic.value!.message.contains("unfinalized statement"))
|
||||||
|
|
||||||
// Database is in a zombie state.
|
// Database is in a zombie state.
|
||||||
// In the zombie state, access throws SQLITE_MISUSE
|
// In the zombie state, access throws SQLITE_MISUSE
|
||||||
|
|||||||
@@ -458,7 +458,7 @@ class DatabaseQueueTests: GRDBTestCase {
|
|||||||
XCTFail("Expected Error")
|
XCTFail("Expected Error")
|
||||||
} catch DatabaseError.SQLITE_BUSY { }
|
} catch DatabaseError.SQLITE_BUSY { }
|
||||||
}
|
}
|
||||||
XCTAssert(lastMessage!.contains("unfinalized statement: SELECT * FROM sqlite_master"))
|
XCTAssert(lastSQLiteDiagnostic.value!.message.contains("unfinalized statement: SELECT * FROM sqlite_master"))
|
||||||
|
|
||||||
// Database is not closed: no error
|
// Database is not closed: no error
|
||||||
try dbQueue.inDatabase { db in
|
try dbQueue.inDatabase { db in
|
||||||
|
|||||||
@@ -424,7 +424,7 @@ class DatabaseSnapshotTests: GRDBTestCase {
|
|||||||
XCTFail("Expected Error")
|
XCTFail("Expected Error")
|
||||||
} catch DatabaseError.SQLITE_BUSY { }
|
} catch DatabaseError.SQLITE_BUSY { }
|
||||||
}
|
}
|
||||||
XCTAssert(lastMessage!.contains("unfinalized statement: SELECT * FROM sqlite_master"))
|
XCTAssert(lastSQLiteDiagnostic.value!.message.contains("unfinalized statement: SELECT * FROM sqlite_master"))
|
||||||
|
|
||||||
// Database is not closed: no error
|
// Database is not closed: no error
|
||||||
try snapshot.read { db in
|
try snapshot.read { db in
|
||||||
|
|||||||
@@ -12,15 +12,14 @@ import XCTest
|
|||||||
@testable import GRDB
|
@testable import GRDB
|
||||||
|
|
||||||
// Support for Database.logError
|
// Support for Database.logError
|
||||||
var lastResultCode: ResultCode? = nil
|
struct SQLiteDiagnostic {
|
||||||
var lastMessage: String? = nil
|
var resultCode: ResultCode
|
||||||
|
var message: String
|
||||||
|
}
|
||||||
|
let lastSQLiteDiagnostic = Mutex<SQLiteDiagnostic?>(nil)
|
||||||
let logErrorSetup: Void = {
|
let logErrorSetup: Void = {
|
||||||
let lock = NSLock()
|
|
||||||
Database.logError = { (resultCode, message) in
|
Database.logError = { (resultCode, message) in
|
||||||
lock.lock()
|
lastSQLiteDiagnostic.value = SQLiteDiagnostic(resultCode: resultCode, message: message)
|
||||||
defer { lock.unlock() }
|
|
||||||
lastResultCode = resultCode
|
|
||||||
lastMessage = message
|
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user