use super::{CatalogFunction, CatalogTable, CatalogTrigger, SchemaCatalog};
use std::collections::BTreeMap;
pub(super) struct TriggerFunctions<'a> {
catalog: &'a SchemaCatalog,
by_name: BTreeMap<String, Vec<&'a CatalogFunction>>,
}
impl<'a> TriggerFunctions<'a> {
pub(super) fn new(catalog: &'a SchemaCatalog) -> Self {
let mut by_name = BTreeMap::<_, Vec<_>>::new();
for function in catalog
.functions()
.filter(|function| function.returns_trigger)
{
by_name
.entry(super::names::normalize_table_name(&function.name))
.or_default()
.push(function);
}
Self { catalog, by_name }
}
pub(super) fn resolve(
&self,
table: &CatalogTable,
trigger: &CatalogTrigger,
) -> Option<&'a CatalogFunction> {
let identity = self
.catalog
.trigger_function_identities
.get(&table.name)?
.get(&trigger.name)?;
let functions = self.by_name.get(identity).or_else(|| {
let (schema, bare) = super::names::split_key(identity);
if schema.as_deref()? != self.catalog.declared_schema()? {
return None;
}
let bare = format!("\"{}\"", bare.replace('"', "\"\""));
self.by_name.get(&super::names::normalize_table_name(&bare))
})?;
let [function] = functions.as_slice() else {
return None;
};
Some(*function)
}
}