review: refactor inner join fragment creation using template and helper function
This commit is contained in:
@@ -1,21 +1,31 @@
|
|||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
const BASE_SELECT: &str = r#"SELECT * FROM entity"#;
|
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> {
|
pub fn run(filter: &Filter) -> Result<String, FailedTo> {
|
||||||
let mut statement = String::from(BASE_SELECT);
|
let mut statement = String::from(BASE_SELECT);
|
||||||
for (i, (name, _comparison, condition)) in filter.conditions().enumerate() {
|
for (i, (name, comparison, condition)) in filter.conditions().enumerate() {
|
||||||
let fragment = match (_comparison, condition) {
|
let fragment = match (comparison, condition) {
|
||||||
(_, Condition::Bool(_)) => format!(
|
(Comparison::Equal, Condition::Bool(_)) => {
|
||||||
" INNER JOIN attribute ON entity.id = attribute.entity_id AND attribute.id = '{}' AND value_bool = ?{}",
|
compose_fragment(name, "value_bool", "=", i + 1)
|
||||||
name,
|
}
|
||||||
i + 1
|
(Comparison::Equal, Condition::SignedInt(_)) => {
|
||||||
),
|
compose_fragment(name, "value_int", "=", 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
|
|
||||||
),
|
|
||||||
_ => todo!(),
|
_ => todo!(),
|
||||||
};
|
};
|
||||||
statement.push_str(&fragment);
|
statement.push_str(&fragment);
|
||||||
|
|||||||
Reference in New Issue
Block a user