diff --git a/README.md b/README.md index e85db57e..ee4e040f 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Guide and protocol draft * column name for parental ID must have absolute namespace prefix, for example `app_browser_id` column for `app_browser_widget` table. For example, if the table has few parental keys, column set could be `id`, `parent_one_id`, `parent_two_id`, `some_data` * _todo_ * [ ] version control for auto-migrations - * [ ] transactions support for update operations + * [x] transactions support for update operations #### GTK diff --git a/src/app.rs b/src/app.rs index 4918c21a..1b3acd29 100644 --- a/src/app.rs +++ b/src/app.rs @@ -13,7 +13,10 @@ use gtk::{ }; use sqlite::Connection; -use std::{path::PathBuf, sync::Arc}; +use std::{ + path::PathBuf, + sync::{Arc, RwLock}, +}; const APPLICATION_ID: &str = "io.github.yggverse.Yoda"; @@ -30,11 +33,32 @@ pub struct App { impl App { // Construct - pub fn new(profile_database_connection: Arc, profile_path: PathBuf) -> Self { + pub fn new( + profile_database_connection: Arc>, + profile_path: PathBuf, + ) -> Self { // Init database - let database = match Database::init(profile_database_connection.clone()) { - Ok(database) => Arc::new(database), - Err(error) => panic!("{error}"), // @TODO + let database = { + // Init writable database connection + let mut connection = match profile_database_connection.write() { + Ok(connection) => connection, + Err(error) => todo!("{error}"), + }; + + // Init new transaction + let transaction = match connection.transaction() { + Ok(transaction) => transaction, + Err(error) => todo!("{error}"), + }; + + // Init database structure + match Database::init(&transaction) { + Ok(database) => match transaction.commit() { + Ok(_) => Arc::new(database), + Err(error) => todo!("{error}"), + }, + Err(error) => todo!("{error}"), + } }; // Init actions @@ -82,7 +106,7 @@ impl App { // Init components let browser = Arc::new(Browser::new( - profile_database_connection, + profile_database_connection.clone(), profile_path, action_tool_debug.simple(), action_tool_profile_directory.simple(), @@ -110,15 +134,28 @@ impl App { application.connect_startup({ let browser = browser.clone(); let database = database.clone(); + let profile_database_connection = profile_database_connection.clone(); move |this| { - // Restore previous session from DB - match database.records() { - Ok(records) => { - for record in records { - browser.restore(&record.id); + // Init readable connection + match profile_database_connection.read() { + Ok(connection) => { + // Create transaction + match connection.unchecked_transaction() { + Ok(transaction) => { + // Restore previous session from DB + match database.records(&transaction) { + Ok(records) => { + for record in records { + browser.restore(&transaction, &record.id); + } + } + Err(error) => todo!("{error}"), + } + } + Err(error) => todo!("{error}"), } } - Err(error) => panic!("{error}"), // @TODO + Err(error) => todo!("{error}"), } // Assign browser window to this application @@ -131,32 +168,52 @@ impl App { application.connect_shutdown({ // let browser = browser.clone(); + let profile_database_connection = profile_database_connection.clone(); let database = database.clone(); move |_| { - // @TODO transaction? - match database.records() { - Ok(records) => { - // Cleanup previous session records - for record in records { - match database.delete(&record.id) { - Ok(_) => { - // Delegate clean action to childs - browser.clean(&record.id); - } - Err(error) => panic!("{error}"), // @TODO - } - } + // Init writable connection + match profile_database_connection.write() { + Ok(mut connection) => { + // Create transaction + match connection.transaction() { + Ok(transaction) => { + match database.records(&transaction) { + Ok(records) => { + // Cleanup previous session records + for record in records { + match database.delete(&transaction, &record.id) { + Ok(_) => { + // Delegate clean action to childs + browser.clean(&transaction, &record.id); + } + Err(error) => todo!("{error}"), + } + } - // Save current session to DB - match database.add() { - Ok(_) => { - // Delegate save action to childs - browser.save(&database.last_insert_id()); + // Save current session to DB + match database.add(&transaction) { + Ok(_) => { + // Delegate save action to childs + browser.save( + &transaction, + &database.last_insert_id(&transaction), + ); + } + Err(error) => todo!("{error}"), + } + } + Err(error) => todo!("{error}"), + } + + // Confirm changes + if let Err(error) = transaction.commit() { + todo!("{error}") + } } - Err(error) => panic!("{error}"), // @TODO + Err(error) => todo!("{error}"), } } - Err(error) => panic!("{error}"), // @TODO + Err(error) => todo!("{error}"), } } }); diff --git a/src/app/browser.rs b/src/app/browser.rs index 14421940..27e4b24f 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -5,6 +5,7 @@ mod window; use database::Database; use header::Header; +use sqlite::Transaction; use widget::Widget; use window::Window; @@ -13,7 +14,10 @@ use gtk::{ prelude::{ActionMapExt, GtkWindowExt}, ApplicationWindow, }; -use std::{path::PathBuf, sync::Arc}; +use std::{ + path::PathBuf, + sync::{Arc, RwLock}, +}; pub struct Browser { // Extras @@ -28,7 +32,7 @@ impl Browser { // Construct pub fn new( // Extras - profile_database_connection: Arc, + profile_database_connection: Arc>, profile_path: PathBuf, // Actions action_tool_debug: Arc, @@ -45,9 +49,27 @@ impl Browser { action_tab_pin: Arc, ) -> Browser { // Init database - let database = match Database::init(profile_database_connection.clone()) { - Ok(database) => Arc::new(database), - Err(error) => panic!("{error}"), // @TODO + let database = { + // Init writable database connection + let mut connection = match profile_database_connection.write() { + Ok(connection) => connection, + Err(error) => todo!("{error}"), + }; + + // Init new transaction + let transaction = match connection.transaction() { + Ok(transaction) => transaction, + Err(error) => todo!("{error}"), + }; + + // Init database structure + match Database::init(&transaction) { + Ok(database) => match transaction.commit() { + Ok(_) => Arc::new(database), + Err(error) => todo!("{error}"), + }, + Err(error) => todo!("{error}"), + } }; // Init components @@ -227,50 +249,54 @@ impl Browser { } // Actions - pub fn clean(&self, app_id: &i64) { - match self.database.records(app_id) { + pub fn clean(&self, tx: &Transaction, app_id: &i64) { + match self.database.records(tx, app_id) { Ok(records) => { for record in records { - match self.database.delete(&record.id) { + match self.database.delete(tx, &record.id) { Ok(_) => { // Delegate clean action to childs // @TODO // self.header.clean(record.id); // self.main.clean(record.id); - self.widget.clean(&record.id); + + self.widget.clean(tx, &record.id); } - Err(error) => panic!("{error}"), // @TODO + Err(error) => todo!("{error}"), } } } - Err(error) => panic!("{error}"), // @TODO + Err(error) => todo!("{error}"), } } - pub fn restore(&self, app_id: &i64) { - match self.database.records(app_id) { + pub fn restore(&self, tx: &Transaction, app_id: &i64) { + match self.database.records(tx, app_id) { Ok(records) => { for record in records { // Delegate restore action to childs // @TODO // self.header.restore(record.id); // self.window.restore(record.id); - self.widget.restore(&record.id); + + self.widget.restore(tx, &record.id); } } Err(error) => panic!("{error}"), // @TODO } } - pub fn save(&self, app_id: &i64) { - match self.database.add(app_id) { + pub fn save(&self, tx: &Transaction, app_id: &i64) { + match self.database.add(tx, app_id) { Ok(_) => { // Delegate save action to childs - let id = self.database.last_insert_id(); + let id = self.database.last_insert_id(tx); + // @TODO // self.header.save(id); // self.window.save(id); - self.widget.save(&id); + + self.widget.save(tx, &id); } Err(error) => panic!("{error}"), // @TODO } diff --git a/src/app/browser/database.rs b/src/app/browser/database.rs index 5bacf634..2385517e 100644 --- a/src/app/browser/database.rs +++ b/src/app/browser/database.rs @@ -1,5 +1,4 @@ -use sqlite::{Connection, Error}; -use std::sync::Arc; +use sqlite::{Error, Transaction}; pub struct Table { pub id: i64, @@ -7,12 +6,12 @@ pub struct Table { } pub struct Database { - connection: Arc, + // nothing yet.. } impl Database { - pub fn init(connection: Arc) -> Result { - connection.execute( + pub fn init(tx: &Transaction) -> Result { + tx.execute( "CREATE TABLE IF NOT EXISTS `app_browser` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, @@ -21,20 +20,17 @@ impl Database { [], )?; - Ok(Self { connection }) + Ok(Self {}) } - pub fn add(&self, app_id: &i64) -> Result { - self.connection - .execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id]) + pub fn add(&self, tx: &Transaction, app_id: &i64) -> Result { + tx.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id]) } - pub fn records(&self, app_id: &i64) -> Result, Error> { - let mut statement = self - .connection - .prepare("SELECT `id`, `app_id` FROM `app_browser` WHERE `app_id` = ?")?; + pub fn records(&self, tx: &Transaction, app_id: &i64) -> Result, Error> { + let mut stmt = tx.prepare("SELECT `id`, `app_id` FROM `app_browser` WHERE `app_id` = ?")?; - let result = statement.query_map([app_id], |row| { + let result = stmt.query_map([app_id], |row| { Ok(Table { id: row.get(0)?, // app_id: row.get(1)?, not in use @@ -51,12 +47,11 @@ impl Database { Ok(records) } - pub fn delete(&self, id: &i64) -> Result { - self.connection - .execute("DELETE FROM `app_browser` WHERE `id` = ?", [id]) + pub fn delete(&self, tx: &Transaction, id: &i64) -> Result { + tx.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id]) } - pub fn last_insert_id(&self) -> i64 { - self.connection.last_insert_rowid() + pub fn last_insert_id(&self, tx: &Transaction) -> i64 { + tx.last_insert_rowid() } } diff --git a/src/app/browser/widget.rs b/src/app/browser/widget.rs index 8fb71441..0e3ce767 100644 --- a/src/app/browser/widget.rs +++ b/src/app/browser/widget.rs @@ -3,7 +3,8 @@ mod database; use database::Database; use gtk::{prelude::GtkWindowExt, ApplicationWindow, Box, HeaderBar}; -use std::sync::Arc; +use sqlite::Transaction; +use std::sync::{Arc, RwLock}; // Default options const DEFAULT_HEIGHT: i32 = 480; @@ -18,14 +19,32 @@ pub struct Widget { impl Widget { // Construct pub fn new( - profile_database_connection: Arc, + profile_database_connection: Arc>, titlebar: &HeaderBar, child: &Box, ) -> Self { // Init database - let database = match Database::init(profile_database_connection) { - Ok(database) => Arc::new(database), - Err(error) => panic!("{error}"), // @TODO + let database = { + // Init writable database connection + let mut connection = match profile_database_connection.write() { + Ok(connection) => connection, + Err(error) => todo!("{error}"), + }; + + // Init new transaction + let transaction = match connection.transaction() { + Ok(transaction) => transaction, + Err(error) => todo!("{error}"), + }; + + // Init database structure + match Database::init(&transaction) { + Ok(database) => match transaction.commit() { + Ok(_) => Arc::new(database), + Err(error) => todo!("{error}"), + }, + Err(error) => todo!("{error}"), + } }; // Init GTK @@ -45,11 +64,11 @@ impl Widget { } // Actions - pub fn clean(&self, app_browser_id: &i64) { - match self.database.records(app_browser_id) { + pub fn clean(&self, tx: &Transaction, app_browser_id: &i64) { + match self.database.records(tx, app_browser_id) { Ok(records) => { for record in records { - match self.database.delete(&record.id) { + match self.database.delete(tx, &record.id) { Ok(_) => { // Delegate clean action to childs // nothing yet.. @@ -62,8 +81,8 @@ impl Widget { } } - pub fn restore(&self, app_browser_id: &i64) { - match self.database.records(app_browser_id) { + pub fn restore(&self, tx: &Transaction, app_browser_id: &i64) { + match self.database.records(tx, app_browser_id) { Ok(records) => { for record in records { // Restore widget @@ -79,8 +98,9 @@ impl Widget { } } - pub fn save(&self, app_browser_id: &i64) { + pub fn save(&self, tx: &Transaction, app_browser_id: &i64) { match self.database.add( + tx, app_browser_id, &self.application_window.default_width(), &self.application_window.default_height(), diff --git a/src/app/browser/widget/database.rs b/src/app/browser/widget/database.rs index 5365931c..e53fd848 100644 --- a/src/app/browser/widget/database.rs +++ b/src/app/browser/widget/database.rs @@ -1,5 +1,4 @@ -use sqlite::{Connection, Error}; -use std::sync::Arc; +use sqlite::{Error, Transaction}; pub struct Table { pub id: i64, @@ -10,12 +9,12 @@ pub struct Table { } pub struct Database { - connection: Arc, + // nothing yet.. } impl Database { - pub fn init(connection: Arc) -> Result { - connection.execute( + pub fn init(tx: &Transaction) -> Result { + tx.execute( "CREATE TABLE IF NOT EXISTS `app_browser_widget` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, @@ -27,17 +26,18 @@ impl Database { [], )?; - Ok(Self { connection }) + Ok(Self {}) } pub fn add( &self, + tx: &Transaction, app_browser_id: &i64, default_width: &i32, default_height: &i32, is_maximized: &bool, ) -> Result { - self.connection.execute( + tx.execute( "INSERT INTO `app_browser_widget` ( `app_browser_id`, `default_width`, @@ -56,8 +56,8 @@ impl Database { ) } - pub fn records(&self, app_browser_id: &i64) -> Result, Error> { - let mut statement = self.connection.prepare( + pub fn records(&self, tx: &Transaction, app_browser_id: &i64) -> Result, Error> { + let mut stmt = tx.prepare( "SELECT `id`, `app_browser_id`, `default_width`, @@ -65,7 +65,7 @@ impl Database { `is_maximized` FROM `app_browser_widget` WHERE `app_browser_id` = ?", )?; - let result = statement.query_map([app_browser_id], |row| { + let result = stmt.query_map([app_browser_id], |row| { Ok(Table { id: row.get(0)?, // app_browser_id: row.get(1)?, not in use @@ -85,13 +85,12 @@ impl Database { Ok(records) } - pub fn delete(&self, id: &i64) -> Result { - self.connection - .execute("DELETE FROM `app_browser_widget` WHERE `id` = ?", [id]) + pub fn delete(&self, tx: &Transaction, id: &i64) -> Result { + tx.execute("DELETE FROM `app_browser_widget` WHERE `id` = ?", [id]) } /* not in use - pub fn last_insert_id(&self) -> i64 { - self.connection.last_insert_rowid() + pub fn last_insert_id(&self, tx: &Transaction) -> i64 { + tx.last_insert_rowid() } */ } diff --git a/src/app/database.rs b/src/app/database.rs index 2c97a534..170971ab 100644 --- a/src/app/database.rs +++ b/src/app/database.rs @@ -1,38 +1,33 @@ -use sqlite::{Connection, Error}; -use std::sync::Arc; +use sqlite::{Error, Transaction}; pub struct Table { pub id: i64, - // pub time: i64, } pub struct Database { - connection: Arc, + // nothing yet.. } impl Database { - pub fn init(connection: Arc) -> Result { - connection.execute( + pub fn init(tx: &Transaction) -> Result { + tx.execute( "CREATE TABLE IF NOT EXISTS `app` ( - `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, - `time` INTEGER NOT NULL DEFAULT (UNIXEPOCH('NOW')) + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL )", [], )?; - Ok(Self { connection }) + Ok(Self {}) } - pub fn add(&self) -> Result { - self.connection - .execute("INSERT INTO `app` DEFAULT VALUES", []) + pub fn add(&self, tx: &Transaction) -> Result { + tx.execute("INSERT INTO `app` DEFAULT VALUES", []) } - pub fn records(&self) -> Result, Error> { - let mut statement = self.connection.prepare("SELECT `id` FROM `app`")?; - - let result = statement.query_map([], |row| Ok(Table { id: row.get(0)? }))?; + pub fn records(&self, tx: &Transaction) -> Result, Error> { + let mut stmt = tx.prepare("SELECT `id` FROM `app`")?; + let result = stmt.query_map([], |row| Ok(Table { id: row.get(0)? }))?; let mut records = Vec::new(); @@ -44,12 +39,11 @@ impl Database { Ok(records) } - pub fn delete(&self, id: &i64) -> Result { - self.connection - .execute("DELETE FROM `app` WHERE `id` = ?", [id]) + pub fn delete(&self, tx: &Transaction, id: &i64) -> Result { + tx.execute("DELETE FROM `app` WHERE `id` = ?", [id]) } - pub fn last_insert_id(&self) -> i64 { - self.connection.last_insert_rowid() + pub fn last_insert_id(&self, tx: &Transaction) -> i64 { + tx.last_insert_rowid() } } diff --git a/src/main.rs b/src/main.rs index 6ea672b2..c7e35374 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,10 @@ mod app; use app::App; use gtk::glib::{user_config_dir, ExitCode}; use sqlite::Connection; -use std::{fs::create_dir_all, sync::Arc}; +use std::{ + fs::create_dir_all, + sync::{Arc, RwLock}, +}; const VENDOR: &str = "YGGverse"; const APP_ID: &str = "Yoda"; // env!("CARGO_PKG_NAME"); @@ -28,7 +31,7 @@ fn main() -> ExitCode { // Init database connection let profile_database_connection = match Connection::open(profile_database_path) { - Ok(connection) => Arc::new(connection), + Ok(connection) => Arc::new(RwLock::new(connection)), Err(error) => panic!("Failed to connect profile database: {error}"), };