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            plan_type_transform: |expression| {
80                uqa_planner::optimizer::optimize_scalar_expression(
81                    expression,
82                    &uqa_planner::OptimizerConfig::new(uqa_execution::scalar::eval_constant_scalar),
83                )
84            },
85            removal: self.column_removal_context(),
86            identities: uqa_execution::schema::table_alteration::identity::IdentityAlterContext {
87                definitions: self.sequence_definition_context(),
88                catalog: self,
89            },
90            lifecycle: self,
91            events: self,
92        }
93    }
94    pub(crate) fn column_removal_context(&self) -> ColumnRemovalContext<'_> {
95        ColumnRemovalContext { deletion: self }
96    }
97}
98impl TableAlterSession for Engine {
99    fn in_transaction_block(&self) -> bool {
100        Engine::in_transaction_block(self)
101    }
102}
103impl TableAlterTransactions<StatementReadSnapshot> for Engine {
104    fn with_table_write(
105        &self,
106        write: TableAlterWrite<'_, StatementReadSnapshot>,
107    ) -> Result<SQLResult, SQLError> {
108        self.with_implicit_definition_transaction(|engine| write(&engine.table_alter_context()))
109    }
110}
111impl RelationEventAlterTransactions for Engine {
112    fn with_event_write(&self, write: RelationEventAlterWrite<'_>) -> Result<SQLResult, SQLError> {
113        self.with_implicit_transaction(|engine| {
114            write(&RelationEventAlterContext {
115                events: engine,
116                foreign_access: engine,
117            })
118        })
119    }
120}
121impl TableLifecycle for Engine {
122    fn rename_table(&self, from: &str, to: &str) -> StorageBackendResult<bool> {
123        self.try_rename_table_inner(from, to)
124    }
125    fn rename_column(&self, table: &str, from: &str, to: &str) -> StorageBackendResult<bool> {
126        self.try_rename_column(table, from, to)
127    }
128}
129impl TableEventLifecycle for Engine {
130    fn rename_trigger(&self, table: &str, from: &str, to: &str) -> Result<(), SQLError> {
131        self.event_lifecycle_context()
132            .rename_trigger(table, from, to)
133    }
134    fn rename_trigger_constraint(&self, table: &str, from: &str, to: &str) -> Result<(), SQLError> {
135        self.event_lifecycle_context()
136            .rename_trigger_constraint(table, from, to)
137    }
138    fn rename_rule(&self, table: &str, from: &str, to: &str) -> Result<(), SQLError> {
139        self.event_lifecycle_context().rename_rule(table, from, to)
140    }
141    fn set_trigger_enable_mode(
142        &self,
143        table: &str,
144        name: Option<&str>,
145        mode: EventEnableMode,
146    ) -> Result<(), SQLError> {
147        self.event_lifecycle_context()
148            .set_trigger_enable_mode(table, name, mode)
149    }
150    fn set_rule_enable_mode(
151        &self,
152        table: &str,
153        name: &str,
154        mode: EventEnableMode,
155    ) -> Result<(), SQLError> {
156        self.event_lifecycle_context()
157            .set_rule_enable_mode(table, name, mode)
158    }
159}
160impl ColumnRemovalViews for Engine {
161    fn dependents(&self, table: &str, column: &str) -> StorageBackendResult<Vec<String>> {
162        self.views_depending_on_column(table, column)
163    }
164}