pub struct Query { /* private fields */ }Expand description
A SPARQL query configured before execution (ADR-009).
§Examples
ASK:
use oxiland::terms::{self, Literal, Triple};
use oxiland::{Model, Query, QueryResults};
let model = Model::new()?;
model.add(Triple::new(
terms::named_node("https://example.com/alice")?,
terms::named_node("https://example.com/name")?,
Literal::new_simple_literal("Alice"),
))?;
let results = Query::new("ASK { ?s ?p ?o }").execute(&model)?;
assert!(matches!(results, QueryResults::Boolean(true)));SELECT with limit:
use oxiland::terms::{self, Literal, Triple};
use oxiland::{Model, Query, QueryResults};
let model = Model::new()?;
model.add(Triple::new(
terms::named_node("https://example.com/alice")?,
terms::named_node("https://example.com/name")?,
Literal::new_simple_literal("Alice"),
))?;
let results = Query::new(
"SELECT ?name WHERE { <https://example.com/alice> <https://example.com/name> ?name }",
)
.limit(1)?
.execute(&model)?;
if let QueryResults::Solutions(mut solutions) = results {
let solution = solutions
.next()
.expect("one row")
.map_err(|error| oxiland::Error::SparqlEvaluation(error.to_string()))?;
assert!(solution.get("name").is_some());
}Implementations§
Source§impl Query
impl Query
Sourcepub fn base_iri(self, base_iri: impl Into<String>) -> Result<Self>
pub fn base_iri(self, base_iri: impl Into<String>) -> Result<Self>
Sets the base IRI used while parsing the query.
Invalid IRIs return Error::InvalidRdf.
Sourcepub fn prefix(
self,
prefix_name: impl Into<String>,
prefix_iri: impl Into<String>,
) -> Result<Self>
pub fn prefix( self, prefix_name: impl Into<String>, prefix_iri: impl Into<String>, ) -> Result<Self>
Adds a default IRI prefix used while parsing the query.
Invalid IRIs return Error::InvalidRdf.
Sourcepub fn limit(self, limit: usize) -> Result<Self>
pub fn limit(self, limit: usize) -> Result<Self>
Sets an API-level result limit (algebra Slice; ADR-009).
Replaces any in-query LIMIT/OFFSET slice on SELECT/CONSTRUCT/DESCRIBE.
ASK queries return Error::Unsupported here (not only at execute).
Sourcepub fn offset(self, offset: usize) -> Result<Self>
pub fn offset(self, offset: usize) -> Result<Self>
Sets an API-level result offset (algebra Slice; ADR-009).
Replaces any in-query LIMIT/OFFSET slice on SELECT/CONSTRUCT/DESCRIBE.
ASK queries return Error::Unsupported here (not only at execute).
Sourcepub fn default_graph(self, graphs: impl IntoIterator<Item = GraphName>) -> Self
pub fn default_graph(self, graphs: impl IntoIterator<Item = GraphName>) -> Self
Restricts the query default graph to the given graph names.
An empty list selects an empty default dataset (no triples match from the default graph).
Sourcepub fn default_graph_as_union(self) -> Self
pub fn default_graph_as_union(self) -> Self
Treats the union of all named graphs as the default graph.
Sourcepub fn available_named_graphs(
self,
graphs: impl IntoIterator<Item = NamedOrBlankNode>,
) -> Self
pub fn available_named_graphs( self, graphs: impl IntoIterator<Item = NamedOrBlankNode>, ) -> Self
Restricts available named graphs for the query dataset.
Sourcepub fn cancellation_token(self, token: CancellationToken) -> Self
pub fn cancellation_token(self, token: CancellationToken) -> Self
Attaches a cooperative cancellation token (ADR-012).
Wall-clock timeouts are caller-driven: cancel the token from another thread when a deadline expires.
Sourcepub fn execute<'a>(&self, model: &'a Model) -> Result<QueryResults<'a>>
pub fn execute<'a>(&self, model: &'a Model) -> Result<QueryResults<'a>>
Parses and executes the query against a model.
Parse failures return Error::SparqlParse. Evaluation failures return
Error::SparqlEvaluation. Invalid configured base/prefix IRIs return
Error::InvalidRdf.