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            ) STRICT;
27        )]
28    }
29}
30
31impl EditorDb {
32    query! {
33        pub fn get_path(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<PathBuf>> {
34            SELECT path FROM editors
35            WHERE item_id = ? AND workspace_id = ?
36        }
37    }
38
39    query! {
40        pub async fn save_path(item_id: ItemId, workspace_id: WorkspaceId, path: PathBuf) -> Result<()> {
41            INSERT OR REPLACE INTO editors(item_id, workspace_id, path)
42            VALUES (?, ?, ?)
43        }
44    }
45}