Skip to main content

uqa_engine/capabilities/
constraint_changes.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind constraint execution to transaction modes, relation permissions, and catalog state.
8use crate::Engine;
9use uqa_execution::schema::columns::rows::RewriteDeferral;
10use uqa_execution::schema::constraints::{
11    deferred::DeferredForeignKeys, ConstraintAlterAccess, ConstraintAlterContext, ConstraintModes,
12    ConstraintRelations,
13};
14use uqa_sql::{
15    ast::{ForeignKey, TableHierarchy},
16    catalog::constraints::ConstraintIdentity,
17    SQLError,
18};
19use uqa_storage::StorageBackendResult;
20impl Engine {
21    /// Give partitions the copies of their partitioned ancestors' foreign keys that earlier releases left them without, when a writer opens the database.
22    pub(crate) fn repair_partition_foreign_keys_on_open(&self) -> StorageBackendResult<()> {
23        let needed =
24            uqa_execution::schema::constraints::partition_foreign_keys::partition_foreign_keys_need_repair(
25                &self.constraint_alter_context(),
26            )
27            .map_err(|error| uqa_storage::StorageBackendError::backend("partition foreign key repair", error))?;
28        if !needed {
29            return Ok(());
30        }
31        self.with_implicit_storage_transaction(|engine| {
32            uqa_execution::schema::constraints::partition_foreign_keys::repair_partition_foreign_keys(
33                &engine.constraint_alter_context(),
34            )
35            .map_err(|error| uqa_storage::StorageBackendError::backend("partition foreign key repair", error))
36        })
37    }
38
39    pub(crate) fn constraint_alter_context(&self) -> ConstraintAlterContext<'_> {
40        let runtime = self.query_runtime_view();
41        ConstraintAlterContext {
42            deferred_rows: RewriteDeferral::default(),
43            pending_foreign_keys: DeferredForeignKeys::default(),
44            deletion: self,
45            catalog: self,
46            relations: self,
47            access: self,
48            locks: self,
49            lock_catalog: self,
50            lock_session: self,
51            modes: self,
52            names: self,
53            rows: self.constraint_execution_context(),
54            foreign_keys: self.foreign_key_definition_context(),
55            publication: self.schema_publication_context(),
56            writes: self,
57            notices: runtime.notices,
58        }
59    }
60    pub(crate) fn constraint_type_context(
61        &self,
62    ) -> uqa_sql::schema::constraint_changes::ConstraintTypeContext<'_> {
63        uqa_sql::schema::constraint_changes::ConstraintTypeContext {
64            foreign_keys: self.foreign_key_definition_context(),
65            referrers: self,
66        }
67    }
68}
69impl uqa_sql::schema::constraint_changes::ConstraintTypeReferrers for Engine {
70    fn try_referrers_to(
71        &self,
72        table: &str,
73    ) -> Result<Vec<(String, ForeignKey)>, uqa_sql::assignment::columns::ColumnCatalogError> {
74        Engine::referrers_in_execution(self, table).map_err(|error| Box::new(error) as _)
75    }
76}
77impl ConstraintRelations for Engine {
78    fn table_names(&self) -> StorageBackendResult<Vec<String>> {
79        Engine::table_names_in_execution(self)
80    }
81    fn table_hierarchy(&self, table: &str) -> StorageBackendResult<TableHierarchy> {
82        self.try_table_hierarchy(table)
83    }
84}
85impl ConstraintModes for Engine {
86    fn prune(&self) -> Result<(), SQLError> {
87        self.prune_constraint_modes()
88    }
89    fn forget(&self, identity: &ConstraintIdentity) {
90        self.forget_named_constraint_mode(identity);
91    }
92}
93impl ConstraintAlterAccess for Engine {
94    fn ensure_table_owner(&self, table: &str) -> Result<(), SQLError> {
95        Engine::ensure_table_owner(self, table).map(|_| ())
96    }
97    fn ensure_no_pending_events(&self, table: &str, action: &str) -> Result<(), SQLError> {
98        self.ensure_no_pending_trigger_events(table, action)
99    }
100    fn constraint_trigger_name(&self, table: &str, name: &str) -> Result<Option<String>, SQLError> {
101        self.event_lookup_context()
102            .constraint_trigger_by_constraint_name(table, name)
103            .map(|trigger| trigger.map(|trigger| trigger.definition.name))
104    }
105}