persistence.rs

 1use std::path::{Path, PathBuf};
 2
 3use db::{connection, sql_method};
 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    sql_method! {
35        get_path(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<PathBuf>>:
36            indoc! {"
37                SELECT path FROM editors 
38                WHERE item_id = ? AND workspace_id = ?"}
39    }
40
41    sql_method! {
42        save_path(item_id: ItemId, workspace_id: WorkspaceId, path: &Path) -> Result<()>:
43            indoc! {"
44                INSERT OR REPLACE INTO editors(item_id, workspace_id, path)
45                VALUES (?, ?, ?)"}
46    }
47}