use no_mistakes_postgres_sql::erased_visit_fn::visit_expressions;
use sqlparser::ast::{Expr, InTarget};
use std::ops::ControlFlow;
pub(super) fn configured_function(expr: &Expr, functions: &[String]) -> bool {
let Expr::Function(function) = no_mistakes_postgres_sql::idents::unwrap_expr(expr) else {
return false;
};
let name = no_mistakes_postgres_sql::idents::object_name_identity(&function.name);
functions.contains(&name) && scalar(expr, functions)
}
pub(super) fn scalar(expr: &Expr, functions: &[String]) -> bool {
visit_expressions(expr, |child| {
let rejected = match child {
Expr::Identifier(_)
| Expr::CompoundIdentifier(_)
| Expr::Subquery(_)
| Expr::Exists { .. }
| Expr::In {
target: InTarget::Subquery(_),
..
} => true,
Expr::Function(function) => {
let name = no_mistakes_postgres_sql::idents::object_name_identity(&function.name);
function.over.is_some()
|| function.filter.is_some()
|| !(functions.contains(&name) || temporal(&name))
}
_ => false,
};
if rejected {
ControlFlow::Break(())
} else {
ControlFlow::Continue(())
}
})
.is_continue()
}
pub(super) fn uses_configured_function(expr: &Expr, functions: &[String]) -> bool {
visit_expressions(expr, |child| {
if let Expr::Function(function) = child {
let name = no_mistakes_postgres_sql::idents::object_name_identity(&function.name);
if functions.contains(&name) && !temporal(&name) {
return ControlFlow::Break(());
}
}
ControlFlow::Continue(())
})
.is_break()
}
fn temporal(name: &str) -> bool {
matches!(
name.strip_prefix("pg_catalog.").unwrap_or(name),
"now" | "current_timestamp" | "current_date" | "localtimestamp"
)
}