Skip to main content

uqa_engine/capabilities/
view_alteration.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind view alteration to the active transaction and retained view registry guards.
8use crate::Engine;
9use uqa_core::RelationIdentity;
10use uqa_execution::{
11    catalog::view::{StoredView, ViewPublication},
12    schema::view_alteration::{
13        ViewAlterAccess, ViewAlterCatalog, ViewAlterContext, ViewAlterPublication,
14        ViewAlterTransactions, ViewAlterWrite,
15    },
16};
17use uqa_sql::{ast::RelationPersistence, SQLError};
18use uqa_storage::{StorageBackendResult, ViewRow};
19
20impl Engine {
21    pub(crate) fn view_alter_context(&self) -> ViewAlterContext<'_> {
22        ViewAlterContext {
23            schema_moves: self.relation_schema_context(),
24            names: self,
25            catalog: self,
26            authority: self.table_privilege_context(),
27            creation: self.relation_creation_context(),
28            locks: self,
29            roles: self.role_transfer_context(),
30            dependencies: self,
31            column_dependencies: self,
32            publication: self,
33            changes: self,
34            rewrite: self.view_rewrite_context(),
35            notices: self.query_runtime_view().notices,
36        }
37    }
38}
39impl uqa_execution::schema::view_alteration::ViewColumnRenameInputs for Engine {
40    fn view_column_rename_context(
41        &self,
42    ) -> uqa_execution::schema::view_alteration::ViewColumnRenameContext<'_> {
43        uqa_execution::schema::view_alteration::ViewColumnRenameContext {
44            views: self.view_reference_context(),
45            routines: self.routine_rewrite_context(),
46            events: self.event_lifecycle_context(),
47            values: self.composite_value_context(),
48        }
49    }
50}
51impl ViewAlterTransactions for Engine {
52    fn with_view_write(&self, write: ViewAlterWrite<'_>) -> Result<(), SQLError> {
53        self.with_implicit_definition_transaction(|engine| write(&engine.view_alter_context()))
54    }
55}
56impl ViewAlterCatalog for Engine {
57    fn view(&self, relation: &RelationIdentity) -> Option<StoredView> {
58        self.durable.views.read().get(relation).cloned()
59    }
60    fn persistence(&self, relation: &RelationIdentity) -> Option<RelationPersistence> {
61        self.durable
62            .views
63            .read()
64            .get(relation)
65            .map(|view| view.persistence)
66    }
67}
68impl ViewAlterAccess for Engine {
69    fn ensure_owner(&self, name: &str, view: &StoredView) -> Result<String, SQLError> {
70        self.ensure_view_owner(name, view)
71    }
72}
73impl ViewPublication for Engine {
74    fn has_catalog(&self) -> bool {
75        self.storage.catalog.is_some()
76    }
77    fn save_view(&self, row: &ViewRow) -> StorageBackendResult<()> {
78        self.storage.catalog.as_ref().map_or(Ok(()), |catalog| {
79            uqa_execution::catalog::definition_revision::publish_view(catalog.as_ref(), row)
80        })
81    }
82    fn save_view_representation(&self, row: &ViewRow) -> StorageBackendResult<()> {
83        self.storage
84            .catalog
85            .as_ref()
86            .map_or(Ok(()), |catalog| catalog.save_view(row))
87    }
88}
89
90impl ViewAlterPublication for Engine {
91    fn persist_rename(
92        &self,
93        from: &RelationIdentity,
94        to: &RelationIdentity,
95    ) -> StorageBackendResult<Option<bool>> {
96        self.storage
97            .catalog
98            .as_ref()
99            .map(|catalog| catalog.rename_view(from, to))
100            .transpose()
101    }
102}