mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
DatabaseWriter.setKey() and reKey()
This commit is contained in:
1 parent
e3d8c06ad0
commit
20a16de9de
5 files changed
+67
-5
No files matched your search
@@ -861,7 +861,8 @@ extension DatabaseCollation {
|
||||
// MARK: - Encryption
|
||||
|
||||
extension Database {
|
||||
|
||||
|
||||
/// TODO
|
||||
class func supportsEncryption() -> Bool {
|
||||
#if SQLITE_HAS_CODEC
|
||||
return true
|
||||
@@ -870,6 +871,7 @@ extension Database {
|
||||
#endif
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func setKey(passphrase: String) throws {
|
||||
#if SQLITE_HAS_CODEC
|
||||
let data = passphrase.dataUsingEncoding(NSUTF8StringEncoding)!
|
||||
@@ -898,6 +900,7 @@ extension Database {
|
||||
#endif
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func reKey(passphrase: String) throws {
|
||||
#if SQLITE_HAS_CODEC
|
||||
let data = passphrase.dataUsingEncoding(NSUTF8StringEncoding)!
|
||||
|
||||
@@ -68,6 +68,9 @@ public final class DatabasePool {
|
||||
schemaCache: databaseSchemaCache)
|
||||
|
||||
serializedDatabase.performSync { db in
|
||||
if let passphrase = self.passphrase {
|
||||
try! db.setKey(passphrase)
|
||||
}
|
||||
for function in self.functions {
|
||||
db.addFunction(function)
|
||||
}
|
||||
@@ -98,6 +101,7 @@ public final class DatabasePool {
|
||||
return store.path
|
||||
}
|
||||
|
||||
private var passphrase: String?
|
||||
|
||||
// MARK: - WAL Management
|
||||
|
||||
@@ -393,6 +397,9 @@ extension DatabasePool : DatabaseWriter {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Reading from Database
|
||||
|
||||
/// Asynchronously executes a read-only block in a protected dispatch queue,
|
||||
/// wrapped in a deferred transaction.
|
||||
///
|
||||
@@ -431,4 +438,28 @@ extension DatabasePool : DatabaseWriter {
|
||||
}
|
||||
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Encryption
|
||||
|
||||
/// TODO
|
||||
public func setKey(passphrase: String) throws {
|
||||
try writer.performSync { db in
|
||||
try db.setKey(passphrase)
|
||||
self.readerPool.clear {
|
||||
self.passphrase = passphrase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func reKey(passphrase: String) throws {
|
||||
try writer.performSync { db in
|
||||
try db.reKey(passphrase)
|
||||
self.readerPool.clear {
|
||||
self.passphrase = passphrase
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -281,6 +281,23 @@ extension DatabaseQueue : DatabaseReader {
|
||||
|
||||
extension DatabaseQueue : DatabaseWriter {
|
||||
|
||||
// MARK: - Encryption
|
||||
|
||||
/// TODO
|
||||
public func setKey(passphrase: String) throws {
|
||||
try serializedDatabase.performSync { db in
|
||||
try db.setKey(passphrase)
|
||||
}
|
||||
}
|
||||
|
||||
/// TODO
|
||||
public func reKey(passphrase: String) throws {
|
||||
try serializedDatabase.performSync { db in
|
||||
try db.reKey(passphrase)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - DatabaseWriter Protocol Adoption
|
||||
|
||||
/// Alias for inDatabase
|
||||
|
||||
@@ -62,6 +62,15 @@ public protocol DatabaseWriter : DatabaseReader {
|
||||
/// returns when the block has completed. In the example above, the
|
||||
/// insertion is run after the select.
|
||||
func readFromWrite(block: (db: Database) -> Void)
|
||||
|
||||
|
||||
// MARK: - Encryption
|
||||
|
||||
/// TODO
|
||||
func setKey(passphrase: String) throws
|
||||
|
||||
/// TODO
|
||||
func reKey(passphrase: String) throws
|
||||
}
|
||||
|
||||
extension DatabaseWriter {
|
||||
|
||||
@@ -212,18 +212,20 @@ final class Pool<T> {
|
||||
|
||||
/// Performs a block on each pool element, available or not.
|
||||
/// The block is run is some arbitrary queue.
|
||||
func forEach(block: (T) -> ()) {
|
||||
dispatch_sync(queue) {
|
||||
func forEach(block: (T) throws -> ()) rethrows {
|
||||
try dispatchSync(queue) {
|
||||
for item in self.items {
|
||||
block(item.element)
|
||||
try block(item.element)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Empty the pool. Currently used items won't be reused.
|
||||
func clear() {
|
||||
/// Eventual block is executed before any other thread can load another element.
|
||||
func clear(block: (() -> ())? = nil) {
|
||||
dispatch_sync(queue) {
|
||||
self.items = []
|
||||
block?()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user