Skip to main content

ff_backend_sqlite/queries/
reads.rs

1//! SQL statements for Wave 9 read-model methods (RFC-023 Phase 3.3).
2//!
3//! Mirrors `ff-backend-postgres/src/exec_core.rs` §4.1 read impls at
4//! the statement level. SQLite translations:
5//!
6//! * `LEFT JOIN LATERAL (... LIMIT 1)` → `LEFT JOIN` on a correlated
7//!   subquery expression. SQLite executes the subquery per-outer-row
8//!   which is equivalent for LIMIT 1 projections.
9//! * `FOR UPDATE` — no-op under `BEGIN IMMEDIATE` single-writer.
10//! * `BYTEA` → `BLOB`. `uuid` column bound as 16-byte BLOB.
11//! * JSON extraction via `json_extract` (JSON1) instead of `->>`.
12
13// ── read_execution_state (§4.1) ────────────────────────────────────
14
15/// Single-column point read on `ff_exec_core.public_state`. Binds:
16/// ?1 partition_key, ?2 execution_id BLOB.
17pub(crate) const SELECT_PUBLIC_STATE_SQL: &str = r#"
18    SELECT public_state
19      FROM ff_exec_core
20     WHERE partition_key = ?1 AND execution_id = ?2
21"#;
22
23// ── read_execution_info (§4.1) ─────────────────────────────────────
24
25/// Multi-column projection of `ff_exec_core` joined with the current
26/// attempt row (for `outcome`). Mirrors PG's single remaining LATERAL
27/// join with a SQLite correlated subquery in the SELECT list. Since
28/// migration 0016 (#356) `ff_exec_core.started_at_ms` is a set-once
29/// column populated on first claim, so the earlier "earliest attempt
30/// started_at_ms" subquery is gone — `ExecutionInfo.started_at` reads
31/// directly from the base row.
32///
33/// Binds: ?1 partition_key, ?2 execution_id BLOB.
34pub(crate) const SELECT_EXECUTION_INFO_SQL: &str = r#"
35    SELECT ec.flow_id,
36           ec.lane_id,
37           ec.priority,
38           ec.lifecycle_phase,
39           ec.ownership_state,
40           ec.eligibility_state,
41           ec.public_state,
42           ec.attempt_state,
43           ec.blocking_reason,
44           ec.attempt_index,
45           ec.created_at_ms,
46           ec.terminal_at_ms,
47           ec.started_at_ms,
48           ec.raw_fields,
49           (SELECT outcome
50              FROM ff_attempt a
51             WHERE a.partition_key = ec.partition_key
52               AND a.execution_id  = ec.execution_id
53               AND a.attempt_index = ec.attempt_index) AS attempt_outcome
54      FROM ff_exec_core ec
55     WHERE ec.partition_key = ?1 AND ec.execution_id = ?2
56"#;
57
58// ── get_execution_result (§4.1 + §7.8) ─────────────────────────────
59
60/// `result` BLOB point read from `ff_exec_core`. Current-attempt
61/// semantics per RFC-020 Rev 7 Fork 3. Binds: ?1 partition_key, ?2
62/// execution_id BLOB.
63pub(crate) const SELECT_EXECUTION_RESULT_SQL: &str = r#"
64    SELECT result
65      FROM ff_exec_core
66     WHERE partition_key = ?1 AND execution_id = ?2
67"#;