uni_query/query/executor/
custom_functions.rs1use std::collections::HashMap;
11use std::sync::Arc;
12
13use uni_common::{Result, Value};
14
15pub type CustomScalarFn = Arc<dyn Fn(&[Value]) -> Result<Value> + Send + Sync>;
19
20#[derive(Default, Clone)]
24pub struct CustomFunctionRegistry {
25 functions: HashMap<String, CustomScalarFn>,
26}
27
28impl CustomFunctionRegistry {
29 pub fn new() -> Self {
31 Self::default()
32 }
33
34 pub fn register(&mut self, name: String, func: CustomScalarFn) {
38 self.functions.insert(name.to_uppercase(), func);
39 }
40
41 pub fn get(&self, name: &str) -> Option<&CustomScalarFn> {
43 self.functions.get(&name.to_uppercase())
44 }
45
46 pub fn iter(&self) -> impl Iterator<Item = (&str, &CustomScalarFn)> {
48 self.functions.iter().map(|(k, v)| (k.as_str(), v))
49 }
50
51 pub fn remove(&mut self, name: &str) -> bool {
53 self.functions.remove(&name.to_uppercase()).is_some()
54 }
55
56 pub fn is_empty(&self) -> bool {
58 self.functions.is_empty()
59 }
60}
61
62impl std::fmt::Debug for CustomFunctionRegistry {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 f.debug_struct("CustomFunctionRegistry")
65 .field("functions", &self.functions.keys().collect::<Vec<_>>())
66 .finish()
67 }
68}