Compare commits

...

2 commits

Author SHA1 Message Date
896de305cc fix(db): fix the issue of order preservation.
-Added CTE (Common Table Expression) to first get ordered items
-Changed result collection from HashMap to Vec to preserve order
-Process rows sequentially while maintaining item order
2025-03-10 17:39:39 +03:00
47c87159ae feat(db): add item_order to track the order that items are saved in 2025-03-10 17:00:38 +03:00

View file

@ -261,6 +261,7 @@ mod db_impl {
name TEXT NOT NULL, name TEXT NOT NULL,
description TEXT, description TEXT,
wikidata_id TEXT, wikidata_id TEXT,
item_order INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY (url_id) REFERENCES urls(id) ON DELETE CASCADE FOREIGN KEY (url_id) REFERENCES urls(id) ON DELETE CASCADE
);", );",
) )
@ -377,14 +378,33 @@ mod db_impl {
log!("Fetching items for URL '{}' (ID: {})", url, url_id); log!("Fetching items for URL '{}' (ID: {})", url, url_id);
let mut stmt = conn.prepare( let mut stmt = conn.prepare(
"SELECT i.id, i.name, i.description, i.wikidata_id, "WITH ordered_items AS (
p.name AS prop_name, ip.value SELECT
i.id,
i.name,
i.description,
i.wikidata_id,
i.item_order
FROM items i FROM items i
LEFT JOIN item_properties ip ON i.id = ip.item_id WHERE i.url_id = ?
ORDER BY i.item_order ASC
)
SELECT
oi.id,
oi.name,
oi.description,
oi.wikidata_id,
p.name AS prop_name,
ip.value
FROM ordered_items oi
LEFT JOIN item_properties ip ON oi.id = ip.item_id
LEFT JOIN properties p ON ip.property_id = p.id LEFT JOIN properties p ON ip.property_id = p.id
WHERE i.url_id = ?", ORDER BY oi.item_order ASC"
)?; )?;
let mut items: HashMap<String, Item> = HashMap::new();
// Change from HashMap to Vec to preserve order
let mut items: Vec<Item> = Vec::new();
let mut current_id: Option<String> = None;
let rows = stmt.query_map([url_id], |row| { let rows = stmt.query_map([url_id], |row| {
Ok(( Ok((
@ -399,20 +419,27 @@ mod db_impl {
for row in rows { for row in rows {
let (id, name, desc, wd_id, prop, val) = row?; let (id, name, desc, wd_id, prop, val) = row?;
let item = items.entry(id.clone()).or_insert(Item {
id, if current_id.as_ref() != Some(&id) {
// New item - push to vector
items.push(Item {
id: id.clone(),
name, name,
description: desc, description: desc,
wikidata_id: wd_id, wikidata_id: wd_id,
custom_properties: HashMap::new(), custom_properties: HashMap::new(),
}); });
current_id = Some(id);
}
if let (Some(p), Some(v)) = (prop, val) { if let (Some(p), Some(v)) = (prop, val) {
item.custom_properties.insert(p, v); if let Some(last_item) = items.last_mut() {
last_item.custom_properties.insert(p, v);
}
} }
} }
Ok(items.into_values().collect()) Ok(items)
} }
async fn get_or_create_property( async fn get_or_create_property(
@ -467,22 +494,30 @@ mod db_impl {
Err(e) => return Err(e.into()), Err(e) => return Err(e.into()),
}; };
let max_order: i32 = tx.query_row(
"SELECT COALESCE(MAX(item_order), 0) FROM items WHERE url_id = ?",
[url_id],
|row| row.get(0),
)?;
// 4. Item insertion // 4. Item insertion
log!("[DB] Upserting item"); log!("[DB] Upserting item");
tx.execute( tx.execute(
"INSERT INTO items (id, url_id, name, description, wikidata_id) "INSERT INTO items (id, url_id, name, description, wikidata_id, item_order)
VALUES (?, ?, ?, ?, ?) VALUES (?, ?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET ON CONFLICT(id) DO UPDATE SET
url_id = excluded.url_id, url_id = excluded.url_id,
name = excluded.name, name = excluded.name,
description = excluded.description, description = excluded.description,
wikidata_id = excluded.wikidata_id", wikidata_id = excluded.wikidata_id,
item_order = excluded.item_order",
rusqlite::params![ rusqlite::params![
&item.id, &item.id,
url_id, url_id,
&item.name, &item.name,
&item.description, &item.description,
&item.wikidata_id &item.wikidata_id,
max_order + 1
], ],
)?; )?;
log!("[DB] Item upserted successfully"); log!("[DB] Item upserted successfully");