use std::cell::RefCell;
use std::sync::Arc;
use crate::udf_registry::UdfRegistry;
thread_local! {
static THREAD_UDF_CONTEXT: RefCell<Option<UdfContext>> = const { RefCell::new(None) };
}
#[derive(Clone)]
pub struct UdfContext {
pub registry: Arc<UdfRegistry>,
pub case_sensitive: bool,
pub session_time_zone: Option<String>,
}
pub fn set_thread_udf_context(registry: Arc<UdfRegistry>, case_sensitive: bool) {
set_thread_udf_context_with_tz(registry, case_sensitive, None);
}
pub fn set_thread_udf_context_with_tz(
registry: Arc<UdfRegistry>,
case_sensitive: bool,
session_time_zone: Option<String>,
) {
THREAD_UDF_CONTEXT.with(|cell| {
*cell.borrow_mut() = Some(UdfContext {
registry,
case_sensitive,
session_time_zone,
})
});
}
pub fn get_thread_udf_context() -> Option<(Arc<UdfRegistry>, bool)> {
THREAD_UDF_CONTEXT.with(|cell| {
cell.borrow()
.clone()
.map(|ctx| (ctx.registry, ctx.case_sensitive))
})
}
pub fn get_thread_session_time_zone() -> String {
THREAD_UDF_CONTEXT.with(|cell| {
cell.borrow()
.as_ref()
.and_then(|ctx| ctx.session_time_zone.as_deref())
.unwrap_or("UTC")
.to_string()
})
}
pub fn update_thread_session_time_zone(tz: Option<String>) {
THREAD_UDF_CONTEXT.with(|cell| {
if let Some(ref mut ctx) = *cell.borrow_mut() {
ctx.session_time_zone = tz;
}
});
}
pub fn clear_thread_udf_context() {
THREAD_UDF_CONTEXT.with(|cell| *cell.borrow_mut() = None);
}