glite/config

Settings for opening a connection.

Every limit has a fixed upper bound. Reaching one is an error, never a silent truncation.

let config =
  config.new("/var/lib/myapp/app.db")
  |> config.call_timeout_ms(5000)
  |> config.max_rows(1000)

Types

Settings for one connection.

pub type Config {
  Config(
    path: String,
    journal_mode: JournalMode,
    synchronous: SynchronousMode,
    foreign_keys: Bool,
    busy_timeout_ms: Int,
    call_timeout_ms: Int,
    max_rows: Int,
    max_batch: Int,
    max_queue: Int,
    statement_cache: Int,
    pragmas: List(#(String, String)),
  )
}

Constructors

  • Config(
      path: String,
      journal_mode: JournalMode,
      synchronous: SynchronousMode,
      foreign_keys: Bool,
      busy_timeout_ms: Int,
      call_timeout_ms: Int,
      max_rows: Int,
      max_batch: Int,
      max_queue: Int,
      statement_cache: Int,
      pragmas: List(#(String, String)),
    )

    Arguments

    path

    Database file path, or “:memory:”.

    foreign_keys

    Enforce FOREIGN KEY constraints.

    busy_timeout_ms

    How long SQLite waits for another connection’s lock before returning Busy. Must be shorter than call_timeout_ms.

    call_timeout_ms

    How long a call may take, waiting for the connection included. Then its SQL is interrupted, and it returns Timeout.

    max_rows

    Most rows one query may return.

    max_batch

    Most parameter sets one exec_many call may take.

    max_queue

    Most calls that may wait for the connection at once.

    statement_cache

    Most prepared statements the connection keeps for reuse, by SQL text. 0 prepares every statement again each time.

    pragmas

    Extra PRAGMAs, applied in order after the settings above.

How SQLite keeps its rollback information.

pub type JournalMode {
  Wal
  Delete
  Memory
  Off
}

Constructors

  • Wal

    Write-ahead log: readers don’t block the writer. The default for files.

  • Delete

    Rollback journal, deleted after each transaction.

  • Memory

    Rollback journal in memory. The only mode an in-memory database has.

  • Off

    No journal: no crash recovery and no rollback.

When SQLite waits for data to reach the disk.

pub type SynchronousMode {
  Full
  Normal
  SyncOff
}

Constructors

  • Full

    Sync on every commit. A committed transaction survives a power cut. The default.

  • Normal

    Sync less often. In WAL mode the last commits can be lost on a power cut, but the database is never corrupted.

  • SyncOff

    Never sync. A power cut can corrupt the database.

Values

pub fn busy_timeout_ms(config: Config, ms: Int) -> Config
pub fn call_timeout_ms(config: Config, ms: Int) -> Config
pub fn foreign_keys(config: Config, enabled: Bool) -> Config
pub fn journal_mode(config: Config, mode: JournalMode) -> Config
pub fn max_batch(config: Config, limit: Int) -> Config
pub fn max_queue(config: Config, limit: Int) -> Config
pub fn max_rows(config: Config, limit: Int) -> Config
pub fn memory() -> Config

Settings for a private in-memory database, otherwise the same as new.

pub fn new(path: String) -> Config

Settings for a database file: WAL, full sync, foreign keys on, a 5 s busy timeout, a 10 s call timeout, and at most 10,000 rows per query, 10,000 parameter sets per batch, 1,000 waiting calls and 200 cached statements.

pub fn pragma(
  config: Config,
  name: String,
  value: String,
) -> Config

Add a PRAGMA to run when the database opens, after the ones before it. The name must be lowercase letters and underscores, and the value a number or a bare word, such as pragma("cache_size", "-8000").

pub fn statement_cache(config: Config, size: Int) -> Config

Keep up to size prepared statements for reuse. Preparing a statement costs about as much as running a small one, so statements that run often are prepared once. Build SQL with placeholders rather than by pasting values into it, or every call is a new statement.

pub fn synchronous(
  config: Config,
  mode: SynchronousMode,
) -> Config
✨ Search Document