use crate::error::{Error, Result};
use crate::transaction::Transaction;
use graphlite::{QueryCoordinator, QueryResult};
use std::sync::Arc;
pub struct GraphLite {
coordinator: Arc<QueryCoordinator>,
}
impl GraphLite {
pub fn open<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
let coordinator = QueryCoordinator::from_path(path)
.map_err(|e| Error::Connection(format!("Failed to open database: {}", e)))?;
Ok(GraphLite { coordinator })
}
pub fn session(&self, username: &str) -> Result<Session> {
let session_id = self
.coordinator
.create_simple_session(username)
.map_err(|e| Error::Session(format!("Failed to create session: {}", e)))?;
Ok(Session {
id: session_id,
coordinator: self.coordinator.clone(),
username: username.to_string(),
})
}
pub fn coordinator(&self) -> &QueryCoordinator {
&self.coordinator
}
}
pub struct Session {
id: String,
coordinator: Arc<QueryCoordinator>,
username: String,
}
impl Session {
pub fn id(&self) -> &str {
&self.id
}
pub fn username(&self) -> &str {
&self.username
}
pub fn query(&self, query: &str) -> Result<QueryResult> {
self.coordinator
.process_query(query, &self.id)
.map_err(|e| Error::Query(format!("Query failed: {}", e)))
}
pub fn execute(&self, statement: &str) -> Result<()> {
self.coordinator
.process_query(statement, &self.id)
.map_err(|e| Error::Query(format!("Execute failed: {}", e)))?;
Ok(())
}
pub fn transaction(&self) -> Result<Transaction<'_>> {
Transaction::begin(self)
}
pub(crate) fn coordinator(&self) -> &QueryCoordinator {
&self.coordinator
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_connection_types_compile() {
}
}