Skip to main content

uqa_engine/capabilities/
table_grants.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Retain table generations and lend live catalog guards to native GRANT execution.
8use crate::{Engine, TableState};
9use parking_lot::RwLockReadGuard;
10use std::{collections::BTreeMap, sync::Arc};
11use uqa_core::RelationIdentity;
12use uqa_execution::catalog::security::{
13    table_grants::context::{
14        TableGrantContext, TableGrantInputs, TableGrantNotices, TableGrantPersistence,
15        TableGrantRead, TableGrantRegistry, TableGrantState, TableSecurityWrite,
16    },
17    table_inquiry::TablePrivilegeState,
18};
19use uqa_sql::{
20    catalog::{
21        resolution::RelationResolution,
22        security::{table_grants::targets::TableGrantResolution, BoundTableSecurity},
23    },
24    SQLError,
25};
26struct GrantTable {
27    state: Arc<TableState>,
28}
29struct GrantTables<'a> {
30    guard: RwLockReadGuard<'a, BTreeMap<RelationIdentity, Arc<TableState>>>,
31}
32impl TableGrantRegistry for Engine {
33    fn tables(&self) -> Box<dyn TableGrantRead<'_> + '_> {
34        Box::new(GrantTables {
35            guard: self.storage.tables.read(),
36        })
37    }
38}
39impl<'a> TableGrantRead<'a> for GrantTables<'a> {
40    fn keys(&self) -> Box<dyn Iterator<Item = &RelationIdentity> + '_> {
41        Box::new(self.guard.keys())
42    }
43    fn retained(&self, relation: &RelationIdentity) -> Option<Box<dyn TableGrantState + 'a>> {
44        self.guard
45            .get(relation)
46            .cloned()
47            .map(|state| Box::new(GrantTable { state }) as Box<dyn TableGrantState + 'a>)
48    }
49}
50impl TablePrivilegeState for GrantTable {
51    fn role_owner(&self) -> uqa_sql::catalog::roles::RoleIdentity {
52        self.state.role_owner()
53    }
54    fn columns(&self) -> uqa_execution::catalog::security::table_inquiry::TableColumnsRead<'_> {
55        Box::new(self.state.columns.read())
56    }
57    fn security(&self) -> BoundTableSecurity {
58        self.state.security()
59    }
60    fn column_names(&self) -> Vec<String> {
61        TablePrivilegeState::column_names(self.state.as_ref())
62    }
63}
64impl TableGrantState for GrantTable {
65    fn relation_oid(&self) -> u32 {
66        self.state.relation_oids().relation
67    }
68    fn security_write(&self) -> TableSecurityWrite<'_> {
69        Box::new(self.state.security.write())
70    }
71    fn persistence(&self) -> uqa_sql::ast::RelationPersistence {
72        self.state.persistence
73    }
74}
75impl TableGrantResolution for Engine {
76    fn resolve_visible_relation_kind(&self, name: &str) -> Result<RelationResolution, SQLError> {
77        Engine::resolve_visible_relation_kind(self, name)
78    }
79}
80impl TableGrantNotices for Engine {
81    fn notice(&self, notice: uqa_sql::SQLNotice) {
82        self.push_sql_notice(notice);
83    }
84}
85impl TableGrantPersistence for Engine {
86    fn persist_relation_acl(
87        &self,
88        relation: &RelationIdentity,
89        column: Option<&str>,
90        entry: &uqa_storage::catalog::relation_acl::RelationAclTuple,
91    ) -> uqa_storage::StorageBackendResult<()> {
92        if let Some(catalog) = &self.storage.catalog {
93            catalog.save_relation_acl(relation, column, entry)?;
94        }
95        Ok(())
96    }
97}
98impl TableGrantInputs for Engine {
99    fn table_grant_context(&self) -> TableGrantContext<'_> {
100        TableGrantContext {
101            writer: self,
102            bindings: self,
103            locks: self,
104            shared_locks: self,
105            rows: self,
106            system: self,
107            resolution: self,
108            namespaces: self,
109            names: self,
110            roles: self,
111            registry: self,
112            tables: self,
113            views: self,
114            foreign: self,
115            acls: self,
116            catalog: self.storage.catalog.as_deref(),
117            changes: self,
118            notices: self,
119            sequences: self.sequence_privilege_context(),
120        }
121    }
122}