Add SQL query to count children by user: Introduced a new query to count distinct children associated with a user across all their characters. Updated UserCharacterWorker to utilize this new query, replacing the previous count query. This enhances user-specific data retrieval capabilities.

This commit is contained in:
Torsten Schulz (local)
2026-02-02 15:25:13 +01:00
parent 460310ae89
commit 658a9034ed
5 changed files with 35 additions and 264 deletions

View File

@@ -580,6 +580,13 @@ pub const QUERY_COUNT_CHILDREN: &str = r#"
SELECT COUNT(*) AS cnt FROM falukant_data.child_relation WHERE (father_character_id = $1 OR mother_character_id = $1) AND child_character_id != $2;
"#;
pub const QUERY_COUNT_CHILDREN_BY_USER: &str = r#"
SELECT COUNT(DISTINCT cr.child_character_id) AS cnt
FROM falukant_data.child_relation cr
JOIN falukant_data.character parent ON (parent.id = cr.father_character_id OR parent.id = cr.mother_character_id)
WHERE parent.user_id = $1;
"#;
// user_character worker queries
pub const QUERY_GET_USERS_TO_UPDATE: &str = r#"
SELECT id, CURRENT_DATE - birthdate::date AS age, health

View File

@@ -25,7 +25,7 @@ use crate::worker::sql::{
QUERY_GET_SETTLEMENT_VALUE,
QUERY_GET_INVENTORY_VALUE,
QUERY_GET_CREDIT_DEBT,
QUERY_COUNT_CHILDREN,
QUERY_COUNT_CHILDREN_BY_USER,
QUERY_GET_HEIR,
QUERY_RANDOM_HEIR,
QUERY_SET_CHARACTER_USER,
@@ -795,8 +795,8 @@ impl UserCharacterWorker {
.get()
.map_err(|e| DbError::new(format!("DB-Verbindung fehlgeschlagen: {e}")))?;
conn.prepare("count_children", QUERY_COUNT_CHILDREN)?;
let rows = conn.execute("count_children", &[&deceased_user_id])?;
conn.prepare("count_children_by_user", QUERY_COUNT_CHILDREN_BY_USER)?;
let rows = conn.execute("count_children_by_user", &[&deceased_user_id])?;
Ok(rows
.first()