persistence.rs

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