Skip to main content

uqa_engine/capabilities/
foreign_table_alteration.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind foreign-table alteration to current catalog values, provider writes and registry guards.
8use crate::Engine;
9use uqa_core::RelationIdentity;
10use uqa_execution::{
11    catalog::{foreign::StoredForeignTable, security::BoundTableSecurity},
12    schema::{
13        foreign_table_alteration::{
14            ForeignMemoryRegistryWrite, ForeignSecurityRegistryWrite, ForeignTableAlterAccess,
15            ForeignTableAlterCatalog, ForeignTableAlterContext, ForeignTableAlterPublication,
16            ForeignTableAlterTransactions, ForeignTableAlterWrite, ForeignTableOwnerWriter,
17            ForeignTableRegistryWrite,
18        },
19        sequences::role_ownership::OwnedSequenceSecurityWrite,
20    },
21};
22use uqa_sql::SQLError;
23use uqa_storage::StorageBackendResult;
24
25impl Engine {
26    pub(crate) fn foreign_table_alter_context(&self) -> ForeignTableAlterContext<'_> {
27        ForeignTableAlterContext {
28            deletion: self,
29            schema_moves: self.relation_schema_context(),
30            names: self,
31            catalog: self,
32            authority: self.table_privilege_context(),
33            creation: self.relation_creation_context(),
34            locks: self,
35            writer: self,
36            roles: self.role_transfer_context(),
37            dependencies: self,
38            owned_sequences: self,
39            sequence_publication: self,
40            publication: self,
41            changes: self,
42            notices: self.query_runtime_view().notices,
43        }
44    }
45}
46impl ForeignTableAlterTransactions for Engine {
47    fn with_foreign_table_write(&self, write: ForeignTableAlterWrite<'_>) -> Result<(), SQLError> {
48        self.with_implicit_definition_transaction(|engine| {
49            write(&engine.foreign_table_alter_context())
50        })
51    }
52}
53impl ForeignTableAlterCatalog for Engine {
54    fn contains_table(&self, relation: &RelationIdentity) -> bool {
55        self.durable.foreign_tables.read().contains_key(relation)
56    }
57    fn security(&self, relation: &RelationIdentity) -> Option<BoundTableSecurity> {
58        self.durable
59            .foreign_table_security
60            .read()
61            .get(relation)
62            .cloned()
63    }
64    fn table(&self, relation: &RelationIdentity) -> Option<StoredForeignTable> {
65        self.durable.foreign_tables.read().get(relation).cloned()
66    }
67}
68impl ForeignTableAlterAccess for Engine {
69    fn ensure_owner(&self, name: &str) -> Result<String, SQLError> {
70        self.ensure_foreign_table_owner(name)
71    }
72}
73impl ForeignTableOwnerWriter for Engine {
74    fn prepare_writer(&self) -> Result<(), SQLError> {
75        self.prepare_explicit_transaction_writer().map(|_| ())
76    }
77}
78impl ForeignTableAlterPublication for Engine {
79    fn persist_definition(
80        &self,
81        relation: &RelationIdentity,
82        table: &StoredForeignTable,
83    ) -> StorageBackendResult<()> {
84        uqa_execution::schema::publication::dependencies::ForeignSchemaPublication::persist_foreign_table(self, relation, table)
85    }
86    fn persist_rename(
87        &self,
88        from: &RelationIdentity,
89        to: &RelationIdentity,
90    ) -> StorageBackendResult<Option<bool>> {
91        self.storage
92            .catalog
93            .as_ref()
94            .map(|catalog| catalog.rename_foreign_table(from, to))
95            .transpose()
96    }
97    fn tables_write(&self) -> ForeignTableRegistryWrite<'_> {
98        Box::new(self.durable.foreign_tables.write())
99    }
100    fn security_write(&self) -> ForeignSecurityRegistryWrite<'_> {
101        Box::new(self.durable.foreign_table_security.write())
102    }
103    fn memory_tables_write(&self) -> ForeignMemoryRegistryWrite<'_> {
104        Box::new(self.extensions.foreign_memory_tables.write())
105    }
106    fn sequence_security_write(&self) -> OwnedSequenceSecurityWrite<'_> {
107        Box::new(self.durable.sequence_security.write())
108    }
109    fn persist_security(
110        &self,
111        relation: &RelationIdentity,
112        security: &BoundTableSecurity,
113    ) -> Result<(), SQLError> {
114        self.persist_foreign_table_security(relation, security)
115    }
116}