From f44d4db3cbd6bc54424365d32d622af93e2f4467 Mon Sep 17 00:00:00 2001 From: alemidev Date: Mon, 14 Mar 2022 14:14:14 +0100 Subject: [PATCH] show memos with expiration date first --- src/storage.rs | 38 ++++++++++++++++++++++++++++++-------- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/src/storage.rs b/src/storage.rs index ce97ca0..f700a90 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -53,16 +53,38 @@ pub fn open_sqlite_storage(path: &str) -> Result { impl MemoStorage for SQLiteStorage { fn all(&self) -> Result, Error> { - let mut statement = self.conn.prepare("SELECT * FROM memo ORDER BY due, id")?; - let mut rows = statement.query([])?; let mut results = Vec::new(); - while let Some(row) = rows.next()? { - results.push(Memo { - id: row.get(0)?, - body: row.get(1)?, - due: row.get(2)?, - }); + /* + * SQLite considers NULL as smallest value, so we will always get events with no due date + * first. To circumvent this, we first query all memos with a due date, and then all + * others. This is kinda jank but will do for now. + */ + + { + let mut statement = self.conn.prepare("SELECT * FROM memo WHERE due IS NOT NULL ORDER BY due, id")?; + let mut rows = statement.query([])?; + + while let Some(row) = rows.next()? { + results.push(Memo { + id: row.get(0)?, + body: row.get(1)?, + due: row.get(2)?, + }); + } + } + + { + let mut statement = self.conn.prepare("SELECT * FROM memo WHERE due IS NULL ORDER BY due, id")?; + let mut rows = statement.query([])?; + + while let Some(row) = rows.next()? { + results.push(Memo { + id: row.get(0)?, + body: row.get(1)?, + due: row.get(2)?, + }); + } } return Ok(results);