pyoxigraph: adds use_default_graph_as_union option to the query method

pull/46/head
Tpt 4 years ago
parent 406550b77b
commit 0c8256c07c
  1. 3
      python/Cargo.toml
  2. 18
      python/src/memory_store.rs
  3. 18
      python/src/sled_store.rs
  4. 14
      python/tests/test_store.py

@ -32,7 +32,8 @@ classifier = [
"Programming Language :: Python :: 3.7", "Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.8",
"Programming Language :: Rust", "Programming Language :: Rust",
"Topic :: Database :: Database Engines/Servers" "Topic :: Database :: Database Engines/Servers",
"Topic :: Software Development :: Libraries :: Python Modules",
] ]
project-url = [ project-url = [
"Documentation, https://oxigraph.org/pyoxigraph/", "Documentation, https://oxigraph.org/pyoxigraph/",

@ -108,6 +108,8 @@ impl PyMemoryStore {
/// ///
/// :param query: the query to execute /// :param query: the query to execute
/// :type query: str /// :type query: str
/// :param use_default_graph_as_union: if the SPARQL query should look for triples in all the dataset graphs by default (i.e. without `GRAPH` operations). Disabled by default.
/// :type use_default_graph_as_union: bool
/// :return: a :py:class:`bool` for ``ASK`` queries, an iterator of :py:class:`Triple` for ``CONSTRUCT`` and ``DESCRIBE`` queries and an iterator of solution bindings for ``SELECT`` queries. /// :return: a :py:class:`bool` for ``ASK`` queries, an iterator of :py:class:`Triple` for ``CONSTRUCT`` and ``DESCRIBE`` queries and an iterator of solution bindings for ``SELECT`` queries.
/// :rtype: iter(QuerySolution) or iter(Triple) or bool /// :rtype: iter(QuerySolution) or iter(Triple) or bool
/// :raises SyntaxError: if the provided query is invalid /// :raises SyntaxError: if the provided query is invalid
@ -132,11 +134,21 @@ impl PyMemoryStore {
/// >>> store.add(Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'))) /// >>> store.add(Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1')))
/// >>> store.query('ASK { ?s ?p ?o }') /// >>> store.query('ASK { ?s ?p ?o }')
/// True /// True
#[text_signature = "($self, query)"] #[text_signature = "($self, query, *, use_default_graph_as_union)"]
fn query(&self, query: &str, py: Python<'_>) -> PyResult<PyObject> { #[args(query, "*", use_default_graph_as_union = "false")]
fn query(
&self,
query: &str,
use_default_graph_as_union: bool,
py: Python<'_>,
) -> PyResult<PyObject> {
let results = py.allow_threads(move || { let results = py.allow_threads(move || {
let mut options = QueryOptions::default();
if use_default_graph_as_union {
options = options.with_default_graph_as_union();
}
self.inner self.inner
.query(query, QueryOptions::default()) .query(query, options)
.map_err(map_evaluation_error) .map_err(map_evaluation_error)
})?; })?;
query_results_to_python(py, results) query_results_to_python(py, results)

@ -123,6 +123,8 @@ impl PySledStore {
/// ///
/// :param query: the query to execute /// :param query: the query to execute
/// :type query: str /// :type query: str
/// :param use_default_graph_as_union: if the SPARQL query should look for triples in all the dataset graphs by default (i.e. without `GRAPH` operations). Disabled by default.
/// :type use_default_graph_as_union: bool
/// :return: a :py:class:`bool` for ``ASK`` queries, an iterator of :py:class:`Triple` for ``CONSTRUCT`` and ``DESCRIBE`` queries and an iterator of solution bindings for ``SELECT`` queries. /// :return: a :py:class:`bool` for ``ASK`` queries, an iterator of :py:class:`Triple` for ``CONSTRUCT`` and ``DESCRIBE`` queries and an iterator of solution bindings for ``SELECT`` queries.
/// :rtype: iter(QuerySolution) or iter(Triple) or bool /// :rtype: iter(QuerySolution) or iter(Triple) or bool
/// :raises SyntaxError: if the provided query is invalid /// :raises SyntaxError: if the provided query is invalid
@ -148,11 +150,21 @@ impl PySledStore {
/// >>> store.add(Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'))) /// >>> store.add(Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1')))
/// >>> store.query('ASK { ?s ?p ?o }') /// >>> store.query('ASK { ?s ?p ?o }')
/// True /// True
#[text_signature = "($self, query)"] #[text_signature = "($self, query, *, use_default_graph_as_union)"]
fn query(&self, query: &str, py: Python<'_>) -> PyResult<PyObject> { #[args(query, "*", use_default_graph_as_union = "false")]
fn query(
&self,
query: &str,
use_default_graph_as_union: bool,
py: Python<'_>,
) -> PyResult<PyObject> {
let results = py.allow_threads(move || { let results = py.allow_threads(move || {
let mut options = QueryOptions::default();
if use_default_graph_as_union {
options = options.with_default_graph_as_union();
}
self.inner self.inner
.query(query, QueryOptions::default()) .query(query, options)
.map_err(map_evaluation_error) .map_err(map_evaluation_error)
})?; })?;
query_results_to_python(py, results) query_results_to_python(py, results)

@ -41,10 +41,10 @@ class TestAbstractStore(unittest.TestCase, ABC):
store.add(Quad(foo, bar, baz)) store.add(Quad(foo, bar, baz))
store.add(Quad(foo, bar, baz, DefaultGraph())) store.add(Quad(foo, bar, baz, DefaultGraph()))
store.add(Quad(foo, bar, baz, graph)) store.add(Quad(foo, bar, baz, graph))
self.assertTrue(Quad(foo, bar, baz) in store) self.assertIn(Quad(foo, bar, baz), store)
self.assertTrue(Quad(foo, bar, baz, DefaultGraph()) in store) self.assertIn(Quad(foo, bar, baz, DefaultGraph()), store)
self.assertTrue(Quad(foo, bar, baz, graph) in store) self.assertIn(Quad(foo, bar, baz, graph), store)
self.assertTrue(Quad(foo, bar, baz, foo) not in store) self.assertNotIn(Quad(foo, bar, baz, foo), store)
def test_iter(self): def test_iter(self):
store = self.store() store = self.store()
@ -97,6 +97,12 @@ class TestAbstractStore(unittest.TestCase, ABC):
self.assertEqual(results[0][0], foo) self.assertEqual(results[0][0], foo)
self.assertEqual(results[0]["s"], foo) self.assertEqual(results[0]["s"], foo)
def test_select_query_union_default_graph(self):
store = self.store()
store.add(Quad(foo, bar, baz, graph))
self.assertEqual(len(list(store.query("SELECT ?s WHERE { ?s ?p ?o }"))), 0)
self.assertEqual(len(list(store.query("SELECT ?s WHERE { ?s ?p ?o }", use_default_graph_as_union=True))), 1)
def test_load_ntriples_to_default_graph(self): def test_load_ntriples_to_default_graph(self):
store = self.store() store = self.store()
store.load( store.load(

Loading…
Cancel
Save