fix: set unique names for attribute table in case of multiple filter conditions

This commit is contained in:
2025-10-20 09:09:17 +02:00
parent 81bed07665
commit 9ee86c9cde
2 changed files with 56 additions and 4 deletions

View File

@@ -2,10 +2,10 @@ 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}
INNER JOIN attribute as attribute_{index}
ON entity.id = attribute_{index}.entity_id
AND attribute_{index}.id = '{attribute_id}'
AND attribute_{index}.{field} {op} ?{index}
"#;
fn compose_fragment(name: &str, field: &str, op: &str, index: usize) -> String {

View File

@@ -2960,4 +2960,56 @@ mod tests {
assert!(catalog2.items.is_empty());
std::fs::remove_file(path).unwrap();
}
#[test]
fn load_by_filter_with_multiple_conditions() {
let db_path = "target/test_dbs/lbf_with_multiple_conditions.db";
let path = std::path::Path::new(db_path);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
if path.exists() {
std::fs::remove_file(path).unwrap();
}
let mut catalog_setup = Catalog::new(db_path);
catalog_setup.init().unwrap();
let items = vec![
Item {
id: "item-1".to_string(),
price: 100,
discount: 0.10,
in_stock: true,
..Default::default()
},
Item {
id: "item-2".to_string(),
price: 200,
discount: 0.10,
in_stock: true,
..Default::default()
},
Item {
id: "item-3".to_string(),
price: 100,
discount: 0.20,
in_stock: true,
..Default::default()
},
Item {
id: "item-4".to_string(),
price: 100,
discount: 0.10,
in_stock: false,
..Default::default()
},
];
catalog_setup.insert_many(items).unwrap();
catalog_setup.persist().unwrap();
let mut catalog = Catalog::new(db_path);
let filter = Filter::new()
.with_unsigned_int("price", Comparison::Equal, 100)
.with_real("discount", Comparison::Equal, 0.10)
.with_bool("in_stock", true);
assert!(catalog.load_by_filter(&filter).is_ok());
assert_eq!(catalog.items.len(), 1);
assert!(catalog.items.contains_key("item-1"));
std::fs::remove_file(path).unwrap();
}
}