Skip to main content

uqa_engine/capabilities/
table_privileges.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Lend relation registries and retained table generations to privilege inquiry.
8
9use crate::{Engine, TableState};
10use parking_lot::RwLockReadGuard;
11use std::{collections::BTreeMap, sync::Arc};
12use uqa_core::RelationIdentity;
13use uqa_execution::catalog::security::table_inquiry::{
14    PrivilegeForeignSecurityRead, PrivilegeForeignTablesRead, PrivilegeViewsRead,
15    TablePrivilegeContext, TablePrivilegeRead, TablePrivilegeRegistry, TablePrivilegeState,
16};
17use uqa_sql::catalog::security::BoundTableSecurity;
18use uqa_storage::StorageBackendResult;
19
20struct TablePrivilegeGuard<'a>(RwLockReadGuard<'a, BTreeMap<RelationIdentity, Arc<TableState>>>);
21impl TablePrivilegeRead for TablePrivilegeGuard<'_> {
22    fn security_entries(
23        &self,
24    ) -> Box<dyn Iterator<Item = (RelationIdentity, BoundTableSecurity)> + '_> {
25        Box::new(
26            self.0
27                .iter()
28                .map(|(relation, table)| (relation.clone(), table.security())),
29        )
30    }
31    fn keys(&self) -> Box<dyn Iterator<Item = &RelationIdentity> + '_> {
32        Box::new(self.0.keys())
33    }
34    fn get(&self, relation: &RelationIdentity) -> Option<&dyn TablePrivilegeState> {
35        self.0
36            .get(relation)
37            .map(|table| table.as_ref() as &dyn TablePrivilegeState)
38    }
39    fn retained(&self, relation: &RelationIdentity) -> Option<Arc<dyn TablePrivilegeState>> {
40        self.0
41            .get(relation)
42            .cloned()
43            .map(|table| table as Arc<dyn TablePrivilegeState>)
44    }
45}
46impl TablePrivilegeState for TableState {
47    fn role_owner(&self) -> uqa_sql::catalog::roles::RoleIdentity {
48        self.role_owner()
49    }
50    fn columns(&self) -> uqa_execution::catalog::security::table_inquiry::TableColumnsRead<'_> {
51        Box::new(self.columns.read())
52    }
53    fn security(&self) -> BoundTableSecurity {
54        self.security()
55    }
56    fn security_snapshot_with_control(
57        &self,
58        control: &uqa_core::memory::ProductionControl<'_>,
59    ) -> Result<uqa_core::memory::Produced<Arc<BoundTableSecurity>>, uqa_sql::SQLError> {
60        Ok(control.finish(self.security.snapshot(), control.empty_reservation())?)
61    }
62    fn with_columns_with_control(
63        &self,
64        control: &uqa_core::memory::ProductionControl<'_>,
65        read: &mut dyn FnMut(&[uqa_sql::ast::ColumnDef]) -> Result<(), uqa_sql::SQLError>,
66    ) -> Result<(), uqa_sql::SQLError> {
67        control.check()?;
68        read(&self.columns.read())?;
69        Ok(control.check()?)
70    }
71    fn column_names(&self) -> Vec<String> {
72        self.columns
73            .read()
74            .iter()
75            .map(|column| column.name.clone())
76            .collect()
77    }
78}
79impl TablePrivilegeRegistry for Engine {
80    fn refresh_tables(&self) -> StorageBackendResult<()> {
81        self.synchronize_table_catalog()
82    }
83    fn refresh_catalog(&self) -> StorageBackendResult<()> {
84        self.synchronize_catalog_registries()
85    }
86    fn retained_table_with_control(
87        &self,
88        relation: &RelationIdentity,
89        control: &uqa_core::memory::ProductionControl<'_>,
90    ) -> Result<Option<Arc<dyn TablePrivilegeState>>, uqa_sql::SQLError> {
91        control.check()?;
92        Ok(self
93            .storage
94            .tables
95            .read()
96            .get(relation)
97            .cloned()
98            .map(|table| table as Arc<dyn TablePrivilegeState>))
99    }
100    fn tables(&self) -> Box<dyn TablePrivilegeRead + '_> {
101        Box::new(TablePrivilegeGuard(self.storage.tables.read()))
102    }
103    fn views(&self) -> PrivilegeViewsRead<'_> {
104        Box::new(self.durable.views.read())
105    }
106    fn foreign_tables(&self) -> PrivilegeForeignTablesRead<'_> {
107        Box::new(self.durable.foreign_tables.read())
108    }
109    fn foreign_security(&self) -> PrivilegeForeignSecurityRead<'_> {
110        Box::new(self.durable.foreign_table_security.read())
111    }
112}
113impl Engine {
114    pub(crate) fn table_privilege_context(&self) -> TablePrivilegeContext<'_> {
115        TablePrivilegeContext {
116            names: self,
117            roles: self,
118            sequences: self.sequence_privilege_inquiry(),
119            catalog: self.catalog_execution(),
120            registry: self,
121            snapshots: self,
122        }
123    }
124}
125
126impl uqa_sql::catalog::security::system_relations::SystemRelationSecurityCatalog for Engine {
127    fn system_relation_securities(
128        &self,
129    ) -> uqa_sql::catalog::security::system_relations::SystemRelationSecurityRead<'_> {
130        Box::new(self.durable.system_relation_security.read())
131    }
132}
133impl uqa_execution::catalog::security::system_relations::SystemRelationSecurityState for Engine {
134    fn system_relation_securities_write(
135        &self,
136    ) -> Box<
137        dyn std::ops::DerefMut<
138                Target = uqa_sql::catalog::security::system_relations::SystemRelationSecurities,
139            > + '_,
140    > {
141        Box::new(self.durable.system_relation_security.write())
142    }
143}