uqa_engine/capabilities/
event_registry.rs1use crate::Engine;
9use uqa_core::RelationIdentity;
10use uqa_execution::schema::events::context::{ConstraintTriggerEvents, EventLifecycleContext};
11use uqa_sql::{
12 ast::TableHierarchy,
13 catalog::{
14 constraints::ConstraintIdentity,
15 events::{
16 definition::lookup::{EventLookupContext, EventPartitionCatalog},
17 reads::{EventCatalogReads, EventLookupState, RuleCatalogRead, TriggerCatalogRead},
18 RuleCatalog, TriggerCatalog,
19 },
20 },
21};
22
23impl Engine {
24 pub(crate) fn event_lookup_context(&self) -> EventLookupContext<'_> {
25 EventLookupContext {
26 analysis: self.event_analysis_context(),
27 partitions: self,
28 registry: self,
29 state: self,
30 }
31 }
32 pub(crate) fn event_lifecycle_context(&self) -> EventLifecycleContext<'_> {
33 EventLifecycleContext {
34 identities: self.catalog_identity_reservation_context(),
35 lookup: self.event_lookup_context(),
36 catalog: self.event_catalog_context(),
37 writer: self,
38 notices: &self.runtime.notices,
39 views: self,
40 constraint_names: self.schema_publication_context().constraint_names(),
41 pending: self,
42 }
43 }
44}
45impl EventCatalogReads for Engine {
46 fn read_rules(&self) -> RuleCatalogRead<'_> {
47 Box::new(self.durable.rules.read())
48 }
49 fn read_triggers(&self) -> TriggerCatalogRead<'_> {
50 Box::new(self.durable.triggers.read())
51 }
52}
53impl EventPartitionCatalog for Engine {
54 fn contains_loaded_table(&self, relation: &RelationIdentity) -> bool {
55 self.storage.tables.read().contains_key(relation)
56 }
57 fn table_names(&self) -> Result<Vec<String>, String> {
58 Engine::table_names_in_execution(self).map_err(|error| error.to_string())
59 }
60 fn try_table_hierarchy(&self, name: &str) -> Result<TableHierarchy, String> {
61 Engine::try_table_hierarchy(self, name).map_err(|error| error.to_string())
62 }
63}
64impl ConstraintTriggerEvents for Engine {
65 fn forget(&self, identity: &ConstraintIdentity) {
66 self.forget_constraint_trigger_events(identity);
67 }
68 fn rename_trigger(&self, identity: &ConstraintIdentity, name: &str) {
69 self.rename_pending_constraint_trigger(identity, name);
70 }
71 fn rename_constraint(&self, identity: &ConstraintIdentity, name: &str) {
72 self.rename_constraint_trigger_identity(identity, name);
73 }
74}
75
76impl Engine {
77 pub(crate) fn event_catalog_context(
78 &self,
79 ) -> uqa_execution::schema::events::EventCatalogContext<'_> {
80 uqa_execution::schema::events::EventCatalogContext {
81 registry: self,
82 publication: self,
83 changes: self,
84 }
85 }
86}
87impl uqa_execution::schema::events::EventCatalogGuards for Engine {
88 fn triggers(&self) -> uqa_execution::schema::events::TriggerCatalogWrite<'_> {
89 Box::new(self.durable.triggers.write())
90 }
91 fn rules(&self) -> uqa_execution::schema::events::RuleCatalogWrite<'_> {
92 Box::new(self.durable.rules.write())
93 }
94}
95impl uqa_execution::schema::events::EventCatalogPublication for Engine {
96 fn finish_publication(&self) -> Result<(), uqa_sql::SQLError> {
97 self.publish_catalog_constraint_tuples()
98 }
99 fn persist_triggers(
100 &self,
101 triggers: &uqa_sql::catalog::events::TriggerCatalog,
102 ) -> Result<(), uqa_sql::SQLError> {
103 uqa_execution::schema::events::persistence::persist_trigger_catalog_snapshot(
104 self.storage.catalog.as_deref(),
105 self,
106 triggers,
107 )
108 }
109 fn persist_rules(
110 &self,
111 rules: &uqa_sql::catalog::events::RuleCatalog,
112 ) -> Result<(), uqa_sql::SQLError> {
113 uqa_execution::schema::events::persistence::persist_rule_catalog_snapshot(
114 self.storage.catalog.as_deref(),
115 self,
116 rules,
117 )
118 }
119}
120
121impl EventLookupState for Engine {
122 fn query_rules(&self) -> Option<&RuleCatalog> {
123 self.query_catalog_snapshot
124 .as_ref()
125 .map(|snapshot| snapshot.rules.as_ref())
126 }
127 fn query_triggers(&self) -> Option<&TriggerCatalog> {
128 self.query_catalog_snapshot
129 .as_ref()
130 .map(|snapshot| snapshot.triggers.as_ref())
131 }
132 fn session_replication_role_is_replica(&self) -> bool {
133 Engine::session_replication_role_is_replica(self)
134 }
135}
136
137impl Engine {
138 pub(crate) fn event_restore_context(
139 &self,
140 ) -> uqa_execution::schema::events::persistence::EventRestoreContext<'_> {
141 uqa_execution::schema::events::persistence::EventRestoreContext {
142 analysis: self.event_analysis_context(),
143 reads: self,
144 catalog: self.event_catalog_context(),
145 relations: self,
146 }
147 }
148}
149use uqa_sql::{ast::RelationPersistence, catalog::events::persistence::EventRelationPersistence};
150impl EventRelationPersistence for Engine {
151 fn rule_relation_is_temporary(&self, relation: &RelationIdentity) -> bool {
152 self.storage
153 .tables
154 .read()
155 .get(relation)
156 .is_some_and(|table| table.persistence == RelationPersistence::Temporary)
157 || self
158 .durable
159 .views
160 .read()
161 .get(relation)
162 .is_some_and(|view| view.persistence == RelationPersistence::Temporary)
163 || self
164 .durable
165 .foreign_tables
166 .read()
167 .get(relation)
168 .is_some_and(|table| table.persistence == RelationPersistence::Temporary)
169 }
170 fn trigger_relation_persistence(
171 &self,
172 relation: &RelationIdentity,
173 ) -> Option<RelationPersistence> {
174 self.storage
175 .tables
176 .read()
177 .get(relation)
178 .map(|table| table.persistence)
179 .or_else(|| {
180 self.durable
181 .views
182 .read()
183 .get(relation)
184 .map(|view| view.persistence)
185 })
186 .or_else(|| {
187 self.durable
188 .foreign_tables
189 .read()
190 .get(relation)
191 .map(|table| table.persistence)
192 })
193 }
194}