diff --git a/src/db.rs b/src/db.rs index 53161b7..82e5d93 100644 --- a/src/db.rs +++ b/src/db.rs @@ -330,6 +330,28 @@ pub fn get_item(conn: &Connection, item_id: i64) -> Result> { } } +pub fn get_item_last(conn: &Connection) -> Result> { + debug!("DB: Getting last item"); + let mut statement = conn + .prepare_cached(" + SELECT id, ts, size, compression + FROM items + ORDER BY id DESC + LIMIT 1") + .context("Problem preparing SQL statement")?; + + let mut rows = statement.query([])?; + + match rows.next()? { + Some(row) => Ok(Some(Item { + id: row.get(0)?, + ts: row.get(1)?, + size: row.get(2)?, + compression: row.get(3)? + })), + None => Ok(None) + } +} pub fn get_item_tags(conn: &Connection, item: &Item) -> Result> { debug!("DB: Getting tags for item: {:?}", item); diff --git a/src/main.rs b/src/main.rs index d104ac9..8cc0ae3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -394,9 +394,7 @@ fn mode_save(cmd: &mut Command, args: Args, ids: &mut Vec, tags: &mut Vec, tags: &mut Vec, conn: &mut Connection, data_path: PathBuf) -> Result<()> { - if ids.is_empty() && tags.is_empty() { - cmd.error(ErrorKind::InvalidValue, "No ID or tags given, you must supply exactly one ID or atleast one tag when using --get").exit(); - } else if ! ids.is_empty() && ! tags.is_empty() { + if ! ids.is_empty() && ! tags.is_empty() { cmd.error(ErrorKind::InvalidValue, "Both ID and tags given, you must supply exactly one ID or atleast one tag when using --get").exit(); } else if ids.len() > 1 { cmd.error(ErrorKind::InvalidValue, "More than one ID given, you must supply exactly one ID or atleast one tag when using --get").exit(); @@ -411,7 +409,7 @@ fn mode_get(cmd: &mut Command, args: Args, ids: &mut Vec, tags: &mut Vec match ids.iter().next() { Some(item_id) => db::get_item(conn, *item_id)?, - None => None + None => db::get_item_last(conn)? }, false => db::get_item_matching(conn, tags, &meta)? };