persistence.rs

 1use std::path::{Path, PathBuf};
 2
 3use anyhow::{Context, Result};
 4use db::connection;
 5use indoc::indoc;
 6use lazy_static::lazy_static;
 7use sqlez::domain::Domain;
 8use workspace::{ItemId, Workspace, WorkspaceId};
 9
10use crate::Editor;
11
12connection!(DB: EditorDb<(Workspace, Editor)>);
13
14impl Domain for Editor {
15    fn name() -> &'static str {
16        "editor"
17    }
18
19    fn migrations() -> &'static [&'static str] {
20        &[indoc! {"
21            CREATE TABLE editors(
22                item_id INTEGER NOT NULL,
23                workspace_id BLOB NOT NULL,
24                path BLOB NOT NULL,
25                PRIMARY KEY(item_id, workspace_id)
26            ) STRICT;
27        "}]
28    }
29}
30
31impl EditorDb {
32    pub fn get_path(&self, item_id: ItemId, workspace_id: WorkspaceId) -> Result<PathBuf> {
33        self.select_row_bound(indoc! {"
34            SELECT path FROM editors 
35            WHERE item_id = ? AND workspace_id = ?"})?((item_id, &workspace_id))?
36        .context("Path not found for serialized editor")
37    }
38
39    pub fn save_path(
40        &self,
41        item_id: ItemId,
42        workspace_id: WorkspaceId,
43        path: PathBuf,
44    ) -> Result<()> {
45        self.exec_bound::<(ItemId, &WorkspaceId, &Path)>(indoc! {"
46            INSERT OR REPLACE INTO editors(item_id, workspace_id, path)
47            VALUES (?, ?, ?)"})?((item_id, &workspace_id, &path))
48    }
49}