Skip to main content

uqa_engine/capabilities/
table_removal.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Lend actual table generations and guard lifetimes to native DROP execution.
8use crate::{Engine, TableState};
9use std::{collections::BTreeMap, sync::Arc};
10use uqa_core::RelationIdentity;
11use uqa_execution::schema::table_removal::context::{
12    TableRemovalCatalog, TableRemovalContext, TableRemovalPublication,
13};
14use uqa_sql::{
15    schema::removal::hierarchy::{
16        HierarchyDropCatalog, HierarchyDropEntries, HierarchyDropRead, HierarchyDropTable,
17        HierarchyDropTables,
18    },
19    SQLError,
20};
21use uqa_storage::StorageBackendResult;
22struct HierarchyTables<'a>(
23    parking_lot::RwLockReadGuard<'a, BTreeMap<RelationIdentity, Arc<TableState>>>,
24);
25impl HierarchyDropCatalog for Engine {
26    fn tables(&self) -> Box<dyn HierarchyDropTables + '_> {
27        Box::new(HierarchyTables(self.storage.tables.read()))
28    }
29}
30impl HierarchyDropTables for HierarchyTables<'_> {
31    fn iter(&self) -> HierarchyDropEntries<'_> {
32        Box::new(
33            self.0
34                .iter()
35                .map(|(identity, state)| (identity, state.as_ref() as &dyn HierarchyDropTable)),
36        )
37    }
38}
39impl HierarchyDropTable for TableState {
40    fn hierarchy(&self) -> HierarchyDropRead<'_> {
41        Box::new(self.hierarchy.read())
42    }
43}
44impl TableRemovalCatalog for Engine {
45    fn contains_relation(&self, relation: &RelationIdentity) -> bool {
46        self.storage.tables.read().contains_key(relation)
47    }
48}
49impl TableRemovalPublication for Engine {
50    fn prune_constraint_modes(&self) -> Result<(), SQLError> {
51        Engine::prune_constraint_modes(self)
52    }
53    fn remove_state(&self, name: &str, relation: &RelationIdentity) -> StorageBackendResult<()> {
54        let table = self.storage.tables.read().get(relation).cloned();
55        let temporary = table
56            .as_ref()
57            .is_some_and(|table| table.persistence == uqa_sql::ast::RelationPersistence::Temporary);
58        if !temporary {
59            if let Some(catalog) = self.storage.catalog.as_ref() {
60                catalog.drop_table_and_data(name)?;
61                if let Some(table) = &table {
62                    uqa_execution::catalog::tuples::publication::queue_relation(
63                        catalog.as_ref(),
64                        relation,
65                        table.object_id(),
66                        false,
67                    )?;
68                    uqa_execution::catalog::definition_revision::remove_table(
69                        catalog.as_ref(),
70                        table.object_id(),
71                    )?;
72                }
73                self.note_table_catalog_changed();
74            }
75        }
76        let removed = self.storage.tables.write().remove(relation);
77        if let Some(table) = removed {
78            self.note_prepared_table_change(&table);
79        }
80        self.forget_constraint_transaction_relation(relation);
81        self.clear_regtype_output_cache();
82        if temporary {
83            self.note_table_catalog_changed();
84        }
85        // Sweep every related per-table registry so catalog state
86        // does not outlive the table.
87        self.durable
88            .table_field_analyzers
89            .write()
90            .retain(|(t, _), _| t != name);
91        self.durable
92            .catalog_indexes
93            .write()
94            .retain(|_, row| row.table_name != name);
95        Ok(())
96    }
97}
98impl Engine {
99    pub(crate) fn table_removal_context(&self) -> TableRemovalContext<'_> {
100        TableRemovalContext {
101            indexes: self.index_registry_context(),
102            catalog: self,
103            hierarchy: self,
104            publication: self,
105            routines: self.routine_removal_context(),
106            events: self.event_lifecycle_context(),
107            views: self.view_removal_context(),
108            sequences: self.sequence_removal_context(),
109        }
110    }
111}