uqa_engine/capabilities/
routine_catalog.rs1use crate::{open::CatalogRestoreMode, Arc, CatalogFacade, Engine, StorageBackendResult};
10use uqa_execution::routines::{
11 catalog,
12 restoration::{self, PendingSQLFunctionRestore, RoutineRestoreContext, RoutineRestoreSchemas},
13};
14use uqa_sql::{
15 ast::FunctionBinding,
16 routines::{
17 lifecycle::{lookup, names, RoutineRegistry},
18 SQLUserFunction,
19 },
20 SQLError,
21};
22
23impl Engine {
24 fn with_routine_lookup_registry<T>(&self, lookup: impl FnOnce(&RoutineRegistry) -> T) -> T {
25 let live_registry;
26 let registry = if let Some(snapshot) = self.query_sql_function_snapshots.as_ref() {
27 snapshot.as_ref()
28 } else {
29 live_registry = self.durable.sql_user_functions.read();
30 &live_registry
31 };
32 lookup(registry)
33 }
34 pub(crate) fn lookup_visible_sql_functions(
35 &self,
36 name: &str,
37 ) -> Result<Option<Vec<Arc<SQLUserFunction>>>, SQLError> {
38 let keys = names::routine_lookup_keys(self, name)?;
39 Ok(self.with_routine_lookup_registry(|registry| {
40 lookup::lookup_sql_functions_by_keys(registry, keys)
41 }))
42 }
43 pub(crate) fn lookup_visible_sql_functions_for_analysis(
44 &self,
45 name: &str,
46 ) -> Result<Option<Vec<Arc<SQLUserFunction>>>, SQLError> {
47 let Some(keys) = names::routine_lookup_keys_for_analysis(self, name)? else {
48 return Ok(None);
49 };
50 Ok(self.with_routine_lookup_registry(|registry| {
51 lookup::lookup_sql_functions_by_keys(registry, keys)
52 }))
53 }
54 pub(crate) fn lookup_bound_sql_functions(
55 &self,
56 name: &str,
57 ) -> Option<Vec<Arc<SQLUserFunction>>> {
58 let keys = std::iter::once(name.to_string());
59 self.with_routine_lookup_registry(|registry| {
60 lookup::lookup_sql_functions_by_keys(registry, keys)
61 })
62 }
63 pub(crate) fn lookup_bound_sql_functions_by_binding(
64 &self,
65 binding: &FunctionBinding,
66 ) -> Option<Vec<Arc<SQLUserFunction>>> {
67 self.with_routine_lookup_registry(|registry| {
68 lookup::lookup_bound_sql_functions_by_binding(registry, binding)
69 })
70 }
71 pub(crate) fn lookup_sql_routine_candidates(
72 &self,
73 name: &str,
74 ) -> Result<Option<Vec<Arc<SQLUserFunction>>>, SQLError> {
75 let keys = names::routine_lookup_keys(self, name)?;
76 Ok(self.with_routine_lookup_registry(|registry| {
77 lookup::lookup_sql_routine_candidates_by_keys(registry, keys)
78 }))
79 }
80 pub(crate) fn lookup_bound_sql_routine_candidates_by_binding(
81 &self,
82 binding: &FunctionBinding,
83 ) -> Option<Vec<Arc<SQLUserFunction>>> {
84 self.with_routine_lookup_registry(|registry| {
85 lookup::lookup_bound_sql_routine_candidates_by_binding(registry, binding)
86 })
87 }
88 pub(crate) fn persist_sql_functions_snapshot(
89 &self,
90 registry: &RoutineRegistry,
91 ) -> Result<(), SQLError> {
92 catalog::persist_sql_functions_snapshot(self.storage.catalog.as_deref(), registry)
93 }
94 fn routine_restore_context(&self) -> RoutineRestoreContext<'_> {
95 RoutineRestoreContext {
96 registry: self,
97 roles: self,
98 publication: self,
99 schemas: self,
100 definition: self.routine_definition_context(),
101 }
102 }
103 pub(crate) fn install_sql_function_restore_placeholders(
104 &self,
105 catalog: &dyn CatalogFacade,
106 mode: CatalogRestoreMode,
107 ) -> StorageBackendResult<Option<PendingSQLFunctionRestore>> {
108 restoration::install_sql_function_restore_placeholders(
109 &self.routine_restore_context(),
110 catalog,
111 mode.allows_migration(),
112 )
113 }
114 pub(crate) fn finalize_sql_function_restore(
115 &self,
116 pending: PendingSQLFunctionRestore,
117 mode: CatalogRestoreMode,
118 ) -> StorageBackendResult<()> {
119 restoration::finalize_sql_function_restore(
120 &self.routine_restore_context(),
121 pending,
122 mode.allows_migration(),
123 )
124 }
125}
126impl RoutineRestoreSchemas for Engine {
127 fn routine_schema_exists(&self, schema: &str) -> bool {
128 self.durable.schemas.read().contains_key(schema)
129 }
130 fn routine_user_type_identity(&self, type_name: &str) -> Option<String> {
131 uqa_execution::catalog::projection::resolve_catalog_column_type(
132 &self.catalog_execution(),
133 type_name,
134 )
135 .and_then(|ty| ty.user_type_identity())
136 }
137}