Skip to main content

uqa_engine/capabilities/
mutation_assignment.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind declared columns, routine scopes and identity state for mutation execution.
8use crate::Engine;
9use uqa_execution::mutation::{
10    assignment::MutationAssignmentContext,
11    identity::{IdentitySequences, InsertIdentityCatalog, InsertIdentityContext},
12};
13use uqa_sql::{
14    assignment::columns::{AssignmentColumnCatalog, ColumnShape},
15    ast::{AutoIncrement, ColumnDef, Expr},
16    SQLError,
17};
18impl AssignmentColumnCatalog for Engine {
19    fn try_describe_table(
20        &self,
21        table: &str,
22    ) -> Result<Option<Vec<ColumnDef>>, uqa_sql::assignment::columns::ColumnCatalogError> {
23        Engine::describe_table_in_execution(self, table)
24            .map_err(|error| Box::new(error) as uqa_sql::assignment::columns::ColumnCatalogError)
25    }
26    fn columns_declared(
27        &self,
28        table: &str,
29    ) -> Result<bool, uqa_sql::assignment::columns::ColumnCatalogError> {
30        self.try_table(table)
31            .map(|table| table.is_some_and(|table| *table.columns_declared.read()))
32            .map_err(|error| Box::new(error) as uqa_sql::assignment::columns::ColumnCatalogError)
33    }
34    fn with_column_metadata_with_control(
35        &self,
36        table: &str,
37        control: &uqa_core::memory::ProductionControl<'_>,
38        read: &mut dyn FnMut(&[ColumnDef]) -> Result<(), SQLError>,
39    ) -> Result<bool, SQLError> {
40        self.with_table_column_metadata_with_control(table, control, read)
41    }
42    fn columns_declared_with_control(
43        &self,
44        table: &str,
45        control: &uqa_core::memory::ProductionControl<'_>,
46    ) -> Result<bool, SQLError> {
47        Ok(self
48            .table_state_snapshot_with_control(table, control)?
49            .is_some_and(|table| *table.columns_declared.read()))
50    }
51    fn try_column_insert_default_expr(
52        &self,
53        table: &str,
54        column: &str,
55    ) -> Result<Option<Expr>, uqa_sql::assignment::columns::ColumnCatalogError> {
56        Engine::try_column_insert_default_expr(self, table, column)
57            .map_err(|error| Box::new(error) as uqa_sql::assignment::columns::ColumnCatalogError)
58    }
59    fn try_identity_column_sequence(
60        &self,
61        table: &str,
62        column: &str,
63    ) -> Result<Option<String>, uqa_sql::SQLError> {
64        let stored = self
65            .try_table(table)
66            .map_err(|error| {
67                uqa_sql::catalog::errors::storage_error("read identity column", &error)
68            })?
69            .ok_or_else(|| uqa_sql::SQLError::UnknownTable(table.to_string()))?;
70        let columns = stored.columns.read();
71        let Some(definition) = columns.iter().find(|definition| definition.name == column) else {
72            return Ok(None);
73        };
74        uqa_sql::schema::sequences::implicit_ownership::identity_column_sequence(
75            self, table, definition,
76        )
77    }
78    fn try_column_shape(
79        &self,
80        table: &str,
81        column: &str,
82    ) -> Result<Option<Option<ColumnShape>>, uqa_sql::assignment::columns::ColumnCatalogError> {
83        let table = self
84            .try_table(table)
85            .map_err(|error| Box::new(error) as uqa_sql::assignment::columns::ColumnCatalogError)?;
86        Ok(table.map(|table| {
87            table
88                .columns
89                .read()
90                .iter()
91                .find(|definition| definition.name == column)
92                .map(|definition| ColumnShape {
93                    ty: definition.ty.clone(),
94                    generated: definition
95                        .generated
96                        .as_ref()
97                        .map(|generated| generated.kind),
98                    identity_sequence: definition
99                        .auto_increment
100                        .as_ref()
101                        .filter(|provenance| provenance.is_identity())
102                        .and_then(|provenance| provenance.sequence.clone()),
103                })
104        }))
105    }
106}
107impl InsertIdentityCatalog for Engine {
108    fn auto_increment_column(&self, table: &str) -> Result<Option<String>, String> {
109        Engine::auto_increment_column(self, table).map_err(|error| error.to_string())
110    }
111    fn auto_increment_columns(&self, table: &str) -> Result<Vec<(String, AutoIncrement)>, String> {
112        Engine::auto_increment_columns(self, table).map_err(|error| error.to_string())
113    }
114}
115impl IdentitySequences for Engine {
116    fn nextval_sql(&self, sequence: &str) -> Result<i64, SQLError> {
117        Engine::nextval_sql(self, sequence)
118    }
119}
120impl Engine {
121    pub(crate) fn mutation_assignment_context(
122        &self,
123    ) -> MutationAssignmentContext<'_, crate::session::StatementReadSnapshot> {
124        MutationAssignmentContext {
125            columns: self,
126            assignment: self,
127            rows: self.mutation_row_context(),
128            expressions: self.mutation_expression_context(),
129            scopes: self,
130        }
131    }
132    pub(crate) fn insert_identity_context(&self) -> InsertIdentityContext<'_> {
133        InsertIdentityContext {
134            catalog: self,
135            columns: self,
136            assignment: self,
137            identifiers: self,
138            sequences: self,
139            locks: self,
140            partitions: self,
141        }
142    }
143}
144
145impl Engine {
146    pub(crate) fn mutation_preparation_context(
147        &self,
148    ) -> uqa_execution::mutation::preparation::MutationPreparationContext<
149        '_,
150        crate::session::StatementReadSnapshot,
151    > {
152        uqa_execution::mutation::preparation::MutationPreparationContext {
153            referential: self.referential_execution_context(),
154            staging: self.mutation_staging_context(),
155            returning: self.returning_execution_context(),
156        }
157    }
158}