1use crate::Engine;
10use uqa_sql::{
11 ast::{ColumnDef, ColumnType},
12 binding::{snapshot::BindingSnapshot, stored_relations::StoredQueryNamespace},
13 plpgsql::PlpgsqlCatalog,
14 routines::{
15 compilation::{RoutineCompilationCatalog, RoutineCompilationContext, RoutineParserCatalog},
16 declaration::RoutineTypeCatalog,
17 merge_columns::StoredMergeColumnCatalog,
18 regclass::RoutineRegclassCatalog,
19 },
20 SQLError,
21};
22
23impl Engine {
24 pub(crate) fn routine_compilation_context(&self) -> RoutineCompilationContext<'_> {
25 RoutineCompilationContext {
26 types: self,
27 parsers: self,
28 catalog: self,
29 routines: self,
30 relations: self,
31 sequences: self,
32 merge: self,
33 regroles: self,
34 }
35 }
36}
37impl RoutineTypeCatalog for Engine {
38 fn shell_type_oid(&self, name: &str) -> Result<Option<u32>, SQLError> {
39 uqa_execution::catalog::projection::shell_type_oid(&self.catalog_execution(), name)
40 }
41 fn require_type_oid_usage(&self, oid: u32) -> Result<(), SQLError> {
42 uqa_execution::catalog::security::type_inquiry::require_type_oid_usage(
43 &self.catalog_execution(),
44 oid,
45 )
46 }
47 fn try_describe_table(&self, name: &str) -> Result<Option<Vec<ColumnDef>>, String> {
48 Engine::describe_table_in_execution(self, name).map_err(|error| error.to_string())
49 }
50 fn resolve_catalog_column_type(&self, name: &str) -> Option<ColumnType> {
51 uqa_execution::catalog::projection::resolve_catalog_column_type(
52 &self.catalog_execution(),
53 name,
54 )
55 }
56 fn resolve_catalog_column_type_name(&self, name: &str) -> Result<ColumnType, SQLError> {
57 uqa_execution::catalog::projection::resolve_catalog_column_type_name(
58 &self.catalog_execution(),
59 name,
60 )
61 }
62 fn resolve_catalog_user_type_by_oid(&self, oid: u32) -> Option<ColumnType> {
63 uqa_execution::catalog::projection::resolve_catalog_user_type_by_oid(
64 &self.catalog_execution(),
65 oid,
66 )
67 }
68 fn format_type(&self, ty: &ColumnType) -> Result<String, SQLError> {
69 uqa_execution::catalog::projection::format_type_name(&self.catalog_execution(), ty)
70 }
71 fn format_type_oid(&self, oid: u32) -> Result<String, SQLError> {
72 uqa_execution::catalog::projection::format_type_object(
73 &self.catalog_execution(),
74 i64::from(oid),
75 )
76 .map_err(SQLError::Internal)?
77 .ok_or_else(|| SQLError::Internal(format!("cache lookup failed for type {oid}")))
78 }
79 fn composite_types(&self) -> Option<&dyn uqa_sql::expr::composites::CompositeTypeCatalog> {
80 Some(self)
81 }
82 fn require_type_usage(&self, ty: &ColumnType) -> Result<(), SQLError> {
83 uqa_execution::catalog::security::type_inquiry::require_type_usage(
84 &self.catalog_execution(),
85 ty,
86 )
87 }
88}
89impl uqa_execution::routines::invocation::bodies::RoutineBodySession for Engine {
90 fn retain_routine_body(
91 &self,
92 function: &uqa_sql::routines::SQLUserFunction,
93 body: uqa_sql::routines::CompiledFunctionBody,
94 ) -> Result<(), SQLError> {
95 self.session.routine_bodies.retain(function, body)
96 }
97}
98impl StoredMergeColumnCatalog for Engine {
99 fn stored_merge_target_definitions(&self, table: &str) -> Option<Vec<ColumnDef>> {
100 self.table_entries()
101 .into_iter()
102 .find(|(name, _)| name == table)
103 .map(|(_, table)| table.columns.read().clone())
104 }
105}
106impl RoutineParserCatalog for Engine {
107 fn parser_settings(&self) -> uqa_sql::parser::ParserSettings {
108 Engine::parser_settings(self)
109 }
110
111 fn parser_notice(&self, notice: uqa_sql::SQLNotice) {
112 self.runtime.notices.push(notice);
113 }
114
115 fn plpgsql_catalog(&self) -> Result<PlpgsqlCatalog, SQLError> {
116 uqa_execution::catalog::projection::plpgsql_catalog(&self.catalog_execution())
117 }
118}
119impl RoutineCompilationCatalog for Engine {
120 fn has_registered_aggregate_function(&self, name: &str) -> bool {
121 Engine::has_registered_aggregate_function(self, name)
122 }
123 fn binding_snapshot(&self) -> Result<BindingSnapshot, SQLError> {
124 let scope = super::query_scope::new_for_catalog_binding(self);
125 uqa_execution::query::binding::binding_context(&scope).map(Into::into)
126 }
127 fn stored_query_namespace(&self) -> StoredQueryNamespace {
128 StoredQueryNamespace {
129 temporary_schema: self.temporary_schema_name(),
130 transition_relations:
131 uqa_execution::mutation::triggers::current_transition_relation_names(),
132 }
133 }
134}
135impl RoutineRegclassCatalog for Engine {
136 fn resolve_routine_regclass(&self, reference: &str) -> Result<Option<i64>, SQLError> {
137 uqa_execution::catalog::projection::resolve_regclass_oid(
138 &self.catalog_execution(),
139 reference,
140 )
141 }
142}
143
144use uqa_core::RelationIdentity;
145use uqa_execution::routines::{
146 compilation::{RoutineCompilationSession, StoredRoutineCompilationContext},
147 definition::RoutineDefinitionContext,
148 rewrites::{self, RoutineRewriteContext},
149};
150
151impl Engine {
152 pub(crate) fn routine_definition_context(&self) -> RoutineDefinitionContext<'_> {
153 RoutineDefinitionContext {
154 compilation: self.stored_routine_compilation_context(),
155 sources: self,
156 overloads: self,
157 regclasses: self,
158 }
159 }
160 pub(crate) fn stored_routine_compilation_context(&self) -> StoredRoutineCompilationContext<'_> {
161 StoredRoutineCompilationContext {
162 analysis: self.routine_compilation_context(),
163 session: self,
164 }
165 }
166 pub(crate) fn routine_rewrite_context(&self) -> RoutineRewriteContext<'_> {
167 RoutineRewriteContext {
168 registry: self,
169 publication: self,
170 compilation: self.stored_routine_compilation_context(),
171 columns: self.stored_column_binding_context(),
172 changes: self,
173 }
174 }
175 pub(crate) fn rewrite_routine_relation_references(
176 &self,
177 from: &RelationIdentity,
178 to: &RelationIdentity,
179 ) -> Result<(), SQLError> {
180 rewrites::rewrite_routine_relation_references(&self.routine_rewrite_context(), from, to)
181 }
182 pub(crate) fn rewrite_routine_column_references(
183 &self,
184 relation: &RelationIdentity,
185 from: &str,
186 to: &str,
187 ) -> Result<(), SQLError> {
188 rewrites::rewrite_routine_column_references(
189 &self.routine_rewrite_context(),
190 relation,
191 from,
192 to,
193 )
194 }
195}
196impl RoutineCompilationSession for Engine {
197 fn routine_search_path(&self) -> Vec<String> {
198 crate::session::effective_search_path(&self.session.state.read())
199 }
200 fn replace_routine_search_path(&self, path: Vec<String>) -> Vec<String> {
201 std::mem::replace(&mut self.session.state.write().search_path, path)
202 }
203 fn restore_routine_search_path(&self, path: Vec<String>) {
204 self.session.state.write().search_path = path;
205 }
206 fn routine_settings_scope(
207 &self,
208 settings: &[(String, String)],
209 ) -> Result<Box<dyn RoutineConfigurationGuard + '_>, SQLError> {
210 let scope = self.routine_invocation_state_guard(!settings.is_empty(), false);
211 for (name, value) in settings {
212 self.set_configured_parameter(name, value)?;
213 }
214 Ok(Box::new(scope))
215 }
216}
217
218use uqa_execution::routines::{
219 catalog::RoutineMutationContext,
220 configuration::{RoutineConfigurationGuard, RoutineConfigurationSession},
221 registration::RoutineRegistrationContext,
222};
223use uqa_sql::routines::registration::RoutineSupportAuthority;
224
225impl RoutineSupportAuthority for Engine {
226 fn current_user_is_superuser(&self) -> bool {
227 Engine::current_user_is_superuser(self)
228 }
229}
230impl RoutineConfigurationGuard for crate::roles::RoutineSessionStateGuard<'_> {}
231impl RoutineConfigurationSession for Engine {
232 fn routine_configuration_guard(&self) -> Box<dyn RoutineConfigurationGuard + '_> {
233 Box::new(self.routine_config_state_guard())
234 }
235 fn set_routine_variable(&self, name: &str, value: &str) -> Result<(), SQLError> {
236 self.set_variable(name, value)
237 }
238 fn show_routine_variable(&self, name: &str) -> Result<String, SQLError> {
239 self.show_variable(name)
240 }
241 fn routine_variable_name(&self, name: &str) -> Result<String, SQLError> {
242 self.session_execution_view()
243 .show_parameter(name)
244 .map(|(name, _)| name)
245 }
246 fn load_language_library(&self, language: &str) {
247 self.load_language(language);
248 }
249}
250impl Engine {
251 pub(crate) fn routine_mutation_context(&self) -> RoutineMutationContext<'_> {
252 RoutineMutationContext {
253 writer: self,
254 names: self,
255 roles: self,
256 registry: self,
257 publication: self,
258 changes: self,
259 }
260 }
261 pub(crate) fn routine_registration_context(&self) -> RoutineRegistrationContext<'_> {
262 RoutineRegistrationContext {
263 catalog: self.routine_mutation_context(),
264 namespace: self.relation_creation_context(),
265 definition: self.routine_definition_context(),
266 support: self,
267 configuration: self,
268 overloads: uqa_sql::routines::resolution::RoutineOverloadContext { catalog: self },
269 session: self,
270 bodies: self,
271 }
272 }
273}