mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
This avoid select statements to crash by default when there is a concurrent write.
113 lines
3.4 KiB
Swift
113 lines
3.4 KiB
Swift
/**
|
|
Configuration are arguments to the DatabaseQueue initializers.
|
|
*/
|
|
public struct Configuration {
|
|
|
|
// MARK: - Utilities
|
|
|
|
/// A tracing function that logs SQL statements
|
|
public static func logSQL(sql: String, arguments: StatementArguments?) {
|
|
if let arguments = arguments {
|
|
NSLog("GRDB: %@ -- arguments: %@", sql, arguments.description)
|
|
} else {
|
|
NSLog("GRDB: %@", sql)
|
|
}
|
|
}
|
|
|
|
// MARK: - Configuration options
|
|
|
|
/// A SQLite threading mode. See https://www.sqlite.org/threadsafe.html.
|
|
enum ThreadingMode {
|
|
case Default
|
|
case MultiThread
|
|
case Serialized
|
|
|
|
var sqliteOpenFlags: Int32 {
|
|
switch self {
|
|
case .Default:
|
|
return 0
|
|
case .MultiThread:
|
|
return SQLITE_OPEN_NOMUTEX
|
|
case .Serialized:
|
|
return SQLITE_OPEN_FULLMUTEX
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
A tracing function.
|
|
|
|
- parameter sql: An SQL query
|
|
- parameter arguments: Eventual query arguments.
|
|
*/
|
|
public typealias TraceFunction = (sql: String, arguments: StatementArguments?) -> Void
|
|
|
|
/// If true, the database has support for foreign keys.
|
|
public var foreignKeysEnabled: Bool
|
|
|
|
/// If true, the database is opened readonly.
|
|
public var readonly: Bool
|
|
|
|
/// Default transaction type
|
|
public var transactionType: Database.TransactionType
|
|
|
|
/// Busy Mode
|
|
public var busyMode: Database.BusyMode
|
|
|
|
/// The Threading mode
|
|
///
|
|
/// Not public because we don't expose any public API that could have a use
|
|
/// for it.
|
|
var threadingMode: ThreadingMode = .Default
|
|
|
|
/**
|
|
An optional tracing function.
|
|
|
|
You can use Configuration.logSQL as a tracing function: it logs all SQL
|
|
statements with NSLog().
|
|
*/
|
|
public var trace: TraceFunction?
|
|
|
|
|
|
// MARK: - Initialization
|
|
|
|
/**
|
|
Setup a configuration.
|
|
|
|
You can use Configuration.logSQL as a tracing function: it logs all SQL
|
|
statements with NSLog().
|
|
|
|
- parameter foreignKeysEnabled: If true (the default), the database has
|
|
support for foreign keys.
|
|
- parameter readonly: If false (the default), the database will be created
|
|
and opened for writing. If true, the database is opened readonly.
|
|
- parameter transactionType: An optional transaction type (default
|
|
.Immediate). Unless specified otherwise, transactions opened by
|
|
DatabaseQueue.inTransaction() will use this transaction type. See
|
|
https://www.sqlite.org/lang_transaction.html for more information.
|
|
- parameter busyMode: A BusyMode (default .ImmediateError).
|
|
- parameter trace: An optional tracing function (default nil).
|
|
- returns: A Configuration.
|
|
*/
|
|
public init(
|
|
foreignKeysEnabled: Bool = true,
|
|
readonly: Bool = false,
|
|
transactionType: Database.TransactionType = .Immediate,
|
|
busyMode: Database.BusyMode = .ImmediateError,
|
|
trace: TraceFunction? = nil)
|
|
{
|
|
self.foreignKeysEnabled = foreignKeysEnabled
|
|
self.readonly = readonly
|
|
self.transactionType = transactionType
|
|
self.busyMode = busyMode
|
|
self.trace = trace
|
|
}
|
|
|
|
|
|
// MARK: - Not public
|
|
|
|
var sqliteOpenFlags: Int32 {
|
|
return threadingMode.sqliteOpenFlags | (readonly ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE))
|
|
}
|
|
}
|