Skip to main content

uqa_execution/mutation/triggers/
context.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Catalog, routine, and transaction capabilities for trigger execution.
8use super::DeferredConstraintTriggerEvent;
9use crate::{
10    catalog::context::CatalogContext,
11    query::runtime::QueryRuntimeView,
12    routines::{context::RoutineExpressions, TriggerRoutineContext},
13};
14use std::sync::Arc;
15use uqa_core::Value;
16use uqa_sql::{
17    ast::{ColumnDef, ColumnType, TriggerEvent, TriggerTiming},
18    catalog::events::StoredTrigger,
19    routines::SQLUserFunction,
20    semantics::{partition::PartitionCatalog, referential::ReferentialCatalog},
21    SQLError,
22};
23
24/// Trigger definitions and relation row types observed by the active command.
25pub trait TriggerCatalog {
26    fn has_row_triggers(&self, table: &str, event: TriggerEvent) -> Result<bool, SQLError>;
27    fn trigger_relation_definitions(&self, table: &str) -> Result<Vec<ColumnDef>, SQLError>;
28    fn rule_relation_columns(&self, table: &str) -> Result<Vec<(String, ColumnType)>, SQLError> {
29        Ok(self
30            .trigger_relation_definitions(table)?
31            .into_iter()
32            .map(|column| (column.name, column.ty))
33            .collect())
34    }
35    fn triggers_for(
36        &self,
37        table: &str,
38        timing: TriggerTiming,
39        event: TriggerEvent,
40        row: bool,
41        updated_columns: &[String],
42    ) -> Result<Vec<StoredTrigger>, SQLError>;
43    /// The row triggers of `table` for a timing and an event, whatever replication role fires them. `None` includes every UPDATE column restriction; a statement resolves definitions once and applies the role at each firing.
44    fn row_trigger_definitions(
45        &self,
46        table: &str,
47        timing: TriggerTiming,
48        event: TriggerEvent,
49        updated_columns: Option<&[String]>,
50    ) -> Result<Vec<StoredTrigger>, SQLError>;
51    fn has_trigger_definition(
52        &self,
53        table: &str,
54        timing: TriggerTiming,
55        event: TriggerEvent,
56        row: bool,
57    ) -> Result<bool, SQLError>;
58    fn hierarchy_scan_tables(
59        &self,
60        table: &str,
61        include_descendants: bool,
62    ) -> Result<Vec<String>, SQLError>;
63}
64/// Enter a stored routine using its bound identity and the caller's security context.
65pub trait TriggerRoutineInvoker {
66    fn resolve_bound_trigger_function(
67        &self,
68        name: &str,
69        object_id: Option<[u8; 16]>,
70    ) -> Result<Arc<SQLUserFunction>, SQLError>;
71    fn execute_trigger_routine(
72        &self,
73        function: &Arc<SQLUserFunction>,
74        context: &TriggerRoutineContext,
75    ) -> Result<Value, SQLError>;
76}
77/// Transaction-owned deferred constraint-trigger queue.
78pub trait ConstraintTriggerQueue {
79    fn constraint_trigger_is_deferred(&self, trigger: &StoredTrigger) -> Result<bool, SQLError>;
80    fn defer_constraint_trigger_event(
81        &self,
82        event: DeferredConstraintTriggerEvent,
83    ) -> Result<(), SQLError>;
84}
85/// Runs the foreign key checks and referential actions that row events carry when a statement's AFTER ROW triggers fire. A referential action queues the events of the rows it writes in `queue`, the queue of the statement whose event fired it.
86pub trait ForeignKeyCheckRunner {
87    fn run_foreign_key_check(
88        &self,
89        check: &crate::mutation::referential::checks::ForeignKeyCheck,
90        queue: &super::queue::AfterTriggerQueue,
91    ) -> Result<(), SQLError>;
92}
93#[derive(Clone, Copy)]
94pub struct TriggerContext<'a> {
95    pub catalog: &'a dyn TriggerCatalog,
96    pub relations: &'a dyn PartitionCatalog,
97    pub routines: &'a dyn TriggerRoutineInvoker,
98    pub deferrals: &'a dyn ConstraintTriggerQueue,
99    pub referrers: &'a dyn ReferentialCatalog,
100    pub expressions: &'a dyn RoutineExpressions,
101    pub projection: CatalogContext<'a>,
102    pub runtime: QueryRuntimeView<'a>,
103    pub foreign_keys: &'a dyn ForeignKeyCheckRunner,
104}