Skip to main content

uqa_engine/capabilities/
rule_targets.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Canonical target and stored-rule metadata for SQL validation.
8
9use crate::Engine;
10use uqa_core::RelationIdentity;
11use uqa_sql::{ast::RuleEvent, catalog::events::StoredRule, SQLError};
12
13impl uqa_sql::semantics::rules::RuleCatalog for Engine {
14    fn relation_has_rules(&self, table: &str) -> Result<bool, SQLError> {
15        self.event_lookup_context().relation_has_rules(table)
16    }
17    fn resolve_rule_relation(&self, table: &str) -> Result<RelationIdentity, SQLError> {
18        self.event_analysis_context().resolve_rule_relation(table)
19    }
20    fn rules_for(&self, table: &str, event: RuleEvent) -> Result<Vec<StoredRule>, SQLError> {
21        self.event_lookup_context().rules_for(table, event)
22    }
23    fn capture_rules(
24        &self,
25        table: &str,
26        event: RuleEvent,
27    ) -> Result<uqa_sql::catalog::events::selection::CapturedRules, SQLError> {
28        let retained = self.query_retention_control()?;
29        let control = uqa_core::memory::ProductionControl::new(
30            retained.memory(),
31            retained.cancellation(),
32            self.query_runtime_view().cancellation,
33        );
34        self.capture_rules_with_control(table, event, &control)
35    }
36    fn capture_rules_with_control(
37        &self,
38        table: &str,
39        event: RuleEvent,
40        control: &uqa_core::memory::ProductionControl<'_>,
41    ) -> Result<uqa_sql::catalog::events::selection::CapturedRules, SQLError> {
42        let relation = self
43            .event_analysis_context()
44            .resolve_rule_relation_with_control(table, control)?;
45        let replica = self.session_replication_role_is_replica_with_control(control)?;
46        uqa_sql::catalog::events::selection::CapturedRules::from_catalog(
47            self.durable.rules.snapshot(),
48            relation,
49            event,
50            replica,
51            control,
52        )
53    }
54    fn resolve_mutation_target(&self, name: &str, bound: bool) -> Result<String, SQLError> {
55        self.resolve_mutation_target_name(name, bound)
56    }
57    fn resolve_rule_relation_with_control(
58        &self,
59        table: &str,
60        control: &uqa_core::memory::ProductionControl<'_>,
61    ) -> Result<uqa_core::memory::Produced<RelationIdentity>, SQLError> {
62        self.event_analysis_context()
63            .resolve_rule_relation_with_control(table, control)
64    }
65    fn resolve_mutation_target_with_control(
66        &self,
67        name: &str,
68        bound: bool,
69        control: &uqa_core::memory::ProductionControl<'_>,
70    ) -> Result<uqa_core::memory::Produced<String>, SQLError> {
71        self.resolve_mutation_target_name_with_control(name, bound, control)
72    }
73    fn visit_rules_with_control(
74        &self,
75        table: &str,
76        event: RuleEvent,
77        reverse: bool,
78        control: &uqa_core::memory::ProductionControl<'_>,
79        visit: &mut dyn FnMut(&StoredRule) -> Result<(), SQLError>,
80    ) -> Result<(), SQLError> {
81        let relation = self
82            .event_analysis_context()
83            .resolve_rule_relation_with_control(table, control)?;
84        let replica = self.session_replication_role_is_replica_with_control(control)?;
85        let rules = self.query_catalog_snapshot.as_ref().map_or_else(
86            || self.durable.rules.snapshot(),
87            |snapshot| snapshot.rules.clone(),
88        );
89        uqa_sql::catalog::events::selection::visit_active_rules_with_control(
90            &rules, &relation, event, replica, reverse, control, visit,
91        )
92    }
93}
94
95impl uqa_sql::semantics::rules::action_binding::RuleSourceCatalog for Engine {
96    fn query_source_columns(
97        &self,
98        name: &str,
99        relations_bound: bool,
100    ) -> Result<Option<Vec<String>>, SQLError> {
101        uqa_execution::catalog::projection::query_source_column_names(
102            &self.catalog_execution(),
103            name,
104            relations_bound,
105        )
106    }
107    fn rule_relation_definitions(
108        &self,
109        name: &str,
110    ) -> Result<Vec<uqa_sql::ast::ColumnDef>, SQLError> {
111        self.event_analysis_context()
112            .rule_relation_definitions(name)
113    }
114    fn rule_relation_columns_with_control(
115        &self,
116        name: &str,
117        control: &uqa_core::memory::ProductionControl<'_>,
118    ) -> Result<uqa_core::memory::Produced<Vec<String>>, SQLError> {
119        self.event_analysis_context()
120            .rule_relation_column_names_with_control(name, control)
121    }
122    fn rule_relation_definitions_with_control(
123        &self,
124        name: &str,
125        control: &uqa_core::memory::ProductionControl<'_>,
126    ) -> Result<uqa_core::memory::Produced<Vec<uqa_sql::ast::ColumnDef>>, SQLError> {
127        self.event_analysis_context()
128            .rule_relation_definitions_with_control(name, control)
129    }
130}