|
|
@ -31,9 +31,9 @@ pub trait PreparedQuery { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// An implementation of `PreparedQuery` for internal use
|
|
|
|
/// An implementation of `PreparedQuery` for internal use
|
|
|
|
pub struct SimplePreparedQuery<S: StoreConnection>(SimplePreparedQueryOptions<S>); |
|
|
|
pub struct SimplePreparedQuery<S: StoreConnection>(SimplePreparedQueryAction<S>); |
|
|
|
|
|
|
|
|
|
|
|
enum SimplePreparedQueryOptions<S: StoreConnection> { |
|
|
|
enum SimplePreparedQueryAction<S: StoreConnection> { |
|
|
|
Select { |
|
|
|
Select { |
|
|
|
plan: PlanNode, |
|
|
|
plan: PlanNode, |
|
|
|
variables: Vec<Variable>, |
|
|
|
variables: Vec<Variable>, |
|
|
@ -55,17 +55,17 @@ enum SimplePreparedQueryOptions<S: StoreConnection> { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl<S: StoreConnection> SimplePreparedQuery<S> { |
|
|
|
impl<S: StoreConnection> SimplePreparedQuery<S> { |
|
|
|
pub(crate) fn new(connection: S, query: &str, base_iri: Option<&str>) -> Result<Self> { |
|
|
|
pub(crate) fn new(connection: S, query: &str, options: QueryOptions) -> Result<Self> { |
|
|
|
let dataset = DatasetView::new(connection); |
|
|
|
let dataset = DatasetView::new(connection, options.default_graph_as_union); |
|
|
|
//TODO avoid inserting terms in the Repository StringStore
|
|
|
|
//TODO avoid inserting terms in the Repository StringStore
|
|
|
|
Ok(Self(match read_sparql_query(query, base_iri)? { |
|
|
|
Ok(Self(match read_sparql_query(query, options.base_iri)? { |
|
|
|
QueryVariants::Select { |
|
|
|
QueryVariants::Select { |
|
|
|
algebra, |
|
|
|
algebra, |
|
|
|
dataset: _, |
|
|
|
dataset: _, |
|
|
|
base_iri, |
|
|
|
base_iri, |
|
|
|
} => { |
|
|
|
} => { |
|
|
|
let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?; |
|
|
|
let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?; |
|
|
|
SimplePreparedQueryOptions::Select { |
|
|
|
SimplePreparedQueryAction::Select { |
|
|
|
plan, |
|
|
|
plan, |
|
|
|
variables, |
|
|
|
variables, |
|
|
|
evaluator: SimpleEvaluator::new(dataset, base_iri), |
|
|
|
evaluator: SimpleEvaluator::new(dataset, base_iri), |
|
|
@ -77,7 +77,7 @@ impl<S: StoreConnection> SimplePreparedQuery<S> { |
|
|
|
base_iri, |
|
|
|
base_iri, |
|
|
|
} => { |
|
|
|
} => { |
|
|
|
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?; |
|
|
|
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?; |
|
|
|
SimplePreparedQueryOptions::Ask { |
|
|
|
SimplePreparedQueryAction::Ask { |
|
|
|
plan, |
|
|
|
plan, |
|
|
|
evaluator: SimpleEvaluator::new(dataset, base_iri), |
|
|
|
evaluator: SimpleEvaluator::new(dataset, base_iri), |
|
|
|
} |
|
|
|
} |
|
|
@ -89,7 +89,7 @@ impl<S: StoreConnection> SimplePreparedQuery<S> { |
|
|
|
base_iri, |
|
|
|
base_iri, |
|
|
|
} => { |
|
|
|
} => { |
|
|
|
let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?; |
|
|
|
let (plan, variables) = PlanBuilder::build(dataset.encoder(), &algebra)?; |
|
|
|
SimplePreparedQueryOptions::Construct { |
|
|
|
SimplePreparedQueryAction::Construct { |
|
|
|
plan, |
|
|
|
plan, |
|
|
|
construct: PlanBuilder::build_graph_template( |
|
|
|
construct: PlanBuilder::build_graph_template( |
|
|
|
dataset.encoder(), |
|
|
|
dataset.encoder(), |
|
|
@ -105,7 +105,7 @@ impl<S: StoreConnection> SimplePreparedQuery<S> { |
|
|
|
base_iri, |
|
|
|
base_iri, |
|
|
|
} => { |
|
|
|
} => { |
|
|
|
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?; |
|
|
|
let (plan, _) = PlanBuilder::build(dataset.encoder(), &algebra)?; |
|
|
|
SimplePreparedQueryOptions::Describe { |
|
|
|
SimplePreparedQueryAction::Describe { |
|
|
|
plan, |
|
|
|
plan, |
|
|
|
evaluator: SimpleEvaluator::new(dataset, base_iri), |
|
|
|
evaluator: SimpleEvaluator::new(dataset, base_iri), |
|
|
|
} |
|
|
|
} |
|
|
@ -117,26 +117,55 @@ impl<S: StoreConnection> SimplePreparedQuery<S> { |
|
|
|
impl<S: StoreConnection> PreparedQuery for SimplePreparedQuery<S> { |
|
|
|
impl<S: StoreConnection> PreparedQuery for SimplePreparedQuery<S> { |
|
|
|
fn exec(&self) -> Result<QueryResult<'_>> { |
|
|
|
fn exec(&self) -> Result<QueryResult<'_>> { |
|
|
|
match &self.0 { |
|
|
|
match &self.0 { |
|
|
|
SimplePreparedQueryOptions::Select { |
|
|
|
SimplePreparedQueryAction::Select { |
|
|
|
plan, |
|
|
|
plan, |
|
|
|
variables, |
|
|
|
variables, |
|
|
|
evaluator, |
|
|
|
evaluator, |
|
|
|
} => evaluator.evaluate_select_plan(&plan, &variables), |
|
|
|
} => evaluator.evaluate_select_plan(&plan, &variables), |
|
|
|
SimplePreparedQueryOptions::Ask { plan, evaluator } => { |
|
|
|
SimplePreparedQueryAction::Ask { plan, evaluator } => { |
|
|
|
evaluator.evaluate_ask_plan(&plan) |
|
|
|
evaluator.evaluate_ask_plan(&plan) |
|
|
|
} |
|
|
|
} |
|
|
|
SimplePreparedQueryOptions::Construct { |
|
|
|
SimplePreparedQueryAction::Construct { |
|
|
|
plan, |
|
|
|
plan, |
|
|
|
construct, |
|
|
|
construct, |
|
|
|
evaluator, |
|
|
|
evaluator, |
|
|
|
} => evaluator.evaluate_construct_plan(&plan, &construct), |
|
|
|
} => evaluator.evaluate_construct_plan(&plan, &construct), |
|
|
|
SimplePreparedQueryOptions::Describe { plan, evaluator } => { |
|
|
|
SimplePreparedQueryAction::Describe { plan, evaluator } => { |
|
|
|
evaluator.evaluate_describe_plan(&plan) |
|
|
|
evaluator.evaluate_describe_plan(&plan) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Options for SPARQL query parsing and evaluation like the query base IRI
|
|
|
|
|
|
|
|
pub struct QueryOptions<'a> { |
|
|
|
|
|
|
|
pub(crate) base_iri: Option<&'a str>, |
|
|
|
|
|
|
|
pub(crate) default_graph_as_union: bool, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> Default for QueryOptions<'a> { |
|
|
|
|
|
|
|
fn default() -> Self { |
|
|
|
|
|
|
|
Self { |
|
|
|
|
|
|
|
base_iri: None, |
|
|
|
|
|
|
|
default_graph_as_union: false, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
impl<'a> QueryOptions<'a> { |
|
|
|
|
|
|
|
/// Allows to set the base IRI of the query
|
|
|
|
|
|
|
|
pub fn with_base_iri(mut self, base_iri: &'a str) -> Self { |
|
|
|
|
|
|
|
self.base_iri = Some(base_iri); |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Consider the union of all graphs in the repository as the default graph
|
|
|
|
|
|
|
|
pub fn with_default_graph_as_union(mut self) -> Self { |
|
|
|
|
|
|
|
self.default_graph_as_union = true; |
|
|
|
|
|
|
|
self |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// A parsed [SPARQL query](https://www.w3.org/TR/sparql11-query/)
|
|
|
|
/// A parsed [SPARQL query](https://www.w3.org/TR/sparql11-query/)
|
|
|
|
#[derive(Eq, PartialEq, Debug, Clone, Hash)] |
|
|
|
#[derive(Eq, PartialEq, Debug, Clone, Hash)] |
|
|
|
pub struct Query(QueryVariants); |
|
|
|
pub struct Query(QueryVariants); |
|
|
|