Simplifies UpdateOptions API

Keeps only useful methods
pull/190/head
Tpt 3 years ago
parent 27f6954175
commit 34a944036f
  1. 29
      lib/src/sparql/mod.rs
  2. 40
      lib/src/store.rs

@ -109,6 +109,19 @@ pub(crate) fn evaluate_query(
/// ///
/// If the `"http_client"` optional feature is enabled, /// If the `"http_client"` optional feature is enabled,
/// a simple HTTP 1.1 client is used to execute [SPARQL 1.1 Federated Query](https://www.w3.org/TR/sparql11-federated-query/) SERVICE calls. /// a simple HTTP 1.1 client is used to execute [SPARQL 1.1 Federated Query](https://www.w3.org/TR/sparql11-federated-query/) SERVICE calls.
///
/// Usage example disabling the federated query support:
/// ```
/// use oxigraph::store::Store;
/// use oxigraph::sparql::QueryOptions;
///
/// let store = Store::new()?;
/// store.query_opt(
/// "SELECT * WHERE { SERVICE <https://query.wikidata.org/sparql> {} }",
/// QueryOptions::default().without_service_handler()
/// )?;
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ```
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct QueryOptions { pub struct QueryOptions {
service_handler: Option<Rc<dyn ServiceHandler<Error = EvaluationError>>>, service_handler: Option<Rc<dyn ServiceHandler<Error = EvaluationError>>>,
@ -186,26 +199,12 @@ impl QueryOptions {
} }
} }
/// Options for SPARQL update evaluation /// Options for SPARQL update evaluation.
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct UpdateOptions { pub struct UpdateOptions {
query_options: QueryOptions, query_options: QueryOptions,
} }
impl UpdateOptions {
/// The options related to the querying part of the updates
#[inline]
pub fn query_options(&self) -> &QueryOptions {
&self.query_options
}
/// The options related to the querying part of the updates
#[inline]
pub fn query_options_mut(&mut self) -> &mut QueryOptions {
&mut self.query_options
}
}
impl From<QueryOptions> for UpdateOptions { impl From<QueryOptions> for UpdateOptions {
#[inline] #[inline]
fn from(query_options: QueryOptions) -> Self { fn from(query_options: QueryOptions) -> Self {

@ -129,6 +129,26 @@ impl Store {
} }
/// Executes a [SPARQL 1.1 query](https://www.w3.org/TR/sparql11-query/) with some options. /// Executes a [SPARQL 1.1 query](https://www.w3.org/TR/sparql11-query/) with some options.
///
///
/// Usage example with a custom function serializing terms to N-Triples:
/// ```
/// use oxigraph::store::Store;
/// use oxigraph::model::*;
/// use oxigraph::sparql::{QueryOptions, QueryResults};
///
/// let store = Store::new()?;
/// if let QueryResults::Solutions(mut solutions) = store.query_opt(
/// "SELECT (<http://www.w3.org/ns/formats/N-Triples>(1) AS ?nt) WHERE {}",
/// QueryOptions::default().with_custom_function(
/// NamedNode::new("http://www.w3.org/ns/formats/N-Triples")?,
/// |args| args.get(0).map(|t| Literal::from(t.to_string()).into())
/// )
/// )? {
/// assert_eq!(solutions.next().unwrap()?.get("nt"), Some(&Literal::from("\"1\"^^<http://www.w3.org/2001/XMLSchema#integer>").into()));
/// }
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ```
pub fn query_opt( pub fn query_opt(
&self, &self,
query: impl TryInto<Query, Error = impl Into<EvaluationError>>, query: impl TryInto<Query, Error = impl Into<EvaluationError>>,
@ -226,15 +246,31 @@ impl Store {
} }
/// Executes a [SPARQL 1.1 update](https://www.w3.org/TR/sparql11-update/) with some options. /// Executes a [SPARQL 1.1 update](https://www.w3.org/TR/sparql11-update/) with some options.
///
/// ```
/// use oxigraph::store::Store;
/// use oxigraph::model::*;
/// use oxigraph::sparql::QueryOptions;
///
/// let store = Store::new()?;
/// store.update_opt(
/// "INSERT { ?s <http://example.com/n-triples-representation> ?n } WHERE { ?s ?p ?o BIND(<http://www.w3.org/ns/formats/N-Triples>(?s) AS ?nt) }",
/// QueryOptions::default().with_custom_function(
/// NamedNode::new("http://www.w3.org/ns/formats/N-Triples")?,
/// |args| args.get(0).map(|t| Literal::from(t.to_string()).into())
/// )
/// )?;
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
/// ```
pub fn update_opt( pub fn update_opt(
&self, &self,
update: impl TryInto<Update, Error = impl Into<EvaluationError>>, update: impl TryInto<Update, Error = impl Into<EvaluationError>>,
options: UpdateOptions, options: impl Into<UpdateOptions>,
) -> Result<(), EvaluationError> { ) -> Result<(), EvaluationError> {
evaluate_update( evaluate_update(
&self.storage, &self.storage,
update.try_into().map_err(std::convert::Into::into)?, update.try_into().map_err(std::convert::Into::into)?,
options, options.into(),
) )
} }

Loading…
Cancel
Save