Skip to main content

uqa_engine/capabilities/
table_alteration.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind table alteration consumers to the active schema, lifecycle registries, and transaction state.
8use crate::{session::StatementReadSnapshot, Engine};
9use uqa_execution::schema::{
10    columns::removal::{ColumnRemovalContext, ColumnRemovalViews},
11    table_alteration::{
12        binding::TableAlterBindingContext,
13        entry::{
14            RelationEventAlterContext, RelationEventAlterTransactions, RelationEventAlterWrite,
15            TableAlterEntryContext, TableAlterSession, TableAlterTransactions, TableAlterWrite,
16        },
17        TableAlterContext, TableEventLifecycle, TableLifecycle,
18    },
19};
20use uqa_sql::{ast::EventEnableMode, SQLError, SQLResult};
21use uqa_storage::StorageBackendResult;
22
23impl uqa_execution::schema::columns::rows::changes::RewriteRowChanges for Engine {
24    fn storage_generation(&self, table: &str) -> Result<[u8; 16], SQLError> {
25        Ok(self.require_table(table)?.storage_generation())
26    }
27
28    fn visit_changes(
29        &self,
30        visit: &mut dyn FnMut(
31            &[uqa_execution::row_locks::publication::TransactionRowChange],
32        ) -> Result<(), SQLError>,
33    ) -> Result<(), SQLError> {
34        let frames = self.session.transactions.lock();
35        for frame in frames.iter() {
36            visit(&frame.row_changes)?;
37        }
38        Ok(())
39    }
40}
41
42impl Engine {
43    pub(crate) fn table_alter_binding_context(&self) -> TableAlterBindingContext<'_> {
44        TableAlterBindingContext {
45            names: self,
46            catalog: self,
47            authority: self.table_privilege_context(),
48            creation: self.relation_creation_context(),
49            locks: self,
50            notices: self.query_runtime_view().notices,
51        }
52    }
53
54    pub(crate) fn table_alter_entry_context(
55        &self,
56    ) -> TableAlterEntryContext<'_, StatementReadSnapshot> {
57        TableAlterEntryContext {
58            session: self,
59            binding: self.table_alter_binding_context(),
60            tables: self,
61            events: self,
62            views: self,
63            foreign_tables: self,
64            sequences: self,
65            indexes: self,
66            notices: self.query_runtime_view().notices,
67        }
68    }
69    pub(crate) fn table_alter_context(&self) -> TableAlterContext<'_, StatementReadSnapshot> {
70        TableAlterContext {
71            schema_moves: self.relation_schema_context(),
72            row_changes: self,
73            binding: self.table_alter_binding_context(),
74            ownership: self.table_ownership_context(),
75            hierarchy: self.hierarchy_execution_context(),
76            constraints: self.constraint_alter_context(),
77            addition: self.column_addition_context(),
78            columns: self.column_alter_context(),
79            expressions: self,
80            removal: self.column_removal_context(),
81            identities: uqa_execution::schema::table_alteration::identity::IdentityAlterContext {
82                definitions: self.sequence_definition_context(),
83                catalog: self,
84            },
85            lifecycle: self,
86            events: self,
87        }
88    }
89    pub(crate) fn column_removal_context(&self) -> ColumnRemovalContext<'_> {
90        ColumnRemovalContext { deletion: self }
91    }
92}
93impl TableAlterSession for Engine {
94    fn in_transaction_block(&self) -> bool {
95        Engine::in_transaction_block(self)
96    }
97}
98impl TableAlterTransactions<StatementReadSnapshot> for Engine {
99    fn with_table_write(
100        &self,
101        write: TableAlterWrite<'_, StatementReadSnapshot>,
102    ) -> Result<SQLResult, SQLError> {
103        self.with_implicit_definition_transaction(|engine| write(&engine.table_alter_context()))
104    }
105}
106impl RelationEventAlterTransactions for Engine {
107    fn with_event_write(&self, write: RelationEventAlterWrite<'_>) -> Result<SQLResult, SQLError> {
108        self.with_implicit_transaction(|engine| {
109            write(&RelationEventAlterContext {
110                events: engine,
111                foreign_access: engine,
112            })
113        })
114    }
115}
116impl TableLifecycle for Engine {
117    fn rename_table(&self, from: &str, to: &str) -> StorageBackendResult<bool> {
118        self.try_rename_table_inner(from, to)
119    }
120    fn rename_column(&self, table: &str, from: &str, to: &str) -> StorageBackendResult<bool> {
121        self.try_rename_column(table, from, to)
122    }
123}
124impl TableEventLifecycle for Engine {
125    fn rename_trigger(&self, table: &str, from: &str, to: &str) -> Result<(), SQLError> {
126        self.event_lifecycle_context()
127            .rename_trigger(table, from, to)
128    }
129    fn rename_trigger_constraint(&self, table: &str, from: &str, to: &str) -> Result<(), SQLError> {
130        self.event_lifecycle_context()
131            .rename_trigger_constraint(table, from, to)
132    }
133    fn rename_rule(&self, table: &str, from: &str, to: &str) -> Result<(), SQLError> {
134        self.event_lifecycle_context().rename_rule(table, from, to)
135    }
136    fn set_trigger_enable_mode(
137        &self,
138        table: &str,
139        name: Option<&str>,
140        mode: EventEnableMode,
141    ) -> Result<(), SQLError> {
142        self.event_lifecycle_context()
143            .set_trigger_enable_mode(table, name, mode)
144    }
145    fn set_rule_enable_mode(
146        &self,
147        table: &str,
148        name: &str,
149        mode: EventEnableMode,
150    ) -> Result<(), SQLError> {
151        self.event_lifecycle_context()
152            .set_rule_enable_mode(table, name, mode)
153    }
154}
155impl ColumnRemovalViews for Engine {
156    fn dependents(&self, table: &str, column: &str) -> StorageBackendResult<Vec<String>> {
157        self.views_depending_on_column(table, column)
158    }
159}