Refactor schema migrations, optimize queries, and improve data fetching logic

- Simplified `run_dev_queries` by delegating schema updates to `run_migrations` for consistency.
- Added universal `fetch_all_*_by_book` methods for streamlined data retrieval across repositories.
- Replaced nested query logic with batch fetching to improve performance and maintainability.
- Optimized HTML-to-text conversion by consolidating regex patterns with `LazyLock`.
This commit is contained in:
natreex
2026-03-24 23:14:33 -04:00
parent 25c7f25a0e
commit b9606e899a
8 changed files with 175 additions and 283 deletions

View File

@@ -427,6 +427,26 @@ pub fn fetch_book_chapter_infos(conn: &Connection, user_id: &str, chapter_id: &s
Ok(rows)
}
pub fn fetch_all_chapter_infos_by_book(conn: &Connection, user_id: &str, book_id: &str, lang: Lang) -> AppResult<Vec<BookChapterInfosTable>> {
let mut statement = conn
.prepare("SELECT chapter_info_id, chapter_id, act_id, incident_id, plot_point_id, book_id, author_id, summary, goal, last_update FROM book_chapter_infos WHERE author_id=?1 AND book_id=?2")
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les infos des chapitres.".to_string() } else { "Unable to retrieve chapter infos.".to_string() }))?;
let rows = statement
.query_map(params![user_id, book_id], |query_row| {
Ok(BookChapterInfosTable {
_chapter_info_id: query_row.get(0)?, chapter_id: query_row.get(1)?,
_act_id: query_row.get(2)?, _incident_id: query_row.get(3)?,
_plot_point_id: query_row.get(4)?, _book_id: query_row.get(5)?,
author_id: query_row.get(6)?, summary: query_row.get(7)?,
goal: query_row.get(8)?, last_update: query_row.get(9)?,
})
})
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les infos des chapitres.".to_string() } else { "Unable to retrieve chapter infos.".to_string() }))?
.collect::<Result<Vec<_>, _>>()
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les infos des chapitres.".to_string() } else { "Unable to retrieve chapter infos.".to_string() }))?;
Ok(rows)
}
/// Retrieves synced chapters for a user.
pub fn fetch_synced_chapters(conn: &Connection, user_id: &str, lang: Lang) -> AppResult<Vec<SyncedChapterResult>> {
let mut statement = conn

View File

@@ -199,6 +199,25 @@ pub fn fetch_book_chapter_contents(conn: &Connection, user_id: &str, chapter_id:
Ok(rows)
}
pub fn fetch_all_chapter_contents_by_book(conn: &Connection, user_id: &str, book_id: &str, lang: Lang) -> AppResult<Vec<BookChapterContentTable>> {
let mut statement = conn
.prepare("SELECT bcc.content_id, bcc.chapter_id, bcc.author_id, bcc.version, bcc.content, bcc.words_count, bcc.time_on_it, bcc.last_update FROM book_chapter_content bcc INNER JOIN book_chapters bc ON bcc.chapter_id = bc.chapter_id WHERE bcc.author_id=?1 AND bc.book_id=?2")
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer le contenu des chapitres.".to_string() } else { "Unable to retrieve chapter contents.".to_string() }))?;
let rows = statement
.query_map(params![user_id, book_id], |query_row| {
Ok(BookChapterContentTable {
content_id: query_row.get(0)?, chapter_id: query_row.get(1)?,
author_id: query_row.get(2)?, version: query_row.get(3)?,
content: query_row.get(4)?, _words_count: query_row.get(5)?,
_time_on_it: query_row.get(6)?, last_update: query_row.get(7)?,
})
})
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer le contenu des chapitres.".to_string() } else { "Unable to retrieve chapter contents.".to_string() }))?
.collect::<Result<Vec<_>, _>>()
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer le contenu des chapitres.".to_string() } else { "Unable to retrieve chapter contents.".to_string() }))?;
Ok(rows)
}
/// Fetches all synced chapter contents for a user (content ID, chapter ID, and last update timestamp).
/// * `conn` - Database connection
/// * `user_id` - The ID of the user/author

View File

@@ -442,6 +442,24 @@ pub fn fetch_book_characters_attributes(conn: &Connection, user_id: &str, charac
Ok(attributes)
}
pub fn fetch_all_character_attributes_by_book(conn: &Connection, user_id: &str, book_id: &str, lang: Lang) -> AppResult<Vec<BookCharactersAttributesTable>> {
let mut statement = conn
.prepare("SELECT bca.attr_id, bca.character_id, bca.user_id, bca.attribute_name, bca.attribute_value, bca.last_update FROM book_characters_attributes bca INNER JOIN book_characters bc ON bca.character_id = bc.character_id WHERE bca.user_id=?1 AND bc.book_id=?2")
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les attributs des personnages.".to_string() } else { "Unable to retrieve character attributes.".to_string() }))?;
let attributes = statement
.query_map(params![user_id, book_id], |query_row| {
Ok(BookCharactersAttributesTable {
attr_id: query_row.get(0)?, character_id: query_row.get(1)?,
user_id: query_row.get(2)?, attribute_name: query_row.get(3)?,
attribute_value: query_row.get(4)?, last_update: query_row.get(5)?,
})
})
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les attributs des personnages.".to_string() } else { "Unable to retrieve character attributes.".to_string() }))?
.collect::<Result<Vec<_>, _>>()
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les attributs des personnages.".to_string() } else { "Unable to retrieve character attributes.".to_string() }))?;
Ok(attributes)
}
/// Fetches all synced characters for a user.
/// * `conn` - Database connection
/// * `user_id` - The unique identifier of the user

View File

@@ -452,6 +452,48 @@ pub fn fetch_location_elements(conn: &Connection, user_id: &str, location_id: &s
Ok(location_elements)
}
pub fn fetch_all_location_elements_by_book(conn: &Connection, user_id: &str, book_id: &str, lang: Lang) -> AppResult<Vec<LocationElementTable>> {
let mut statement = conn
.prepare("SELECT e.element_id, e.location, e.user_id, e.element_name, e.original_name, e.element_description, e.last_update FROM location_element e INNER JOIN book_location l ON e.location = l.loc_id AND e.user_id = l.user_id WHERE e.user_id = ?1 AND l.book_id = ?2")
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les éléments de lieu.".to_string() } else { "Unable to retrieve location elements.".to_string() }))?;
let location_elements = statement
.query_map(params![user_id, book_id], |query_row| {
Ok(LocationElementTable {
element_id: query_row.get(0)?, location: query_row.get(1)?,
user_id: query_row.get(2)?, element_name: query_row.get(3)?,
original_name: query_row.get(4)?, element_description: query_row.get(5)?,
last_update: query_row.get(6)?,
})
})
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les éléments de lieu.".to_string() } else { "Unable to retrieve location elements.".to_string() }))?
.collect::<Result<Vec<_>, _>>()
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les éléments de lieu.".to_string() } else { "Unable to retrieve location elements.".to_string() }))?;
Ok(location_elements)
}
pub fn fetch_all_location_sub_elements_by_book(conn: &Connection, user_id: &str, book_id: &str, lang: Lang) -> AppResult<Vec<LocationSubElementTable>> {
let mut statement = conn
.prepare("SELECT se.sub_element_id, se.element_id, se.user_id, se.sub_elem_name, se.original_name, se.sub_elem_description, se.last_update FROM location_sub_element se INNER JOIN location_element e ON se.element_id = e.element_id AND se.user_id = e.user_id INNER JOIN book_location l ON e.location = l.loc_id AND e.user_id = l.user_id WHERE se.user_id = ?1 AND l.book_id = ?2")
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les sous-éléments de lieu.".to_string() } else { "Unable to retrieve location sub-elements.".to_string() }))?;
let location_sub_elements = statement
.query_map(params![user_id, book_id], |query_row| {
Ok(LocationSubElementTable {
sub_element_id: query_row.get(0)?, element_id: query_row.get(1)?,
user_id: query_row.get(2)?, sub_elem_name: query_row.get(3)?,
original_name: query_row.get(4)?, sub_elem_description: query_row.get(5)?,
last_update: query_row.get(6)?,
})
})
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les sous-éléments de lieu.".to_string() } else { "Unable to retrieve location sub-elements.".to_string() }))?
.collect::<Result<Vec<_>, _>>()
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les sous-éléments de lieu.".to_string() } else { "Unable to retrieve location sub-elements.".to_string() }))?;
Ok(location_sub_elements)
}
/// Fetches all sub-elements for a specific location element.
/// * `conn` - Database connection
/// * `user_id` - The user's unique identifier

View File

@@ -51,39 +51,12 @@ pub fn upload_book_for_sync(conn: &Connection, user_id: &str, book_id: &str, lan
let encrypted_spells: Vec<spell_repo::BookSpellsTable> = spell_repo::fetch_book_spells_table(conn, user_id, book_id, lang)?;
let encrypted_spell_tags: Vec<spell_tag_repo::BookSpellTagsTable> = spell_tag_repo::fetch_book_spell_tags_table(conn, user_id, book_id, lang)?;
let mut nested_chapter_contents: Vec<Vec<chapter_content_repo::BookChapterContentTable>> = Vec::with_capacity(encrypted_chapters.len());
let mut nested_chapter_infos: Vec<Vec<chapter_repo::BookChapterInfosTable>> = Vec::with_capacity(encrypted_chapters.len());
for chapter in &encrypted_chapters {
nested_chapter_contents.push(chapter_content_repo::fetch_book_chapter_contents(conn, user_id, &chapter.chapter_id, lang)?);
nested_chapter_infos.push(chapter_repo::fetch_book_chapter_infos(conn, user_id, &chapter.chapter_id, lang)?);
}
let mut nested_character_attributes: Vec<Vec<character_repo::BookCharactersAttributesTable>> = Vec::with_capacity(encrypted_characters.len());
for character in &encrypted_characters {
nested_character_attributes.push(character_repo::fetch_book_characters_attributes(conn, user_id, &character.character_id, lang)?);
}
let mut nested_world_elements: Vec<Vec<world_repo::BookWorldElementsTable>> = Vec::with_capacity(encrypted_worlds.len());
for world in &encrypted_worlds {
nested_world_elements.push(world_repo::fetch_book_world_elements(conn, user_id, &world.world_id, lang)?);
}
let mut nested_location_elements: Vec<Vec<location_repo::LocationElementTable>> = Vec::with_capacity(encrypted_locations.len());
for location in &encrypted_locations {
nested_location_elements.push(location_repo::fetch_location_elements(conn, user_id, &location.loc_id, lang)?);
}
let encrypted_chapter_contents: Vec<chapter_content_repo::BookChapterContentTable> = nested_chapter_contents.into_iter().flatten().collect();
let encrypted_chapter_infos: Vec<chapter_repo::BookChapterInfosTable> = nested_chapter_infos.into_iter().flatten().collect();
let encrypted_character_attributes: Vec<character_repo::BookCharactersAttributesTable> = nested_character_attributes.into_iter().flatten().collect();
let encrypted_world_elements: Vec<world_repo::BookWorldElementsTable> = nested_world_elements.into_iter().flatten().collect();
let encrypted_location_elements: Vec<location_repo::LocationElementTable> = nested_location_elements.into_iter().flatten().collect();
let mut nested_location_sub_elements: Vec<Vec<location_repo::LocationSubElementTable>> = Vec::with_capacity(encrypted_location_elements.len());
for element in &encrypted_location_elements {
nested_location_sub_elements.push(location_repo::fetch_location_sub_elements(conn, user_id, &element.element_id, lang)?);
}
let encrypted_location_sub_elements: Vec<location_repo::LocationSubElementTable> = nested_location_sub_elements.into_iter().flatten().collect();
let encrypted_chapter_contents: Vec<chapter_content_repo::BookChapterContentTable> = chapter_content_repo::fetch_all_chapter_contents_by_book(conn, user_id, book_id, lang)?;
let encrypted_chapter_infos: Vec<chapter_repo::BookChapterInfosTable> = chapter_repo::fetch_all_chapter_infos_by_book(conn, user_id, book_id, lang)?;
let encrypted_character_attributes: Vec<character_repo::BookCharactersAttributesTable> = character_repo::fetch_all_character_attributes_by_book(conn, user_id, book_id, lang)?;
let encrypted_world_elements: Vec<world_repo::BookWorldElementsTable> = world_repo::fetch_all_world_elements_by_book(conn, user_id, book_id, lang)?;
let encrypted_location_elements: Vec<location_repo::LocationElementTable> = location_repo::fetch_all_location_elements_by_book(conn, user_id, book_id, lang)?;
let encrypted_location_sub_elements: Vec<location_repo::LocationSubElementTable> = location_repo::fetch_all_location_sub_elements_by_book(conn, user_id, book_id, lang)?;
let mut erit_books: Vec<book_repo::EritBooksTable> = Vec::with_capacity(encrypted_books.len());
for book in encrypted_books {

View File

@@ -308,6 +308,27 @@ pub fn fetch_book_world_elements(conn: &Connection, user_id: &str, world_id: &st
Ok(elements)
}
pub fn fetch_all_world_elements_by_book(conn: &Connection, user_id: &str, book_id: &str, lang: Lang) -> AppResult<Vec<BookWorldElementsTable>> {
let mut statement = conn
.prepare("SELECT e.element_id, e.world_id, e.user_id, e.element_type, e.name, e.original_name, e.description, e.last_update FROM book_world_elements e INNER JOIN book_world w ON e.world_id = w.world_id AND e.user_id = w.author_id WHERE e.user_id=?1 AND w.book_id=?2")
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les éléments du monde.".to_string() } else { "Unable to retrieve world elements.".to_string() }))?;
let elements = statement
.query_map(params![user_id, book_id], |query_row| {
Ok(BookWorldElementsTable {
element_id: query_row.get(0)?, world_id: query_row.get(1)?,
user_id: query_row.get(2)?, element_type: query_row.get(3)?,
name: query_row.get(4)?, original_name: query_row.get(5)?,
description: query_row.get(6)?, last_update: query_row.get(7)?,
})
})
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les éléments du monde.".to_string() } else { "Unable to retrieve world elements.".to_string() }))?
.collect::<Result<Vec<_>, _>>()
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les éléments du monde.".to_string() } else { "Unable to retrieve world elements.".to_string() }))?;
Ok(elements)
}
/// Fetches all synced worlds for a specific user.
/// * `conn` - Database connection
/// * `user_id` - The unique identifier of the user