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 temporary_schema(&self) -> Option<String> {
27        self.temporary_namespace_allocated()
28            .then(|| self.temporary_schema_name())
29    }
30    fn require_schema_usage(&self, schema: &str, role: &RoleReference) -> Result<(), SQLError> {
31        self.require_schema_privilege(schema, role, SchemaAclPrivilege::Usage)
32    }
33    fn schema_has_usage(&self, schema: &str, role: &RoleReference) -> bool {
34        self.schema_has_privilege_for_role(schema, role, SchemaAclPrivilege::Usage)
35    }
36    fn routine_type_display(&self, type_name: &str) -> String {
37        // Pseudo-types such as `anyelement` have no catalog column type and keep their names.
38        uqa_execution::catalog::projection::resolve_catalog_column_type(
39            &self.catalog_execution(),
40            type_name,
41        )
42        .map_or_else(|| type_name.to_string(), |ty| ty.display_name())
43    }
44    fn routine_identity_display(&self, oid: u32) -> Result<String, SQLError> {
45        uqa_execution::catalog::projection::resolve_regtype_output(
46            &self.catalog_execution(),
47            &uqa_sql::ast::ColumnType::Regprocedure,
48            i64::from(oid),
49        )
50        .map_err(SQLError::Internal)?
51        .ok_or_else(|| SQLError::Internal(format!("routine OID {oid} has no catalog identity")))
52    }
53}
54
55use uqa_execution::routines::removal::context::{
56    RoutineDropNotices, RoutineRegistryPublication, RoutineRegistryState, RoutineRegistryWrite,
57    RoutineRemovalContext,
58};
59use uqa_sql::routines::lifecycle::RoutineRegistry;
60
61impl Engine {
62    pub(crate) fn routine_removal_context(&self) -> RoutineRemovalContext<'_> {
63        RoutineRemovalContext {
64            deletion: self,
65            names: self,
66            registry: self,
67            publication: self,
68            roles: self,
69            catalog: self.catalog_execution(),
70            bodies: self.routine_rewrite_context(),
71            notices: self,
72            changes: self,
73        }
74    }
75}
76impl RoutineRegistryState for Engine {
77    fn routine_snapshot(&self) -> RoutineRegistry {
78        self.durable.sql_user_functions.read().clone()
79    }
80    fn routines_write(&self) -> RoutineRegistryWrite<'_> {
81        Box::new(self.durable.sql_user_functions.write())
82    }
83}
84impl RoutineRegistryPublication for Engine {
85    fn persist_routine_definitions(&self, registry: &RoutineRegistry) -> Result<(), SQLError> {
86        self.persist_sql_functions_snapshot(registry)
87    }
88}
89impl RoutineDropNotices for Engine {
90    fn routine_drop_notice(&self, notice: uqa_sql::SQLNotice) {
91        self.push_sql_notice(notice);
92    }
93}