From b43c705ad6b2c9fa1dfd2512e19fbf87a9748a9d Mon Sep 17 00:00:00 2001 From: davidemazzocchi Date: Sun, 19 Oct 2025 08:44:23 +0200 Subject: [PATCH] review: refactor inner join fragment creation using template and helper function --- .../heave/src/fun/sqlite_build_statement.rs | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/01.workspace/heave/src/fun/sqlite_build_statement.rs b/01.workspace/heave/src/fun/sqlite_build_statement.rs index 12402d4..2004a18 100644 --- a/01.workspace/heave/src/fun/sqlite_build_statement.rs +++ b/01.workspace/heave/src/fun/sqlite_build_statement.rs @@ -1,21 +1,31 @@ use crate::*; const BASE_SELECT: &str = r#"SELECT * FROM entity"#; +const INNER_JOIN_FRAGMENT: &str = r#" + INNER JOIN attribute + ON entity.id = attribute.entity_id + AND attribute.id = '{attribute_id}' + AND {field} {op} ?{index} +"#; + +fn compose_fragment(name: &str, field: &str, op: &str, index: usize) -> String { + INNER_JOIN_FRAGMENT + .replace("{attribute_id}", name) + .replace("{field}", field) + .replace("{op}", op) + .replace("{index}", &index.to_string()) +} pub fn run(filter: &Filter) -> Result { let mut statement = String::from(BASE_SELECT); - for (i, (name, _comparison, condition)) in filter.conditions().enumerate() { - let fragment = match (_comparison, condition) { - (_, Condition::Bool(_)) => format!( - " INNER JOIN attribute ON entity.id = attribute.entity_id AND attribute.id = '{}' AND value_bool = ?{}", - name, - i + 1 - ), - (Comparison::Equal, Condition::SignedInt(_)) => format!( - " INNER JOIN attribute ON entity.id = attribute.entity_id AND attribute.id = '{}' AND value_int = ?{}", - name, - i + 1 - ), + for (i, (name, comparison, condition)) in filter.conditions().enumerate() { + let fragment = match (comparison, condition) { + (Comparison::Equal, Condition::Bool(_)) => { + compose_fragment(name, "value_bool", "=", i + 1) + } + (Comparison::Equal, Condition::SignedInt(_)) => { + compose_fragment(name, "value_int", "=", i + 1) + } _ => todo!(), }; statement.push_str(&fragment);