Skip to main content

uqa_engine/session/
schemas.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Schema/catalog enumeration and schema lifecycle.
8
9use super::{CatalogIndexRow, Engine, StorageBackendResult};
10
11pub(crate) use uqa_sql::catalog::is_virtual_system_schema;
12
13impl Engine {
14    pub fn list_catalog_indexes(&self) -> StorageBackendResult<Vec<CatalogIndexRow>> {
15        self.with_catalog_read_snapshot(Self::catalog_indexes_in_execution)
16    }
17
18    pub(crate) fn catalog_indexes_in_execution(
19        &self,
20    ) -> StorageBackendResult<Vec<CatalogIndexRow>> {
21        let mut out = self
22            .catalog_index_snapshot_in_execution()?
23            .iter()
24            .cloned()
25            .collect::<Vec<_>>();
26        out.sort_by(|a, b| a.relation.cmp(&b.relation));
27        Ok(out)
28    }
29
30    pub(crate) fn catalog_index_snapshot_in_execution(
31        &self,
32    ) -> StorageBackendResult<uqa_core::catalog_index::CatalogIndexSnapshot> {
33        if let Some(snapshot) = self.query_catalog_snapshot.as_ref() {
34            return Ok(snapshot.catalog_indexes.clone().into());
35        }
36        self.synchronize_catalog_registries()?;
37        Ok(self.durable.catalog_indexes.snapshot().into())
38    }
39
40    /// Create a durable schema catalog object. Returns `true` when a new
41    /// schema was created and `false` only for `IF NOT EXISTS`.
42    pub fn register_schema(&self, name: &str, if_not_exists: bool) -> StorageBackendResult<bool> {
43        self.with_implicit_storage_transaction(|engine| {
44            uqa_execution::schema::namespaces::register_api_schema(
45                &engine.schema_creation_context(),
46                name,
47                if_not_exists,
48            )
49            .map_err(|error| uqa_storage::StorageBackendError::backend("CREATE SCHEMA", error))
50        })
51    }
52
53    /// Drop an empty durable schema. Virtual system namespaces cannot be removed.
54    pub fn drop_schema(&self, name: &str) -> StorageBackendResult<bool> {
55        self.with_implicit_storage_transaction(|engine| {
56            uqa_execution::schema::namespaces::removal::drop_empty_schema(
57                &engine.empty_schema_removal_context(),
58                name,
59            )
60        })
61    }
62
63    pub fn has_schema(&self, name: &str) -> StorageBackendResult<bool> {
64        self.with_catalog_read_snapshot(|engine| {
65            engine.synchronize_catalog_registries()?;
66            Ok(engine.durable.schemas.read().contains_key(name))
67        })
68    }
69
70    /// Whether `name` resolves as a namespace: a durable schema, a virtual
71    /// system schema (`pg_catalog`, `information_schema`, `ag_catalog`), or
72    /// the namespace a named graph owns.
73    pub fn has_namespace(&self, name: &str) -> StorageBackendResult<bool> {
74        self.with_catalog_read_snapshot(|engine| engine.has_namespace_in_execution(name))
75    }
76
77    pub(crate) fn has_namespace_in_execution(&self, name: &str) -> StorageBackendResult<bool> {
78        self.synchronize_catalog_registries()?;
79        Ok(is_virtual_system_schema(name)
80            || self.durable.schemas.read().contains_key(name)
81            || self.durable.graphs.read().contains_key(name))
82    }
83
84    pub(crate) fn validate_stored_schema_name(name: &str) -> StorageBackendResult<()> {
85        crate::capabilities::validate_stored_schema_name(name)
86    }
87
88    pub(crate) fn schema_is_empty(&self, schema: &str) -> bool {
89        !self
90            .storage
91            .tables
92            .read()
93            .keys()
94            .any(|relation| relation.schema == schema)
95            && !self
96                .durable
97                .views
98                .read()
99                .keys()
100                .any(|relation| relation.schema == schema)
101            && !self
102                .durable
103                .sequences
104                .read()
105                .keys()
106                .any(|relation| relation.schema == schema)
107            && !self
108                .durable
109                .foreign_tables
110                .read()
111                .keys()
112                .any(|relation| relation.schema == schema)
113            && !self
114                .durable
115                .catalog_indexes
116                .read()
117                .keys()
118                .any(|relation| relation.schema == schema)
119            && !self
120                .durable
121                .domains
122                .read()
123                .values()
124                .any(|domain| domain.identity.schema == schema)
125            && !self
126                .durable
127                .enums
128                .read()
129                .values()
130                .any(|definition| definition.identity.schema == schema)
131            && !self
132                .durable
133                .composites
134                .read()
135                .values()
136                .any(|definition| definition.identity.schema == schema)
137            && !self.durable.sql_user_functions.read().keys().any(|name| {
138                uqa_sql::schema::namespaces::removal::routine_name_occupies_schema(name, schema)
139            })
140    }
141
142    /// Return every registered schema in sorted order.
143    pub fn list_schemas(&self) -> StorageBackendResult<Vec<String>> {
144        self.with_catalog_read_snapshot(|engine| {
145            if let Some(snapshot) = engine.query_catalog_snapshot.as_ref() {
146                return Ok(snapshot.schemas.keys().cloned().collect());
147            }
148            engine.synchronize_catalog_registries()?;
149            Ok(engine.durable.schemas.read().keys().cloned().collect())
150        })
151    }
152
153    /// Local names of tables whose structural relation identity is owned by
154    /// `schema`. No string-prefix inference participates in this lookup.
155    pub fn tables_in_schema(&self, schema: &str) -> StorageBackendResult<Vec<String>> {
156        self.with_catalog_read_snapshot(|engine| engine.schema_tables_in_execution(schema))
157    }
158
159    pub(crate) fn schema_tables_in_execution(
160        &self,
161        schema: &str,
162    ) -> StorageBackendResult<Vec<String>> {
163        self.synchronize_table_catalog()?;
164        let mut out: Vec<String> = Vec::new();
165        for relation in self.storage.tables.read().keys() {
166            if relation.schema == schema {
167                out.push(relation.name.clone());
168            }
169        }
170        out.sort_unstable();
171        Ok(out)
172    }
173
174    pub fn list_sequences(&self) -> StorageBackendResult<Vec<String>> {
175        self.with_catalog_read_snapshot(|engine| Ok(engine.query_sequence_snapshot()?.names()))
176    }
177}