Skip to main content

uqa_engine/capabilities/
triggers.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Connect trigger catalog reads, routine entry, and transaction deferral to engine state.
8use crate::Engine;
9use std::sync::Arc;
10use uqa_core::Value;
11use uqa_execution::{
12    mutation::triggers::{
13        context::{ConstraintTriggerQueue, TriggerCatalog, TriggerContext, TriggerRoutineInvoker},
14        DeferredConstraintTriggerEvent,
15    },
16    routines::TriggerRoutineContext,
17};
18use uqa_sql::{
19    ast::{ColumnDef, ForeignKey, TriggerEvent, TriggerTiming},
20    catalog::events::StoredTrigger,
21    routines::SQLUserFunction,
22    semantics::referential::ReferentialCatalog,
23    SQLError,
24};
25
26impl Engine {
27    pub(crate) fn trigger_execution_context(&self) -> TriggerContext<'_> {
28        TriggerContext {
29            catalog: self,
30            relations: self,
31            routines: self,
32            deferrals: self,
33            referrers: self,
34            expressions: self,
35            projection: self.catalog_execution(),
36            runtime: self.query_runtime_view(),
37            foreign_keys: self,
38        }
39    }
40}
41impl uqa_execution::mutation::triggers::context::ForeignKeyCheckRunner for Engine {
42    fn run_foreign_key_check(
43        &self,
44        check: &uqa_execution::mutation::referential::checks::ForeignKeyCheck,
45        queue: &uqa_execution::mutation::triggers::queue::AfterTriggerQueue,
46    ) -> Result<(), SQLError> {
47        uqa_execution::mutation::referential::checks::run_foreign_key_check(
48            &self.mutation_statement_context().mutation,
49            check,
50            queue,
51        )
52    }
53}
54impl TriggerCatalog for Engine {
55    fn has_row_triggers(
56        &self,
57        table: &str,
58        event: uqa_sql::ast::TriggerEvent,
59    ) -> Result<bool, SQLError> {
60        self.event_lookup_context().has_row_triggers(table, event)
61    }
62    fn trigger_relation_definitions(&self, table: &str) -> Result<Vec<ColumnDef>, SQLError> {
63        self.event_analysis_context()
64            .trigger_relation_definitions(table)
65    }
66    fn triggers_for(
67        &self,
68        table: &str,
69        timing: TriggerTiming,
70        event: TriggerEvent,
71        row: bool,
72        updated_columns: &[String],
73    ) -> Result<Vec<StoredTrigger>, SQLError> {
74        self.event_lookup_context()
75            .triggers_for(table, timing, event, row, updated_columns)
76    }
77    fn row_trigger_definitions(
78        &self,
79        table: &str,
80        timing: TriggerTiming,
81        event: TriggerEvent,
82        updated_columns: Option<&[String]>,
83    ) -> Result<Vec<StoredTrigger>, SQLError> {
84        self.event_lookup_context().trigger_definitions_for(
85            table,
86            timing,
87            event,
88            true,
89            updated_columns,
90        )
91    }
92    fn has_trigger_definition(
93        &self,
94        table: &str,
95        timing: TriggerTiming,
96        event: TriggerEvent,
97        row: bool,
98    ) -> Result<bool, SQLError> {
99        self.event_lookup_context()
100            .has_trigger_definition(table, timing, event, row)
101    }
102    fn hierarchy_scan_tables(
103        &self,
104        table: &str,
105        descendants: bool,
106    ) -> Result<Vec<String>, SQLError> {
107        Engine::hierarchy_scan_tables(self, table, descendants)
108    }
109}
110impl TriggerRoutineInvoker for Engine {
111    fn resolve_bound_trigger_function(
112        &self,
113        name: &str,
114        object_id: Option<[u8; 16]>,
115    ) -> Result<Arc<SQLUserFunction>, SQLError> {
116        self.event_analysis_context()
117            .resolve_bound_trigger_function(name, object_id)
118    }
119    fn execute_trigger_routine(
120        &self,
121        function: &Arc<SQLUserFunction>,
122        context: &TriggerRoutineContext,
123    ) -> Result<Value, SQLError> {
124        crate::capabilities::routine_invocation::execute_trigger_routine(self, function, context)
125    }
126}
127impl ConstraintTriggerQueue for Engine {
128    fn constraint_trigger_is_deferred(&self, trigger: &StoredTrigger) -> Result<bool, SQLError> {
129        Engine::constraint_trigger_is_deferred(self, trigger)
130    }
131    fn defer_constraint_trigger_event(
132        &self,
133        event: DeferredConstraintTriggerEvent,
134    ) -> Result<(), SQLError> {
135        Engine::defer_constraint_trigger_event(self, event)
136    }
137}
138impl ReferentialCatalog for Engine {
139    fn session_replication_role_is_replica(&self) -> bool {
140        Engine::session_replication_role_is_replica(self)
141    }
142    fn partition_ancestor_tables(&self, table: &str) -> Result<Vec<String>, SQLError> {
143        uqa_sql::semantics::partition::partition_ancestor_tables(self, table)
144    }
145    fn try_referrers_to(&self, table: &str) -> Result<Vec<(String, ForeignKey)>, String> {
146        Engine::referrers_in_execution(self, table).map_err(|error| error.to_string())
147    }
148}