From 9ea30911745f8a96e3487a0c30d52b9c22fccd31 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 4 Oct 2024 02:22:37 +0300 Subject: [PATCH] draft app records selection --- src/app/database.rs | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/src/app/database.rs b/src/app/database.rs index 3552d207..134d2637 100644 --- a/src/app/database.rs +++ b/src/app/database.rs @@ -1,11 +1,11 @@ -use sqlite::Connection; +use sqlite::{Connection, Error}; use std::sync::Arc; const DEBUG: bool = true; // @TODO -enum Table { - Id, - Time, +pub struct Table { + pub id: i64, + pub time: i64, } pub struct Database { @@ -42,11 +42,29 @@ impl Database { }; } - pub fn clean(&self) -> Result { - return match self.connection.execute("DELETE FROM `app`", []) { + pub fn records(&self) -> Result, Error> { + let mut records: Vec = Vec::new(); + + let mut statement = self.connection.prepare("SELECT `id`, `time` FROM `app`")?; + let _ = statement.query_map([], |row| { + records.push(Table { + id: row.get(0)?, + time: row.get(1)?, + }); + Ok(()) + }); + + Ok(records) + } + + pub fn delete(&self, id: i64) -> Result { + return match self + .connection + .execute("DELETE FROM `app` WHERE `id` = ?", [id]) + { Ok(total) => { if DEBUG { - println!("Deleted {total} rows from `app` table"); + println!("Deleted {total} row(s) from `app` table"); } Ok(total) }