Compare commits

..

No commits in common. "896de305ccd1779205ba29e0dda7b88342f086ef" and "8ac1d77e06060b5d53d3c4ab3ebec555d8d530ef" have entirely different histories.

View file

@ -261,7 +261,6 @@ 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
);", );",
) )
@ -378,68 +377,42 @@ 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(
"WITH ordered_items AS ( "SELECT i.id, i.name, i.description, i.wikidata_id,
SELECT p.name AS prop_name, ip.value
i.id, FROM items i
i.name, LEFT JOIN item_properties ip ON i.id = ip.item_id
i.description, LEFT JOIN properties p ON ip.property_id = p.id
i.wikidata_id, WHERE i.url_id = ?",
i.item_order
FROM items i
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
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((
row.get::<_, String>(0)?, // id row.get::<_, String>(0)?, // id
row.get::<_, String>(1)?, // name row.get::<_, String>(1)?, // name
row.get::<_, String>(2)?, // description row.get::<_, String>(2)?, // description
row.get::<_, Option<String>>(3)?, // wikidata_id row.get::<_, Option<String>>(3)?, // wikidata_id
row.get::<_, Option<String>>(4)?, // prop_name row.get::<_, Option<String>>(4)?, // prop_name
row.get::<_, Option<String>>(5)?, // value row.get::<_, Option<String>>(5)?, // value
)) ))
})?; })?;
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 {
if current_id.as_ref() != Some(&id) { id,
// New item - push to vector name,
items.push(Item { description: desc,
id: id.clone(), wikidata_id: wd_id,
name, custom_properties: HashMap::new(),
description: desc, });
wikidata_id: wd_id,
custom_properties: HashMap::new(),
});
current_id = Some(id);
}
if let (Some(p), Some(v)) = (prop, val) { if let (Some(p), Some(v)) = (prop, val) {
if let Some(last_item) = items.last_mut() { item.custom_properties.insert(p, v);
last_item.custom_properties.insert(p, v);
}
} }
} }
Ok(items) Ok(items.into_values().collect())
} }
async fn get_or_create_property( async fn get_or_create_property(
@ -494,30 +467,22 @@ 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, item_order) "INSERT INTO items (id, url_id, name, description, wikidata_id)
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");