persistence.rs

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