perspt_store/store/
handle.rs1use super::*;
2
3use std::sync::Mutex;
4
5pub struct SessionStore {
7 pub(crate) conn: Mutex<Connection>,
8 database_path: PathBuf,
9}
10
11impl SessionStore {
12 pub fn new() -> Result<Self> {
14 let db_path = Self::default_db_path()?;
15 Self::open(&db_path)
16 }
17
18 pub fn open(path: &std::path::Path) -> Result<Self> {
20 let path = absolute_database_path(path)?;
21 if let Some(parent) = path.parent() {
23 std::fs::create_dir_all(parent)?;
24 }
25
26 let conn = Connection::open(&path).context("Failed to open DuckDB")?;
27 init_schema(&conn)?;
28
29 Ok(Self {
30 conn: Mutex::new(conn),
31 database_path: path,
32 })
33 }
34
35 pub fn open_read_only(path: &std::path::Path) -> Result<Self> {
41 let path = absolute_database_path(path)?;
42 let config = duckdb::Config::default()
43 .access_mode(duckdb::AccessMode::ReadOnly)
44 .context("Failed to configure DuckDB read-only mode")?;
45 let conn = Connection::open_with_flags(&path, config)
46 .context("Failed to open DuckDB in read-only mode")?;
47 Ok(Self {
48 conn: Mutex::new(conn),
49 database_path: path,
50 })
51 }
52
53 pub fn database_path(&self) -> &std::path::Path {
55 &self.database_path
56 }
57
58 pub fn default_db_path() -> Result<PathBuf> {
60 perspt_core::paths::database_path().context("Could not determine platform data directory")
61 }
62}
63
64fn absolute_database_path(path: &std::path::Path) -> Result<PathBuf> {
65 if path.is_absolute() {
66 Ok(path.to_path_buf())
67 } else {
68 Ok(std::env::current_dir()?.join(path))
69 }
70}