resolve
Id.A prefix is looked up first among active rows (not archived, not in a terminal status) and then among all
rows, so a short prefix keeps working after an old entity with the same prefix is archived. Table is the
closed list of table names that may be read back from the database, such as transaction_events.table_name,
and used in dynamic SQL.
enum Table
pub enum TableTables that can be searched by ID prefix resolution or referenced in
persisted audit rows (e.g. transaction_events.table_name).
Using an enum (instead of raw &str) guarantees that every SQL string built
against one of these tables interpolates a canonical, hand-audited
identifier — no caller-controlled table name can ever reach a SQL format
string. The allowlist is closed: Table::from_sql_ident is the gate
call sites use to reject any unknown name stored in the database.
| Variant | Description |
|---|---|
Artifacts |
The artifacts table. |
EntityTags |
The entity_tags join table associating tags with arbitrary entities. Constructed only via Table::from_sql_ident during transaction replay; no direct call site exists yet, so the variant may appear unused in cross-crate analyses. |
IterationTasks |
The iteration_tasks join table assigning tasks to iteration phases. Constructed only via Table::from_sql_ident during transaction replay; no direct call site exists yet, so the variant may appear unused in cross-crate analyses. |
Iterations |
The iterations table. |
Notes |
The notes table. |
Projects |
The projects table. |
Relationships |
The relationships table linking entities via typed directed edges. Constructed only via Table::from_sql_ident during transaction replay; no direct call site exists yet, so the variant may appear unused in cross-crate analyses. |
Tasks |
The tasks table. |
Implements Clone, Copy, Debug, Eq, Hash, PartialEq.
Table::from_sql_ident
pub fn from_sql_ident(s: &str) -> Option<Self>Parses a SQL table identifier back into a Table variant.
| Parameter | Type | Description |
|---|---|---|
s |
&str | the table name, such as iteration_tasks; matched exactly and case-sensitively |
| returns | Option<Self> | Returns None for any identifier outside the closed allowlist. This is the gate that rejects caller- or database-controlled table names (e.g. transaction_events.table_name rows) before they are interpolated into dynamic SQL. Otherwise it is the variant whose Table::as_sql_ident is s. |
Table::active_filter
pub fn active_filter(self) -> Option<&'static str>Returns the SQL fragment that filters this table to its “active” set.
Active means:
- artifacts: not archived (
archived_at IS NULL) - projects: not archived (
archived_at IS NULL) - tasks: not in a terminal state (
status NOT IN ('done', 'cancelled')) - iterations: not in a terminal state (
status NOT IN ('completed', 'cancelled')) - notes, entity_tags, iteration_tasks, relationships: no active concept — always returns
None
| Parameter | Type | Description |
|---|---|---|
| returns | Option<&'static str> | A condition to put after WHERE or AND. |
Table::as_sql_ident
pub fn as_sql_ident(self) -> &'static strReturns the canonical SQL identifier for this table.
| Parameter | Type | Description |
|---|---|---|
| returns | &'static str | The returned string is a compile-time constant drawn from a closed set of safe identifiers, which is what makes dynamic SQL construction in this module safe. For example, tasks. |
Table::columns
pub fn columns(self) -> &'static [&'static str]Returns the closed allowlist of column identifiers defined on this table’s schema.
Call sites that interpolate column names into dynamic SQL (e.g. replaying
transaction_events.before_data JSON keys into an UPDATE ... SET or
INSERT ... (columns) clause) MUST validate each key against this list
to prevent a malicious or corrupt before_data payload from injecting
arbitrary identifiers into the rendered SQL.
The lists are kept in sync by hand with the migration DDL under
src/store/migration/; any schema change must update the matching
variant here.
| Parameter | Type | Description |
|---|---|---|
| returns | &'static [&'static str] | The names, in alphabetical order. |
Table::key_columns
pub fn key_columns(self) -> &'static [&'static str]Returns the columns that together identify one row of this table.
| Parameter | Type | Description |
|---|---|---|
| returns | &'static [&'static str] | ["id"] for every table with an id column. The join tables have none, so they return their primary key: entity_type, entity_id and tag_id for entity_tags, and iteration_id and task_id for iteration_tasks. |
Table::has_column
pub fn has_column(self, column: &str) -> boolReturns true iff column is an allowlisted identifier on this table.
Convenience wrapper over Table::columns for single-key checks.
| Parameter | Type | Description |
|---|---|---|
column |
&str | the column name to check, matched exactly |
| returns | bool | false otherwise. |
fn query_matches
async fn query_matches(
conn: &Connection,
table: Table,
prefix: &str,
active_only: bool,
) -> Result<Vec<String>, Error>Query the given table for IDs matching the prefix, optionally restricted to the active set.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the connection to query |
table |
Table | the table to search |
prefix |
&str | the start of the id; matched with SQL LIKE |
active_only |
bool | whether to add the table’s [Table::active_filter]; a table without one is searched whole |
| returns | Result<Vec<String>, Error> | The full ids that start with the prefix, possibly none, or a database error. |
fn resolve_id
pub async fn resolve_id(conn: &Connection, table: Table, prefix: &str) -> Result<Id, Error>Resolve an ID prefix to a full ID by querying the given table.
Resolution is two-phase:
- First, search the active set (non-archived / non-terminal rows). If exactly one match → return it. If more than one → ambiguous error.
- If zero active matches, fall back to searching all rows. Apply the same one/many/none logic.
The fallback is silent: a prefix matching one active and one archived/terminal entity returns the active one with no ambiguity error or hint.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the connection to query |
table |
Table | the table to search |
prefix |
&str | the start of the id, 1 to 32 characters from k to z |
| returns | Result<Id, Error> | The one matching full id. It is Error::InvalidPrefix for a malformed prefix or a stored id that does not parse, Error::Ambiguous when several rows match, Error::NotFound when none does, or a database error. |
const ENTITY_TABLES
const ENTITY_TABLES: &[(EntityType, Table)] = &[
(EntityType::Artifact, Table::Artifacts),
(EntityType::Iteration, Table::Iterations),
(EntityType::Task, Table::Tasks),
];Tables to search when resolving an entity across all types.
fn collect_entity_matches
async fn collect_entity_matches(
conn: &Connection,
prefix: &str,
active_only: bool,
) -> Result<Vec<(EntityType, Id)>, Error>Collect matches across all entity tables, optionally restricted to active rows.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the connection to query |
prefix |
&str | the start of the id |
active_only |
bool | whether to search only the active rows of each table |
| returns | Result<Vec<(EntityType, Id)>, Error> | Each match with its entity type: artifacts first, then iterations, then tasks. It is Error::InvalidPrefix when a stored id does not parse, or a database error. |
fn resolve_entity
pub async fn resolve_entity(conn: &Connection, prefix: &str) -> Result<(EntityType, Id), Error>Resolve an ID prefix across all entity tables.
Resolution is two-phase:
- First, search the active set across artifacts, iterations, and tasks. If exactly one match → return it. If more than one → ambiguous error.
- If zero active matches, fall back to searching all rows. Apply the same one/many/none logic.
The fallback is silent: a prefix matching one active and one archived/terminal entity returns the active one with no ambiguity error or hint.
| Parameter | Type | Description |
|---|---|---|
conn |
&Connection | the connection to query |
prefix |
&str | the start of the id, 1 to 32 characters from k to z |
| returns | Result<(EntityType, Id), Error> | The type and full id of the one match. It is Error::InvalidPrefix for a malformed prefix or a stored id that does not parse, Error::Ambiguous when several entities match, Error::NotFound when none does, or a database error. |