diff --git a/crates/db/src/pane.rs b/crates/db/src/pane.rs index 0716d19b1d..3292cc031d 100644 --- a/crates/db/src/pane.rs +++ b/crates/db/src/pane.rs @@ -1,7 +1,9 @@ +use std::str::FromStr; + use gpui::Axis; use indoc::indoc; -use sqlez::migrations::Migration; +use sqlez::{migrations::Migration, bindable::{Bind, Column}, connection::Connection, statement::Statement}; use crate::{items::ItemId, workspace::WorkspaceId}; @@ -138,7 +140,6 @@ pub struct SerializedPane { //********* CURRENTLY IN USE TYPES: ********* - #[derive(Default, Debug, PartialEq, Eq)] pub enum DockAnchor { #[default] @@ -147,6 +148,29 @@ pub enum DockAnchor { Expanded, } +impl ToString for DockAnchor { + fn to_string(&self) -> String { + match self { + DockAnchor::Bottom => "Bottom".to_string(), + DockAnchor::Right => "Right".to_string(), + DockAnchor::Expanded => "Expanded".to_string(), + } + } +} + +impl FromStr for DockAnchor { + type Err = anyhow::Error; + + fn from_str(s: &str) -> anyhow::Result { + match s { + "Bottom" => Ok(DockAnchor::Bottom), + "Right" => Ok(DockAnchor::Right), + "Expanded" => Ok(DockAnchor::Expanded), + _ => anyhow::bail!("Not a valid dock anchor") + } + } +} + #[derive(Default, Debug, PartialEq, Eq)] pub struct SerializedDockPane { pub anchor_position: DockAnchor, @@ -159,6 +183,7 @@ impl SerializedDockPane { } } + #[derive(Default, Debug, PartialEq, Eq)] pub(crate) struct DockRow { workspace_id: WorkspaceId, @@ -172,6 +197,21 @@ impl DockRow { } } +impl Bind for DockRow { + fn bind(&self, statement: &Statement, start_index: i32) -> anyhow::Result { + statement.bind((self.workspace_id, self.anchor_position.to_string(), self.visible), start_index) + } +} + +impl Column for DockRow { + fn column(statement: &mut Statement, start_index: i32) -> anyhow::Result<(Self, i32)> { + <(WorkspaceId, &str, bool) as Column>::column(statement, start_index) + .map(|((workspace_id, anchor_position, visible), next_index)| { + + }) + } +} + impl Db { pub fn get_pane_group(&self, pane_group_id: PaneGroupId) -> SerializedPaneGroup { let axis = self.get_pane_group_axis(pane_group_id); @@ -229,7 +269,10 @@ impl Db { pub fn get_dock_pane(&self, workspace: WorkspaceId) -> Option { fn logic(conn: &Connection, workspace: WorkspaceId) -> anyhow::Result> { - let mut stmt = conn.prepare("SELECT workspace_id, anchor_position, visible FROM dock_panes WHERE workspace_id = ?")?; + let mut stmt = conn.prepare("SELECT workspace_id, anchor_position, visible FROM dock_panes WHERE workspace_id = ?")? + .maybe_row() + .map(|row| DockRow::col); + let dock_panes = stmt.query_row([workspace.raw_id()], |row_ref| from_row::).optional(); diff --git a/crates/db/src/workspace.rs b/crates/db/src/workspace.rs index 126a34676e..f454151cbb 100644 --- a/crates/db/src/workspace.rs +++ b/crates/db/src/workspace.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Result, anyhow}; use std::{ ffi::OsStr, @@ -10,7 +10,7 @@ use std::{ use indoc::indoc; use sqlez::{ - connection::Connection, migrations::Migration, + connection::Connection, migrations::Migration, bindable::{Column, Bind}, }; use crate::pane::SerializedDockPane; @@ -45,6 +45,18 @@ impl WorkspaceId { } } +impl Bind for WorkspaceId { + fn bind(&self, statement: &sqlez::statement::Statement, start_index: i32) -> Result { + todo!(); + } +} + +impl Column for WorkspaceId { + fn column(statement: &mut sqlez::statement::Statement, start_index: i32) -> Result<(Self, i32)> { + todo!(); + } +} + #[derive(Default, Debug)] pub struct SerializedWorkspace { pub workspace_id: WorkspaceId, diff --git a/crates/sqlez/src/bindable.rs b/crates/sqlez/src/bindable.rs index 9b8308f70c..e2cdde039e 100644 --- a/crates/sqlez/src/bindable.rs +++ b/crates/sqlez/src/bindable.rs @@ -10,6 +10,18 @@ pub trait Column: Sized { fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)>; } +impl Bind for bool { + fn bind(&self, statement: &Statement, start_index: i32) -> Result { + statement.bind(self.then_some(1).unwrap_or(0), start_index) + } +} + +impl Column for bool { + fn column(statement: &mut Statement, start_index: i32) -> Result<(Self, i32)> { + i32::column(statement, start_index).map(|(i, next_index)| (i != 0, next_index)) + } +} + impl Bind for &[u8] { fn bind(&self, statement: &Statement, start_index: i32) -> Result { statement.bind_blob(start_index, self)?; diff --git a/crates/sqlez/src/statement.rs b/crates/sqlez/src/statement.rs index 06a090c417..14683171a7 100644 --- a/crates/sqlez/src/statement.rs +++ b/crates/sqlez/src/statement.rs @@ -114,7 +114,7 @@ impl<'a> Statement<'a> { unsafe { sqlite3_bind_int(self.raw_statement, index, int); - } + }; self.connection.last_error() }