Added extra sql methods

This commit is contained in:
Mikayla Maki 2022-11-21 10:52:19 -08:00
parent 37174f45f0
commit 2dc1130902
3 changed files with 49 additions and 14 deletions

View file

@ -83,7 +83,7 @@ macro_rules! connection {
#[macro_export] #[macro_export]
macro_rules! sql_method { macro_rules! sql_method {
($id:ident() -> Result<()>: $sql:literal) => { ($id:ident() -> Result<()>: $sql:expr) => {
pub fn $id(&self) -> $crate::sqlez::anyhow::Result<()> { pub fn $id(&self) -> $crate::sqlez::anyhow::Result<()> {
use $crate::anyhow::Context; use $crate::anyhow::Context;
@ -94,7 +94,7 @@ macro_rules! sql_method {
)) ))
} }
}; };
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<()>: $sql:literal) => { ($id:ident($($arg:ident: $arg_type:ty),+) -> Result<()>: $sql:expr) => {
pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result<()> { pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result<()> {
use $crate::anyhow::Context; use $crate::anyhow::Context;
@ -106,7 +106,7 @@ macro_rules! sql_method {
)) ))
} }
}; };
($id:ident() -> Result<Vec<$return_type:ty>>: $sql:literal) => { ($id:ident() -> Result<Vec<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self) -> $crate::sqlez::anyhow::Result<Vec<$return_type>> { pub fn $id(&self) -> $crate::sqlez::anyhow::Result<Vec<$return_type>> {
use $crate::anyhow::Context; use $crate::anyhow::Context;
@ -118,7 +118,7 @@ macro_rules! sql_method {
)) ))
} }
}; };
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>>: $sql:literal) => { ($id:ident($($arg:ident: $arg_type:ty),+) -> Result<Vec<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result<Vec<$return_type>> { pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result<Vec<$return_type>> {
use $crate::anyhow::Context; use $crate::anyhow::Context;
@ -130,7 +130,7 @@ macro_rules! sql_method {
)) ))
} }
}; };
($id:ident() -> Result<Option<$return_type:ty>>: $sql:literal) => { ($id:ident() -> Result<Option<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self) -> $crate::sqlez::anyhow::Result<Option<$return_type>> { pub fn $id(&self) -> $crate::sqlez::anyhow::Result<Option<$return_type>> {
use $crate::anyhow::Context; use $crate::anyhow::Context;
@ -142,7 +142,7 @@ macro_rules! sql_method {
)) ))
} }
}; };
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>>: $sql:literal) => { ($id:ident($($arg:ident: $arg_type:ty),+) -> Result<Option<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result<Option<$return_type>> { pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result<Option<$return_type>> {
use $crate::anyhow::Context; use $crate::anyhow::Context;
@ -155,4 +155,38 @@ macro_rules! sql_method {
} }
}; };
($id:ident() -> Result<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self) -> $crate::sqlez::anyhow::Result<$return_type> {
use $crate::anyhow::Context;
self.select_row::<$return_type>($sql)?(($($arg),+))
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
))?
.context(::std::format!(
"Error in {}, select_row_bound expected single row result but found none for: {}",
::std::stringify!($id),
::std::stringify!($sql),
))
}
};
($id:ident($($arg:ident: $arg_type:ty),+) -> Result<$return_type:ty>>: $sql:expr) => {
pub fn $id(&self, $($arg: $arg_type),+) -> $crate::sqlez::anyhow::Result<$return_type> {
use $crate::anyhow::Context;
self.select_row_bound::<($($arg_type),+), $return_type>($sql)?(($($arg),+))
.context(::std::format!(
"Error in {}, select_row_bound failed to execute or parse for: {}",
::std::stringify!($id),
::std::stringify!($sql),
))?
.context(::std::format!(
"Error in {}, select_row_bound expected single row result but found none for: {}",
::std::stringify!($id),
::std::stringify!($sql),
))
}
};
} }

View file

@ -568,6 +568,7 @@ impl Item for Editor {
if let Some(project_item) = project.update(cx, |project, cx| { if let Some(project_item) = project.update(cx, |project, cx| {
// Look up the path with this key associated, create a self with that path // Look up the path with this key associated, create a self with that path
let path = DB.get_path(item_id, workspace_id).ok()?; let path = DB.get_path(item_id, workspace_id).ok()?;
let (worktree, path) = project.find_local_worktree(&path, cx)?; let (worktree, path) = project.find_local_worktree(&path, cx)?;
let project_path = ProjectPath { let project_path = ProjectPath {
worktree_id: worktree.read(cx).id(), worktree_id: worktree.read(cx).id(),

View file

@ -1,6 +1,5 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use anyhow::{Context, Result};
use db::{connection, sql_method}; use db::{connection, sql_method};
use indoc::indoc; use indoc::indoc;
use sqlez::domain::Domain; use sqlez::domain::Domain;
@ -32,16 +31,17 @@ impl Domain for Editor {
} }
impl EditorDb { impl EditorDb {
pub fn get_path(&self, item_id: ItemId, workspace_id: WorkspaceId) -> Result<PathBuf> { sql_method! {
self.select_row_bound(indoc! {" get_path(item_id: ItemId, workspace_id: WorkspaceId) -> Result<Option<PathBuf>>:
SELECT path FROM editors indoc! {"
WHERE item_id = ? AND workspace_id = ?"})?((item_id, workspace_id))? SELECT path FROM editors
.context("Path not found for serialized editor") WHERE item_id = ? AND workspace_id = ?"}
} }
sql_method! { sql_method! {
save_path(item_id: ItemId, workspace_id: WorkspaceId, path: &Path) -> Result<()>: save_path(item_id: ItemId, workspace_id: WorkspaceId, path: &Path) -> Result<()>:
"INSERT OR REPLACE INTO editors(item_id, workspace_id, path) indoc! {"
VALUES (?, ?, ?)" INSERT OR REPLACE INTO editors(item_id, workspace_id, path)
VALUES (?, ?, ?)"}
} }
} }