feat: ensure entity and attributes are read in a single transaction

This commit is contained in:
2025-10-02 20:08:21 +02:00
parent 5230b8e5df
commit 767f00ae36
3 changed files with 12 additions and 8 deletions

View File

@@ -6,8 +6,8 @@ const SELECT_ATTRIBUTE_BY_FK: &str = r#"
WHERE entity_id = ?1;
"#;
pub fn run(connection: &Connection, entity: &mut Entity) {
let mut select_attributes_statement = connection.prepare(SELECT_ATTRIBUTE_BY_FK).unwrap();
pub fn run(transaction: &Transaction, entity: &mut Entity) {
let mut select_attributes_statement = transaction.prepare(SELECT_ATTRIBUTE_BY_FK).unwrap();
let attributes = select_attributes_statement
.query_map([&entity.id], sqlite::map::row_to_attribute)
.unwrap();

View File

@@ -8,14 +8,16 @@ const SELECT_ENTITY_BY_CLASS: &str = r#"
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 mut connection = Connection::open(path).unwrap();
let mut transaction = connection.transaction().unwrap();
transaction.set_drop_behavior(DropBehavior::Commit);
let mut statement = transaction.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);
sqlite::load::attributes(&transaction, &mut entity);
entity.state = EntityState::Loaded;
entities.push(entity);
}

View File

@@ -7,13 +7,15 @@ const SELECT_ENTITY_BY_ID: &str = r#"
"#;
pub fn run(path: &path::Path, entity_id: &str) -> Option<Entity> {
let connection = Connection::open(path).unwrap();
let result = connection
let mut connection = Connection::open(path).unwrap();
let mut transaction = connection.transaction().unwrap();
transaction.set_drop_behavior(DropBehavior::Commit);
let result = transaction
.query_one(SELECT_ENTITY_BY_ID, [entity_id], sqlite::map::row_to_entity)
.optional();
let mut entity = result.unwrap();
if let Some(ref mut entity) = entity {
sqlite::load::attributes(&connection, entity);
sqlite::load::attributes(&transaction, entity);
entity.state = EntityState::Loaded;
}
entity