From 180ae2229376f9f96692b0921334f90d4efa5d3e Mon Sep 17 00:00:00 2001 From: Tpt Date: Tue, 12 Sep 2023 09:00:13 +0200 Subject: [PATCH] Python: introduces QueryBoolean class --- python/src/lib.rs | 1 + python/src/sparql.rs | 34 +++++++++++++++++++++++++++++++++- python/src/store.rs | 6 +++--- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/python/src/lib.rs b/python/src/lib.rs index bdc38681..474b0abc 100644 --- a/python/src/lib.rs +++ b/python/src/lib.rs @@ -32,6 +32,7 @@ fn pyoxigraph(_py: Python<'_>, module: &PyModule) -> PyResult<()> { module.add_class::()?; module.add_class::()?; module.add_class::()?; + module.add_class::()?; module.add_class::()?; io::add_to_module(module) } diff --git a/python/src/sparql.rs b/python/src/sparql.rs index c7ad693b..4310652a 100644 --- a/python/src/sparql.rs +++ b/python/src/sparql.rs @@ -65,7 +65,7 @@ pub fn query_results_to_python(py: Python<'_>, results: QueryResults) -> PyObjec match results { QueryResults::Solutions(inner) => PyQuerySolutions { inner }.into_py(py), QueryResults::Graph(inner) => PyQueryTriples { inner }.into_py(py), - QueryResults::Boolean(b) => b.into_py(py), + QueryResults::Boolean(inner) => PyQueryBoolean { inner }.into_py(py), } } @@ -204,6 +204,38 @@ impl PyQuerySolutions { } } +/// A boolean returned by a SPARQL ``ASK`` query. +/// +/// It can be easily casted to a regular boolean using the :py:func:`bool` function. +/// +/// >>> store = Store() +/// >>> store.add(Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'))) +/// >>> bool(store.query('ASK { ?s ?p ?o }')) +/// True +#[pyclass(unsendable, name = "QueryBoolean", module = "pyoxigraph")] +pub struct PyQueryBoolean { + inner: bool, +} + +#[pymethods] +impl PyQueryBoolean { + fn __bool__(&self) -> bool { + self.inner + } + + fn __richcmp__(&self, other: &Self, op: CompareOp) -> bool { + op.matches(self.inner.cmp(&other.inner)) + } + + fn __hash__(&self) -> u64 { + self.inner.into() + } + + fn __repr__(&self) -> String { + format!("", self.inner) + } +} + /// An iterator of :py:class:`Triple` returned by a SPARQL ``CONSTRUCT`` or ``DESCRIBE`` query /// /// >>> store = Store() diff --git a/python/src/store.rs b/python/src/store.rs index 80a04709..6e5329de 100644 --- a/python/src/store.rs +++ b/python/src/store.rs @@ -251,8 +251,8 @@ impl PyStore { /// :type default_graph: NamedNode or BlankNode or DefaultGraph or list(NamedNode or BlankNode or DefaultGraph) or None, optional /// :param named_graphs: list of the named graphs that could be used in SPARQL `GRAPH` clause. By default, all the store named graphs are available. /// :type named_graphs: list(NamedNode or BlankNode) or None, optional - /// :return: a :py:class:`bool` for ``ASK`` queries, an iterator of :py:class:`Triple` for ``CONSTRUCT`` and ``DESCRIBE`` queries and an iterator of :py:class:`QuerySolution` for ``SELECT`` queries. - /// :rtype: QuerySolutions or QueryTriples or bool + /// :return: a :py:class:`QueryBoolean` for ``ASK`` queries, an iterator of :py:class:`Triple` for ``CONSTRUCT`` and ``DESCRIBE`` queries and an iterator of :py:class:`QuerySolution` for ``SELECT`` queries. + /// :rtype: QuerySolutions or QueryBoolean or QueryTriples /// :raises SyntaxError: if the provided query is invalid. /// :raises OSError: if an error happens while reading the store. /// @@ -274,7 +274,7 @@ impl PyStore { /// /// >>> store = Store() /// >>> store.add(Quad(NamedNode('http://example.com'), NamedNode('http://example.com/p'), Literal('1'))) - /// >>> store.query('ASK { ?s ?p ?o }') + /// >>> bool(store.query('ASK { ?s ?p ?o }')) /// True #[pyo3(signature = (query, *, base_iri = None, use_default_graph_as_union = false, default_graph = None, named_graphs = None))] fn query(