feat: add error handling for load functions to catalog

This commit is contained in:
2025-10-04 07:32:17 +02:00
parent 5ec86bac7f
commit cac890aa06
5 changed files with 42 additions and 29 deletions

View File

@@ -6,13 +6,16 @@ const SELECT_ATTRIBUTE_BY_FK: &str = r#"
WHERE entity_id = ?1;
"#;
pub fn run(transaction: &Transaction, entity: &mut Entity) {
let mut select_attributes_statement = transaction.prepare(SELECT_ATTRIBUTE_BY_FK).unwrap();
pub fn run(transaction: &Transaction, entity: &mut Entity) -> Result<(), FailedTo> {
let mut select_attributes_statement = transaction
.prepare(SELECT_ATTRIBUTE_BY_FK)
.map_err(|_| FailedTo::PrepareSQLiteStatement)?;
let attributes = select_attributes_statement
.query_map([&entity.id], sqlite::map::row_to_attribute)
.unwrap();
.map_err(|_| FailedTo::ExecuteSQLiteQuery)?;
for attribute in attributes {
let attribute = attribute.unwrap();
let attribute = attribute.map_err(|_| FailedTo::MapAttribute)?;
entity.attributes.insert(attribute.id.clone(), attribute);
}
Ok(())
}

View File

@@ -6,20 +6,24 @@ const SELECT_ENTITY_BY_CLASS: &str = r#"
WHERE class = ?1;
"#;
pub fn run(path: &path::Path, entity_class: &str) -> Vec<Entity> {
pub fn run(path: &path::Path, entity_class: &str) -> Result<Vec<Entity>, FailedTo> {
let mut entities = Vec::<Entity>::new();
let mut connection = Connection::open(path).unwrap();
let mut transaction = connection.transaction().unwrap();
let mut connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?;
let mut transaction = connection
.transaction()
.map_err(|_| FailedTo::BeginSQLiteTransaction)?;
transaction.set_drop_behavior(DropBehavior::Commit);
let mut statement = transaction.prepare(SELECT_ENTITY_BY_CLASS).unwrap();
let mut statement = transaction
.prepare(SELECT_ENTITY_BY_CLASS)
.map_err(|_| FailedTo::PrepareSQLiteStatement)?;
let result = statement
.query_map([entity_class], sqlite::map::row_to_entity)
.unwrap();
.map_err(|_| FailedTo::ExecuteSQLiteQuery)?;
for entity in result {
let mut entity = entity.unwrap();
sqlite::load::attributes(&transaction, &mut entity);
let mut entity = entity.map_err(|_| FailedTo::MapEntity)?;
sqlite::load::attributes(&transaction, &mut entity)?;
entity.state = EntityState::Loaded;
entities.push(entity);
}
entities
Ok(entities)
}

View File

@@ -6,17 +6,19 @@ const SELECT_ENTITY_BY_ID: &str = r#"
WHERE id = ?1;
"#;
pub fn run(path: &path::Path, entity_id: &str) -> Option<Entity> {
let mut connection = Connection::open(path).unwrap();
let mut transaction = connection.transaction().unwrap();
pub fn run(path: &path::Path, entity_id: &str) -> Result<Option<Entity>, FailedTo> {
let mut connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?;
let mut transaction = connection
.transaction()
.map_err(|_| FailedTo::BeginSQLiteTransaction)?;
transaction.set_drop_behavior(DropBehavior::Commit);
let result = transaction
let mut entity = transaction
.query_one(SELECT_ENTITY_BY_ID, [entity_id], sqlite::map::row_to_entity)
.optional();
let mut entity = result.unwrap();
.optional()
.map_err(|_| FailedTo::ExecuteSQLiteQuery)?;
if let Some(ref mut entity) = entity {
sqlite::load::attributes(&transaction, entity);
sqlite::load::attributes(&transaction, entity)?;
entity.state = EntityState::Loaded;
}
entity
Ok(entity)
}

View File

@@ -150,27 +150,26 @@ impl O {
/// # Arguments
///
/// * `id` - The ID of the entity to load.
pub fn load_by_id(&mut self, id: &str) {
pub fn load_by_id(&mut self, id: &str) -> Result<(), FailedTo> {
let path = path::Path::new(&self.path);
let entity = sqlite::load::by_id(path, id);
match entity {
None => (),
Some(entity) => {
let entity = sqlite::load::by_id(path, id).map_err(|_| FailedTo::LoadFromDB)?;
if let Some(entity) = entity {
self.items.insert(entity.id.clone(), entity);
}
}
Ok(())
}
/// Loads all entities of a specific class from the database into the catalog.
pub fn load_by_class<T>(&mut self)
pub fn load_by_class<T>(&mut self) -> Result<(), FailedTo>
where
T: EAV,
{
let class = T::class();
let path = path::Path::new(&self.path);
let entities = sqlite::load::by_class(path, class);
let entities = sqlite::load::by_class(path, class)?;
for entity in entities {
self.items.insert(entity.id.clone(), entity);
}
Ok(())
}
}

View File

@@ -3,8 +3,13 @@ pub enum E {
BeginSQLiteTransaction,
CommitSQLiteTransaction,
ExecuteSQLiteBatch,
ExecuteSQLiteQuery,
ExecuteSQLiteStatement,
InitDatabase,
LoadFromDB,
MapAttribute,
MapEntity,
OpenSQLiteConnection,
PersistCatalog,
PrepareSQLiteStatement,
}