feat: ensure entity and attributes are read in a single transaction
This commit is contained in:
@@ -6,8 +6,8 @@ const SELECT_ATTRIBUTE_BY_FK: &str = r#"
|
|||||||
WHERE entity_id = ?1;
|
WHERE entity_id = ?1;
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
pub fn run(connection: &Connection, entity: &mut Entity) {
|
pub fn run(transaction: &Transaction, entity: &mut Entity) {
|
||||||
let mut select_attributes_statement = connection.prepare(SELECT_ATTRIBUTE_BY_FK).unwrap();
|
let mut select_attributes_statement = transaction.prepare(SELECT_ATTRIBUTE_BY_FK).unwrap();
|
||||||
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();
|
.unwrap();
|
||||||
|
|||||||
@@ -8,14 +8,16 @@ const SELECT_ENTITY_BY_CLASS: &str = r#"
|
|||||||
|
|
||||||
pub fn run(path: &path::Path, entity_class: &str) -> Vec<Entity> {
|
pub fn run(path: &path::Path, entity_class: &str) -> Vec<Entity> {
|
||||||
let mut entities = Vec::<Entity>::new();
|
let mut entities = Vec::<Entity>::new();
|
||||||
let connection = Connection::open(path).unwrap();
|
let mut connection = Connection::open(path).unwrap();
|
||||||
let mut statement = connection.prepare(SELECT_ENTITY_BY_CLASS).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
|
let result = statement
|
||||||
.query_map([entity_class], sqlite::map::row_to_entity)
|
.query_map([entity_class], sqlite::map::row_to_entity)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
for entity in result {
|
for entity in result {
|
||||||
let mut entity = entity.unwrap();
|
let mut entity = entity.unwrap();
|
||||||
sqlite::load::attributes(&connection, &mut entity);
|
sqlite::load::attributes(&transaction, &mut entity);
|
||||||
entity.state = EntityState::Loaded;
|
entity.state = EntityState::Loaded;
|
||||||
entities.push(entity);
|
entities.push(entity);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,13 +7,15 @@ const SELECT_ENTITY_BY_ID: &str = r#"
|
|||||||
"#;
|
"#;
|
||||||
|
|
||||||
pub fn run(path: &path::Path, entity_id: &str) -> Option<Entity> {
|
pub fn run(path: &path::Path, entity_id: &str) -> Option<Entity> {
|
||||||
let connection = Connection::open(path).unwrap();
|
let mut connection = Connection::open(path).unwrap();
|
||||||
let result = connection
|
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)
|
.query_one(SELECT_ENTITY_BY_ID, [entity_id], sqlite::map::row_to_entity)
|
||||||
.optional();
|
.optional();
|
||||||
let mut entity = result.unwrap();
|
let mut entity = result.unwrap();
|
||||||
if let Some(ref mut entity) = entity {
|
if let Some(ref mut entity) = entity {
|
||||||
sqlite::load::attributes(&connection, entity);
|
sqlite::load::attributes(&transaction, entity);
|
||||||
entity.state = EntityState::Loaded;
|
entity.state = EntityState::Loaded;
|
||||||
}
|
}
|
||||||
entity
|
entity
|
||||||
|
|||||||
Reference in New Issue
Block a user