/// A DatabaseSnapshot sees an unchanging database content, as it existed at the /// moment it was created. /// /// See DatabasePool.makeSnapshot() /// /// For more information, read about "snapshot isolation" at https://sqlite.org/isolation.html public class DatabaseSnapshot : DatabaseReader { private var serializedDatabase: SerializedDatabase /// The database configuration var configuration: Configuration { return serializedDatabase.configuration } init(path: String, configuration: Configuration = Configuration()) throws { var configuration = configuration configuration.readonly = true configuration.allowsUnsafeTransactions = true // Snaphost keeps a long-lived transaction serializedDatabase = try SerializedDatabase( path: path, configuration: configuration, schemaCache: SimpleDatabaseSchemaCache()) try serializedDatabase.sync { db in // Assert WAL mode let journalMode = try String.fetchOne(db, "PRAGMA journal_mode") guard journalMode == "wal" else { throw DatabaseError(message: "WAL mode is not activated at path: \(path)") } // Establish snapshot isolation (see deinit) try db.beginTransaction(.deferred) // Take snapshot // See DatabasePool.readFromCurrentState for a complete discussion try db.makeSelectStatement("SELECT rootpage FROM sqlite_master").cursor().next() } } deinit { // Leave snapshot isolation serializedDatabase.sync { db in try? db.commit() } } } // DatabaseReader extension DatabaseSnapshot { // MARK: - Reading from Database /// Synchronously executes a read-only block that takes a database /// connection, and returns its result. /// /// let players = try snapshot.read { db in /// try Player.fetchAll(...) /// } /// /// - parameter block: A block that accesses the database. /// - throws: The error thrown by the block. public func read(_ block: (Database) throws -> T) rethrows -> T { return try serializedDatabase.sync(block) } /// Alias for `read`. See `DatabaseReader.unsafeRead`. /// /// :nodoc: public func unsafeRead(_ block: (Database) throws -> T) rethrows -> T { return try serializedDatabase.sync(block) } /// Alias for `read`. See `DatabaseReader.unsafeReentrantRead`. /// /// :nodoc: public func unsafeReentrantRead(_ block: (Database) throws -> T) throws -> T { return try serializedDatabase.sync(block) } // MARK: - Functions /// Add or redefine an SQL function. /// /// let fn = DatabaseFunction("succ", argumentCount: 1) { dbValues in /// guard let int = Int.fromDatabaseValue(dbValues[0]) else { /// return nil /// } /// return int + 1 /// } /// snapshot.add(function: fn) /// try snapshot.read { db in /// try Int.fetchOne(db, "SELECT succ(1)") // 2 /// } public func add(function: DatabaseFunction) { serializedDatabase.sync { $0.add(function: function) } } /// Remove an SQL function. public func remove(function: DatabaseFunction) { serializedDatabase.sync { $0.remove(function: function) } } // MARK: - Collations /// Add or redefine a collation. /// /// let collation = DatabaseCollation("localized_standard") { (string1, string2) in /// return (string1 as NSString).localizedStandardCompare(string2) /// } /// snapshot.add(collation: collation) /// let files = try snapshot.read { db in /// try File.fetchAll(db, "SELECT * FROM files ORDER BY name COLLATE localized_standard") /// } public func add(collation: DatabaseCollation) { serializedDatabase.sync { $0.add(collation: collation) } } /// Remove a collation. public func remove(collation: DatabaseCollation) { serializedDatabase.sync { $0.remove(collation: collation) } } }