use uqa_core::Value;
pub fn sql_null_test_with_control(
value: &Value,
negated: bool,
engine: Option<&dyn super::EngineHook>,
control: &uqa_core::memory::ProductionControl<'_>,
) -> Result<bool, crate::SQLError> {
control.check()?;
if let Value::Datum(datum) = value {
let catalog = engine.map(super::EngineValueCatalog);
if let Some(result) = super::datums::record_null_test_with_control(
datum,
negated,
catalog
.as_ref()
.map(|catalog| catalog as &dyn super::SQLValueCatalog),
control,
)? {
return Ok(result);
}
}
Ok(uqa_core::sql_null_test(Some(value), negated))
}
#[derive(Clone, Copy, Default)]
pub(crate) struct OutputCarriers {
pub(crate) datum: bool,
pub(crate) enumeration: bool,
}
pub(crate) fn output_carriers(value: &Value) -> OutputCarriers {
let mut found = OutputCarriers::default();
any_nested_value(value, &mut |value| {
found.datum |= matches!(value, Value::Datum(_));
found.enumeration |= matches!(value, Value::Enum(_));
found.datum
});
found
}
pub(crate) fn any_nested_value(value: &Value, predicate: &mut impl FnMut(&Value) -> bool) -> bool {
#[cfg(test)]
record_visit();
if predicate(value) {
return true;
}
match value {
Value::Array(array) => array
.elements()
.iter()
.any(|value| any_nested_value(value, predicate)),
Value::List(values) => values
.iter()
.any(|value| any_nested_value(value, predicate)),
Value::Row(values) => values
.iter()
.any(|value| any_nested_value(value, predicate)),
Value::Record(fields) => fields
.values()
.iter()
.any(|value| any_nested_value(value, predicate)),
Value::Map(values) => values
.values()
.any(|value| any_nested_value(value, predicate)),
_ => false,
}
}
#[cfg(test)]
thread_local! {
static VALUE_VISITS: std::cell::Cell<usize> = const { std::cell::Cell::new(0) };
}
#[cfg(test)]
pub(crate) fn record_visit() {
VALUE_VISITS.with(|visits| visits.set(visits.get() + 1));
}
#[cfg(test)]
pub(crate) fn visits() -> usize {
VALUE_VISITS.with(std::cell::Cell::get)
}