Class: Honker::Transaction
- Inherits:
-
Object
- Object
- Honker::Transaction
- Defined in:
- lib/honker/transaction.rb
Overview
Wraps a SQLite transaction with helpers that thread SQL through a stable connection handle. Matches the shape of the Rust binding’s ‘Transaction` — `execute` and `query_row` route to the underlying connection, which `*_tx` methods (`Queue#enqueue_tx`, `Stream#publish_tx`, `Stream#save_offset_tx`, `Database#notify_tx`) use to stay inside the open transaction.
Obtain one via ‘Database#transaction do |tx| … end`. The block auto-commits on normal return and auto-rolls-back on exception; the wrapping `sqlite3` gem handles the BEGIN/COMMIT/ROLLBACK for us. Callers may also invoke `tx.rollback!` mid-block to abort without raising.
Defined Under Namespace
Classes: Rollback
Instance Attribute Summary collapse
-
#conn ⇒ Object
readonly
The raw ‘SQLite3::Database` underneath.
Instance Method Summary collapse
-
#execute(sql, params = []) ⇒ Object
Execute a SQL statement inside the transaction.
-
#initialize(conn) ⇒ Transaction
constructor
A new instance of Transaction.
-
#query_row(sql, params = []) ⇒ Object
Run a query and return the first row (or nil).
-
#rollback! ⇒ Object
Abort the transaction early.
- #rolled_back? ⇒ Boolean
Constructor Details
#initialize(conn) ⇒ Transaction
Returns a new instance of Transaction.
22 23 24 25 |
# File 'lib/honker/transaction.rb', line 22 def initialize(conn) @conn = conn @rolled_back = false end |
Instance Attribute Details
#conn ⇒ Object (readonly)
The raw ‘SQLite3::Database` underneath. Exposed as `conn` to mirror the Rust shape for advanced users who need `prepare` or other APIs not wrapped here.
20 21 22 |
# File 'lib/honker/transaction.rb', line 20 def conn @conn end |
Instance Method Details
#execute(sql, params = []) ⇒ Object
Execute a SQL statement inside the transaction.
28 29 30 |
# File 'lib/honker/transaction.rb', line 28 def execute(sql, params = []) @conn.execute(sql, params) end |
#query_row(sql, params = []) ⇒ Object
Run a query and return the first row (or nil).
33 34 35 |
# File 'lib/honker/transaction.rb', line 33 def query_row(sql, params = []) @conn.get_first_row(sql, params) end |
#rollback! ⇒ Object
Abort the transaction early. Raises ‘Transaction::Rollback` internally so the outer sqlite3-gem block sees an exception and rolls back; we then swallow it at the `Database#transaction` boundary.
41 42 43 44 |
# File 'lib/honker/transaction.rb', line 41 def rollback! @rolled_back = true raise Rollback end |
#rolled_back? ⇒ Boolean
46 47 48 |
# File 'lib/honker/transaction.rb', line 46 def rolled_back? @rolled_back end |