Skip to main content

sqlitegraph_cli/
client.rs

1use sqlitegraph::backend::GraphBackend;
2use sqlitegraph::backend::SqliteGraphBackend;
3
4/// CLI backend client supporting SQLite and V3 backends
5pub enum CliClient {
6    Sqlite(SqliteGraphBackend),
7    #[cfg(feature = "native-v3")]
8    V3(sqlitegraph::backend::native::v3::V3Backend),
9}
10
11impl CliClient {
12    /// Open database with specified backend
13    pub fn open(backend: super::cli::BackendType, path: &std::path::Path) -> anyhow::Result<Self> {
14        match backend {
15            super::cli::BackendType::Sqlite => {
16                let graph = sqlitegraph::SqliteGraph::open(path)?;
17                Ok(Self::Sqlite(SqliteGraphBackend::from_graph(graph)))
18            }
19            #[cfg(feature = "native-v3")]
20            super::cli::BackendType::V3 => {
21                use sqlitegraph::backend::native::v3::V3Backend;
22                let backend = if path.exists() {
23                    V3Backend::open(path)?
24                } else {
25                    V3Backend::create(path)?
26                };
27                Ok(Self::V3(backend))
28            }
29        }
30    }
31
32    /// Open in-memory database
33    pub fn open_in_memory(backend: super::cli::BackendType) -> anyhow::Result<Self> {
34        match backend {
35            super::cli::BackendType::Sqlite => {
36                let graph = sqlitegraph::SqliteGraph::open_in_memory()?;
37                Ok(Self::Sqlite(SqliteGraphBackend::from_graph(graph)))
38            }
39            #[cfg(feature = "native-v3")]
40            super::cli::BackendType::V3 => {
41                anyhow::bail!("V3 backend does not support in-memory mode")
42            }
43        }
44    }
45
46    /// Get backend trait object
47    pub fn backend(&self) -> &dyn GraphBackend {
48        match self {
49            Self::Sqlite(b) => b,
50            #[cfg(feature = "native-v3")]
51            Self::V3(b) => b,
52        }
53    }
54
55    /// Get SQLite graph reference (if SQLite backend)
56    pub fn sqlite_graph(&self) -> Option<&sqlitegraph::SqliteGraph> {
57        match self {
58            Self::Sqlite(b) => Some(b.graph()),
59            #[cfg(feature = "native-v3")]
60            Self::V3(_) => None,
61        }
62    }
63
64    /// Get backend name
65    pub fn backend_name(&self) -> &'static str {
66        match self {
67            Self::Sqlite(_) => "sqlite",
68            #[cfg(feature = "native-v3")]
69            Self::V3(_) => "v3",
70        }
71    }
72
73    /// Get node count
74    pub fn node_count(&self) -> anyhow::Result<usize> {
75        let ids = self.backend().entity_ids()?;
76        Ok(ids.len())
77    }
78}