use std::collections::HashMap;
use std::path::Path;
pub use marsdb_graph::PropertyValue;
pub use marsdb_query::{Literal, PathElem, QueryResult, Value};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("graph error: {0}")]
Graph(#[from] marsdb_graph::GraphError),
#[error("query error: {0}")]
Query(#[from] marsdb_query::QueryError),
}
pub struct Database {
store: marsdb_graph::GraphStore,
}
impl Database {
pub fn open(path: impl AsRef<Path>) -> Result<Self, Error> {
Ok(Self {
store: marsdb_graph::GraphStore::open_file(path)?,
})
}
pub fn in_memory() -> Result<Self, Error> {
Ok(Self {
store: marsdb_graph::GraphStore::open_memory()?,
})
}
pub fn execute(&self, cypher: &str) -> Result<QueryResult, Error> {
self.execute_with_params(cypher, &HashMap::new())
}
pub fn execute_with_params(
&self,
cypher: &str,
params: &HashMap<String, PropertyValue>,
) -> Result<QueryResult, Error> {
let mut stmt = marsdb_query::parse(cypher)?;
marsdb_query::substitute_params(&mut stmt, params)?;
let result = marsdb_query::Executor::new(&self.store).execute(&stmt)?;
Ok(result)
}
pub fn execute_batch(&self, cypher: &str) -> Result<Vec<QueryResult>, Error> {
let stmts = marsdb_query::parse_many(cypher)?;
let executor = marsdb_query::Executor::new(&self.store);
stmts.iter().map(|stmt| Ok(executor.execute(stmt)?)).collect()
}
}