Skip to main content

uqa_engine/capabilities/
namespaces.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind namespace execution to current authorization state and catalog publication guards.
8use super::MutationCoordinator;
9use crate::Engine;
10use uqa_execution::schema::namespaces::{
11    privileges::{
12        SchemaPrivilegeContext, SchemaPrivilegeNotices, SchemaPrivilegeRegistry, SchemaRegistryRead,
13    },
14    NamespaceCatalogChanges, NamespaceCatalogRefresh, SchemaCreationContext, SchemaOwnerContext,
15    SchemaRegistration, SchemaRegistrationPersistence, SchemaRegistrationState,
16    SchemaRegistryWrite, SchemaSecurityCatalog, SchemaSecurityPersistence,
17    SchemaSecurityPublication, SchemaStatementWriter,
18};
19use uqa_sql::{catalog::security::BoundSchemaSecurity, SQLError};
20use uqa_storage::StorageBackendResult;
21
22impl Engine {
23    pub(super) fn schema_lock_context(
24        &self,
25    ) -> uqa_execution::schema::namespaces::locking::SchemaLockContext<'_> {
26        uqa_execution::schema::namespaces::locking::SchemaLockContext {
27            objects: self,
28            relations: self,
29            rows: self,
30            catalog: self,
31        }
32    }
33    pub(crate) fn schema_creation_context(&self) -> SchemaCreationContext<'_> {
34        SchemaCreationContext {
35            tuples: self.schema_lock_context(),
36            writer: self,
37            session: self,
38            roles: self,
39            catalog: self,
40            schemas: self,
41            notices: self,
42            locks: self,
43            database: self,
44            registration: self,
45        }
46    }
47    pub(crate) fn schema_owner_context(&self) -> SchemaOwnerContext<'_> {
48        SchemaOwnerContext {
49            tuples: self.schema_lock_context(),
50            writer: self,
51            refresh: self,
52            session: self,
53            roles: self,
54            locks: self,
55            database: self,
56            catalog: self,
57            publication: self,
58            persistence: self,
59            changes: self,
60        }
61    }
62    pub(crate) fn schema_privilege_context(&self) -> SchemaPrivilegeContext<'_> {
63        SchemaPrivilegeContext {
64            tuples: self.schema_lock_context(),
65            locks: self,
66            writer: self,
67            refresh: self,
68            session: self,
69            roles: self,
70            registry: self,
71            persistence: self,
72            notices: self,
73            changes: self,
74        }
75    }
76}
77impl SchemaStatementWriter for Engine {
78    fn prepare_writer(&self) -> Result<(), SQLError> {
79        self.prepare_explicit_transaction_writer().map(|_| ())
80    }
81}
82impl NamespaceCatalogRefresh for Engine {
83    fn refresh_catalog(&self) -> StorageBackendResult<()> {
84        self.synchronize_catalog_registries()
85    }
86}
87impl SchemaRegistrationState for MutationCoordinator<'_> {
88    fn schemas_write(&self) -> SchemaRegistryWrite<'_> {
89        Box::new(self.durable.schemas.write())
90    }
91    fn contains_graph(&self, name: &str) -> bool {
92        self.durable.graphs.read().contains_key(name)
93    }
94}
95impl SchemaRegistrationPersistence for MutationCoordinator<'_> {
96    fn persist_schema(
97        &self,
98        name: &str,
99        security: &BoundSchemaSecurity,
100    ) -> StorageBackendResult<()> {
101        if let Some(catalog) = self.storage.catalog.as_ref() {
102            catalog.save_schema_row(&security.row(name).into())?;
103        }
104        Ok(())
105    }
106}
107impl NamespaceCatalogChanges for MutationCoordinator<'_> {
108    fn prepared_catalog_changed(
109        &self,
110        change: uqa_execution::statement::prepared::invalidation::PreparedCatalogChange,
111    ) {
112        self.note_prepared_catalog_change(change);
113    }
114    fn catalog_registry_changed(&self) {
115        self.note_catalog_registry_changed();
116    }
117    fn catalog_registry_changed_kind(
118        &self,
119        change: uqa_execution::statement::prepared::invalidation::CatalogRegistryChange,
120    ) {
121        self.note_catalog_registry_change(change);
122    }
123}
124impl NamespaceCatalogChanges for Engine {
125    fn prepared_catalog_changed(
126        &self,
127        change: uqa_execution::statement::prepared::invalidation::PreparedCatalogChange,
128    ) {
129        self.note_prepared_catalog_change(change);
130    }
131    fn catalog_registry_changed(&self) {
132        self.note_catalog_registry_changed();
133    }
134    fn catalog_registry_changed_kind(
135        &self,
136        change: uqa_execution::statement::prepared::invalidation::CatalogRegistryChange,
137    ) {
138        self.mutation_coordinator()
139            .note_catalog_registry_change(change);
140    }
141}
142impl SchemaRegistration for Engine {
143    fn register_schema(
144        &self,
145        name: &str,
146        if_not_exists: bool,
147        role_owner: uqa_core::catalog_role::RoleIdentity,
148        tuple: uqa_core::catalog_schema::SchemaTupleIdentity,
149    ) -> StorageBackendResult<bool> {
150        self.mutation_coordinator()
151            .register_schema(name, if_not_exists, role_owner, tuple)
152    }
153}
154impl SchemaSecurityCatalog for Engine {
155    fn schema_security(&self, name: &str) -> Option<BoundSchemaSecurity> {
156        self.schema_security_for_privilege(name)
157    }
158}
159impl SchemaSecurityPersistence for Engine {
160    fn persist_security(&self, name: &str, security: &BoundSchemaSecurity) -> Result<(), SQLError> {
161        self.persist_schema_security(name, security)
162    }
163}
164impl SchemaSecurityPublication for Engine {
165    fn publish_security(&self, name: &str, security: BoundSchemaSecurity) {
166        self.durable
167            .schemas
168            .write()
169            .insert(name.to_string(), security);
170    }
171}
172impl SchemaPrivilegeRegistry for Engine {
173    fn schemas_read(&self) -> SchemaRegistryRead<'_> {
174        Box::new(self.durable.schemas.read())
175    }
176    fn schemas_write(&self) -> SchemaRegistryWrite<'_> {
177        Box::new(self.durable.schemas.write())
178    }
179}
180impl SchemaPrivilegeNotices for Engine {
181    fn schema_privilege_notice(&self, notice: uqa_sql::SQLNotice) {
182        self.push_sql_notice(notice);
183    }
184}
185
186use uqa_execution::schema::namespaces::removal::{
187    EmptySchemaRemovalContext, EmptySchemaRemovalPersistence, EmptySchemaRemovalState,
188    SchemaDropNotices, SchemaRemovalContext, SchemaRemovalNames, SchemaRemovalPublication,
189};
190use uqa_sql::schema::namespaces::removal::{EmptySchemaCatalog, SchemaDropCatalog};
191
192impl Engine {
193    pub(crate) fn schema_removal_context(&self) -> SchemaRemovalContext<'_> {
194        SchemaRemovalContext {
195            deletion: self,
196            tuples: self.schema_lock_context(),
197            refresh: self,
198            catalog: self,
199            names: self,
200            locks: self,
201            publication: self,
202            notices: self,
203        }
204    }
205    pub(crate) fn empty_schema_removal_context(&self) -> EmptySchemaRemovalContext<'_> {
206        EmptySchemaRemovalContext {
207            tuples: self.schema_lock_context(),
208            refresh: self,
209            catalog: self,
210            state: self,
211            persistence: self,
212            changes: self,
213        }
214    }
215}
216impl EmptySchemaCatalog for Engine {
217    fn schema_registered(&self, name: &str) -> bool {
218        self.durable.schemas.read().contains_key(name)
219    }
220    fn schema_is_empty(&self, name: &str) -> bool {
221        Engine::schema_is_empty(self, name)
222    }
223}
224impl SchemaDropCatalog for Engine {
225    fn schema_security(&self, name: &str) -> Option<BoundSchemaSecurity> {
226        self.schema_security_for_privilege(name)
227    }
228    fn current_user_has_role_privileges(
229        &self,
230        role: &uqa_core::catalog_role::RoleIdentity,
231    ) -> bool {
232        Engine::current_user_has_role_privileges(self, role)
233    }
234    fn schema_is_graph(&self, name: &str) -> Result<bool, String> {
235        self.has_graph_in_execution(name)
236            .map_err(|error| error.to_string())
237    }
238}
239impl SchemaRemovalNames for Engine {
240    fn graph_tables(&self, schema: &str) -> StorageBackendResult<Vec<String>> {
241        self.schema_tables_in_execution(schema)
242    }
243}
244impl SchemaRemovalPublication for Engine {
245    fn drop_graph(&self, name: &str) -> StorageBackendResult<()> {
246        Engine::drop_graph(self, name).map(|_| ())
247    }
248}
249impl SchemaDropNotices for Engine {
250    fn schema_drop_notice(&self, notice: uqa_sql::SQLNotice) {
251        self.push_sql_notice(notice);
252    }
253}
254impl EmptySchemaRemovalState for Engine {
255    fn schema_registry_write(&self) -> SchemaRegistryWrite<'_> {
256        Box::new(self.durable.schemas.write())
257    }
258}
259impl EmptySchemaRemovalPersistence for Engine {
260    fn drop_schema_row(&self, name: &str) -> StorageBackendResult<()> {
261        if let Some(catalog) = self.storage.catalog.as_ref() {
262            catalog.drop_schema(name)?;
263        }
264        Ok(())
265    }
266}
267
268impl uqa_sql::catalog::resolution::candidates::RelationCandidateState for Engine {
269    fn temporary_schema_name_with_control(
270        &self,
271        control: &uqa_core::memory::ProductionControl<'_>,
272    ) -> Result<uqa_core::memory::Produced<String>, uqa_sql::SQLError> {
273        Ok(control.format(format_args!("pg_temp_{}", self.session_id))?)
274    }
275    fn with_search_path_with_control(
276        &self,
277        control: &uqa_core::memory::ProductionControl<'_>,
278        read: &mut dyn FnMut(
279            &dyn uqa_sql::catalog::resolution::candidates::SearchPathView,
280        ) -> Result<(), uqa_sql::SQLError>,
281    ) -> Result<(), uqa_sql::SQLError> {
282        control.check()?;
283        let state = self.session.state.read();
284        read(&crate::session::BorrowedSearchPath(&state))
285    }
286    fn temporary_schema_name(&self) -> String {
287        Engine::temporary_schema_name(self)
288    }
289    fn search_path(&self) -> uqa_sql::catalog::resolution::candidates::SearchPathRead<'_> {
290        Box::new(crate::session::LockedSearchPath::new(
291            self.session.state.read(),
292        ))
293    }
294}