persistence.rs

 1use anyhow::Result;
 2use collections::HashMap;
 3use std::path::{Path, PathBuf};
 4use workspace::WorkspaceDb;
 5
 6use db::sqlez_macros::sql;
 7use db::{define_connection, query};
 8
 9define_connection!(
10    pub static ref DB: ZetaDb<WorkspaceDb> = &[
11        sql! (
12            CREATE TABLE zeta_preferences(
13                worktree_path BLOB NOT NULL PRIMARY KEY,
14                accepted_data_collection INTEGER
15            ) STRICT;
16        ),
17    ];
18);
19
20impl ZetaDb {
21    pub fn get_all_zeta_preferences(&self) -> Result<HashMap<PathBuf, bool>> {
22        Ok(self.get_all_zeta_preferences_query()?.into_iter().collect())
23    }
24
25    query! {
26        fn get_all_zeta_preferences_query() -> Result<Vec<(PathBuf, bool)>> {
27            SELECT worktree_path, accepted_data_collection FROM zeta_preferences
28        }
29    }
30
31    query! {
32        pub fn get_accepted_data_collection(worktree_path: &Path) -> Result<Option<bool>> {
33            SELECT accepted_data_collection FROM zeta_preferences
34            WHERE worktree_path = ?
35        }
36    }
37
38    query! {
39        pub async fn save_accepted_data_collection(worktree_path: PathBuf, accepted_data_collection: bool) -> Result<()> {
40            INSERT INTO zeta_preferences
41                (worktree_path, accepted_data_collection)
42            VALUES
43                (?1, ?2)
44            ON CONFLICT (worktree_path) DO UPDATE SET
45                accepted_data_collection = ?2
46        }
47    }
48
49    query! {
50        pub async fn clear_all_zeta_preferences() -> Result<()> {
51            DELETE FROM zeta_preferences
52        }
53    }
54}