From fecfc467510df6318600929aaf5e5d29d38e869e Mon Sep 17 00:00:00 2001 From: davidemazzocchi Date: Tue, 30 Sep 2025 12:23:52 +0200 Subject: [PATCH] feat: add load_by_class function to catalog --- .../heave/src/fun/sqlite_load_by_class.rs | 26 ++++++++++++----- 01.workspace/heave/src/str/catalog.rs | 18 +++++------- 01.workspace/heave/src/tst/intended_use.rs | 28 +++++++++++++++++++ 3 files changed, 54 insertions(+), 18 deletions(-) diff --git a/01.workspace/heave/src/fun/sqlite_load_by_class.rs b/01.workspace/heave/src/fun/sqlite_load_by_class.rs index 6fd1a80..6426565 100644 --- a/01.workspace/heave/src/fun/sqlite_load_by_class.rs +++ b/01.workspace/heave/src/fun/sqlite_load_by_class.rs @@ -1,10 +1,22 @@ use crate::*; +use rusqlite::*; -pub fn run() { - todo!("sqlite_load_by_class: missing implementation"); +const SELECT_ENTITY_BY_CLASS: &str = r#" + SELECT * FROM entity + WHERE class = ?1; +"#; + +pub fn run(path: &path::Path, entity_class: &str) -> Vec { + let mut entities = Vec::::new(); + let connection = Connection::open(path).unwrap(); + let mut statement = connection.prepare(SELECT_ENTITY_BY_CLASS).unwrap(); + let result = statement + .query_map([entity_class], sqlite::map::row_to_entity) + .unwrap(); + for entity in result { + let mut entity = entity.unwrap(); + sqlite::load::attributes(&connection, &mut entity); + entities.push(entity); + } + entities } - -// #[cfg(test)] -// mod unit_tests { -// use super::*; -// } diff --git a/01.workspace/heave/src/str/catalog.rs b/01.workspace/heave/src/str/catalog.rs index 8c9c33f..2e140d9 100644 --- a/01.workspace/heave/src/str/catalog.rs +++ b/01.workspace/heave/src/str/catalog.rs @@ -46,15 +46,11 @@ impl O { Some(entity) => self.insert(entity), } } + pub fn load_by_class(&mut self, class: &str) { + let path = path::Path::new(&self.path); + let entities = sqlite::load::by_class(path, class); + for entity in entities { + self.insert(entity); + } + } } - -// impl std::fmt::Display for O { -// fn fmt(&self, _f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { -// todo!(); -// } -// } - -// #[cfg(test)] -// mod unit_tests { -// use super::*; -// } diff --git a/01.workspace/heave/src/tst/intended_use.rs b/01.workspace/heave/src/tst/intended_use.rs index 2506582..ef0a93f 100644 --- a/01.workspace/heave/src/tst/intended_use.rs +++ b/01.workspace/heave/src/tst/intended_use.rs @@ -190,4 +190,32 @@ mod tests { ) ); } + #[test] + fn check_011() { + // demonstrate load by class + let tempfile = tempfile::NamedTempFile::new().unwrap(); + let path = tempfile.path(); + let mut catalog = Catalog::new(path.to_str().unwrap()); + catalog.init(); + let product_01 = Product { + id: short_uuid::short!().to_string(), + name: "laptop".to_string(), + price: 200000u64, + }; + let product_02 = Product { + id: short_uuid::short!().to_string(), + name: "desktop".to_string(), + price: 300000u64, + }; + let expected_value_01 = product_01.clone(); + catalog.insert(product_01); + catalog.insert(product_02); + catalog.persist(); + // new empty catalog + let mut catalog = Catalog::new(path.to_str().unwrap()); + catalog.load_by_class("product"); + assert_eq!(catalog.items.len(), 2); + let loaded_value_01: Product = catalog.get(&expected_value_01.id).unwrap(); + assert_eq!(loaded_value_01, expected_value_01); + } }