Skip to main content

uqa_engine/capabilities/
routine_removal.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Bind routine namespace lookup to live session and schema authorization state.
8
9use crate::{schema_security::SchemaAclPrivilege, Engine};
10use uqa_sql::catalog::roles::RoleReference;
11use uqa_sql::{
12    catalog::security::BoundSchemaSecurity, routines::lifecycle::names::RoutineNameCatalog,
13    SQLError,
14};
15
16impl RoutineNameCatalog for Engine {
17    fn schema_security(&self, schema: &str) -> Option<BoundSchemaSecurity> {
18        self.schema_security_for_privilege(schema)
19    }
20    fn current_role(&self) -> RoleReference {
21        Engine::current_role(self)
22    }
23    fn search_path(&self) -> Vec<String> {
24        crate::session::effective_search_path(&self.session.state.read())
25    }
26    fn search_path_with_control(
27        &self,
28        control: &uqa_core::memory::ProductionControl<'_>,
29    ) -> Result<uqa_core::memory::Produced<Vec<String>>, SQLError> {
30        control.check()?;
31        let state = self.session.state.read();
32        let role = state.authorization.current();
33        let mut path = uqa_core::memory::ProductionVec::new(*control);
34        path.reserve(state.search_path.len())?;
35        for entry in &state.search_path {
36            path.push_produced(control.copy_text(if entry == "$user" {
37                &role.name
38            } else {
39                entry
40            })?)?;
41        }
42        Ok(path.finish()?)
43    }
44    fn temporary_schema(&self) -> Option<String> {
45        self.temporary_namespace_allocated()
46            .then(|| self.temporary_schema_name())
47    }
48    fn temporary_schema_with_control(
49        &self,
50        control: &uqa_core::memory::ProductionControl<'_>,
51    ) -> Result<Option<uqa_core::memory::Produced<String>>, SQLError> {
52        control.check()?;
53        self.temporary_namespace_allocated()
54            .then(|| {
55                control
56                    .format(format_args!("pg_temp_{}", self.session_id))
57                    .map_err(Into::into)
58            })
59            .transpose()
60    }
61    fn schema_exists_with_control(
62        &self,
63        schema: &str,
64        control: &uqa_core::memory::ProductionControl<'_>,
65    ) -> Result<bool, SQLError> {
66        let (catalog, temporary) = self.routine_namespace_state(control)?;
67        Ok(uqa_execution::catalog::namespaces::namespace_schema_exists(
68            &catalog,
69            temporary.as_ref().map_or("", |name| name.as_str()),
70            schema,
71        ))
72    }
73    fn require_schema_usage(&self, schema: &str, role: &RoleReference) -> Result<(), SQLError> {
74        self.require_schema_privilege(schema, role, SchemaAclPrivilege::Usage)
75    }
76    fn schema_has_usage(&self, schema: &str, role: &RoleReference) -> bool {
77        self.schema_has_privilege_for_role(schema, role, SchemaAclPrivilege::Usage)
78    }
79    fn schema_has_usage_with_control(
80        &self,
81        schema: &str,
82        role: &RoleReference,
83        control: &uqa_core::memory::ProductionControl<'_>,
84    ) -> Result<bool, SQLError> {
85        let (catalog, temporary) = self.routine_namespace_state(control)?;
86        uqa_execution::catalog::namespaces::schema_has_privilege_with_control(
87            &catalog,
88            temporary.as_ref().map_or("", |name| name.as_str()),
89            schema,
90            role,
91            SchemaAclPrivilege::Usage,
92            control,
93        )
94    }
95    fn require_schema_usage_with_control(
96        &self,
97        schema: &str,
98        role: &RoleReference,
99        control: &uqa_core::memory::ProductionControl<'_>,
100    ) -> Result<(), SQLError> {
101        uqa_sql::routines::lifecycle::names::require_routine_schema_usage_with_control(
102            self, schema, role, control,
103        )
104    }
105
106    fn routine_type_display(&self, type_name: &str) -> String {
107        // Pseudo-types such as `anyelement` have no catalog column type and keep their names.
108        uqa_execution::catalog::projection::resolve_catalog_column_type(
109            &self.catalog_execution(),
110            type_name,
111        )
112        .map_or_else(|| type_name.to_string(), |ty| ty.display_name())
113    }
114    fn routine_identity_display(&self, oid: u32) -> Result<String, SQLError> {
115        uqa_execution::catalog::projection::resolve_regtype_output(
116            &self.catalog_execution(),
117            &uqa_sql::ast::ColumnType::Regprocedure,
118            i64::from(oid),
119        )
120        .map_err(SQLError::Internal)?
121        .ok_or_else(|| SQLError::Internal(format!("routine OID {oid} has no catalog identity")))
122    }
123}
124
125use uqa_execution::routines::removal::context::{
126    RoutineDropNotices, RoutineRegistryPublication, RoutineRegistryState, RoutineRegistryWrite,
127    RoutineRemovalContext,
128};
129use uqa_sql::routines::lifecycle::RoutineRegistry;
130
131impl Engine {
132    fn routine_namespace_state(
133        &self,
134        control: &uqa_core::memory::ProductionControl<'_>,
135    ) -> Result<
136        (
137            uqa_execution::catalog::CatalogReadView,
138            Option<uqa_core::memory::Produced<String>>,
139        ),
140        SQLError,
141    > {
142        control.check()?;
143        Ok((
144            self.catalog_execution().catalog.catalog_snapshot(),
145            <Self as RoutineNameCatalog>::temporary_schema_with_control(self, control)?,
146        ))
147    }
148    pub(crate) fn routine_removal_context(&self) -> RoutineRemovalContext<'_> {
149        RoutineRemovalContext {
150            deletion: self,
151            names: self,
152            registry: self,
153            publication: self,
154            roles: self,
155            catalog: self.catalog_execution(),
156            bodies: self.routine_rewrite_context(),
157            notices: self,
158            changes: self,
159        }
160    }
161}
162impl RoutineRegistryState for Engine {
163    fn routine_snapshot(&self) -> RoutineRegistry {
164        self.durable.sql_user_functions.read().clone()
165    }
166    fn routines_write(&self) -> RoutineRegistryWrite<'_> {
167        Box::new(self.durable.sql_user_functions.write())
168    }
169}
170impl RoutineRegistryPublication for Engine {
171    fn persist_routine_definitions(&self, registry: &RoutineRegistry) -> Result<(), SQLError> {
172        self.persist_sql_functions_snapshot(registry)
173    }
174}
175impl RoutineDropNotices for Engine {
176    fn routine_drop_notice(&self, notice: uqa_sql::SQLNotice) {
177        self.push_sql_notice(notice);
178    }
179}