From 8aa5474c20941074d7a22bedba83d0aeedfae02c Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 2 Oct 2024 15:22:50 +0300 Subject: [PATCH] draft app database model --- src/app.rs | 33 +++++++++++++++++++++------------ src/app/database.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 src/app/database.rs diff --git a/src/app.rs b/src/app.rs index a522a5ab..4692f323 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,8 +1,10 @@ mod action; mod browser; +mod database; use action::Action; use browser::Browser; +use database::Database; use gtk::{ glib::{user_config_dir, ExitCode}, @@ -19,30 +21,33 @@ pub struct App { app: Application, // Components //browser: Arc, + database: Arc, } impl App { // Construct pub fn new() -> Self { - // Init profile directory - let mut fs = user_config_dir(); + // Init profile directory path + let mut profile_path = user_config_dir(); - fs.push(APPLICATION_ID); + profile_path.push(APPLICATION_ID); - if let Err(e) = create_dir_all(&fs) { + if let Err(e) = create_dir_all(&profile_path) { panic!("Failed to create profile directory: {e}") } - // Init profile database - /* @TODO - let mut db = fs.clone(); + // Init profile database path + let mut database_path = profile_path.clone(); - db.push("database.sqlite3"); + database_path.push("database.sqlite3"); - let db = match sqlite::open(db) { - Ok(db) => Arc::new(db), + let connection = match sqlite::open(database_path) { + Ok(connection) => Arc::new(connection), Err(e) => panic!("Failed to connect profile database: {e}"), - };*/ + }; + + // Init database model + let database = Arc::new(Database::init(connection, env!("CARGO_PKG_VERSION"))); // Init actions let action_debug = Action::new("win", true); @@ -88,7 +93,11 @@ impl App { // Init events app.connect_activate({ + let database = database.clone(); move |application| { + // Restore previous session + database.restore(); + // Init components let browser = Arc::new(Browser::new( &application, @@ -115,7 +124,7 @@ impl App { }); // Return activated struct - Self { app } + Self { app, database } } // Actions diff --git a/src/app/database.rs b/src/app/database.rs new file mode 100644 index 00000000..65a8b533 --- /dev/null +++ b/src/app/database.rs @@ -0,0 +1,43 @@ +use std::sync::Arc; + +pub struct Database { + connection: Arc, + // Autostart migrate feature on app and db versions mismatch + version: String, +} + +impl Database { + // Construct new application DB + pub fn init(connection: Arc, version: &str) -> Database { + // Create app table if not exist yet + /* + connection + .execute( + r" + CREATE TABLE IF NOT EXISTS `app` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP, + `version` VARCHAR NOT NULL + ) + ", + ) + .unwrap(); // @TODO handle errors */ + + // Return struct + Self { + connection, + version: String::from(version), + } + } + + // Restore previous browser session from DB + pub fn restore(&self) { + // @TODO migration test + } + + // Save browser session to DB + pub fn save(&self) { + // @TODO migration test + } +}