draft db actions

This commit is contained in:
yggverse 2024-10-03 00:31:41 +03:00
parent ad3f6083e1
commit 17dedbd36e
3 changed files with 52 additions and 31 deletions

View File

@ -113,4 +113,28 @@ impl App {
pub fn run(&self) -> ExitCode {
self.app.run()
}
pub fn save(&self) {
// Cleanup previous record
match self.database.clean() {
Ok(_) => {
// Delegate clean action to children components
// self.browser.clean(app_id) @TODO
// ..
// Create new record
match self.database.add() {
Ok(_) => {
// let app_id = self.database.last_insert_id();
// Delegate save action to children components
// self.browser.save(app_id) @TODO
// ..
}
Err(error) => panic!("{error}"), // @TODO
}
}
Err(error) => panic!("{error}"), // @TODO
}
}
}

View File

@ -1,24 +0,0 @@
/* @TODO
use std::sync::Arc;
pub struct Browser {
connection: Arc<sqlite::Connection>,
}
impl Browser {
// Construct new browser DB (connection)
pub fn new(connection: Arc<sqlite::Connection>) -> Browser {
let this = Self { connection };
this.init();
this
}
// Create browser table in DB if not exist yet
fn init(&self) {}
// Save active browser session to DB
fn save(&self) {}
// Restore previous browser session from DB
fn restore(&self) {}
}*/

View File

@ -1,17 +1,18 @@
use sqlite::Connection;
use std::sync::Arc;
const DEBUG: bool = true; // @TODO
enum Table {
Id,
Time,
}
pub struct Database {
connection: Arc<sqlite::Connection>,
connection: Arc<Connection>,
}
impl Database {
// Construct new application DB
pub fn init(connection: Arc<Connection>) -> Database {
// Init app table
if let Err(error) = connection.execute(
@ -22,18 +23,38 @@ impl Database {
)",
[],
) {
panic!("{error}");
panic!("{error}"); // @TODO
}
// Return struct
Self { connection }
}
pub fn add(&self) -> i64 {
if let Err(error) = self.connection.execute("INSERT INTO `app`", []) {
panic!("{error}");
}
pub fn add(&self) -> Result<usize, String> {
return match self.connection.execute("INSERT INTO `app`", []) {
Ok(total) => {
if DEBUG {
println!("Inserted {total} row to `app` table");
}
Ok(total)
}
Err(error) => Err(error.to_string()),
};
}
pub fn clean(&self) -> Result<usize, String> {
return match self.connection.execute("DELETE FROM `app`", []) {
Ok(total) => {
if DEBUG {
println!("Deleted {total} rows from `app` table");
}
Ok(total)
}
Err(error) => Err(error.to_string()),
};
}
pub fn last_insert_id(&self) -> i64 {
self.connection.last_insert_rowid()
}
}