DatabaseQueue

public final class DatabaseQueue : DatabaseWriter

A DatabaseQueue serializes access to an SQLite database.

Configuration

Initializers

  • Opens the SQLite database at path path.

    let dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite")
    

    Database connections get closed when the database queue gets deallocated.

    Throws

    A DatabaseError whenever an SQLite error occurs.

    Declaration

    Swift

    public init(path: String, configuration: Configuration = Configuration()) throws

    Parameters

    path

    The path to the database file.

    configuration

    A configuration.

  • Opens an in-memory SQLite database.

    let dbQueue = DatabaseQueue()
    

    Database memory is released when the database queue gets deallocated.

    Declaration

    Swift

    public init(configuration: Configuration = Configuration())

    Parameters

    configuration

    A configuration.

  • Declaration

    Swift

    public func close() throws

Memory management

  • Free as much memory as possible.

    This method blocks the current thread until all database accesses are completed.

    Declaration

    Swift

    public func releaseMemory()

Interrupting Database Operations

Reading from Database

Writing in Database

  • Synchronously executes database updates in a protected dispatch queue, wrapped inside a transaction, and returns the result.

    If the updates throws an error, the transaction is rollbacked and the error is rethrown. If the updates return .rollback, the transaction is also rollbacked, but no error is thrown.

    Eventual concurrent database accesses are postponed until the transaction has completed.

    This method is not reentrant.

    try dbQueue.writeInTransaction { db in
        db.execute(...)
        return .commit
    }
    

    Throws

    The error thrown by the updates, or by the wrapping transaction.

    Declaration

    Swift

    public func inTransaction(
        _ kind: Database.TransactionKind? = nil,
        _ updates: (Database) throws -> Database.TransactionCompletion)
    throws

    Parameters

    kind

    The transaction type (default nil). If nil, the transaction type is configuration.defaultTransactionKind, which itself defaults to .deferred. See https://www.sqlite.org/lang_transaction.html for more information.

    updates

    The updates to the database.

  • Declaration

    Swift

    @_disfavoredOverload
    public func writeWithoutTransaction<T>(_ updates: (Database) throws -> T) rethrows -> T
  • Declaration

    Swift

    @_disfavoredOverload
    public func barrierWriteWithoutTransaction<T>(_ updates: (Database) throws -> T) rethrows -> T
  • Declaration

    Swift

    public func asyncBarrierWriteWithoutTransaction(_ updates: @escaping (Database) -> Void)
  • Synchronously executes database updates in a protected dispatch queue, outside of any transaction, and returns the result.

    Eventual concurrent database updates are postponed until the updates are completed.

    Eventual concurrent reads may see partial updates unless you wrap them in a transaction.

    This method is not reentrant.

    Throws

    The error thrown by the updates.

    Declaration

    Swift

    public func inDatabase<T>(_ updates: (Database) throws -> T) rethrows -> T

    Parameters

    updates

    The updates to the database.

  • Declaration

    Swift

    public func unsafeReentrantWrite<T>(_ updates: (Database) throws -> T) rethrows -> T
  • Declaration

    Swift

    public func asyncWriteWithoutTransaction(_ updates: @escaping (Database) -> Void)