draft database features

This commit is contained in:
yggverse 2024-10-06 18:20:36 +03:00
parent 227cfb30a1
commit 3dffcb029a
5 changed files with 305 additions and 14 deletions

View file

@ -0,0 +1,64 @@
use sqlite::{Error, Transaction};
pub struct Table {
pub id: i64,
// pub app_browser_id: i64, not in use
}
pub struct Database {
// nothing yet..
}
impl Database {
pub fn init(tx: &Transaction) -> Result<Database, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_id` INTEGER NOT NULL
)",
[],
)?;
Ok(Self {})
}
pub fn add(&self, tx: &Transaction, app_browser_id: &i64) -> Result<usize, Error> {
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<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_id` FROM `app_browser_window`
WHERE `app_browser_id` = ?",
)?;
let result = stmt.query_map([app_browser_id], |row| {
Ok(Table {
id: row.get(0)?,
// app_browser_id: row.get(1)?, not in use
})
})?;
let mut records = Vec::new();
for record in result {
let table = record?;
records.push(table);
}
Ok(records)
}
pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `app_browser_window` WHERE `id` = ?", [id])
}
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
tx.last_insert_rowid()
}
}

View file

@ -1,9 +1,12 @@
mod database;
mod label;
mod page;
mod widget;
use database::Database;
use label::Label;
use page::Page;
use sqlite::{Connection, Transaction};
use widget::Widget;
use gtk::{
@ -13,7 +16,11 @@ use gtk::{
GestureClick, Notebook,
};
use std::{cell::RefCell, collections::HashMap, sync::Arc};
use std::{
cell::RefCell,
collections::HashMap,
sync::{Arc, RwLock},
};
// Common struct for HashMap index
struct TabItem {
@ -23,7 +30,9 @@ struct TabItem {
// Main
pub struct Tab {
// Keep action links in memory to not require them on every tab append
// Extras
database: Arc<Database>,
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
@ -38,14 +47,42 @@ pub struct Tab {
impl Tab {
// Construct
pub fn new(
// Extras
profile_database_connection: Arc<RwLock<Connection>>,
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
) -> Self {
// Init database
let database = {
// Init writable database connection
let mut connection = match profile_database_connection.write() {
Ok(connection) => connection,
Err(e) => todo!("{e}"),
};
// Init new transaction
let transaction = match connection.transaction() {
Ok(transaction) => transaction,
Err(e) => todo!("{e}"),
};
// Init database structure
match Database::init(&transaction) {
Ok(database) => match transaction.commit() {
Ok(_) => Arc::new(database),
Err(e) => todo!("{e}"),
},
Err(e) => todo!("{e}"),
}
};
// Return non activated struct
Self {
database,
// Define action links
action_tab_page_navigation_base,
action_tab_page_navigation_history_back,
@ -196,6 +233,46 @@ impl Tab {
}
}
pub fn clean(&self, tx: &Transaction, app_browser_window_id: &i64) {
match self.database.records(tx, app_browser_window_id) {
Ok(records) => {
for record in records {
match self.database.delete(tx, &record.id) {
Ok(_) => {
// Delegate clean action to childs
// nothing yet..
}
Err(e) => todo!("{e}"),
}
}
}
Err(e) => todo!("{e}"),
}
}
pub fn restore(&self, tx: &Transaction, app_browser_window_id: &i64) {
match self.database.records(tx, app_browser_window_id) {
Ok(records) => {
for record in records {
// Delegate restore action to childs
// nothing yet..
}
}
Err(e) => todo!("{e}"),
}
}
pub fn save(&self, tx: &Transaction, app_browser_window_id: &i64) {
match self.database.add(tx, app_browser_window_id) {
Ok(_) => {
// Delegate save action to childs
// let id = self.database.last_insert_id(tx);
// nothing yet..
}
Err(e) => todo!("{e}"),
}
}
// Getters
pub fn page_title(&self) -> Option<GString> {
if let Some(id) = self.widget.current_name() {
@ -203,7 +280,6 @@ impl Tab {
return item.page.title();
}
}
None
}
@ -214,7 +290,6 @@ impl Tab {
return item.page.description();
}
}
None
}

View file

@ -0,0 +1,66 @@
use sqlite::{Error, Transaction};
pub struct Table {
pub id: i64,
// pub app_browser_window_id: i64, not in use
}
pub struct Database {
// nothing yet..
}
impl Database {
pub fn init(tx: &Transaction) -> Result<Database, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_id` INTEGER NOT NULL
)",
[],
)?;
Ok(Self {})
}
pub fn add(&self, tx: &Transaction, app_browser_window_id: &i64) -> Result<usize, Error> {
tx.execute(
"INSERT INTO `app_browser_window_tab` (`app_browser_window_id`) VALUES (?)",
[app_browser_window_id],
)
}
pub fn records(
&self,
tx: &Transaction,
app_browser_window_id: &i64,
) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare("SELECT `id`,
`app_browser_window_id` FROM `app_browser_window_tab`
WHERE `app_browser_window_id` = ?")?;
let result = stmt.query_map([app_browser_window_id], |row| {
Ok(Table {
id: row.get(0)?,
// app_browser_window_id: row.get(1)?, not in use
})
})?;
let mut records = Vec::new();
for record in result {
let table = record?;
records.push(table);
}
Ok(records)
}
pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `app_browser_window_tab` WHERE `id` = ?", [id])
}
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
tx.last_insert_rowid()
}
}