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 column_names(&self) -> Vec<String> {
57        self.columns
58            .read()
59            .iter()
60            .map(|column| column.name.clone())
61            .collect()
62    }
63}
64impl TablePrivilegeRegistry for Engine {
65    fn refresh_tables(&self) -> StorageBackendResult<()> {
66        self.synchronize_table_catalog()
67    }
68    fn refresh_catalog(&self) -> StorageBackendResult<()> {
69        self.synchronize_catalog_registries()
70    }
71    fn tables(&self) -> Box<dyn TablePrivilegeRead + '_> {
72        Box::new(TablePrivilegeGuard(self.storage.tables.read()))
73    }
74    fn views(&self) -> PrivilegeViewsRead<'_> {
75        Box::new(self.durable.views.read())
76    }
77    fn foreign_tables(&self) -> PrivilegeForeignTablesRead<'_> {
78        Box::new(self.durable.foreign_tables.read())
79    }
80    fn foreign_security(&self) -> PrivilegeForeignSecurityRead<'_> {
81        Box::new(self.durable.foreign_table_security.read())
82    }
83}
84impl Engine {
85    pub(crate) fn table_privilege_context(&self) -> TablePrivilegeContext<'_> {
86        TablePrivilegeContext {
87            names: self,
88            roles: self,
89            sequences: self.sequence_privilege_inquiry(),
90            catalog: self.catalog_execution(),
91            registry: self,
92            snapshots: self,
93        }
94    }
95}
96
97impl uqa_sql::catalog::security::system_relations::SystemRelationSecurityCatalog for Engine {
98    fn system_relation_securities(
99        &self,
100    ) -> uqa_sql::catalog::security::system_relations::SystemRelationSecurityRead<'_> {
101        Box::new(self.durable.system_relation_security.read())
102    }
103}
104impl uqa_execution::catalog::security::system_relations::SystemRelationSecurityState for Engine {
105    fn system_relation_securities_write(
106        &self,
107    ) -> Box<
108        dyn std::ops::DerefMut<
109                Target = uqa_sql::catalog::security::system_relations::SystemRelationSecurities,
110            > + '_,
111    > {
112        Box::new(self.durable.system_relation_security.write())
113    }
114}