From 07c07385fc416479c1f9d30fb8806aa5faf2a938 Mon Sep 17 00:00:00 2001 From: Kevin Hoerr Date: Mon, 4 May 2026 16:55:40 -0400 Subject: sqlite.rs: Add inserts for accounts, symbols, events --- src/app/store/sqlite.rs | 68 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 3 deletions(-) (limited to 'src/app') diff --git a/src/app/store/sqlite.rs b/src/app/store/sqlite.rs index 9ca66e6..f87bb39 100644 --- a/src/app/store/sqlite.rs +++ b/src/app/store/sqlite.rs @@ -5,6 +5,7 @@ use crate::db; use super::records::{EquityChangeEvent, EquitySymbol, FiduciaryAccount}; use super::{ContextError, Store}; +use bigdecimal::ToPrimitive; use chrono::Local; use miette::Result; use sqlite::{Connection, Row, Value}; @@ -115,6 +116,14 @@ impl TryFrom> for EquityChangeEvent { } impl SQLiteStore { + const FIDUCIARY_ACCOUNT_INSERT_QUERY: &str = " + INSERT INTO fiduciary_account VALUES ( + :fiduciary_account_id, + :fiduciary_account_number, + :fiduciary_account_name, + :fiduciary_account_description, + :fiduciary_account_created_timestamp + )"; const FIDUCIARY_ACCOUNT_SELECT_QUERY: &str = " SELECT fiduciary_account_id, @@ -123,6 +132,14 @@ impl SQLiteStore { fiduciary_account_description, fiduciary_account_created_timestamp FROM fiduciary_account"; + const EQUITY_SYMBOL_INSERT_QUERY: &str = " + INSERT INTO equity_symbol VALUES ( + :equity_symbol_id, + :equity_symbol_name, + :equity_symbol_type, + :equity_symbol_managing_company, + :equity_symbol_created_timestamp + )"; const EQUITY_SYMBOL_SELECT_QUERY: &str = " SELECT equity_symbol_id, @@ -131,6 +148,16 @@ impl SQLiteStore { equity_symbol_managing_company, equity_symbol_created_timestamp FROM equity_symbol"; + const EQUITY_CHANGE_EVENT_INSERT_QUERY: &str = " + INSERT INTO equity_change_event VALUES ( + :equity_change_event_id, + :fiduciary_account_id, + :equity_symbol_id, + :equity_change_quantity, + :equity_change_cost_basis_usd, + :equity_change_type, + :equity_change_timestamp + )"; const EQUITY_CHANGE_EVENT_SELECT_QUERY: &str = " SELECT equity_change_event_id, @@ -271,7 +298,18 @@ impl SQLiteStore { } fn insert_account(&mut self, account: &FiduciaryAccount) -> Result<(), ContextError> { - todo!(); + let conn = self.get_connection()?; + + let mut statement = conn.prepare(Self::FIDUCIARY_ACCOUNT_INSERT_QUERY)?; + + statement.bind::<&[(_, Value)]>(&[ + (":fiduciary_account_id", i64::from(account.fiduciary_account_id).into()), + (":fiduciary_account_name", account.fiduciary_account_name.clone().into()), + (":fiduciary_account_number", i64::try_from(account.fiduciary_account_number)?.into()), + (":fiduciary_account_description", account.fiduciary_account_description.clone().into()), + (":fiduciary_account_created_timestamp", account.fiduciary_account_created_timestamp.to_string().into()), + ])?; + Ok(()) } fn select_symbols(&self) -> Result, ContextError> { @@ -297,7 +335,18 @@ impl SQLiteStore { } fn insert_symbol(&mut self, symbol: &EquitySymbol) -> Result<(), ContextError> { - todo!(); + let conn = self.get_connection()?; + + let mut statement = conn.prepare(Self::EQUITY_SYMBOL_INSERT_QUERY)?; + + statement.bind::<&[(_, Value)]>(&[ + (":equity_symbol_id", i64::try_from(symbol.equity_symbol_id)?.into()), + (":equity_symbol_name", symbol.equity_symbol_name.clone().into()), + (":equity_symbol_type", symbol.equity_symbol_type.to_string().into()), + (":equity_symbol_managing_company", symbol.equity_symbol_managing_company.clone().into()), + (":equity_symbol_created_timestamp", symbol.equity_symbol_created_timestamp.to_string().into()), + ])?; + Ok(()) } fn select_events(&self) -> Result, ContextError> { @@ -327,7 +376,20 @@ impl SQLiteStore { } fn insert_event(&mut self, event: &EquityChangeEvent) -> Result<(), ContextError> { - todo!(); + let conn = self.get_connection()?; + + let mut statement = conn.prepare(Self::EQUITY_SYMBOL_INSERT_QUERY)?; + + statement.bind::<&[(_, Value)]>(&[ + (":equity_change_event_id", i64::try_from(event.equity_change_event_id)?.into()), + (":fiduciary_account_id", i64::from(event.fiduciary_account_id).into()), + (":equity_symbol_id", i64::try_from(event.equity_symbol_id)?.into()), + (":equity_change_quantity", event.equity_change_quantity.to_f64().into()), + (":equity_change_cost_basis", event.equity_change_cost_basis.to_f64().into()), + (":equity_change_type", event.equity_change_type.to_string().into()), + (":equity_change_timestamp", event.equity_change_timestamp.to_string().into()), + ])?; + Ok(()) } } -- cgit v1.3