refactor: group sqlite-related errors under one enum
This commit is contained in:
@@ -22,10 +22,10 @@ pub fn run(path: &path::Path) -> result::Result<(), FailedTo> {
|
||||
CREATE INDEX IF NOT EXISTS entity_class ON entity (class);
|
||||
CREATE INDEX IF NOT EXISTS attribute_id ON attribute (id);
|
||||
"#;
|
||||
let connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?;
|
||||
let connection = Connection::open(path).map_err(|_| sqlite::FailedTo::OpenConnection)?;
|
||||
connection
|
||||
.execute_batch(init_statement)
|
||||
.map_err(|_| FailedTo::ExecuteSQLiteBatch)?;
|
||||
.map_err(|_| sqlite::FailedTo::ExecuteBatch)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
@@ -9,10 +9,10 @@ const SELECT_ATTRIBUTE_BY_FK: &str = r#"
|
||||
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)?;
|
||||
.map_err(|_| sqlite::FailedTo::PrepareStatement)?;
|
||||
let attributes = select_attributes_statement
|
||||
.query_map([&entity.id], sqlite::map::row_to_attribute)
|
||||
.map_err(|_| FailedTo::ExecuteSQLiteQuery)?;
|
||||
.map_err(|_| sqlite::FailedTo::ExecuteQuery)?;
|
||||
for attribute in attributes {
|
||||
let attribute = attribute.map_err(|_| FailedTo::MapAttribute)?;
|
||||
entity.attributes.insert(attribute.id.clone(), attribute);
|
||||
|
||||
@@ -8,17 +8,17 @@ const SELECT_ENTITY_BY_CLASS: &str = r#"
|
||||
|
||||
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).map_err(|_| FailedTo::OpenSQLiteConnection)?;
|
||||
let mut connection = Connection::open(path).map_err(|_| sqlite::FailedTo::OpenConnection)?;
|
||||
let mut transaction = connection
|
||||
.transaction()
|
||||
.map_err(|_| FailedTo::BeginSQLiteTransaction)?;
|
||||
.map_err(|_| sqlite::FailedTo::BeginTransaction)?;
|
||||
transaction.set_drop_behavior(DropBehavior::Commit);
|
||||
let mut statement = transaction
|
||||
.prepare(SELECT_ENTITY_BY_CLASS)
|
||||
.map_err(|_| FailedTo::PrepareSQLiteStatement)?;
|
||||
.map_err(|_| sqlite::FailedTo::PrepareStatement)?;
|
||||
let result = statement
|
||||
.query_map([entity_class], sqlite::map::row_to_entity)
|
||||
.map_err(|_| FailedTo::ExecuteSQLiteQuery)?;
|
||||
.map_err(|_| sqlite::FailedTo::ExecuteQuery)?;
|
||||
for entity in result {
|
||||
let mut entity = entity.map_err(|_| FailedTo::MapEntity)?;
|
||||
sqlite::load::attributes(&transaction, &mut entity)?;
|
||||
|
||||
@@ -7,15 +7,15 @@ const SELECT_ENTITY_BY_ID: &str = r#"
|
||||
"#;
|
||||
|
||||
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 connection = Connection::open(path).map_err(|_| sqlite::FailedTo::OpenConnection)?;
|
||||
let mut transaction = connection
|
||||
.transaction()
|
||||
.map_err(|_| FailedTo::BeginSQLiteTransaction)?;
|
||||
.map_err(|_| sqlite::FailedTo::BeginTransaction)?;
|
||||
transaction.set_drop_behavior(DropBehavior::Commit);
|
||||
let mut entity = transaction
|
||||
.query_one(SELECT_ENTITY_BY_ID, [entity_id], sqlite::map::row_to_entity)
|
||||
.optional()
|
||||
.map_err(|_| FailedTo::ExecuteSQLiteQuery)?;
|
||||
.map_err(|_| sqlite::FailedTo::ExecuteQuery)?;
|
||||
if let Some(ref mut entity) = entity {
|
||||
sqlite::load::attributes(&transaction, entity)?;
|
||||
entity.state = EntityState::Loaded;
|
||||
|
||||
@@ -37,7 +37,7 @@ fn write_attribute(
|
||||
INSERT_ATTRIBUTE_STATEMENT_TEMPLATE.replace("{column}", column);
|
||||
transaction
|
||||
.execute(&insert_attribute_statement, attribute_values)
|
||||
.map_err(|_| FailedTo::ExecuteSQLiteStatement)?;
|
||||
.map_err(|_| sqlite::FailedTo::ExecuteStatement)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -46,10 +46,10 @@ fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) -> Result<
|
||||
let entity_values = (&entity.id, &entity.class, entity.ref_date);
|
||||
transaction
|
||||
.execute(DELETE_ENTITY_STATEMENT, entity_id)
|
||||
.map_err(|_| FailedTo::ExecuteSQLiteStatement)?;
|
||||
.map_err(|_| sqlite::FailedTo::ExecuteStatement)?;
|
||||
transaction
|
||||
.execute(INSERT_ENTITY_STATEMENT, entity_values)
|
||||
.map_err(|_| FailedTo::ExecuteSQLiteStatement)?;
|
||||
.map_err(|_| sqlite::FailedTo::ExecuteStatement)?;
|
||||
for attribute in entity.attributes.values() {
|
||||
write_attribute(attribute, entity, transaction)?;
|
||||
}
|
||||
@@ -57,10 +57,10 @@ fn write_entity(entity: &Entity, transaction: &rusqlite::Transaction) -> Result<
|
||||
}
|
||||
|
||||
pub fn run(path: &path::Path, catalog: &Catalog) -> result::Result<(), FailedTo> {
|
||||
let mut connection = Connection::open(path).map_err(|_| FailedTo::OpenSQLiteConnection)?;
|
||||
let mut connection = Connection::open(path).map_err(|_| sqlite::FailedTo::OpenConnection)?;
|
||||
let transaction = connection
|
||||
.transaction()
|
||||
.map_err(|_| FailedTo::BeginSQLiteTransaction)?;
|
||||
.map_err(|_| sqlite::FailedTo::BeginTransaction)?;
|
||||
for entity in catalog
|
||||
.items
|
||||
.values()
|
||||
@@ -70,6 +70,6 @@ pub fn run(path: &path::Path, catalog: &Catalog) -> result::Result<(), FailedTo>
|
||||
}
|
||||
transaction
|
||||
.commit()
|
||||
.map_err(|_| FailedTo::CommitSQLiteTransaction)?;
|
||||
.map_err(|_| sqlite::FailedTo::CommitTransaction)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ pub use crate::str::value::E as Value;
|
||||
pub use crate::trt::eav::T as EAV;
|
||||
|
||||
mod sqlite {
|
||||
pub use crate::str::sqlite_failed_to::E as FailedTo;
|
||||
pub mod init {
|
||||
pub use crate::fun::sqlite_init_db::run as db;
|
||||
}
|
||||
|
||||
@@ -1,15 +1,17 @@
|
||||
use crate::*;
|
||||
|
||||
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
|
||||
pub enum E {
|
||||
BeginSQLiteTransaction,
|
||||
CommitSQLiteTransaction,
|
||||
ExecuteSQLiteBatch,
|
||||
ExecuteSQLiteQuery,
|
||||
ExecuteSQLiteStatement,
|
||||
InitDatabase,
|
||||
LoadFromDB,
|
||||
MapAttribute,
|
||||
MapEntity,
|
||||
OpenSQLiteConnection,
|
||||
PersistCatalog,
|
||||
PrepareSQLiteStatement,
|
||||
SQLite(sqlite::FailedTo),
|
||||
}
|
||||
|
||||
impl From<sqlite::FailedTo> for E {
|
||||
fn from(value: sqlite::FailedTo) -> Self {
|
||||
Self::SQLite(value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,4 +3,5 @@ pub mod catalog;
|
||||
pub mod entity;
|
||||
pub mod entity_state;
|
||||
pub mod failed_to;
|
||||
pub mod sqlite_failed_to;
|
||||
pub mod value;
|
||||
|
||||
10
01.workspace/heave/src/str/sqlite_failed_to.rs
Normal file
10
01.workspace/heave/src/str/sqlite_failed_to.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
#[derive(Debug, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Hash)]
|
||||
pub enum E {
|
||||
BeginTransaction,
|
||||
CommitTransaction,
|
||||
ExecuteBatch,
|
||||
ExecuteQuery,
|
||||
ExecuteStatement,
|
||||
OpenConnection,
|
||||
PrepareStatement,
|
||||
}
|
||||
Reference in New Issue
Block a user