From a1f2d57b6de4d67487bed049776e5379465cd839 Mon Sep 17 00:00:00 2001 From: yggverse Date: Mon, 7 Oct 2024 20:34:48 +0300 Subject: [PATCH] make transactionable mathods static --- src/app.rs | 15 +++++-------- src/app/browser.rs | 16 +++++--------- src/app/browser/database.rs | 12 ++++------- src/app/browser/widget.rs | 15 +++++-------- src/app/browser/widget/database.rs | 11 +++------- src/app/browser/window.rs | 20 ++++++------------ src/app/browser/window/database.rs | 12 ++++------- src/app/browser/window/tab.rs | 15 +++++-------- src/app/browser/window/tab/database.rs | 15 +++---------- src/app/browser/window/tab/label.rs | 22 +++++--------------- src/app/browser/window/tab/label/database.rs | 15 +++---------- src/app/database.rs | 12 ++++------- 12 files changed, 52 insertions(+), 128 deletions(-) diff --git a/src/app.rs b/src/app.rs index d0ee9941..ff80175d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -37,9 +37,6 @@ impl App { profile_database_connection: Arc>, profile_path: PathBuf, ) -> Self { - // Init database - let database = Arc::new(Database::new()); - // Init actions let action_tool_debug = Action::new("win", true); let action_tool_profile_directory = Action::new("win", true); @@ -111,7 +108,6 @@ impl App { gobject.connect_startup({ let browser = browser.clone(); - let database = database.clone(); let profile_database_connection = profile_database_connection.clone(); move |this| { // Init readable connection @@ -121,7 +117,7 @@ impl App { match connection.unchecked_transaction() { Ok(transaction) => { // Restore previous session from DB - match database.records(&transaction) { + match Database::records(&transaction) { Ok(records) => { for record in records { browser.restore(&transaction, &record.id); @@ -147,7 +143,6 @@ impl App { gobject.connect_shutdown({ // let browser = browser.clone(); let profile_database_connection = profile_database_connection.clone(); - let database = database.clone(); let browser = browser.clone(); move |_| { // Init writable connection @@ -156,11 +151,11 @@ impl App { // Create transaction match connection.transaction() { Ok(transaction) => { - match database.records(&transaction) { + match Database::records(&transaction) { Ok(records) => { // Cleanup previous session records for record in records { - match database.delete(&transaction, &record.id) { + match Database::delete(&transaction, &record.id) { Ok(_) => { // Delegate clean action to childs browser.clean(&transaction, &record.id); @@ -170,12 +165,12 @@ impl App { } // Save current session to DB - match database.add(&transaction) { + match Database::add(&transaction) { Ok(_) => { // Delegate save action to childs browser.save( &transaction, - &database.last_insert_id(&transaction), + &Database::last_insert_id(&transaction), ); } Err(e) => todo!("{e}"), diff --git a/src/app/browser.rs b/src/app/browser.rs index 124b463a..370f9841 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -17,8 +17,6 @@ use sqlite::Transaction; use std::{path::PathBuf, sync::Arc}; pub struct Browser { - // Extras - database: Arc, // Components // header: Arc
, window: Arc, @@ -44,9 +42,6 @@ impl Browser { action_tab_page_navigation_reload: Arc, action_tab_pin: Arc, ) -> Browser { - // Init database - let database = Arc::new(Database::new()); - // Init components let header = Arc::new(Header::new( action_tool_debug.clone(), @@ -189,7 +184,6 @@ impl Browser { // Return new activated Browser struct Self { - database, widget, // header, window, @@ -198,10 +192,10 @@ impl Browser { // Actions pub fn clean(&self, tx: &Transaction, app_id: &i64) { - match self.database.records(tx, app_id) { + match Database::records(tx, app_id) { Ok(records) => { for record in records { - match self.database.delete(tx, &record.id) { + match Database::delete(tx, &record.id) { Ok(_) => { // Delegate clean action to childs // @TODO @@ -218,7 +212,7 @@ impl Browser { } pub fn restore(&self, tx: &Transaction, app_id: &i64) { - match self.database.records(tx, app_id) { + match Database::records(tx, app_id) { Ok(records) => { for record in records { // Delegate restore action to childs @@ -233,10 +227,10 @@ impl Browser { } pub fn save(&self, tx: &Transaction, app_id: &i64) { - match self.database.add(tx, app_id) { + match Database::add(tx, app_id) { Ok(_) => { // Delegate save action to childs - let id = self.database.last_insert_id(tx); + let id = Database::last_insert_id(tx); // @TODO // self.header.save(id); diff --git a/src/app/browser/database.rs b/src/app/browser/database.rs index a20b986a..fba87236 100644 --- a/src/app/browser/database.rs +++ b/src/app/browser/database.rs @@ -10,10 +10,6 @@ pub struct Database { } impl Database { - pub fn new() -> Self { - Self {} - } - pub fn init(tx: &Transaction) -> Result { tx.execute( "CREATE TABLE IF NOT EXISTS `app_browser` @@ -25,11 +21,11 @@ impl Database { ) } - pub fn add(&self, tx: &Transaction, app_id: &i64) -> Result { + pub fn add(tx: &Transaction, app_id: &i64) -> Result { tx.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id]) } - pub fn records(&self, tx: &Transaction, app_id: &i64) -> Result, Error> { + pub fn records(tx: &Transaction, app_id: &i64) -> Result, Error> { let mut stmt = tx.prepare("SELECT `id`, `app_id` FROM `app_browser` WHERE `app_id` = ?")?; let result = stmt.query_map([app_id], |row| { @@ -49,11 +45,11 @@ impl Database { Ok(records) } - pub fn delete(&self, tx: &Transaction, id: &i64) -> Result { + pub fn delete(tx: &Transaction, id: &i64) -> Result { tx.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id]) } - pub fn last_insert_id(&self, tx: &Transaction) -> i64 { + pub fn last_insert_id(tx: &Transaction) -> i64 { tx.last_insert_rowid() } } diff --git a/src/app/browser/widget.rs b/src/app/browser/widget.rs index 059e2ae0..fa22ae0b 100644 --- a/src/app/browser/widget.rs +++ b/src/app/browser/widget.rs @@ -4,7 +4,6 @@ use database::Database; use gtk::{prelude::GtkWindowExt, ApplicationWindow, Box, HeaderBar}; use sqlite::Transaction; -use std::sync::Arc; // Default options const DEFAULT_HEIGHT: i32 = 480; @@ -12,16 +11,12 @@ const DEFAULT_WIDTH: i32 = 640; const MAXIMIZED: bool = false; pub struct Widget { - database: Arc, gobject: ApplicationWindow, } impl Widget { // Construct pub fn new(titlebar: &HeaderBar, child: &Box) -> Self { - // Init database - let database = Arc::new(Database::new()); - // Init GTK let gobject = ApplicationWindow::builder() .titlebar(titlebar) @@ -32,15 +27,15 @@ impl Widget { .build(); // Return new struct - Self { database, gobject } + Self { gobject } } // Actions pub fn clean(&self, tx: &Transaction, app_browser_id: &i64) { - match self.database.records(tx, app_browser_id) { + match Database::records(tx, app_browser_id) { Ok(records) => { for record in records { - match self.database.delete(tx, &record.id) { + match Database::delete(tx, &record.id) { Ok(_) => { // Delegate clean action to childs // nothing yet.. @@ -54,7 +49,7 @@ impl Widget { } pub fn restore(&self, tx: &Transaction, app_browser_id: &i64) { - match self.database.records(tx, app_browser_id) { + match Database::records(tx, app_browser_id) { Ok(records) => { for record in records { // Restore widget @@ -71,7 +66,7 @@ impl Widget { } pub fn save(&self, tx: &Transaction, app_browser_id: &i64) { - match self.database.add( + match Database::add( tx, app_browser_id, &self.gobject.default_width(), diff --git a/src/app/browser/widget/database.rs b/src/app/browser/widget/database.rs index 0ed91b4b..7e1948ef 100644 --- a/src/app/browser/widget/database.rs +++ b/src/app/browser/widget/database.rs @@ -13,10 +13,6 @@ pub struct Database { } impl Database { - pub fn new() -> Self { - Self {} - } - pub fn init(tx: &Transaction) -> Result { tx.execute( "CREATE TABLE IF NOT EXISTS `app_browser_widget` @@ -32,7 +28,6 @@ impl Database { } pub fn add( - &self, tx: &Transaction, app_browser_id: &i64, default_width: &i32, @@ -55,7 +50,7 @@ impl Database { ) } - pub fn records(&self, tx: &Transaction, app_browser_id: &i64) -> Result, Error> { + pub fn records(tx: &Transaction, app_browser_id: &i64) -> Result, Error> { let mut stmt = tx.prepare( "SELECT `id`, `app_browser_id`, @@ -84,12 +79,12 @@ impl Database { Ok(records) } - pub fn delete(&self, tx: &Transaction, id: &i64) -> Result { + pub fn delete(tx: &Transaction, id: &i64) -> Result { tx.execute("DELETE FROM `app_browser_widget` WHERE `id` = ?", [id]) } /* not in use - pub fn last_insert_id(&self, tx: &Transaction) -> i64 { + pub fn last_insert_id(tx: &Transaction) -> i64 { tx.last_insert_rowid() } */ } diff --git a/src/app/browser/window.rs b/src/app/browser/window.rs index cf309d93..2d0bad36 100644 --- a/src/app/browser/window.rs +++ b/src/app/browser/window.rs @@ -12,7 +12,6 @@ use std::sync::Arc; use gtk::{gio::SimpleAction, glib::GString, Box}; pub struct Window { - database: Arc, tab: Arc, widget: Arc, } @@ -27,9 +26,6 @@ impl Window { action_tab_page_navigation_reload: Arc, action_update: Arc, ) -> Self { - // Init database - let database = Arc::new(Database::new()); - // Init components let tab = Arc::new(Tab::new( action_tab_page_navigation_base, @@ -44,11 +40,7 @@ impl Window { let widget = Arc::new(Widget::new(tab.gobject())); // Init struct - Self { - database, - tab, - widget, - } + Self { tab, widget } } // Actions @@ -89,10 +81,10 @@ impl Window { } pub fn clean(&self, tx: &Transaction, app_browser_id: &i64) { - match self.database.records(tx, app_browser_id) { + match Database::records(tx, app_browser_id) { Ok(records) => { for record in records { - match self.database.delete(tx, &record.id) { + match Database::delete(tx, &record.id) { Ok(_) => { // Delegate clean action to childs self.tab.clean(tx, &record.id); @@ -106,7 +98,7 @@ impl Window { } pub fn restore(&self, tx: &Transaction, app_browser_id: &i64) { - match self.database.records(tx, app_browser_id) { + match Database::records(tx, app_browser_id) { Ok(records) => { for record in records { // Delegate restore action to childs @@ -118,10 +110,10 @@ impl Window { } pub fn save(&self, tx: &Transaction, app_browser_id: &i64) { - match self.database.add(tx, app_browser_id) { + match Database::add(tx, app_browser_id) { Ok(_) => { // Delegate save action to childs - self.tab.save(tx, &self.database.last_insert_id(tx)); + self.tab.save(tx, &Database::last_insert_id(tx)); } Err(e) => todo!("{e}"), } diff --git a/src/app/browser/window/database.rs b/src/app/browser/window/database.rs index c6d461e1..182ac3a1 100644 --- a/src/app/browser/window/database.rs +++ b/src/app/browser/window/database.rs @@ -10,10 +10,6 @@ pub struct Database { } impl Database { - pub fn new() -> Self { - Self {} - } - pub fn init(tx: &Transaction) -> Result { tx.execute( "CREATE TABLE IF NOT EXISTS `app_browser_window` @@ -25,14 +21,14 @@ impl Database { ) } - pub fn add(&self, tx: &Transaction, app_browser_id: &i64) -> Result { + pub fn add(tx: &Transaction, app_browser_id: &i64) -> Result { tx.execute( "INSERT INTO `app_browser_window` (`app_browser_id`) VALUES (?)", [app_browser_id], ) } - pub fn records(&self, tx: &Transaction, app_browser_id: &i64) -> Result, Error> { + pub fn records(tx: &Transaction, app_browser_id: &i64) -> Result, Error> { let mut stmt = tx.prepare( "SELECT `id`, `app_browser_id` FROM `app_browser_window` @@ -56,11 +52,11 @@ impl Database { Ok(records) } - pub fn delete(&self, tx: &Transaction, id: &i64) -> Result { + pub fn delete(tx: &Transaction, id: &i64) -> Result { tx.execute("DELETE FROM `app_browser_window` WHERE `id` = ?", [id]) } - pub fn last_insert_id(&self, tx: &Transaction) -> i64 { + pub fn last_insert_id(tx: &Transaction) -> i64 { tx.last_insert_rowid() } } diff --git a/src/app/browser/window/tab.rs b/src/app/browser/window/tab.rs index 20d4f3d5..f799b084 100644 --- a/src/app/browser/window/tab.rs +++ b/src/app/browser/window/tab.rs @@ -26,7 +26,6 @@ pub struct TabItem { // Main pub struct Tab { - database: Arc, // Actions action_tab_page_navigation_base: Arc, action_tab_page_navigation_history_back: Arc, @@ -49,9 +48,6 @@ impl Tab { action_tab_page_navigation_reload: Arc, action_update: Arc, ) -> Self { - // Init database - let database = Arc::new(Database::new()); - // Init empty HashMap index as no tabs appended yet let index = RefCell::new(HashMap::new()); @@ -60,7 +56,6 @@ impl Tab { // Return non activated struct Self { - database, // Define action links action_tab_page_navigation_base, action_tab_page_navigation_history_back, @@ -219,10 +214,10 @@ impl Tab { } pub fn clean(&self, tx: &Transaction, app_browser_window_id: &i64) { - match self.database.records(tx, app_browser_window_id) { + match Database::records(tx, app_browser_window_id) { Ok(records) => { for record in records { - match self.database.delete(tx, &record.id) { + match Database::delete(tx, &record.id) { Ok(_) => { // Delegate clean action to childs for (_, item) in self.index.borrow().iter() { @@ -239,7 +234,7 @@ impl Tab { } pub fn restore(&self, tx: &Transaction, app_browser_window_id: &i64) { - match self.database.records(tx, app_browser_window_id) { + match Database::records(tx, app_browser_window_id) { Ok(records) => { for record in records { let item = self.append(None, record.is_current); @@ -256,7 +251,7 @@ impl Tab { let mut page_number = 0; for (_, item) in self.index.borrow().iter() { - match self.database.add( + match Database::add( tx, app_browser_window_id, &match self.widget.gobject().current_page() { @@ -266,7 +261,7 @@ impl Tab { ) { Ok(_) => { // Delegate save action to childs - let id = self.database.last_insert_id(tx); + let id = Database::last_insert_id(tx); item.label.save(tx, &id); diff --git a/src/app/browser/window/tab/database.rs b/src/app/browser/window/tab/database.rs index 5124a850..80b6b16b 100644 --- a/src/app/browser/window/tab/database.rs +++ b/src/app/browser/window/tab/database.rs @@ -11,10 +11,6 @@ pub struct Database { } impl Database { - pub fn new() -> Self { - Self {} - } - pub fn init(tx: &Transaction) -> Result { tx.execute( "CREATE TABLE IF NOT EXISTS `app_browser_window_tab` @@ -28,7 +24,6 @@ impl Database { } pub fn add( - &self, tx: &Transaction, app_browser_window_id: &i64, is_current: &bool, @@ -42,11 +37,7 @@ impl Database { ) } - pub fn records( - &self, - tx: &Transaction, - app_browser_window_id: &i64, - ) -> Result, Error> { + pub fn records(tx: &Transaction, app_browser_window_id: &i64) -> Result, Error> { let mut stmt = tx.prepare( "SELECT `id`, `app_browser_window_id`, @@ -72,11 +63,11 @@ impl Database { Ok(records) } - pub fn delete(&self, tx: &Transaction, id: &i64) -> Result { + pub fn delete(tx: &Transaction, id: &i64) -> Result { tx.execute("DELETE FROM `app_browser_window_tab` WHERE `id` = ?", [id]) } - pub fn last_insert_id(&self, tx: &Transaction) -> i64 { + pub fn last_insert_id(tx: &Transaction) -> i64 { tx.last_insert_rowid() } } diff --git a/src/app/browser/window/tab/label.rs b/src/app/browser/window/tab/label.rs index 7506d8a0..737fb7a2 100644 --- a/src/app/browser/window/tab/label.rs +++ b/src/app/browser/window/tab/label.rs @@ -13,7 +13,6 @@ use gtk::{glib::GString, Box}; use std::sync::Arc; pub struct Label { - database: Arc, // Components pin: Arc, title: Arc, @@ -24,9 +23,6 @@ pub struct Label { impl Label { // Construct pub fn new(name: GString, is_pinned: bool) -> Label { - // Init database - let database = Arc::new(Database::new()); - // Components let pin = Arc::new(Pin::new(is_pinned)); let title = Arc::new(Title::new()); @@ -35,20 +31,15 @@ impl Label { let widget = Arc::new(Widget::new(name, pin.gobject(), title.gobject())); // Result - Self { - database, - pin, - title, - widget, - } + Self { pin, title, widget } } // Actions pub fn clean(&self, tx: &Transaction, app_browser_window_tab_id: &i64) { - match self.database.records(tx, app_browser_window_tab_id) { + match Database::records(tx, app_browser_window_tab_id) { Ok(records) => { for record in records { - match self.database.delete(tx, &record.id) { + match Database::delete(tx, &record.id) { Ok(_) => { // Delegate clean action to childs // nothing yet.. @@ -62,7 +53,7 @@ impl Label { } pub fn restore(&self, tx: &Transaction, app_browser_window_tab_id: &i64) { - match self.database.records(tx, app_browser_window_tab_id) { + match Database::records(tx, app_browser_window_tab_id) { Ok(records) => { for record in records { self.pin(record.is_pinned); @@ -76,10 +67,7 @@ impl Label { } pub fn save(&self, tx: &Transaction, app_browser_window_tab_id: &i64) { - match self - .database - .add(tx, app_browser_window_tab_id, &self.is_pinned()) - { + match Database::add(tx, app_browser_window_tab_id, &self.is_pinned()) { Ok(_) => { // Delegate save action to childs // nothing yet.. diff --git a/src/app/browser/window/tab/label/database.rs b/src/app/browser/window/tab/label/database.rs index 7cce0401..361cf33e 100644 --- a/src/app/browser/window/tab/label/database.rs +++ b/src/app/browser/window/tab/label/database.rs @@ -11,10 +11,6 @@ pub struct Database { } impl Database { - pub fn new() -> Self { - Self {} - } - pub fn init(tx: &Transaction) -> Result<usize, Error> { tx.execute( "CREATE TABLE IF NOT EXISTS `app_browser_window_tab_label` @@ -28,7 +24,6 @@ impl Database { } pub fn add( - &self, tx: &Transaction, app_browser_window_tab_id: &i64, is_pinned: &bool, @@ -42,11 +37,7 @@ impl Database { ) } - pub fn records( - &self, - tx: &Transaction, - app_browser_window_tab_id: &i64, - ) -> Result<Vec<Table>, Error> { + pub fn records(tx: &Transaction, app_browser_window_tab_id: &i64) -> Result<Vec<Table>, Error> { let mut stmt = tx.prepare( "SELECT `id`, `app_browser_window_tab_id`, @@ -72,7 +63,7 @@ impl Database { Ok(records) } - pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> { + pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> { tx.execute( "DELETE FROM `app_browser_window_tab_label` WHERE `id` = ?", [id], @@ -80,7 +71,7 @@ impl Database { } /* not in use - pub fn last_insert_id(&self, tx: &Transaction) -> i64 { + pub fn last_insert_id(tx: &Transaction) -> i64 { tx.last_insert_rowid() } */ } diff --git a/src/app/database.rs b/src/app/database.rs index dd6a0405..dddb5d39 100644 --- a/src/app/database.rs +++ b/src/app/database.rs @@ -9,10 +9,6 @@ pub struct Database { } impl Database { - pub fn new() -> Self { - Self {} - } - pub fn init(tx: &Transaction) -> Result<usize, Error> { tx.execute( "CREATE TABLE IF NOT EXISTS `app` @@ -23,11 +19,11 @@ impl Database { ) } - pub fn add(&self, tx: &Transaction) -> Result<usize, Error> { + pub fn add(tx: &Transaction) -> Result<usize, Error> { tx.execute("INSERT INTO `app` DEFAULT VALUES", []) } - pub fn records(&self, tx: &Transaction) -> Result<Vec<Table>, Error> { + pub fn records(tx: &Transaction) -> Result<Vec<Table>, Error> { let mut stmt = tx.prepare("SELECT `id` FROM `app`")?; let result = stmt.query_map([], |row| Ok(Table { id: row.get(0)? }))?; @@ -41,11 +37,11 @@ impl Database { Ok(records) } - pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> { + pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> { tx.execute("DELETE FROM `app` WHERE `id` = ?", [id]) } - pub fn last_insert_id(&self, tx: &Transaction) -> i64 { + pub fn last_insert_id(tx: &Transaction) -> i64 { tx.last_insert_rowid() } }