Added extra sql methods

Mikayla Maki created

Change summary

crates/db/src/db.rs              | 46 +++++++++++++++++++++++++++++----
crates/editor/src/items.rs       |  1 
crates/editor/src/persistence.rs | 16 +++++-----
3 files changed, 49 insertions(+), 14 deletions(-)

Detailed changes

crates/db/src/db.rs 🔗

@@ -83,7 +83,7 @@ macro_rules! connection {
 
 #[macro_export]
 macro_rules! sql_method {
-    ($id:ident() ->  Result<()>: $sql:literal) => {
+    ($id:ident() ->  Result<()>: $sql:expr) => {
         pub fn $id(&self) -> $crate::sqlez::anyhow::Result<()> {
             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<()> {
             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>> {
              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>> {
              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>> {
              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>>  {
              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),
+                 ))
+         }
+    };
 }

crates/editor/src/items.rs 🔗

@@ -568,6 +568,7 @@ impl Item for Editor {
         if let Some(project_item) = project.update(cx, |project, cx| {
             // 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 (worktree, path) = project.find_local_worktree(&path, cx)?;
             let project_path = ProjectPath {
                 worktree_id: worktree.read(cx).id(),

crates/editor/src/persistence.rs 🔗

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