zed/crates/editor/src/persistence.rs
Mikayla Maki 3e0f9d27a7 Made dev tools not break everything about the db
Also improved multi statements to allow out of order parameter binding in statements
Ensured that all statements are run for maybe_row and single, and that of all statements only 1 of them returns only 1 row
Made bind and column calls add useful context to errors

Co-authored-by: kay@zed.dev
2022-12-03 16:06:01 -08:00

47 lines
1.3 KiB
Rust

use std::path::{Path, PathBuf};
use db::{connection, sql_method};
use indoc::indoc;
use sqlez::domain::Domain;
use workspace::{ItemId, Workspace, WorkspaceId};
use crate::Editor;
connection!(DB: EditorDb<(Workspace, Editor)>);
impl Domain for Editor {
fn name() -> &'static str {
"editor"
}
fn migrations() -> &'static [&'static str] {
&[indoc! {"
CREATE TABLE editors(
item_id INTEGER NOT NULL,
workspace_id INTEGER NOT NULL,
path BLOB NOT NULL,
PRIMARY KEY(item_id, workspace_id),
FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
ON DELETE CASCADE
ON UPDATE CASCADE
) STRICT;
"}]
}
}
impl EditorDb {
sql_method! {
get_path(item_id: ItemId, workspace_id: WorkspaceId) -> Result<PathBuf>:
indoc! {"
SELECT path FROM editors
WHERE item_id = ? AND workspace_id = ?"}
}
sql_method! {
save_path(item_id: ItemId, workspace_id: WorkspaceId, path: &Path) -> Result<()>:
indoc! {"
INSERT OR REPLACE INTO editors(item_id, workspace_id, path)
VALUES (?, ?, ?)"}
}
}