Diffinery

homeflare/celldpublic

celld/crates/logic/sqlite.rs
29 lines · 1.4 KBRaw History
1// Copyright 2026 Deno Land Inc. Apache-2.0 license.
2
3//! SQLite failure handling, reified sans-IO. Production reads SQLite's
4//! autocommit flag over FFI and uses this predicate to decide whether the actor
5//! must fail closed after SQLite destroys an active transaction.
6
7/// SQLite primary result codes that mean the ENGINE failed (not just one
8/// statement) — stable values from the SQLite C API. Production asserts these
9/// match `rusqlite::ffi::*`, so debug builds catch any drift.
10pub const SQLITE_NOMEM: i32 = 7;
11pub const SQLITE_INTERRUPT: i32 = 9;
12pub const SQLITE_IOERR: i32 = 10;
13pub const SQLITE_FULL: i32 = 13;
14
15/// Does a failed SQLite operation poison the actor? Poison only when a critical engine error
16/// (`FULL`/`IOERR`/`NOMEM`/`INTERRUPT`) coincides with a destroyed transaction —
17/// SQLite rolled an active transaction back, observable as autocommit being
18/// re-enabled. Extended result codes carry the primary code in the low byte, so
19/// mask before comparing. Anything else — a statement error, a critical error
20/// that spared the transaction, or a failure outside a transaction — recovers.
21pub fn poisons_actor(result_code: i32, started_in_transaction: bool, now_autocommit: bool) -> bool {
22 let primary = result_code & 0xff;
23 started_in_transaction
24 && now_autocommit
25 && matches!(
26 primary,
27 SQLITE_FULL | SQLITE_IOERR | SQLITE_NOMEM | SQLITE_INTERRUPT
28 )
29}