uqa_sql/routines/
lifecycle.rs1pub 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 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 value_binding: None,
74 resolution_error: None,
75 input_collation: None,
76 }
77 }
78}
79
80pub fn routine_signature_label(name: &str, types: &[String]) -> String {
81 let display_types = types
82 .iter()
83 .map(|type_name| {
84 crate::ast::ColumnType::from_sql_name(type_name)
85 .map_or_else(|_| type_name.clone(), |column_type| column_type.sql_name())
86 })
87 .collect::<Vec<_>>();
88 format!("{name}({})", display_types.join(", "))
89}
90
91pub fn routine_name_display(name: &str) -> String {
93 crate::compiler::parse_regobject_name(name)
94 .map_or_else(|| name.to_owned(), |parts| parts.join("."))
95}
96
97pub fn routine_signature_display(
99 catalog: &dyn names::RoutineNameCatalog,
100 name: &str,
101 types: &[String],
102) -> String {
103 let name = routine_name_display(name);
104 let types = types
105 .iter()
106 .map(|type_name| catalog.routine_type_display(type_name))
107 .collect::<Vec<_>>();
108 format!("{name}({})", types.join(", "))
109}
110
111pub fn wrong_routine_kind_error(signature: &str, expected_kind: &str) -> SQLError {
113 SQLError::Routine {
114 sqlstate: "42809".into(),
115 message: format!("{signature} is not a {expected_kind}"),
116 }
117}
118
119pub fn ambiguous_routine_error(kind: &str, name: &str) -> SQLError {
121 let name = routine_name_display(name);
122 SQLError::Diagnostic {
123 sqlstate: "42725".into(),
124 message: format!("{kind} name \"{name}\" is not unique"),
125 detail: None,
126 hint: Some(format!(
127 "Specify the argument list to select the {kind} unambiguously."
128 )),
129 }
130}
131
132pub fn alter_routine_kind_name(kind: AlterRoutineKind) -> &'static str {
133 match kind {
134 AlterRoutineKind::Function => "function",
135 AlterRoutineKind::Procedure => "procedure",
136 AlterRoutineKind::Routine => "routine",
137 }
138}
139
140pub fn alter_routine_kind_matches(kind: AlterRoutineKind, def: &CreateFunction) -> bool {
141 match kind {
142 AlterRoutineKind::Function => !def.is_procedure,
143 AlterRoutineKind::Procedure => def.is_procedure,
144 AlterRoutineKind::Routine => true,
145 }
146}
147
148pub fn require_routine_ownership(
150 kind: &str,
151 name: &str,
152 current_user_has_owner_privileges: bool,
153) -> Result<(), SQLError> {
154 if current_user_has_owner_privileges {
155 Ok(())
156 } else {
157 Err(SQLError::Routine {
158 sqlstate: "42501".into(),
159 message: format!("must be owner of {kind} {name}"),
160 })
161 }
162}