use crate::{errors::SqliteGraphError, graph::SqliteGraph};
use super::{
pattern::PatternTriple,
property::matches_property_filters,
query::{execute_complex_edge_query, execute_simple_edge_query},
};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TripleMatch {
pub start_id: i64,
pub end_id: i64,
pub edge_id: i64,
}
impl TripleMatch {
pub fn new(start_id: i64, edge_id: i64, end_id: i64) -> Self {
Self {
start_id,
end_id,
edge_id,
}
}
}
pub fn match_triples(
graph: &SqliteGraph,
pattern: &PatternTriple,
) -> Result<Vec<TripleMatch>, SqliteGraphError> {
pattern.validate()?;
let _conn = graph.connection();
let matches = if pattern.start_label.is_none() && pattern.end_label.is_none() {
execute_simple_edge_query(graph, pattern)?
} else {
execute_complex_edge_query(graph, pattern)?
};
let mut filtered_matches = Vec::new();
for triple_match in matches {
if matches_property_filters(graph, &triple_match, pattern)? {
filtered_matches.push(triple_match);
}
}
filtered_matches.sort_by(|a, b| {
a.start_id
.cmp(&b.start_id)
.then_with(|| a.edge_id.cmp(&b.edge_id))
.then_with(|| a.end_id.cmp(&b.end_id))
});
Ok(filtered_matches)
}