Skip to main content

rudof_rdf/rdf_core/query/
query_rdf.rs

1use crate::rdf_core::{
2    Rdf,
3    query::{QueryResultFormat, QuerySolutions},
4};
5
6/// Trait for RDF graphs that support SPARQL query execution.
7///
8/// This trait extends [`Rdf`] with methods for executing SPARQL queries against
9/// the RDF graph. SPARQL is the W3C standard query language for RDF, providing
10/// pattern matching, filtering, and graph construction capabilities.
11///
12/// # Query Forms
13///
14/// SPARQL defines four query forms, three of which are supported by this trait:
15///
16/// - **SELECT**: Pattern matching that returns variable bindings as a table
17/// - **CONSTRUCT**: Pattern matching that builds a new RDF graph from templates
18/// - **ASK**: Boolean query testing whether a pattern exists in the graph
19/// - **DESCRIBE** (not yet supported): Retrieves RDF data about resources
20pub trait QueryRDF: Rdf {
21    /// Executes a SPARQL SELECT query and returns variable bindings.
22    ///
23    /// SELECT queries match patterns in the RDF graph and return a table-like
24    /// structure containing variable bindings for each match. This is the most
25    /// common SPARQL query form, analogous to SELECT in SQL.
26    ///
27    /// # Arguments
28    ///
29    /// * `query` - A SPARQL SELECT query string
30    fn query_select(&self, query: &str) -> Result<QuerySolutions<Self>, Self::Err>
31    where
32        Self: Sized;
33
34    /// Executes a SPARQL CONSTRUCT query and returns a serialized RDF graph.
35    ///
36    /// CONSTRUCT queries match patterns in the RDF graph and use those matches
37    /// to build a new RDF graph according to a template. The resulting graph
38    /// is serialized to a string in the specified format.
39    ///
40    /// # Arguments
41    ///
42    /// * `query` - A SPARQL CONSTRUCT query string
43    /// * `result_format` - The serialization format for the resulting RDF graph
44    ///   (e.g., Turtle, RDF/XML, N-Triples)
45    fn query_construct(&self, query: &str, result_format: &QueryResultFormat) -> Result<String, Self::Err>
46    where
47        Self: Sized;
48
49    /// Executes a SPARQL ASK query and returns a boolean result.
50    ///
51    /// ASK queries test whether a pattern exists in the RDF graph. They return
52    /// `true` if at least one solution to the query pattern exists, `false`
53    /// otherwise. This is more efficient than SELECT when only existence needs
54    /// to be checked.
55    ///
56    /// # Arguments
57    ///
58    /// * `query` - A SPARQL ASK query string
59    fn query_ask(&self, query: &str) -> Result<bool, Self::Err>;
60}