use std::collections::BTreeMap;
use relate_core::{Error, Result, Value};
use relate_ir::Expr;
use crate::overlay::ExecGraph;
use super::super::{eval, Row};
pub(super) fn property_exists_form(
graph: &ExecGraph<'_>,
row: &Row,
params: &BTreeMap<String, Value>,
n: &str,
args: &[Expr],
cypher_version: Option<u32>,
) -> Option<Result<Value>> {
if n != "property_exists" {
return None;
}
Some(property_exists(graph, row, params, args, cypher_version))
}
fn property_exists(
graph: &ExecGraph<'_>,
row: &Row,
params: &BTreeMap<String, Value>,
args: &[Expr],
cypher_version: Option<u32>,
) -> Result<Value> {
if args.len() != 2 {
return Err(Error::syntax(
"InvalidNumberOfArguments: property_exists() requires two arguments",
));
}
let element = eval(graph, row, &args[0], params, cypher_version)?;
let property = match &args[1] {
Expr::Variable(name) => name.to_string(),
Expr::Null => "null".to_string(),
Expr::String(name) => name.clone(),
_ => {
return Err(Error::invalid_argument_type(
"property name must be an identifier",
))
}
};
Ok(match element {
Value::Node(node) => Value::Bool(
graph
.node(node.id)
.and_then(|node| node.properties.get(property.as_str()).cloned())
.is_some_and(|value| !value.is_null()),
),
Value::Relationship(rel) => Value::Bool(
graph
.rel(rel.id)
.and_then(|rel| rel.properties.get(property.as_str()).cloned())
.is_some_and(|value| !value.is_null()),
),
Value::Null => Value::Null,
_ => {
return Err(Error::invalid_argument_type(
"expected NODE or RELATIONSHIP",
))
}
})
}