Honker.Transaction (honker v0.1.4)

Copy Markdown

A SQLite transaction handle. Wraps the underlying Exqlite connection while a transaction is open so *_tx helpers (e.g. Honker.Queue.enqueue_tx/5, Honker.Stream.publish_tx/4) can take it as an explicit parameter.

{:ok, tx} = Honker.Transaction.begin(db)
:ok = Honker.Transaction.execute(tx, "INSERT INTO orders ...", [])
{:ok, _} = Honker.Queue.enqueue_tx(tx, "emails", %{order_id: 1}, [], [])
:ok = Honker.Transaction.commit(tx)

Higher-level transaction/2 runs a function inside BEGIN IMMEDIATE, commits on success, and rolls back on raised exception — matching the Rust RAII shape with Elixir ergonomics.

Summary

Functions

Begin a transaction with BEGIN IMMEDIATE. Returns {:ok, tx} where tx is a %Honker.Transaction{} carrying the raw connection.

Commit the transaction.

Run a statement for side effects. Returns :ok on a :row or :done step, or an error tuple. Accepts positional parameters via ?1-style placeholders.

Run a statement and return the first row as a list [col0, col1, ...], or {:ok, nil} when the query returns no rows. Mirrors Honker.query_first/3.

Roll back the transaction.

Run fun.(tx) inside BEGIN IMMEDIATE. Commits on :ok / {:ok, _} / any non-error term; rolls back and re-raises on exception. Returns whatever fun returned.

Functions

begin(database)

Begin a transaction with BEGIN IMMEDIATE. Returns {:ok, tx} where tx is a %Honker.Transaction{} carrying the raw connection.

The Exqlite connection is single-threaded; the caller is responsible for serialization. Pair with an explicit commit/1 or rollback/1.

commit(transaction)

Commit the transaction.

execute(transaction, sql, params \\ [])

Run a statement for side effects. Returns :ok on a :row or :done step, or an error tuple. Accepts positional parameters via ?1-style placeholders.

query_row(transaction, sql, params \\ [])

Run a statement and return the first row as a list [col0, col1, ...], or {:ok, nil} when the query returns no rows. Mirrors Honker.query_first/3.

rollback(transaction)

Roll back the transaction.

transaction(db, fun)

Run fun.(tx) inside BEGIN IMMEDIATE. Commits on :ok / {:ok, _} / any non-error term; rolls back and re-raises on exception. Returns whatever fun returned.

{:error, _} returned by fun rolls back and the error is propagated. Use this when you want the common "atomic business write

  • enqueue" shape without hand-rolling commit/rollback.