2022-11-19 23:14:13 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
2022-11-21 18:38:16 +00:00
|
|
|
use db::{connection, indoc, sql_method, sqlez::domain::Domain};
|
2022-11-21 06:41:10 +00:00
|
|
|
|
2022-11-19 23:14:13 +00:00
|
|
|
use workspace::{ItemId, Workspace, WorkspaceId};
|
|
|
|
|
|
|
|
use crate::Terminal;
|
|
|
|
|
|
|
|
connection!(TERMINAL_CONNECTION: TerminalDb<(Workspace, Terminal)>);
|
|
|
|
|
|
|
|
impl Domain for Terminal {
|
|
|
|
fn name() -> &'static str {
|
|
|
|
"terminal"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn migrations() -> &'static [&'static str] {
|
|
|
|
&[indoc! {"
|
|
|
|
CREATE TABLE terminals (
|
2022-11-21 07:44:30 +00:00
|
|
|
workspace_id INTEGER,
|
2022-11-21 06:41:10 +00:00
|
|
|
item_id INTEGER,
|
2022-11-19 23:14:13 +00:00
|
|
|
working_directory BLOB,
|
2022-11-21 06:41:10 +00:00
|
|
|
PRIMARY KEY(workspace_id, item_id),
|
2022-11-19 23:14:13 +00:00
|
|
|
FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
|
|
|
|
ON DELETE CASCADE
|
|
|
|
) STRICT;
|
|
|
|
"}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TerminalDb {
|
2022-11-21 18:38:16 +00:00
|
|
|
sql_method! {
|
2022-11-21 21:42:26 +00:00
|
|
|
save_working_directory(item_id: ItemId,
|
|
|
|
workspace_id: WorkspaceId,
|
|
|
|
working_directory: &Path) -> Result<()>:
|
|
|
|
indoc!{"
|
|
|
|
INSERT OR REPLACE INTO terminals(item_id, workspace_id, working_directory)
|
|
|
|
VALUES (?1, ?2, ?3)
|
|
|
|
"}
|
2022-11-21 18:38:16 +00:00
|
|
|
}
|
2022-11-19 23:14:13 +00:00
|
|
|
|
2022-11-21 18:38:16 +00:00
|
|
|
sql_method! {
|
|
|
|
get_working_directory(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<PathBuf>>:
|
2022-11-21 21:42:26 +00:00
|
|
|
indoc!{"
|
|
|
|
SELECT working_directory
|
|
|
|
FROM terminals
|
|
|
|
WHERE item_id = ? AND workspace_id = ?
|
|
|
|
"}
|
2022-11-21 18:38:16 +00:00
|
|
|
}
|
2022-11-19 23:14:13 +00:00
|
|
|
}
|