draft app database model

This commit is contained in:
yggverse 2024-10-02 15:22:50 +03:00
parent 4191227db4
commit 8aa5474c20
2 changed files with 64 additions and 12 deletions

View File

@ -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<Browser>,
database: Arc<Database>,
}
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

43
src/app/database.rs Normal file
View File

@ -0,0 +1,43 @@
use std::sync::Arc;
pub struct Database {
connection: Arc<sqlite::Connection>,
// Autostart migrate feature on app and db versions mismatch
version: String,
}
impl Database {
// Construct new application DB
pub fn init(connection: Arc<sqlite::Connection>, 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
}
}