feat: add load_by_class function to catalog
This commit is contained in:
@@ -1,10 +1,22 @@
|
|||||||
use crate::*;
|
use crate::*;
|
||||||
|
use rusqlite::*;
|
||||||
|
|
||||||
pub fn run() {
|
const SELECT_ENTITY_BY_CLASS: &str = r#"
|
||||||
todo!("sqlite_load_by_class: missing implementation");
|
SELECT * FROM entity
|
||||||
|
WHERE class = ?1;
|
||||||
|
"#;
|
||||||
|
|
||||||
|
pub fn run(path: &path::Path, entity_class: &str) -> Vec<Entity> {
|
||||||
|
let mut entities = Vec::<Entity>::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::*;
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -46,15 +46,11 @@ impl O {
|
|||||||
Some(entity) => self.insert(entity),
|
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::*;
|
|
||||||
// }
|
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user