make transactionable mathods static

This commit is contained in:
yggverse 2024-10-07 20:34:48 +03:00
parent bd6138fced
commit a1f2d57b6d
12 changed files with 52 additions and 128 deletions

View file

@ -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 {
)
}
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 {
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()
}
}

View file

@ -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 {
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 {
// 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 {
}
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 {
}
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 {
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 {
) {
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);

View file

@ -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 {
}
pub fn add(
&self,
tx: &Transaction,
app_browser_window_id: &i64,
is_current: &bool,
@ -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 {
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()
}
}

View file

@ -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 {
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 {
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 {
}
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 {
}
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..

View file

@ -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 {
}
pub fn add(
&self,
tx: &Transaction,
app_browser_window_tab_id: &i64,
is_pinned: &bool,
@ -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 {
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 {
}
/* not in use
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
} */
}