review: refactor inner join fragment creation using template and helper function

This commit is contained in:
2025-10-19 08:44:23 +02:00
parent 3ee7d97275
commit b43c705ad6

View File

@@ -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<String, FailedTo> {
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);