uqa_sql/semantics/
mutation_rows.rs1use super::{DOC_ID_COLUMN, TABLE_OID_COLUMN, XMIN_COLUMN};
9use crate::{ast::ColumnDef, ColumnType, RowSchema, SQLError};
10
11pub trait MutationRowCatalog: Sync {
13 fn column_definitions(&self, table: &str) -> Result<Option<Vec<ColumnDef>>, String>;
14 fn column_names(&self, table: &str) -> Result<Vec<String>, String>;
15 fn visit_column_names_with_control(
17 &self,
18 table: &str,
19 control: &uqa_core::memory::ProductionControl<'_>,
20 read: &mut dyn FnMut(&str) -> Result<(), SQLError>,
21 ) -> Result<(), SQLError> {
22 control.check()?;
23 let names = self.column_names(table).map_err(SQLError::Internal)?;
24 for name in &names {
25 control.check()?;
26 read(name)?;
27 }
28 Ok(())
29 }
30 fn view_schema(&self, name: &str) -> Result<RowSchema, SQLError>;
31}
32
33pub fn null_target_schema(
34 relations: &dyn MutationRowCatalog,
35 table: &str,
36 qualifier: &str,
37) -> Result<RowSchema, SQLError> {
38 let definitions = relations
39 .column_definitions(table)
40 .map_err(|error| dml_storage_error("DML row schema lookup", error))?
41 .ok_or_else(|| SQLError::UnknownTable(table.to_string()))?;
42 let mut columns = if definitions.is_empty() {
43 relations
44 .column_names(table)
45 .map_err(|error| dml_storage_error("DML row schema lookup", error))?
46 } else {
47 definitions
48 .iter()
49 .map(|definition| definition.name.clone())
50 .collect::<Vec<_>>()
51 };
52 let mut types = columns
53 .iter()
54 .map(|column| {
55 definitions
56 .iter()
57 .find(|definition| definition.name == *column)
58 .map(|definition| definition.ty.clone())
59 })
60 .collect::<Vec<_>>();
61 if !columns.iter().any(|column| column == DOC_ID_COLUMN) {
62 columns.push(DOC_ID_COLUMN.into());
63 types.push(Some(ColumnType::BigInteger));
64 }
65 columns.push(TABLE_OID_COLUMN.into());
66 types.push(Some(ColumnType::Oid));
67 columns.push(XMIN_COLUMN.into());
68 types.push(Some(ColumnType::Xid));
69 Ok(RowSchema::with_qualified_types(qualifier, columns, types))
70}
71
72fn dml_storage_error(action: &str, error: impl std::fmt::Display) -> SQLError {
73 SQLError::Internal(format!("{action} failed in storage backend: {error}"))
74}