Skip to main content

uqa_engine/
domains.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Durable domain identities and catalog publication.
8
9use uqa_sql::ast::ColumnType;
10use uqa_storage::CatalogFacade;
11
12use crate::{Engine, SQLError, StorageBackendResult};
13
14pub(crate) use uqa_sql::catalog::domain::StoredDomain;
15
16impl Engine {
17    pub(crate) fn domain_default_expression(&self, ty: &ColumnType) -> Option<uqa_sql::ast::Expr> {
18        uqa_sql::catalog::domain::domain_default_expression(self, ty)
19    }
20
21    pub(crate) fn try_column_insert_default_expr(
22        &self,
23        table: &str,
24        column: &str,
25    ) -> StorageBackendResult<Option<uqa_sql::ast::Expr>> {
26        if let Some(default) = self.column_default_expr_in_execution(table, column)? {
27            return Ok(Some(default));
28        }
29        let columns = self.describe_table_in_execution(table)?.unwrap_or_default();
30        let Some(column) = columns.iter().find(|definition| definition.name == column) else {
31            return Ok(None);
32        };
33        Ok(self.domain_default_expression(&column.ty).or_else(|| {
34            matches!(column.ty, ColumnType::Domain { .. })
35                .then_some(uqa_sql::ast::Expr::Literal(uqa_core::Value::Null))
36        }))
37    }
38    pub(crate) fn domain_by_oid(&self, oid: u32) -> Option<StoredDomain> {
39        self.durable
40            .domains
41            .read()
42            .values()
43            .find(|domain| domain.oid == oid)
44            .cloned()
45    }
46
47    pub(crate) fn restore_domains_from_catalog(
48        &self,
49        catalog: &dyn CatalogFacade,
50        allow_migration: bool,
51    ) -> StorageBackendResult<uqa_execution::catalog::domain::DomainRestoreState> {
52        let restored = uqa_execution::catalog::domain::restore(
53            catalog,
54            &self.durable.roles.read(),
55            allow_migration,
56        )?;
57        *self.durable.domains.write() = restored.registry;
58        Ok(restored.state)
59    }
60
61    pub(crate) fn finish_domain_restoration(
62        &self,
63        catalog: &dyn CatalogFacade,
64        state: uqa_execution::catalog::domain::DomainRestoreState,
65    ) -> StorageBackendResult<()> {
66        if let Some(registry) = uqa_execution::catalog::domain::finish_restore(
67            catalog,
68            &self.restored_catalog_read_view(),
69            &self.session_execution_view().relation_name_resolution(),
70            state,
71        )? {
72            *self.durable.domains.write() = registry;
73        }
74        Ok(())
75    }
76}
77
78impl uqa_sql::catalog::domain::DomainCatalog for Engine {
79    fn domain_by_oid_with_control(
80        &self,
81        oid: u32,
82        control: &uqa_core::memory::ProductionControl<'_>,
83    ) -> Result<Option<uqa_core::memory::Produced<StoredDomain>>, SQLError> {
84        for domain in self.durable.domains.read().values() {
85            control.check()?;
86            if domain.oid == oid {
87                return domain.clone_with_control(control).map(Some);
88            }
89        }
90        control.check()?;
91        Ok(None)
92    }
93
94    fn domain_by_oid(&self, oid: u32) -> Option<StoredDomain> {
95        Engine::domain_by_oid(self, oid)
96    }
97}