use std::{collections::HashMap, sync::LazyLock};
pub use fraiseql_db::where_clause::{OperatorCategory, WHERE_OPERATORS, WhereOperatorSpec};
#[derive(Debug, Clone)]
pub struct OperatorInfo {
pub name: &'static str,
pub sql_op: &'static str,
pub category: OperatorCategory,
pub requires_array: bool,
pub jsonb_operator: bool,
}
impl From<&'static WhereOperatorSpec> for OperatorInfo {
fn from(spec: &'static WhereOperatorSpec) -> Self {
Self {
name: spec.name,
sql_op: spec.sql_op,
category: spec.category,
requires_array: spec.requires_array,
jsonb_operator: spec.jsonb_operator,
}
}
}
pub static OPERATOR_REGISTRY: LazyLock<HashMap<&'static str, OperatorInfo>> = LazyLock::new(|| {
let mut m = HashMap::new();
for spec in WHERE_OPERATORS {
for name in spec.all_names() {
m.insert(name, OperatorInfo::from(spec));
}
}
m
});
#[must_use]
pub fn get_operator_info(name: &str) -> Option<&'static OperatorInfo> {
OPERATOR_REGISTRY.get(name)
}
#[must_use]
pub fn is_operator(name: &str) -> bool {
OPERATOR_REGISTRY.contains_key(name)
}
#[must_use]
pub fn get_operators_by_category(category: OperatorCategory) -> Vec<&'static OperatorInfo> {
OPERATOR_REGISTRY.values().filter(|op| op.category == category).collect()
}
#[cfg(test)]
#[path = "operators_tests.rs"]
mod operators_tests;