From 17dedbd36e66d97c7453f85e045ecd8b7f96ff1a Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 3 Oct 2024 00:31:41 +0300 Subject: [PATCH] draft db actions --- src/app.rs | 24 ++++++++++++++++++++++++ src/app/browser/db.rs | 24 ------------------------ src/app/database.rs | 35 ++++++++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 31 deletions(-) delete mode 100644 src/app/browser/db.rs diff --git a/src/app.rs b/src/app.rs index 3a2335fd..ab547e7b 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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 + } + } } diff --git a/src/app/browser/db.rs b/src/app/browser/db.rs deleted file mode 100644 index ed8aae00..00000000 --- a/src/app/browser/db.rs +++ /dev/null @@ -1,24 +0,0 @@ -/* @TODO -use std::sync::Arc; - -pub struct Browser { - connection: Arc, -} - -impl Browser { - // Construct new browser DB (connection) - pub fn new(connection: Arc) -> 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) {} -}*/ diff --git a/src/app/database.rs b/src/app/database.rs index d146d61a..3552d207 100644 --- a/src/app/database.rs +++ b/src/app/database.rs @@ -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, + connection: Arc, } impl Database { - // Construct new application DB pub fn init(connection: Arc) -> 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 { + 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 { + 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() } }