Skip to main content

uqa_engine/capabilities/
table_ownership.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Retain the selected table generation and lend schema/security publication state to ownership execution.
8use crate::{Engine, TableState};
9use std::sync::Arc;
10use uqa_core::RelationIdentity;
11use uqa_execution::{
12    catalog::security::{
13        table_grants::context::TableSecurityWrite,
14        table_inquiry::{TableColumnsRead, TablePrivilegeState},
15        table_ownership::{
16            TableOwnerPublication, TableOwnerRegistry, TableOwnerSchema, TableOwnerState,
17            TableOwnershipContext,
18        },
19    },
20    schema::sequences::role_ownership::OwnedSequenceSecurityWrite,
21};
22use uqa_sql::{ast::RelationPersistence, catalog::security::BoundTableSecurity};
23use uqa_storage::StorageBackendResult;
24struct OwnedTable<'a> {
25    engine: &'a Engine,
26    state: Arc<TableState>,
27}
28impl TableOwnerRegistry for Engine {
29    fn table(&self, relation: &RelationIdentity) -> Option<Box<dyn TableOwnerState + '_>> {
30        self.storage
31            .tables
32            .read()
33            .get(relation)
34            .cloned()
35            .map(|state| {
36                Box::new(OwnedTable {
37                    engine: self,
38                    state,
39                }) as Box<dyn TableOwnerState>
40            })
41    }
42}
43impl TablePrivilegeState for OwnedTable<'_> {
44    fn role_owner(&self) -> uqa_sql::catalog::roles::RoleIdentity {
45        self.state.role_owner()
46    }
47    fn security(&self) -> BoundTableSecurity {
48        self.state.security()
49    }
50    fn columns(&self) -> TableColumnsRead<'_> {
51        Box::new(self.state.columns.read())
52    }
53    fn column_names(&self) -> Vec<String> {
54        TablePrivilegeState::column_names(self.state.as_ref())
55    }
56}
57impl TableOwnerState for OwnedTable<'_> {
58    fn object_id(&self) -> [u8; 16] {
59        self.state.object_id()
60    }
61    fn persistence(&self) -> RelationPersistence {
62        self.state.persistence
63    }
64    fn schema(&self) -> TableOwnerSchema {
65        let table = self.state.as_ref();
66        let columns = table.columns.read().clone();
67        let constraints = uqa_sql::ast::TableConstraintSet {
68            columns_declared: Some(*table.columns_declared.read()),
69            checks: table.table_checks.read().clone(),
70            foreign_keys: table.foreign_keys.read().clone(),
71            key_constraints: table.key_constraints.read().clone(),
72            persistence: table.persistence,
73            on_commit: table.on_commit,
74            hierarchy: table.hierarchy.read().clone(),
75            catalog_oids: table.recorded_catalog_oids(),
76            row_type_array_name: table.row_type_array_name.read().clone(),
77            dropped_attributes: table.dropped_attributes.read().clone(),
78        };
79        TableOwnerSchema {
80            columns,
81            constraints,
82        }
83    }
84    fn persist_schema(
85        &self,
86        name: &str,
87        schema: &TableOwnerSchema,
88        security: &BoundTableSecurity,
89    ) -> StorageBackendResult<()> {
90        self.engine
91            .try_save_table_schema_with_components_and_security(
92                name,
93                self.state.as_ref(),
94                &schema.columns,
95                &schema.constraints,
96                security,
97            )
98    }
99    fn security_write(&self) -> TableSecurityWrite<'_> {
100        Box::new(self.state.security.write())
101    }
102}
103impl TableOwnerPublication for Engine {
104    fn sequence_security_write(&self) -> OwnedSequenceSecurityWrite<'_> {
105        Box::new(self.durable.sequence_security.write())
106    }
107}
108impl Engine {
109    pub(crate) fn table_ownership_context(&self) -> TableOwnershipContext<'_> {
110        TableOwnershipContext {
111            writer: self,
112            tables: self,
113            roles: self.role_transfer_context(),
114            owned_sequences: self,
115            sequences: self,
116            publication: self,
117            changes: self,
118        }
119    }
120}