Browse Source

make transactionable mathods static

master
yggverse 2 months ago
parent
commit
a1f2d57b6d
  1. 15
      src/app.rs
  2. 16
      src/app/browser.rs
  3. 12
      src/app/browser/database.rs
  4. 15
      src/app/browser/widget.rs
  5. 11
      src/app/browser/widget/database.rs
  6. 20
      src/app/browser/window.rs
  7. 12
      src/app/browser/window/database.rs
  8. 15
      src/app/browser/window/tab.rs
  9. 15
      src/app/browser/window/tab/database.rs
  10. 22
      src/app/browser/window/tab/label.rs
  11. 15
      src/app/browser/window/tab/label/database.rs
  12. 12
      src/app/database.rs

15
src/app.rs

@ -37,9 +37,6 @@ impl App { @@ -37,9 +37,6 @@ impl App {
profile_database_connection: Arc<RwLock<Connection>>,
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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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}"),

16
src/app/browser.rs

@ -17,8 +17,6 @@ use sqlite::Transaction; @@ -17,8 +17,6 @@ use sqlite::Transaction;
use std::{path::PathBuf, sync::Arc};
pub struct Browser {
// Extras
database: Arc<Database>,
// Components
// header: Arc<Header>,
window: Arc<Window>,
@ -44,9 +42,6 @@ impl Browser { @@ -44,9 +42,6 @@ impl Browser {
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_tab_pin: Arc<SimpleAction>,
) -> 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 { @@ -189,7 +184,6 @@ impl Browser {
// Return new activated Browser struct
Self {
database,
widget,
// header,
window,
@ -198,10 +192,10 @@ impl Browser { @@ -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 { @@ -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 { @@ -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);

12
src/app/browser/database.rs

@ -10,10 +10,6 @@ pub struct Database { @@ -10,10 +10,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`
@ -25,11 +21,11 @@ impl Database { @@ -25,11 +21,11 @@ impl Database {
)
}
pub fn add(&self, tx: &Transaction, app_id: &i64) -> Result<usize, Error> {
pub fn add(tx: &Transaction, app_id: &i64) -> Result<usize, Error> {
tx.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id])
}
pub fn records(&self, tx: &Transaction, app_id: &i64) -> Result<Vec<Table>, Error> {
pub fn records(tx: &Transaction, app_id: &i64) -> Result<Vec<Table>, 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 { @@ -49,11 +45,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_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()
}
}

15
src/app/browser/widget.rs

@ -4,7 +4,6 @@ use database::Database; @@ -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; @@ -12,16 +11,12 @@ const DEFAULT_WIDTH: i32 = 640;
const MAXIMIZED: bool = false;
pub struct Widget {
database: Arc<Database>,
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 { @@ -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 { @@ -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 { @@ -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(),

11
src/app/browser/widget/database.rs

@ -13,10 +13,6 @@ pub struct Database { @@ -13,10 +13,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_widget`
@ -32,7 +28,6 @@ impl Database { @@ -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 { @@ -55,7 +50,7 @@ impl Database {
)
}
pub fn records(&self, tx: &Transaction, app_browser_id: &i64) -> Result<Vec<Table>, Error> {
pub fn records(tx: &Transaction, app_browser_id: &i64) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_id`,
@ -84,12 +79,12 @@ impl Database { @@ -84,12 +79,12 @@ 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_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()
} */
}

20
src/app/browser/window.rs

@ -12,7 +12,6 @@ use std::sync::Arc; @@ -12,7 +12,6 @@ use std::sync::Arc;
use gtk::{gio::SimpleAction, glib::GString, Box};
pub struct Window {
database: Arc<Database>,
tab: Arc<Tab>,
widget: Arc<Widget>,
}
@ -27,9 +26,6 @@ impl Window { @@ -27,9 +26,6 @@ impl Window {
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
) -> 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 { @@ -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 { @@ -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 { @@ -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 { @@ -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}"),
}

12
src/app/browser/window/database.rs

@ -10,10 +10,6 @@ pub struct Database { @@ -10,10 +10,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`
@ -25,14 +21,14 @@ impl Database { @@ -25,14 +21,14 @@ impl Database {
)
}
pub fn add(&self, tx: &Transaction, app_browser_id: &i64) -> Result<usize, Error> {
pub fn add(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> {
pub fn records(tx: &Transaction, app_browser_id: &i64) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_id` FROM `app_browser_window`
@ -56,11 +52,11 @@ impl Database { @@ -56,11 +52,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_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()
}
}

15
src/app/browser/window/tab.rs

@ -26,7 +26,6 @@ pub struct TabItem { @@ -26,7 +26,6 @@ pub struct TabItem {
// Main
pub struct Tab {
database: Arc<Database>,
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
@ -49,9 +48,6 @@ impl Tab { @@ -49,9 +48,6 @@ impl Tab {
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
) -> 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 { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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);

15
src/app/browser/window/tab/database.rs

@ -11,10 +11,6 @@ pub struct Database { @@ -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`
@ -28,7 +24,6 @@ impl Database { @@ -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 { @@ -42,11 +37,7 @@ impl Database {
)
}
pub fn records(
&self,
tx: &Transaction,
app_browser_window_id: &i64,
) -> Result<Vec<Table>, Error> {
pub fn records(tx: &Transaction, app_browser_window_id: &i64) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_id`,
@ -72,11 +63,11 @@ impl Database { @@ -72,11 +63,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_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()
}
}

22
src/app/browser/window/tab/label.rs

@ -13,7 +13,6 @@ use gtk::{glib::GString, Box}; @@ -13,7 +13,6 @@ use gtk::{glib::GString, Box};
use std::sync::Arc;
pub struct Label {
database: Arc<Database>,
// Components
pin: Arc<Pin>,
title: Arc<Title>,
@ -24,9 +23,6 @@ pub struct Label { @@ -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 { @@ -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 { @@ -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 { @@ -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..

15
src/app/browser/window/tab/label/database.rs

@ -11,10 +11,6 @@ pub struct Database { @@ -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 { @@ -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 { @@ -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 { @@ -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 { @@ -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()
} */
}

12
src/app/database.rs

@ -9,10 +9,6 @@ pub struct Database { @@ -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 { @@ -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 { @@ -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()
}
}

Loading…
Cancel
Save