From 32eb53ba2bd6af2cef82ff8fb8f14a39e0f86c7e Mon Sep 17 00:00:00 2001 From: alemidev Date: Mon, 21 Mar 2022 00:44:55 +0100 Subject: [PATCH] store done time --- src/storage.rs | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/storage.rs b/src/storage.rs index 1950570..a3f9e67 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -46,7 +46,7 @@ pub fn open_sqlite_storage(path: &str) -> Result { id INTEGER PRIMARY KEY, body TEXT NOT NULL, due DATETIME, - done BOOL DEFAULT FALSE + done DATETIME DEFAULT NULL );", [], )?; @@ -56,6 +56,7 @@ pub fn open_sqlite_storage(path: &str) -> Result { impl MemoStorage for SQLiteStorage { fn all(&self, done: bool) -> Result, Error> { let mut results = Vec::new(); + let not_null = if done { "NOT" } else { "" }; /* * SQLite considers NULL as smallest value, so we will always get events with no due date @@ -65,9 +66,13 @@ impl MemoStorage for SQLiteStorage { { let mut statement = self.conn.prepare( - "SELECT * FROM memo WHERE due IS NOT NULL AND done = ? ORDER BY due, id", + format!(// TODO eww but I can't find a way to insert "NOT" with rusqlite + "SELECT * FROM memo WHERE due IS NOT NULL AND done IS {} NULL ORDER BY due, id", + not_null + ) + .as_str(), )?; - let mut rows = statement.query(params![done as u8])?; + let mut rows = statement.query([])?; while let Some(row) = rows.next()? { results.push(Memo { @@ -79,10 +84,14 @@ impl MemoStorage for SQLiteStorage { } { - let mut statement = self - .conn - .prepare("SELECT * FROM memo WHERE due IS NULL AND done = ? ORDER BY due, id")?; - let mut rows = statement.query(params![done as u8])?; + let mut statement = self.conn.prepare( + format!(// TODO eww but I can't find a way to insert "NOT" with rusqlite + "SELECT * FROM memo WHERE due IS NULL AND done IS {} NULL ORDER BY due, id", + not_null + ) + .as_str(), + )?; + let mut rows = statement.query([])?; while let Some(row) = rows.next()? { results.push(Memo { @@ -125,7 +134,7 @@ impl MemoStorage for SQLiteStorage { fn del(&self, id: u32) -> Result { let count = self .conn - .execute("UPDATE memo SET done = 1 WHERE id = ?", params![id])?; + .execute("UPDATE memo SET done = ? WHERE id = ?", params![Utc::now(), id])?; if count > 0 { return Ok(true); } else {