feat: add error handling for load functions to catalog
This commit is contained in:
@@ -6,13 +6,16 @@ const SELECT_ATTRIBUTE_BY_FK: &str = r#"
|
|||||||
WHERE entity_id = ?1;
|
WHERE entity_id = ?1;
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
pub fn run(transaction: &Transaction, entity: &mut Entity) {
|
pub fn run(transaction: &Transaction, entity: &mut Entity) -> Result<(), FailedTo> {
|
||||||
let mut select_attributes_statement = transaction.prepare(SELECT_ATTRIBUTE_BY_FK).unwrap();
|
let mut select_attributes_statement = transaction
|
||||||
|
.prepare(SELECT_ATTRIBUTE_BY_FK)
|
||||||
|
.map_err(|_| FailedTo::PrepareSQLiteStatement)?;
|
||||||
let attributes = select_attributes_statement
|
let attributes = select_attributes_statement
|
||||||
.query_map([&entity.id], sqlite::map::row_to_attribute)
|
.query_map([&entity.id], sqlite::map::row_to_attribute)
|
||||||
.unwrap();
|
.map_err(|_| FailedTo::ExecuteSQLiteQuery)?;
|
||||||
for attribute in attributes {
|
for attribute in attributes {
|
||||||
let attribute = attribute.unwrap();
|
let attribute = attribute.map_err(|_| FailedTo::MapAttribute)?;
|
||||||
entity.attributes.insert(attribute.id.clone(), attribute);
|
entity.attributes.insert(attribute.id.clone(), attribute);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,20 +6,24 @@ const SELECT_ENTITY_BY_CLASS: &str = r#"
|
|||||||
WHERE class = ?1;
|
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 entities = Vec::<Entity>::new();
|
||||||
let mut connection = Connection::open(path).unwrap();
|
let mut connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?;
|
||||||
let mut transaction = connection.transaction().unwrap();
|
let mut transaction = connection
|
||||||
|
.transaction()
|
||||||
|
.map_err(|_| FailedTo::BeginSQLiteTransaction)?;
|
||||||
transaction.set_drop_behavior(DropBehavior::Commit);
|
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
|
let result = statement
|
||||||
.query_map([entity_class], sqlite::map::row_to_entity)
|
.query_map([entity_class], sqlite::map::row_to_entity)
|
||||||
.unwrap();
|
.map_err(|_| FailedTo::ExecuteSQLiteQuery)?;
|
||||||
for entity in result {
|
for entity in result {
|
||||||
let mut entity = entity.unwrap();
|
let mut entity = entity.map_err(|_| FailedTo::MapEntity)?;
|
||||||
sqlite::load::attributes(&transaction, &mut entity);
|
sqlite::load::attributes(&transaction, &mut entity)?;
|
||||||
entity.state = EntityState::Loaded;
|
entity.state = EntityState::Loaded;
|
||||||
entities.push(entity);
|
entities.push(entity);
|
||||||
}
|
}
|
||||||
entities
|
Ok(entities)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,17 +6,19 @@ const SELECT_ENTITY_BY_ID: &str = r#"
|
|||||||
WHERE id = ?1;
|
WHERE id = ?1;
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
pub fn run(path: &path::Path, entity_id: &str) -> Option<Entity> {
|
pub fn run(path: &path::Path, entity_id: &str) -> Result<Option<Entity>, FailedTo> {
|
||||||
let mut connection = Connection::open(path).unwrap();
|
let mut connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?;
|
||||||
let mut transaction = connection.transaction().unwrap();
|
let mut transaction = connection
|
||||||
|
.transaction()
|
||||||
|
.map_err(|_| FailedTo::BeginSQLiteTransaction)?;
|
||||||
transaction.set_drop_behavior(DropBehavior::Commit);
|
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)
|
.query_one(SELECT_ENTITY_BY_ID, [entity_id], sqlite::map::row_to_entity)
|
||||||
.optional();
|
.optional()
|
||||||
let mut entity = result.unwrap();
|
.map_err(|_| FailedTo::ExecuteSQLiteQuery)?;
|
||||||
if let Some(ref mut entity) = entity {
|
if let Some(ref mut entity) = entity {
|
||||||
sqlite::load::attributes(&transaction, entity);
|
sqlite::load::attributes(&transaction, entity)?;
|
||||||
entity.state = EntityState::Loaded;
|
entity.state = EntityState::Loaded;
|
||||||
}
|
}
|
||||||
entity
|
Ok(entity)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -150,27 +150,26 @@ impl O {
|
|||||||
/// # Arguments
|
/// # Arguments
|
||||||
///
|
///
|
||||||
/// * `id` - The ID of the entity to load.
|
/// * `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 path = path::Path::new(&self.path);
|
||||||
let entity = sqlite::load::by_id(path, id);
|
let entity = sqlite::load::by_id(path, id).map_err(|_| FailedTo::LoadFromDB)?;
|
||||||
match entity {
|
if let Some(entity) = entity {
|
||||||
None => (),
|
|
||||||
Some(entity) => {
|
|
||||||
self.items.insert(entity.id.clone(), entity);
|
self.items.insert(entity.id.clone(), entity);
|
||||||
}
|
}
|
||||||
}
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads all entities of a specific class from the database into the catalog.
|
/// 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
|
where
|
||||||
T: EAV,
|
T: EAV,
|
||||||
{
|
{
|
||||||
let class = T::class();
|
let class = T::class();
|
||||||
let path = path::Path::new(&self.path);
|
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 {
|
for entity in entities {
|
||||||
self.items.insert(entity.id.clone(), entity);
|
self.items.insert(entity.id.clone(), entity);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,13 @@ pub enum E {
|
|||||||
BeginSQLiteTransaction,
|
BeginSQLiteTransaction,
|
||||||
CommitSQLiteTransaction,
|
CommitSQLiteTransaction,
|
||||||
ExecuteSQLiteBatch,
|
ExecuteSQLiteBatch,
|
||||||
|
ExecuteSQLiteQuery,
|
||||||
ExecuteSQLiteStatement,
|
ExecuteSQLiteStatement,
|
||||||
InitDatabase,
|
InitDatabase,
|
||||||
|
LoadFromDB,
|
||||||
|
MapAttribute,
|
||||||
|
MapEntity,
|
||||||
OpenSQLiteConnection,
|
OpenSQLiteConnection,
|
||||||
PersistCatalog,
|
PersistCatalog,
|
||||||
|
PrepareSQLiteStatement,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user