draft app records selection

This commit is contained in:
yggverse 2024-10-04 02:22:37 +03:00
parent efa97b820e
commit 9ea3091174

View file

@ -1,11 +1,11 @@
use sqlite::Connection; use sqlite::{Connection, Error};
use std::sync::Arc; use std::sync::Arc;
const DEBUG: bool = true; // @TODO const DEBUG: bool = true; // @TODO
enum Table { pub struct Table {
Id, pub id: i64,
Time, pub time: i64,
} }
pub struct Database { pub struct Database {
@ -42,11 +42,29 @@ impl Database {
}; };
} }
pub fn clean(&self) -> Result<usize, String> { pub fn records(&self) -> Result<Vec<Table>, Error> {
return match self.connection.execute("DELETE FROM `app`", []) { let mut records: Vec<Table> = 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<usize, String> {
return match self
.connection
.execute("DELETE FROM `app` WHERE `id` = ?", [id])
{
Ok(total) => { Ok(total) => {
if DEBUG { if DEBUG {
println!("Deleted {total} rows from `app` table"); println!("Deleted {total} row(s) from `app` table");
} }
Ok(total) Ok(total)
} }