|
|
@ -41,23 +41,21 @@ impl QueryResults { |
|
|
|
/// let ex = NamedNodeRef::new("http://example.com")?;
|
|
|
|
/// let ex = NamedNodeRef::new("http://example.com")?;
|
|
|
|
/// store.insert(QuadRef::new(ex, ex, ex, GraphNameRef::DefaultGraph))?;
|
|
|
|
/// store.insert(QuadRef::new(ex, ex, ex, GraphNameRef::DefaultGraph))?;
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// let mut results = Vec::new();
|
|
|
|
/// let results = store.query("SELECT ?s WHERE { ?s ?p ?o }")?;
|
|
|
|
/// store.query("SELECT ?s WHERE { ?s ?p ?o }")?.write(&mut results, QueryResultsFormat::Json)?;
|
|
|
|
/// assert_eq!(
|
|
|
|
/// assert_eq!(results, r#"{"head":{"vars":["s"]},"results":{"bindings":[{"s":{"type":"uri","value":"http://example.com"}}]}}"#.as_bytes());
|
|
|
|
/// results.write(Vec::new(), QueryResultsFormat::Json)?,
|
|
|
|
|
|
|
|
/// r#"{"head":{"vars":["s"]},"results":{"bindings":[{"s":{"type":"uri","value":"http://example.com"}}]}}"#.as_bytes()
|
|
|
|
|
|
|
|
/// );
|
|
|
|
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
|
|
|
|
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
pub fn write( |
|
|
|
pub fn write<W: Write>( |
|
|
|
self, |
|
|
|
self, |
|
|
|
write: impl Write, |
|
|
|
write: W, |
|
|
|
format: QueryResultsFormat, |
|
|
|
format: QueryResultsFormat, |
|
|
|
) -> Result<(), EvaluationError> { |
|
|
|
) -> Result<W, EvaluationError> { |
|
|
|
let serializer = QueryResultsSerializer::from_format(format); |
|
|
|
let serializer = QueryResultsSerializer::from_format(format); |
|
|
|
match self { |
|
|
|
match self { |
|
|
|
Self::Boolean(value) => { |
|
|
|
Self::Boolean(value) => serializer.serialize_boolean_to_write(write, value), |
|
|
|
serializer |
|
|
|
|
|
|
|
.serialize_boolean_to_write(write, value) |
|
|
|
|
|
|
|
.map_err(EvaluationError::ResultsSerialization)?; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
Self::Solutions(solutions) => { |
|
|
|
Self::Solutions(solutions) => { |
|
|
|
let mut writer = serializer |
|
|
|
let mut writer = serializer |
|
|
|
.serialize_solutions_to_write(write, solutions.variables().to_vec()) |
|
|
|
.serialize_solutions_to_write(write, solutions.variables().to_vec()) |
|
|
@ -67,9 +65,7 @@ impl QueryResults { |
|
|
|
.write(&solution?) |
|
|
|
.write(&solution?) |
|
|
|
.map_err(EvaluationError::ResultsSerialization)?; |
|
|
|
.map_err(EvaluationError::ResultsSerialization)?; |
|
|
|
} |
|
|
|
} |
|
|
|
writer |
|
|
|
writer.finish() |
|
|
|
.finish() |
|
|
|
|
|
|
|
.map_err(EvaluationError::ResultsSerialization)?; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
Self::Graph(triples) => { |
|
|
|
Self::Graph(triples) => { |
|
|
|
let s = VariableRef::new_unchecked("subject"); |
|
|
|
let s = VariableRef::new_unchecked("subject"); |
|
|
@ -91,12 +87,10 @@ impl QueryResults { |
|
|
|
]) |
|
|
|
]) |
|
|
|
.map_err(EvaluationError::ResultsSerialization)?; |
|
|
|
.map_err(EvaluationError::ResultsSerialization)?; |
|
|
|
} |
|
|
|
} |
|
|
|
writer |
|
|
|
writer.finish() |
|
|
|
.finish() |
|
|
|
|
|
|
|
.map_err(EvaluationError::ResultsSerialization)?; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
Ok(()) |
|
|
|
.map_err(EvaluationError::ResultsSerialization) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Writes the graph query results.
|
|
|
|
/// Writes the graph query results.
|
|
|
@ -118,18 +112,18 @@ impl QueryResults { |
|
|
|
/// None,
|
|
|
|
/// None,
|
|
|
|
/// )?;
|
|
|
|
/// )?;
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// let mut results = Vec::new();
|
|
|
|
/// let results = store.query("CONSTRUCT WHERE { ?s ?p ?o }")?;
|
|
|
|
/// store
|
|
|
|
/// assert_eq!(
|
|
|
|
/// .query("CONSTRUCT WHERE { ?s ?p ?o }")?
|
|
|
|
/// results.write_graph(Vec::new(), RdfFormat::NTriples)?,
|
|
|
|
/// .write_graph(&mut results, RdfFormat::NTriples)?;
|
|
|
|
/// graph.as_bytes()
|
|
|
|
/// assert_eq!(results, graph.as_bytes());
|
|
|
|
/// );
|
|
|
|
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
|
|
|
|
/// # Result::<_,Box<dyn std::error::Error>>::Ok(())
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
pub fn write_graph( |
|
|
|
pub fn write_graph<W: Write>( |
|
|
|
self, |
|
|
|
self, |
|
|
|
write: impl Write, |
|
|
|
write: W, |
|
|
|
format: impl Into<RdfFormat>, |
|
|
|
format: impl Into<RdfFormat>, |
|
|
|
) -> Result<(), EvaluationError> { |
|
|
|
) -> Result<W, EvaluationError> { |
|
|
|
if let Self::Graph(triples) = self { |
|
|
|
if let Self::Graph(triples) = self { |
|
|
|
let mut writer = RdfSerializer::from_format(format.into()).serialize_to_write(write); |
|
|
|
let mut writer = RdfSerializer::from_format(format.into()).serialize_to_write(write); |
|
|
|
for triple in triples { |
|
|
|
for triple in triples { |
|
|
@ -139,8 +133,7 @@ impl QueryResults { |
|
|
|
} |
|
|
|
} |
|
|
|
writer |
|
|
|
writer |
|
|
|
.finish() |
|
|
|
.finish() |
|
|
|
.map_err(EvaluationError::ResultsSerialization)?; |
|
|
|
.map_err(EvaluationError::ResultsSerialization) |
|
|
|
Ok(()) |
|
|
|
|
|
|
|
} else { |
|
|
|
} else { |
|
|
|
Err(EvaluationError::NotAGraph) |
|
|
|
Err(EvaluationError::NotAGraph) |
|
|
|
} |
|
|
|
} |
|
|
|