diff --git a/sparql/src/exec.rs b/sparql/src/exec.rs index 9305451..57288c1 100644 --- a/sparql/src/exec.rs +++ b/sparql/src/exec.rs @@ -135,6 +135,16 @@ impl<'a, D: Dataset> ExecState<'a, D> { } } + pub fn ask( + &mut self, + pattern: &GraphPattern, + graph_matcher: &[Option], + binding: Option<&Binding>, + ) -> Result> { + self.select(pattern, graph_matcher, binding) + .map(|binding| binding.into_iter().next().is_some()) + } + fn bgp( &mut self, patterns: &[TriplePattern], diff --git a/sparql/src/test.rs b/sparql/src/test.rs index 4f16fe9..e9dda63 100644 --- a/sparql/src/test.rs +++ b/sparql/src/test.rs @@ -14,15 +14,24 @@ use test_case::test_case; vec!["", "", "", "", "", ]; "predicates" )] -fn test_select_1(query: &str, exp: Vec<&str>) -> TestResult { +#[test_case( + "SELECT ?x { ?x ?y \"not in the repo\" }", + vec![]; + "no result" +)] +fn test_select_1_and_ask(query: &str, exp: Vec<&str>) -> TestResult { let dataset = dataset_101()?; let dataset = SparqlWrapper(&dataset); - let query = SparqlQuery::parse(query)?; - let bindings = dataset.query(&query)?.into_bindings(); + let parsed_query = SparqlQuery::parse(query)?; + let bindings = dataset.query(&parsed_query)?.into_bindings(); assert_eq!(bindings.variables(), &["x"]); let mut got = bindings_to_vec(bindings); got.sort(); assert_eq!(exp, got); + + let parsed_query = SparqlQuery::parse(&query.replace("SELECT ?x", "ASK"))?; + let response = dataset.query(&parsed_query)?.into_boolean(); + assert_eq!(response, !exp.is_empty()); Ok(()) } diff --git a/sparql/src/wrapper.rs b/sparql/src/wrapper.rs index 9af7c4b..3e49224 100644 --- a/sparql/src/wrapper.rs +++ b/sparql/src/wrapper.rs @@ -58,7 +58,12 @@ impl<'a, D: Dataset> SparqlDataset for SparqlWrapper<'a, D> { dataset, pattern, base_iri, - } => Err(SparqlWrapperError::NotImplemented("ASK query")), + } => { + let mut exec = ExecState::new(self.0, dataset)?; + let cfg = exec.config_cloned(); + exec.ask(pattern, &cfg.default_matcher, None) + .map(SparqlResult::Boolean) + } } } }