Skip to main content

uqa_sql/routines/
lifecycle.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Routine removal identities, dependency results, and declaration diagnostics.
8
9pub mod binding;
10pub mod dependencies;
11pub mod lookup;
12pub mod names;
13pub mod relations;
14pub mod rename;
15pub mod restoration;
16pub mod rewrites;
17
18use super::SQLUserFunction;
19use crate::{
20    ast::{AlterRoutineKind, CreateFunction, FunctionBinding},
21    SQLError,
22};
23use std::{
24    collections::{BTreeMap, BTreeSet},
25    sync::Arc,
26};
27
28pub type RoutineRegistry = BTreeMap<String, Vec<Arc<SQLUserFunction>>>;
29
30#[derive(Default)]
31pub struct RoutineDropResolution {
32    pub targets: Vec<RoutineDropTarget>,
33    pub seen_targets: BTreeSet<RoutineDropTarget>,
34    pub notices: Vec<crate::SQLNotice>,
35}
36
37#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
38pub struct RoutineDropTarget {
39    pub object_id: Option<[u8; 16]>,
40    pub name: String,
41    pub argument_types: Vec<String>,
42    pub is_procedure: bool,
43}
44
45impl RoutineDropTarget {
46    pub fn kind(&self) -> &'static str {
47        if self.is_procedure {
48            "procedure"
49        } else {
50            "function"
51        }
52    }
53
54    pub fn label(&self) -> String {
55        routine_signature_label(&self.name, &self.argument_types)
56    }
57
58    /// Whether `function`, an overload registered under the target's name, is the routine the target resolved: the same kind and signature, and the same identity when the target has one.
59    pub fn names(&self, function: &SQLUserFunction) -> bool {
60        function.def.is_procedure == self.is_procedure
61            && super::routine_signature_types(&function.def) == self.argument_types
62            && (self.object_id.is_none() || function.def.object_id == self.object_id)
63    }
64
65    pub fn binding(&self) -> FunctionBinding {
66        FunctionBinding {
67            object_id: self.object_id,
68            name: self.name.clone(),
69            argument_types: self.argument_types.clone(),
70            builtin: false,
71            dispatch: None,
72            invocation: None,
73            resolution_error: None,
74        }
75    }
76}
77
78pub fn routine_signature_label(name: &str, types: &[String]) -> String {
79    let display_types = types
80        .iter()
81        .map(|type_name| {
82            crate::ast::ColumnType::from_sql_name(type_name)
83                .map_or_else(|_| type_name.clone(), |column_type| column_type.sql_name())
84        })
85        .collect::<Vec<_>>();
86    format!("{name}({})", display_types.join(", "))
87}
88
89/// `func_signature_string`: the routine name as the command wrote it and each argument type as `format_type_be` spells it.
90pub fn routine_signature_display(
91    catalog: &dyn names::RoutineNameCatalog,
92    name: &str,
93    types: &[String],
94) -> String {
95    let types = types
96        .iter()
97        .map(|type_name| catalog.routine_type_display(type_name))
98        .collect::<Vec<_>>();
99    format!("{name}({})", types.join(", "))
100}
101
102/// `LookupFuncWithArgs`: the routine an argument list selects is not of the kind the command names.
103pub fn wrong_routine_kind_error(signature: &str, expected_kind: &str) -> SQLError {
104    SQLError::Routine {
105        sqlstate: "42809".into(),
106        message: format!("{signature} is not a {expected_kind}"),
107    }
108}
109
110/// `LookupFuncWithArgs`: a name without an argument list selects more than one routine of the command's kind.
111pub fn ambiguous_routine_error(kind: &str, name: &str) -> SQLError {
112    SQLError::Diagnostic {
113        sqlstate: "42725".into(),
114        message: format!("{kind} name \"{name}\" is not unique"),
115        detail: None,
116        hint: Some(format!(
117            "Specify the argument list to select the {kind} unambiguously."
118        )),
119    }
120}
121
122pub fn alter_routine_kind_name(kind: AlterRoutineKind) -> &'static str {
123    match kind {
124        AlterRoutineKind::Function => "function",
125        AlterRoutineKind::Procedure => "procedure",
126        AlterRoutineKind::Routine => "routine",
127    }
128}
129
130pub fn alter_routine_kind_matches(kind: AlterRoutineKind, def: &CreateFunction) -> bool {
131    match kind {
132        AlterRoutineKind::Function => !def.is_procedure,
133        AlterRoutineKind::Procedure => def.is_procedure,
134        AlterRoutineKind::Routine => true,
135    }
136}
137
138/// `must be owner of <kind> <name>`, which `aclcheck_error` reports for a routine whose owner the current user is not; each command names the routine its own way.
139pub fn require_routine_ownership(
140    kind: &str,
141    name: &str,
142    current_user_has_owner_privileges: bool,
143) -> Result<(), SQLError> {
144    if current_user_has_owner_privileges {
145        Ok(())
146    } else {
147        Err(SQLError::Routine {
148            sqlstate: "42501".into(),
149            message: format!("must be owner of {kind} {name}"),
150        })
151    }
152}