From 7c0ca54249bb6fb8f9d174164d33d37d1559cdb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 10 Sep 2016 17:52:15 +0200 Subject: [PATCH] Swift 3: move some types declarations inside the Database class --- CHANGELOG.md | 29 ++- GRDB/Core/Configuration.swift | 6 +- GRDB/Core/Database.swift | 178 +++++++++--------- GRDB/Core/DatabasePool.swift | 12 +- GRDB/Core/DatabaseQueue.swift | 2 +- TODO.md | 2 - .../DatabaseQueueConcurrencyTests.swift | 4 +- 7 files changed, 129 insertions(+), 104 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 701e029e2..63f9c3112 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/GRDB/Core/Configuration.swift b/GRDB/Core/Configuration.swift index 872ee04a2..83ea72a66 100644 --- a/GRDB/Core/Configuration.swift +++ b/GRDB/Core/Configuration.swift @@ -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: (() -> ())? diff --git a/GRDB/Core/Database.swift b/GRDB/Core/Database.swift index a56a0d95e..3d5538aac 100644 --- a/GRDB/Core/Database.swift +++ b/GRDB/Core/Database.swift @@ -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. /// diff --git a/GRDB/Core/DatabasePool.swift b/GRDB/Core/DatabasePool.swift index e335ff52c..c2e85a73e 100644 --- a/GRDB/Core/DatabasePool.swift +++ b/GRDB/Core/DatabasePool.swift @@ -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() } -/// 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) diff --git a/GRDB/Core/DatabaseQueue.swift b/GRDB/Core/DatabaseQueue.swift index a0e3daa39..0d77fcd28 100644 --- a/GRDB/Core/DatabaseQueue.swift +++ b/GRDB/Core/DatabaseQueue.swift @@ -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) diff --git a/TODO.md b/TODO.md index 1c76af634..60a1e7116 100644 --- a/TODO.md +++ b/TODO.md @@ -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 diff --git a/Tests/Public/Core/DatabaseQueue/DatabaseQueueConcurrencyTests.swift b/Tests/Public/Core/DatabaseQueue/DatabaseQueueConcurrencyTests.swift index fe645dc45..b3386236e 100644 --- a/Tests/Public/Core/DatabaseQueue/DatabaseQueueConcurrencyTests.swift +++ b/Tests/Public/Core/DatabaseQueue/DatabaseQueueConcurrencyTests.swift @@ -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 {