Allows to set the base IRI for SPARQL queries

pull/10/head
Tpt 5 years ago
parent 27d0c2a67a
commit 8e87396d5e
  1. 2
      lib/src/lib.rs
  2. 6
      lib/src/repository.rs
  3. 4
      lib/src/sparql/mod.rs
  4. 2
      lib/src/store/memory.rs
  5. 4
      lib/src/store/mod.rs
  6. 2
      lib/src/store/rocksdb.rs
  7. 10
      lib/tests/sparql_test_cases.rs
  8. 2
      server/src/main.rs

@ -27,7 +27,7 @@
//! assert_eq!(vec![quad], results.unwrap()); //! assert_eq!(vec![quad], results.unwrap());
//! //!
//! // SPARQL query //! // SPARQL query
//! let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }").unwrap(); //! let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
//! let results = prepared_query.exec().unwrap(); //! let results = prepared_query.exec().unwrap();
//! if let QueryResult::Bindings(results) = results { //! if let QueryResult::Bindings(results) = results {
//! assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into())); //! assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));

@ -30,7 +30,7 @@ use std::io::BufRead;
/// assert_eq!(vec![quad], results.unwrap()); /// assert_eq!(vec![quad], results.unwrap());
/// ///
/// // SPARQL query /// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }").unwrap(); /// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let results = prepared_query.exec().unwrap(); /// let results = prepared_query.exec().unwrap();
/// if let QueryResult::Bindings(results) = results { /// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into())); /// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
@ -75,13 +75,13 @@ pub trait RepositoryConnection: Clone {
/// connection.insert(&Quad::new(ex.clone(), ex.clone(), ex.clone(), None)); /// connection.insert(&Quad::new(ex.clone(), ex.clone(), ex.clone(), None));
/// ///
/// // SPARQL query /// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }").unwrap(); /// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let results = prepared_query.exec().unwrap(); /// let results = prepared_query.exec().unwrap();
/// if let QueryResult::Bindings(results) = results { /// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into())); /// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));
/// } /// }
/// ``` /// ```
fn prepare_query(&self, query: &str) -> Result<Self::PreparedQuery>; fn prepare_query(&self, query: &str, base_iri: Option<&str>) -> Result<Self::PreparedQuery>;
/// Retrieves quads with a filter on each quad component /// Retrieves quads with a filter on each quad component
/// ///

@ -54,8 +54,8 @@ enum SimplePreparedQueryOptions<S: StoreConnection> {
} }
impl<S: StoreConnection> SimplePreparedQuery<S> { impl<S: StoreConnection> SimplePreparedQuery<S> {
pub(crate) fn new(connection: S, query: &str) -> Result<Self> { pub(crate) fn new(connection: S, query: &str, base_iri: Option<&str>) -> Result<Self> {
Ok(Self(match read_sparql_query(query, None)? { Ok(Self(match read_sparql_query(query, base_iri)? {
QueryVariants::Select { QueryVariants::Select {
algebra, algebra,
dataset: _, dataset: _,

@ -32,7 +32,7 @@ use std::sync::RwLockWriteGuard;
/// assert_eq!(vec![quad], results.unwrap()); /// assert_eq!(vec![quad], results.unwrap());
/// ///
/// // SPARQL query /// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }").unwrap(); /// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let results = prepared_query.exec().unwrap(); /// let results = prepared_query.exec().unwrap();
/// if let QueryResult::Bindings(results) = results { /// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into())); /// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));

@ -59,8 +59,8 @@ impl<S: StoreConnection> From<S> for StoreRepositoryConnection<S> {
impl<S: StoreConnection> RepositoryConnection for StoreRepositoryConnection<S> { impl<S: StoreConnection> RepositoryConnection for StoreRepositoryConnection<S> {
type PreparedQuery = SimplePreparedQuery<S>; type PreparedQuery = SimplePreparedQuery<S>;
fn prepare_query(&self, query: &str) -> Result<SimplePreparedQuery<S>> { fn prepare_query(&self, query: &str, base_iri: Option<&str>) -> Result<SimplePreparedQuery<S>> {
SimplePreparedQuery::new(self.inner.clone(), query) //TODO: avoid clone SimplePreparedQuery::new(self.inner.clone(), query, base_iri) //TODO: avoid clone
} }
fn quads_for_pattern<'a>( fn quads_for_pattern<'a>(

@ -42,7 +42,7 @@ use std::sync::Mutex;
/// assert_eq!(vec![quad], results.unwrap()); /// assert_eq!(vec![quad], results.unwrap());
/// ///
/// // SPARQL query /// // SPARQL query
/// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }").unwrap(); /// let prepared_query = connection.prepare_query("SELECT ?s WHERE { ?s ?p ?o }", None).unwrap();
/// let results = prepared_query.exec().unwrap(); /// let results = prepared_query.exec().unwrap();
/// if let QueryResult::Bindings(results) = results { /// if let QueryResult::Bindings(results) = results {
/// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into())); /// assert_eq!(results.into_values_iter().next().unwrap().unwrap()[0], Some(ex.into()));

@ -89,8 +89,6 @@ fn sparql_w3c_query_evaluation_testsuite() -> Result<()> {
NamedNode::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/expr-builtin/manifest#sameTerm-not-eq").unwrap(), NamedNode::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/expr-builtin/manifest#sameTerm-not-eq").unwrap(),
//Simple literal vs xsd:string. We apply RDF 1.1 //Simple literal vs xsd:string. We apply RDF 1.1
NamedNode::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/distinct/manifest#distinct-2").unwrap(), NamedNode::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/distinct/manifest#distinct-2").unwrap(),
//URI normalization: we are not normalizing well
NamedNode::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/i18n/manifest#normalization-2").unwrap(),
//Test on curly brace scoping with OPTIONAL filter //Test on curly brace scoping with OPTIONAL filter
NamedNode::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/optional-filter/manifest#dawg-optional-filter-005-not-simplified").unwrap(), NamedNode::parse("http://www.w3.org/2001/sw/DataAccess/tests/data-r2/optional-filter/manifest#dawg-optional-filter-005-not-simplified").unwrap(),
//DATATYPE("foo"@en) returns rdf:langString in SPARQL 1.1 //DATATYPE("foo"@en) returns rdf:langString in SPARQL 1.1
@ -119,7 +117,7 @@ fn sparql_w3c_query_evaluation_testsuite() -> Result<()> {
} }
match repository match repository
.connection()? .connection()?
.prepare_query(&read_file_to_string(&test.query)?) .prepare_query(&read_file_to_string(&test.query)?, Some(&test.query))
{ {
Err(error) => assert!( Err(error) => assert!(
false, false,
@ -140,16 +138,18 @@ fn sparql_w3c_query_evaluation_testsuite() -> Result<()> {
.next() .next()
.is_some(); .is_some();
let actual_graph = to_graph(result, with_order)?; let actual_graph = to_graph(result, with_order)?;
if !actual_graph.is_isomorphic(&expected_graph) {
assert!( assert!(
actual_graph.is_isomorphic(&expected_graph), false,
"Failure on {}.\nExpected file:\n{}\nOutput file:\n{}\nParsed query:\n{}\nData:\n{}\n", "Failure on {}.\nExpected file:\n{}\nOutput file:\n{}\nParsed query:\n{}\nData:\n{}\n",
test, test,
expected_graph, expected_graph,
actual_graph, actual_graph,
read_file_to_string(&test.query)?, Query::parse(&read_file_to_string(&test.query)?, Some(&test.query)).unwrap(),
repository_to_string(&repository) repository_to_string(&repository)
) )
} }
}
}, },
} }
} else { } else {

@ -134,7 +134,7 @@ fn evaluate_urlencoded_sparql_query<R: RepositoryConnection>(
fn evaluate_sparql_query<R: RepositoryConnection>(connection: R, query: &str) -> Response { fn evaluate_sparql_query<R: RepositoryConnection>(connection: R, query: &str) -> Response {
//TODO: stream //TODO: stream
match connection.prepare_query(query) { match connection.prepare_query(query, None) {
Ok(query) => match query.exec().unwrap() { Ok(query) => match query.exec().unwrap() {
QueryResult::Graph(triples) => { QueryResult::Graph(triples) => {
let mut result = String::default(); let mut result = String::default();

Loading…
Cancel
Save