mod batch;
mod bulk;
mod edges;
mod nodes;
mod queries;
pub use bulk::BulkInsertResult;
use crate::query_builder::CypherQuery;
use crate::{Connection, CypherResult, Result};
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GraphStats {
pub node_count: i64,
pub edge_count: i64,
}
pub struct Graph {
conn: Connection,
}
impl Graph {
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
let conn = Connection::open(path)?;
Ok(Graph { conn })
}
#[cfg(not(feature = "bundled-extension"))]
pub fn open_with_extension<P: AsRef<Path>, E: AsRef<Path>>(
path: P,
extension_path: E,
) -> Result<Self> {
let conn = Connection::open_with_extension(path, extension_path)?;
Ok(Graph { conn })
}
pub fn open_in_memory() -> Result<Self> {
let conn = Connection::open_in_memory()?;
Ok(Graph { conn })
}
pub fn from_connection(conn: Connection) -> Self {
Graph { conn }
}
pub fn connection(&self) -> &Connection {
&self.conn
}
pub fn query(&self, cypher: &str) -> Result<CypherResult> {
self.conn.cypher(cypher)
}
#[deprecated(since = "0.4.0", note = "Use query_builder() instead")]
pub fn query_with_params(
&self,
cypher: &str,
params: &serde_json::Value,
) -> Result<CypherResult> {
self.conn.execute_cypher_with_params(cypher, params)
}
pub fn query_builder<'a>(&'a self, cypher: &'a str) -> CypherQuery<'a> {
self.conn.cypher_builder(cypher)
}
pub fn query_params(
&self,
cypher: &str,
params: &[(&str, &serde_json::Value)],
) -> Result<CypherResult> {
let mut builder = self.conn.cypher_builder(cypher);
for (key, value) in params {
builder = builder.param(key, (*value).clone());
}
builder.run()
}
pub fn load_graph(&self) -> Result<CacheStatus> {
let json: String =
self.conn
.sqlite_connection()
.query_row("SELECT gql_load_graph()", [], |row| row.get(0))?;
let status: CacheStatus = serde_json::from_str(&json)?;
Ok(status)
}
pub fn unload_graph(&self) -> Result<CacheStatus> {
let json: String =
self.conn
.sqlite_connection()
.query_row("SELECT gql_unload_graph()", [], |row| row.get(0))?;
let status: CacheStatus = serde_json::from_str(&json)?;
Ok(status)
}
pub fn reload_graph(&self) -> Result<CacheStatus> {
let json: String =
self.conn
.sqlite_connection()
.query_row("SELECT gql_reload_graph()", [], |row| row.get(0))?;
let status: CacheStatus = serde_json::from_str(&json)?;
Ok(status)
}
pub fn graph_loaded(&self) -> Result<bool> {
let json: String =
self.conn
.sqlite_connection()
.query_row("SELECT gql_graph_loaded()", [], |row| row.get(0))?;
let status: CacheLoadedStatus = serde_json::from_str(&json)?;
Ok(status.loaded)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CacheStatus {
pub status: String,
#[serde(default, alias = "nodes")]
pub node_count: Option<i64>,
#[serde(default, alias = "edges")]
pub edge_count: Option<i64>,
}
#[derive(Debug, Clone, Deserialize)]
struct CacheLoadedStatus {
loaded: bool,
}
pub fn graph<P: AsRef<Path>>(path: P) -> Result<Graph> {
Graph::open(path)
}