mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Swift 3: move some types declarations inside the Database class
This commit is contained in:
1 parent
d10d4220a8
commit
7c0ca54249
7 files changed
+129
-104
No files matched your search
+28
-1
@@ -11,11 +11,17 @@ Release Notes
|
||||
|
||||
- The Swift 3 *Grand Renaming* has impacted GRDB a lot.
|
||||
|
||||
All enum cases now start with a lowercase letter.
|
||||
**General**
|
||||
|
||||
- All enum cases now start with a lowercase letter.
|
||||
|
||||
**Database Connections**
|
||||
|
||||
```diff
|
||||
-enum BusyMode
|
||||
-enum CheckpointMode
|
||||
-enum TransactionKind
|
||||
-enum TransactionCompletion
|
||||
struct Configuration {
|
||||
- var fileAttributes: [String: AnyObject]?
|
||||
+ var fileAttributes: [FileAttributeKey: Any]
|
||||
@@ -25,6 +31,27 @@ Release Notes
|
||||
- func removeTransactionObserver(transactionObserver: TransactionObserverType)
|
||||
+ func add(transactionObserver: TransactionObserver, forDatabaseEvents filter: ((DatabaseEventKind) -> Bool)? = nil)
|
||||
+ func remove(transactionObserver: TransactionObserver)
|
||||
+ typealias BusyCallback = (_ numberOfTries: Int) -> Bool
|
||||
+ enum BusyMode {
|
||||
+ case immediateError
|
||||
+ case timeout(TimeInterval)
|
||||
+ case callback(BusyCallback)
|
||||
+ }
|
||||
+ enum CheckpointMode: Int32 {
|
||||
+ case passive
|
||||
+ case full
|
||||
+ case restart
|
||||
+ case truncate
|
||||
+ }
|
||||
+ enum TransactionKind {
|
||||
+ case deferred
|
||||
+ case immediate
|
||||
+ case exclusive
|
||||
+ }
|
||||
+ enum TransactionCompletion {
|
||||
+ case commit
|
||||
+ case rollback
|
||||
+ }
|
||||
}
|
||||
class DatabasePool {
|
||||
#if os(iOS)
|
||||
|
||||
@@ -71,7 +71,7 @@ public struct Configuration {
|
||||
/// The default kind of transaction.
|
||||
///
|
||||
/// Default: Immediate
|
||||
public var defaultTransactionKind: TransactionKind = .immediate
|
||||
public var defaultTransactionKind: Database.TransactionKind = .immediate
|
||||
|
||||
|
||||
// MARK: - Concurrency
|
||||
@@ -79,7 +79,7 @@ public struct Configuration {
|
||||
/// The behavior in case of SQLITE_BUSY error. See https://www.sqlite.org/rescode.html#busy
|
||||
///
|
||||
/// Default: immediateError
|
||||
public var busyMode: BusyMode = .immediateError
|
||||
public var busyMode: Database.BusyMode = .immediateError
|
||||
|
||||
/// The maximum number of concurrent readers (applies to database
|
||||
/// pools only).
|
||||
@@ -96,7 +96,7 @@ public struct Configuration {
|
||||
|
||||
// MARK: - Not Public
|
||||
|
||||
var threadingMode: ThreadingMode = .SQLiteDefault
|
||||
var threadingMode: Database.ThreadingMode = .`default`
|
||||
var SQLiteConnectionDidOpen: (() -> ())?
|
||||
var SQLiteConnectionWillClose: ((SQLiteConnection) -> ())?
|
||||
var SQLiteConnectionDidClose: (() -> ())?
|
||||
|
||||
+93
-85
@@ -40,6 +40,99 @@ public final class Database {
|
||||
// The Database class is not thread-safe. An instance should always be
|
||||
// used through a SerializedDatabase.
|
||||
|
||||
// MARK: - Database Types
|
||||
|
||||
/// See BusyMode and https://www.sqlite.org/c3ref/busy_handler.html
|
||||
public typealias BusyCallback = (_ numberOfTries: Int) -> Bool
|
||||
|
||||
/// When there are several connections to a database, a connection may try
|
||||
/// to access the database while it is locked by another connection.
|
||||
///
|
||||
/// The BusyMode enum describes the behavior of GRDB when such a situation
|
||||
/// occurs:
|
||||
///
|
||||
/// - .immediateError: The SQLITE_BUSY error is immediately returned to the
|
||||
/// connection that tries to access the locked database.
|
||||
///
|
||||
/// - .timeout: The SQLITE_BUSY error will be returned only if the database
|
||||
/// remains locked for more than the specified duration.
|
||||
///
|
||||
/// - .callback: Perform your custom lock handling.
|
||||
///
|
||||
/// To set the busy mode of a database, use Configuration:
|
||||
///
|
||||
/// let configuration = Configuration(busyMode: .timeout(1))
|
||||
/// let dbQueue = DatabaseQueue(path: "...", configuration: configuration)
|
||||
///
|
||||
/// Relevant SQLite documentation:
|
||||
///
|
||||
/// - https://www.sqlite.org/c3ref/busy_timeout.html
|
||||
/// - https://www.sqlite.org/c3ref/busy_handler.html
|
||||
/// - https://www.sqlite.org/lang_transaction.html
|
||||
/// - https://www.sqlite.org/wal.html
|
||||
public enum BusyMode {
|
||||
/// The SQLITE_BUSY error is immediately returned to the connection that
|
||||
/// tries to access the locked database.
|
||||
case immediateError
|
||||
|
||||
/// The SQLITE_BUSY error will be returned only if the database remains
|
||||
/// locked for more than the specified duration.
|
||||
case timeout(TimeInterval)
|
||||
|
||||
/// A custom callback that is called when a database is locked.
|
||||
/// See https://www.sqlite.org/c3ref/busy_handler.html
|
||||
case callback(BusyCallback)
|
||||
}
|
||||
|
||||
/// The available [checkpoint modes](https://www.sqlite.org/c3ref/wal_checkpoint_v2.html).
|
||||
public enum CheckpointMode: Int32 {
|
||||
case passive = 0 // SQLITE_CHECKPOINT_PASSIVE
|
||||
case full = 1 // SQLITE_CHECKPOINT_FULL
|
||||
case restart = 2 // SQLITE_CHECKPOINT_RESTART
|
||||
case truncate = 3 // SQLITE_CHECKPOINT_TRUNCATE
|
||||
}
|
||||
|
||||
/// An 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// An SQLite transaction kind. See https://www.sqlite.org/lang_transaction.html
|
||||
public enum TransactionKind {
|
||||
case deferred
|
||||
case immediate
|
||||
case exclusive
|
||||
}
|
||||
|
||||
/// The end of a transaction: Commit, or Rollback
|
||||
public enum TransactionCompletion {
|
||||
case commit
|
||||
case rollback
|
||||
}
|
||||
|
||||
/// The states that keep track of transaction completions in order to notify
|
||||
/// transaction observers.
|
||||
private enum TransactionState {
|
||||
case waitForTransactionCompletion
|
||||
case commit
|
||||
case rollback
|
||||
case rollbackFromTransactionObserver(Error)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Database Information
|
||||
|
||||
/// The database configuration
|
||||
@@ -288,68 +381,6 @@ private func closeConnection(_ sqliteConnection: SQLiteConnection) {
|
||||
}
|
||||
|
||||
|
||||
/// An SQLite threading mode. See https://www.sqlite.org/threadsafe.html.
|
||||
enum ThreadingMode {
|
||||
case SQLiteDefault
|
||||
case multiThread
|
||||
case serialized
|
||||
|
||||
var SQLiteOpenFlags: Int32 {
|
||||
switch self {
|
||||
case .SQLiteDefault:
|
||||
return 0
|
||||
case .multiThread:
|
||||
return SQLITE_OPEN_NOMUTEX
|
||||
case .serialized:
|
||||
return SQLITE_OPEN_FULLMUTEX
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// See BusyMode and https://www.sqlite.org/c3ref/busy_handler.html
|
||||
public typealias BusyCallback = (_ numberOfTries: Int) -> Bool
|
||||
|
||||
/// When there are several connections to a database, a connection may try to
|
||||
/// access the database while it is locked by another connection.
|
||||
///
|
||||
/// The BusyMode enum describes the behavior of GRDB when such a situation
|
||||
/// occurs:
|
||||
///
|
||||
/// - .immediateError: The SQLITE_BUSY error is immediately returned to the
|
||||
/// connection that tries to access the locked database.
|
||||
///
|
||||
/// - .timeout: The SQLITE_BUSY error will be returned only if the database
|
||||
/// remains locked for more than the specified duration.
|
||||
///
|
||||
/// - .callback: Perform your custom lock handling.
|
||||
///
|
||||
/// To set the busy mode of a database, use Configuration:
|
||||
///
|
||||
/// let configuration = Configuration(busyMode: .timeout(1))
|
||||
/// let dbQueue = DatabaseQueue(path: "...", configuration: configuration)
|
||||
///
|
||||
/// Relevant SQLite documentation:
|
||||
///
|
||||
/// - https://www.sqlite.org/c3ref/busy_timeout.html
|
||||
/// - https://www.sqlite.org/c3ref/busy_handler.html
|
||||
/// - https://www.sqlite.org/lang_transaction.html
|
||||
/// - https://www.sqlite.org/wal.html
|
||||
public enum BusyMode {
|
||||
/// The SQLITE_BUSY error is immediately returned to the connection that
|
||||
/// tries to access the locked database.
|
||||
case immediateError
|
||||
|
||||
/// The SQLITE_BUSY error will be returned only if the database remains
|
||||
/// locked for more than the specified duration.
|
||||
case timeout(TimeInterval)
|
||||
|
||||
/// A custom callback that is called when a database is locked.
|
||||
/// See https://www.sqlite.org/c3ref/busy_handler.html
|
||||
case callback(BusyCallback)
|
||||
}
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// MARK: - Statements
|
||||
|
||||
@@ -1692,29 +1723,6 @@ extension Database {
|
||||
}
|
||||
|
||||
|
||||
/// An SQLite transaction kind. See https://www.sqlite.org/lang_transaction.html
|
||||
public enum TransactionKind {
|
||||
case deferred
|
||||
case immediate
|
||||
case exclusive
|
||||
}
|
||||
|
||||
|
||||
/// The end of a transaction: Commit, or Rollback
|
||||
public enum TransactionCompletion {
|
||||
case commit
|
||||
case rollback
|
||||
}
|
||||
|
||||
/// The states that keep track of transaction completions in order to notify
|
||||
/// transaction observers.
|
||||
private enum TransactionState {
|
||||
case waitForTransactionCompletion
|
||||
case commit
|
||||
case rollback
|
||||
case rollbackFromTransactionObserver(Error)
|
||||
}
|
||||
|
||||
/// A transaction observer is notified of all changes and transactions committed
|
||||
/// or rollbacked on a database.
|
||||
///
|
||||
|
||||
@@ -128,7 +128,7 @@ public final class DatabasePool {
|
||||
/// more information.
|
||||
///
|
||||
/// - parameter kind: The checkpoint mode (default passive)
|
||||
public func checkpoint(_ kind: CheckpointMode = .passive) throws {
|
||||
public func checkpoint(_ kind: Database.CheckpointMode = .passive) throws {
|
||||
try writer.sync { db in
|
||||
// TODO: read https://www.sqlite.org/c3ref/wal_checkpoint_v2.html and
|
||||
// check whether we need a busy handler on writer and/or readers
|
||||
@@ -220,14 +220,6 @@ public final class DatabasePool {
|
||||
fileprivate var collations = Set<DatabaseCollation>()
|
||||
}
|
||||
|
||||
/// The available [checkpoint modes](https://www.sqlite.org/c3ref/wal_checkpoint_v2.html).
|
||||
public enum CheckpointMode: Int32 {
|
||||
case passive = 0 // SQLITE_CHECKPOINT_PASSIVE
|
||||
case full = 1 // SQLITE_CHECKPOINT_FULL
|
||||
case restart = 2 // SQLITE_CHECKPOINT_RESTART
|
||||
case truncate = 3 // SQLITE_CHECKPOINT_TRUNCATE
|
||||
}
|
||||
|
||||
|
||||
// =========================================================================
|
||||
// MARK: - Encryption
|
||||
@@ -423,7 +415,7 @@ extension DatabasePool : DatabaseWriter {
|
||||
/// - block: A block that executes SQL statements and return either
|
||||
/// .commit or .rollback.
|
||||
/// - throws: The error thrown by the block.
|
||||
public func writeInTransaction(_ kind: TransactionKind? = nil, _ block: (Database) throws -> TransactionCompletion) throws {
|
||||
public func writeInTransaction(_ kind: Database.TransactionKind? = nil, _ block: (Database) throws -> Database.TransactionCompletion) throws {
|
||||
try writer.sync { db in
|
||||
try db.inTransaction(kind) {
|
||||
try block(db)
|
||||
|
||||
@@ -104,7 +104,7 @@ public final class DatabaseQueue {
|
||||
/// - block: A block that executes SQL statements and return either
|
||||
/// .commit or .rollback.
|
||||
/// - throws: The error thrown by the block.
|
||||
public func inTransaction(_ kind: TransactionKind? = nil, _ block: (Database) throws -> TransactionCompletion) throws {
|
||||
public func inTransaction(_ kind: Database.TransactionKind? = nil, _ block: (Database) throws -> Database.TransactionCompletion) throws {
|
||||
try serializedDatabase.sync { db in
|
||||
try db.inTransaction(kind) {
|
||||
try block(db)
|
||||
|
||||
@@ -7,8 +7,6 @@
|
||||
- DatabasePoolReadOnlyTests
|
||||
- DatabaseQueueConcurrencyTests
|
||||
- [ ] Swift3: return Self when possible
|
||||
- [ ] Swift3: have a look at the new GCD: https://medium.com/swift-and-ios-writing/a-quick-look-at-gcd-and-swift-3-732bef6e1838#.oplqethl5
|
||||
- [ ] Swift3: move more top-level enums inside an owner type. Foundation uses lowercase for those enum *types* (besides the lowercase *values*)
|
||||
- [ ] Swift3: check .autoreleaseWorkItem option in GCD Queue creation
|
||||
- [ ] Swift3: move database event filtering into the TransactionObserver protocol
|
||||
- [ ] Swift3: Fondation conversion between Row and DatabaseValue
|
||||
|
||||
@@ -8,13 +8,13 @@ import XCTest
|
||||
#endif
|
||||
|
||||
class ConcurrencyTests: GRDBTestCase {
|
||||
var busyCallback: BusyCallback?
|
||||
var busyCallback: Database.BusyCallback?
|
||||
|
||||
override func setUp() {
|
||||
super.setUp()
|
||||
|
||||
self.busyCallback = nil
|
||||
let busyCallback: BusyCallback = { numberOfTries in
|
||||
let busyCallback: Database.BusyCallback = { numberOfTries in
|
||||
if let busyCallback = self.busyCallback {
|
||||
return busyCallback(numberOfTries)
|
||||
} else {
|
||||
|
||||
Reference in new issue
Block a user