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