very happy cat db
cargo devel CI / cargo CI (push) Successful in 2m40s Details

This commit is contained in:
Christoph J. Scherr 2024-06-26 12:10:13 +02:00
parent b43053b716
commit 1a71f1932b
1 changed files with 10 additions and 21 deletions

View File

@ -33,8 +33,8 @@ fn interactive_add_cat(conn: &Connection) -> anyhow::Result<usize> {
let cat_id = new_cat(conn, &cat_name, get_color_id(conn, &cat_color)?)?; let cat_id = new_cat(conn, &cat_name, get_color_id(conn, &cat_color)?)?;
assert!(check_if_cat_exists(conn, cat_id)?); assert!(check_if_cat_exists(conn, cat_id)?);
println!("your cat has id {cat_id}"); println!("\n-> your cat has id {cat_id}");
println!("your cat has color '{}'", get_cat_color(conn, cat_id)?); println!("-> your cat has color '{}'", get_cat_color(conn, cat_id)?);
Ok(cat_id) Ok(cat_id)
} }
@ -60,7 +60,7 @@ fn interactive_find_cat(conn: &Connection) -> anyhow::Result<()> {
))?; ))?;
let mut fitting_cats = stmt.query([cat_name, cat_color])?; let mut fitting_cats = stmt.query([cat_name, cat_color])?;
println!("These cats might fit your description:\n"); println!("\nThese cats might fit your description:\n");
print_cats(conn, &mut fitting_cats)?; print_cats(conn, &mut fitting_cats)?;
Ok(()) Ok(())
@ -88,6 +88,7 @@ fn interactive_delete(conn: &Connection, buf: &mut String) -> anyhow::Result<()>
let mut stmt = conn.prepare(&format!("DELETE FROM {TABLE_CAT} WHERE id = (?1)"))?; let mut stmt = conn.prepare(&format!("DELETE FROM {TABLE_CAT} WHERE id = (?1)"))?;
for n in nums { for n in nums {
stmt.execute([n])?; stmt.execute([n])?;
println!("deleted cat with id {n}");
} }
} }
"COLOR" => { "COLOR" => {
@ -116,38 +117,26 @@ fn interactive_delete(conn: &Connection, buf: &mut String) -> anyhow::Result<()>
as they have the color id {color_id}. Type 'YES' to confirm." as they have the color id {color_id}. Type 'YES' to confirm."
); );
print_cats(conn, &mut cats)?; print_cats(conn, &mut cats)?;
drop(cats); // we need to renew this, because Rows is not Clone and we have
// consumed the Rows in print_cats. The Rows index can not be reset,
// probably because of a sqlite API limitation.
let mut cats = stmt_cats_with_color.query([color_id])?;
let mut cat_ids: Vec<usize> = Vec::new();
while let Some(cat) = cats.next()? {
cat_ids.push(cat.get::<_, usize>(0)?);
}
buf.clear(); buf.clear();
let _ = stdin.lock().read_line(buf); // wait for enter as confirmation let _ = stdin.lock().read_line(buf); // wait for enter as confirmation
*buf = buf.trim().to_string(); *buf = buf.trim().to_string();
*buf = buf.to_uppercase().to_string(); *buf = buf.to_uppercase().to_string();
dbg!(&buf);
if buf.as_str() != "YES" { if buf.as_str() != "YES" {
continue; continue;
} }
println!();
let mut stmt = let mut stmt =
conn.prepare(&format!("DELETE FROM {TABLE_CAT} WHERE id = (?1)"))?; conn.prepare(&format!("DELETE FROM {TABLE_CAT} WHERE color_id = (?1)"))?;
for cat_id in cat_ids { stmt.execute([color_id])?;
stmt.execute([cat_id])?; println!("-> deleted cats with color_id {color_id}");
}
} }
let mut stmt = let mut stmt =
conn.prepare(&format!("DELETE FROM {TABLE_CAT_COLOR} WHERE id = (?1)"))?; conn.prepare(&format!("DELETE FROM {TABLE_CAT_COLOR} WHERE id = (?1)"))?;
for n in &nums { stmt.execute([color_id])?;
stmt.execute([n])?; println!("-> deleted color with id {color_id}");
}
} }
println!("deleted ids {nums:?}");
} }
_ => { _ => {
println!("{USAGE_DELETE}"); println!("{USAGE_DELETE}");