use crate::graph::core::Graph;
use crate::utils::error::Result;
use oxigraph::sparql::{QueryResults, SparqlEvaluator};
use std::collections::BTreeMap;
pub struct GraphQuery<'a> {
graph: &'a Graph,
}
impl<'a> GraphQuery<'a> {
pub fn new(graph: &'a Graph) -> Self {
Self { graph }
}
pub fn execute_with_prefixes(
&self, sparql: &str, prefixes: &BTreeMap<String, String>, base: Option<&str>,
) -> Result<QueryResults<'a>> {
self.graph.query_with_prolog(sparql, prefixes, base)
}
pub fn execute_cached(&self, sparql: &str) -> Result<crate::graph::types::CachedResult> {
self.graph.query_cached(sparql)
}
pub fn execute(&self, sparql: &str) -> Result<QueryResults<'a>> {
self.graph.query(sparql)
}
pub fn execute_prepared(&self, q: &str) -> Result<QueryResults<'a>> {
self.graph.query_prepared(q)
}
pub fn builder(&self) -> SparqlEvaluator {
SparqlEvaluator::new()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::graph::core::Graph;
use std::collections::BTreeMap;
#[test]
fn test_query_execute_cached() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice ex:name "Alice" .
"#,
)
.unwrap();
let query = GraphQuery::new(&graph);
let result = query
.execute_cached("SELECT ?name WHERE { ?s <http://example.org/name> ?name }")
.unwrap();
match result {
crate::graph::types::CachedResult::Solutions(rows) => {
assert!(!rows.is_empty());
assert!(rows[0].contains_key("name"));
}
_ => panic!("Expected solutions"),
}
}
#[test]
fn test_query_execute() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice ex:name "Alice" .
"#,
)
.unwrap();
let query = GraphQuery::new(&graph);
let results = query
.execute("SELECT ?name WHERE { ?s <http://example.org/name> ?name }")
.unwrap();
match results {
QueryResults::Solutions(_) => {}
_ => panic!("Expected solutions"),
}
}
#[test]
fn test_query_execute_with_prefixes() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice ex:name "Alice" .
"#,
)
.unwrap();
let query = GraphQuery::new(&graph);
let mut prefixes = BTreeMap::new();
prefixes.insert("ex".to_string(), "http://example.org/".to_string());
let results = query
.execute_with_prefixes("SELECT ?name WHERE { ?s ex:name ?name }", &prefixes, None)
.unwrap();
match results {
QueryResults::Solutions(_) => {}
_ => panic!("Expected solutions"),
}
}
#[test]
fn test_query_execute_with_base() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice ex:name "Alice" .
"#,
)
.unwrap();
let query = GraphQuery::new(&graph);
let prefixes = BTreeMap::new();
let results = query
.execute_with_prefixes(
"SELECT ?name WHERE { ?s <http://example.org/name> ?name }",
&prefixes,
Some("http://example.org/"),
)
.unwrap();
match results {
QueryResults::Solutions(_) => {}
_ => panic!("Expected solutions"),
}
}
#[test]
fn test_query_execute_prepared() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice ex:name "Alice" .
"#,
)
.unwrap();
let query = GraphQuery::new(&graph);
let results = query
.execute_prepared("SELECT ?name WHERE { ?s <http://example.org/name> ?name }")
.unwrap();
match results {
QueryResults::Solutions(_) => {}
_ => panic!("Expected solutions"),
}
}
#[test]
fn test_query_builder() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice ex:name "Alice" .
"#,
)
.unwrap();
let query = GraphQuery::new(&graph);
let builder = query.builder();
let results = builder
.parse_query("SELECT ?name WHERE { ?s <http://example.org/name> ?name }")
.unwrap()
.on_store(graph.inner())
.execute()
.unwrap();
match results {
QueryResults::Solutions(_) => {}
_ => panic!("Expected solutions"),
}
}
#[test]
fn test_query_ask_query() {
let graph = Graph::new().unwrap();
graph
.insert_turtle(
r#"
@prefix ex: <http://example.org/> .
ex:alice a ex:Person .
"#,
)
.unwrap();
let query = GraphQuery::new(&graph);
let result = query
.execute_cached("ASK { ?s a <http://example.org/Person> }")
.unwrap();
match result {
crate::graph::types::CachedResult::Boolean(true) => {}
_ => panic!("Expected true"),
}
}
#[test]
fn test_query_invalid_syntax() {
let graph = Graph::new().unwrap();
let query = GraphQuery::new(&graph);
let result = query.execute_cached("INVALID SPARQL SYNTAX");
assert!(result.is_err());
}
}