"""`Store`: every SQL statement in the application. The connection plumbing is a copy of newsdesk's `Store`, on purpose: `desk` has no storage package. A connection is opened per call and closed again, so one caller's commit can never land inside another's transaction; in WAL mode that costs a file handle. """ from __future__ import annotations import sqlite3 from collections.abc import Iterator, MutableMapping from contextlib import closing, contextmanager from pathlib import Path from typing import Any from roledesk.storage import schema class _StateMap(MutableMapping[str, str]): """`Store.state`: the key/value table, read and written one key at a time.""" def __init__(self, store: Store) -> None: self._store = store def __getitem__(self, key: str) -> str: row = self._store._row("SELECT value FROM state WHERE key=?", (key,)) if row is None: raise KeyError(key) return str(row["value"]) def __setitem__(self, key: str, value: str) -> None: self._store._write( """INSERT INTO state (key, value) VALUES (?,?) ON CONFLICT(key) DO UPDATE SET value=excluded.value""", (key, value)) def __delitem__(self, key: str) -> None: if not self._store._write("DELETE FROM state WHERE key=?", (key,)): raise KeyError(key) def __iter__(self) -> Iterator[str]: return iter([r["key"] for r in self._store._rows("SELECT key FROM state ORDER BY key")]) def __len__(self) -> int: row = self._store._row("SELECT COUNT(*) AS n FROM state") return int(row["n"]) if row else 0 class Store: def __init__(self, path: str | Path) -> None: self.path = Path(path).expanduser() self.path.parent.mkdir(parents=True, exist_ok=True) with self.transaction() as conn: # Persistent: recorded in the file, inherited by every connection. conn.execute("PRAGMA journal_mode=WAL") #: Migration versions this open applied; empty when already current. self.migrations_applied: list[int] = schema.initialise(conn) # ------------------------------------------------------------ connections def _connect(self) -> sqlite3.Connection: """A fresh connection. `foreign_keys` is off by default per connection; `busy_timeout` matches `pipeline/table.py`, which writes the same file; `synchronous=NORMAL` is the standard WAL companion.""" conn = sqlite3.connect(self.path) conn.row_factory = sqlite3.Row conn.execute("PRAGMA foreign_keys=ON") conn.execute("PRAGMA busy_timeout=30000") conn.execute("PRAGMA synchronous=NORMAL") return conn @contextmanager def transaction(self) -> Iterator[sqlite3.Connection]: """One connection, one transaction, closed on the way out.""" conn = self._connect() try: with conn: yield conn finally: conn.close() def _rows(self, sql: str, params: Any = ()) -> list[sqlite3.Row]: with closing(self._connect()) as conn: return conn.execute(sql, params).fetchall() def _row(self, sql: str, params: Any = ()) -> sqlite3.Row | None: with closing(self._connect()) as conn: row: sqlite3.Row | None = conn.execute(sql, params).fetchone() return row def _write(self, sql: str, params: Any = ()) -> int: """One statement in its own transaction. Returns the rowcount.""" with self.transaction() as conn: return conn.execute(sql, params).rowcount # ---------------------------------------------------------------- state @property def state(self) -> MutableMapping[str, str]: """The `state` table as a mapping. What the runner hands a plugin as `self.state`: it can keep a counter without being able to read roles.""" return _StateMap(self)