diff options
Diffstat (limited to 'src/app/store')
| -rw-r--r-- | src/app/store/sqlite.rs | 131 |
1 files changed, 33 insertions, 98 deletions
diff --git a/src/app/store/sqlite.rs b/src/app/store/sqlite.rs index 002d0cb..631acc6 100644 --- a/src/app/store/sqlite.rs +++ b/src/app/store/sqlite.rs @@ -21,7 +21,6 @@ pub struct SQLiteMigration { pub struct SQLiteStore { location: String, connection: Option<Connection>, - counter: Option<u8>, } /// Define data conversion from sqlite::Row to SQLiteMigration @@ -272,33 +271,43 @@ impl SQLiteStore { Ok(()) } - fn select_accounts(&self) -> Result<Vec<FiduciaryAccount>, ContextError> { - let conn = self.get_connection()?; +} - let statement = conn.prepare(Self::FIDUCIARY_ACCOUNT_SELECT_QUERY)?; +impl Default for SQLiteStore { + fn default() -> Self { + SQLiteStore { + location: ":memory:".into(), + connection: Option::None, + } + } +} - statement - .into_iter() - .map(FiduciaryAccount::try_from) - .collect() +impl Store for SQLiteStore { + /// Initialize SQLite database connection and execute any pending migrations + fn init(&mut self) -> Result<(), ContextError> { + let conn_path = self.location.clone(); + let conn = sqlite::open(conn_path)?; + self.connection = Option::Some(conn); + + self.run_migrations()?; + + Ok(()) } - fn select_account(&self, account_id: u8) -> Result<FiduciaryAccount, ContextError> { + /// Selects accounts from the fiduciary_account table + fn get_accounts(&self) -> Result<Vec<FiduciaryAccount>, ContextError> { let conn = self.get_connection()?; - let mut statement = conn.prepare( - Self::FIDUCIARY_ACCOUNT_SELECT_QUERY.to_owned() + " WHERE fiduciary_account_id = ?", - )?; - statement.bind((1, i64::from(account_id)))?; + let statement = conn.prepare(Self::FIDUCIARY_ACCOUNT_SELECT_QUERY)?; statement .into_iter() .map(FiduciaryAccount::try_from) - .last() - .unwrap_or(Err(ContextError::ItemNotFoundError {})) + .collect() } - fn insert_account(&mut self, account: &FiduciaryAccount) -> Result<(), ContextError> { + /// Inserts an account into the fiduciary_account table + fn add_account(&mut self, account: &FiduciaryAccount) -> Result<(), ContextError> { let conn = self.get_connection()?; let mut statement = conn.prepare(Self::FIDUCIARY_ACCOUNT_INSERT_QUERY)?; @@ -331,7 +340,8 @@ impl SQLiteStore { Ok(()) } - fn select_symbols(&self) -> Result<Vec<EquitySymbol>, ContextError> { + /// Selects symbols from the equity_symbol table + fn get_symbols(&self) -> Result<Vec<EquitySymbol>, ContextError> { let conn = self.get_connection()?; let statement = conn.prepare(Self::EQUITY_SYMBOL_SELECT_QUERY)?; @@ -339,21 +349,8 @@ impl SQLiteStore { statement.into_iter().map(EquitySymbol::try_from).collect() } - fn select_symbol(&self, symbol_id: usize) -> Result<EquitySymbol, ContextError> { - let conn = self.get_connection()?; - - let mut statement = conn - .prepare(Self::EQUITY_SYMBOL_SELECT_QUERY.to_owned() + " WHERE equity_symbol_id = ?")?; - statement.bind((1, i64::try_from(symbol_id)?))?; - - statement - .into_iter() - .map(EquitySymbol::try_from) - .last() - .unwrap_or(Err(ContextError::ItemNotFoundError())) - } - - fn insert_symbol(&mut self, symbol: &EquitySymbol) -> Result<(), ContextError> { + /// Inserts a symbol into the equity_symbol table + fn add_symbol(&mut self, symbol: &EquitySymbol) -> Result<(), ContextError> { let conn = self.get_connection()?; let mut statement = conn.prepare(Self::EQUITY_SYMBOL_INSERT_QUERY)?; @@ -383,7 +380,8 @@ impl SQLiteStore { Ok(()) } - fn select_events(&self) -> Result<Vec<EquityChangeEvent>, ContextError> { + /// Selects events from the equity_change_event table + fn get_events(&self) -> Result<Vec<EquityChangeEvent>, ContextError> { let conn = self.get_connection()?; let statement = conn.prepare(Self::EQUITY_CHANGE_EVENT_SELECT_QUERY)?; @@ -394,22 +392,8 @@ impl SQLiteStore { .collect() } - fn select_event(&self, event_id: usize) -> Result<EquityChangeEvent, ContextError> { - let conn = self.get_connection()?; - - let mut statement = conn.prepare( - Self::EQUITY_CHANGE_EVENT_SELECT_QUERY.to_owned() + " WHERE equity_change_event_id = ?", - )?; - statement.bind((1, i64::try_from(event_id)?))?; - - statement - .into_iter() - .map(EquityChangeEvent::try_from) - .last() - .unwrap_or(Err(ContextError::ItemNotFoundError())) - } - - fn insert_event(&mut self, event: &EquityChangeEvent) -> Result<(), ContextError> { + /// Inserts an event into the equity_change_event table + fn add_event(&mut self, event: &EquityChangeEvent) -> Result<(), ContextError> { let conn = self.get_connection()?; let mut statement = conn.prepare(Self::EQUITY_SYMBOL_INSERT_QUERY)?; @@ -448,55 +432,6 @@ impl SQLiteStore { } } -impl Default for SQLiteStore { - fn default() -> Self { - SQLiteStore { - location: ":memory:".into(), - connection: Option::None, - counter: None, - } - } -} - -impl Store for SQLiteStore { - /// Init SQLite for counting. Creates `RATAT` table with single row, with - /// counter set to `0` - fn init(&mut self) -> Result<(), ContextError> { - let conn_path = self.location.clone(); - let conn = sqlite::open(conn_path)?; - self.connection = Option::Some(conn); - - self.run_migrations()?; - self.counter = Some(0_u8); - - Ok(()) - } - - fn get_accounts(&self) -> Result<Vec<FiduciaryAccount>, ContextError> { - self.select_accounts() - } - - fn get_symbols(&self) -> Result<Vec<EquitySymbol>, ContextError> { - self.select_symbols() - } - - fn get_events(&self) -> Result<Vec<EquityChangeEvent>, ContextError> { - self.select_events() - } - - fn add_account(&mut self, account: &FiduciaryAccount) -> Result<(), ContextError> { - self.insert_account(account) - } - - fn add_symbol(&mut self, symbol: &EquitySymbol) -> Result<(), ContextError> { - self.insert_symbol(symbol) - } - - fn add_event(&mut self, event: &EquityChangeEvent) -> Result<(), ContextError> { - self.insert_event(event) - } -} - #[cfg(test)] mod tests { |
