DatabaseSnapshot

public class DatabaseSnapshot : DatabaseReader

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

  • 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(...)
    }
    

    Throws

    The error thrown by the block.

    Declaration

    Swift

    public func read<T>(_ block: (Database) throws -> T) rethrows -> T

    Parameters

    block

    A block that accesses the database.

  • 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
    }
    

    Declaration

    Swift

    public func add(function: DatabaseFunction)
  • Remove an SQL function.

    Declaration

    Swift

    public func remove(function: DatabaseFunction)
  • 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 file ORDER BY name COLLATE localized_standard")
    }
    

    Declaration

    Swift

    public func add(collation: DatabaseCollation)
  • Remove a collation.

    Declaration

    Swift

    public func remove(collation: DatabaseCollation)