draf db features for label mod

This commit is contained in:
yggverse 2024-10-07 04:38:22 +03:00
parent 3bc74f17af
commit 259ec321be
4 changed files with 212 additions and 25 deletions

View file

@ -74,8 +74,7 @@ impl Database {
tx.execute("DELETE FROM `app_browser_window_tab` WHERE `id` = ?", [id])
}
/* not in use
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
tx.last_insert_rowid()
} */
}
}

View file

@ -1,15 +1,19 @@
mod database;
mod pin;
mod title;
mod widget;
use database::Database;
use pin::Pin;
use sqlite::{Connection, Transaction};
use title::Title;
use widget::Widget;
use gtk::{glib::GString, Box};
use std::sync::Arc;
use std::sync::{Arc, RwLock};
pub struct Label {
database: Arc<Database>,
// Components
pin: Arc<Pin>,
title: Arc<Title>,
@ -19,7 +23,38 @@ pub struct Label {
impl Label {
// Construct
pub fn new(name: GString, is_pinned: bool) -> Label {
pub fn new(
profile_database_connection: Arc<RwLock<Connection>>,
name: GString,
is_pinned: bool,
) -> Label {
// Init database
let database = {
/* @TODO init outside
// 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}"),
} */
Arc::new(Database::new())
};
// Components
let pin = Arc::new(Pin::new(is_pinned));
let title = Arc::new(Title::new());
@ -28,10 +63,59 @@ impl Label {
let widget = Arc::new(Widget::new(name, pin.gobject(), title.gobject()));
// Result
Self { pin, title, widget }
Self {
database,
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) {
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_tab_id: &i64) {
match self.database.records(tx, app_browser_window_tab_id) {
Ok(records) => {
for record in records {
self.pin(record.is_pinned);
// Delegate restore action to childs
// nothing yet..
}
}
Err(e) => todo!("{e}"),
}
}
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())
{
Ok(_) => {
// Delegate save action to childs
// nothing yet..
}
Err(e) => todo!("{e}"),
}
}
pub fn update(&self, title: Option<&GString>) {
self.title.update(title);
self.widget.update(title);
@ -45,7 +129,7 @@ impl Label {
// Getters
pub fn is_pinned(&self) -> bool {
self.pin.is_pinned()
self.pin.is_pinned() // @TODO
}
pub fn gobject(&self) -> &Box {

View file

@ -0,0 +1,88 @@
use sqlite::{Error, Transaction};
pub struct Table {
pub id: i64,
// pub app_browser_window_tab_id: i64, not in use
pub is_pinned: bool,
}
pub struct Database {
// nothing yet..
}
impl Database {
pub fn new() -> Self {
Self {}
}
pub fn init(tx: &Transaction) -> Result<Database, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab_label`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_id` INTEGER NOT NULL,
`is_pinned` INTEGER NOT NULL
)",
[],
)?;
Ok(Self {})
}
pub fn add(
&self,
tx: &Transaction,
app_browser_window_tab_id: &i64,
is_pinned: &bool,
) -> Result<usize, Error> {
tx.execute(
"INSERT INTO `app_browser_window_tab_label` (
`app_browser_window_tab_id`,
`is_pinned`
) VALUES (?,?)",
[app_browser_window_tab_id, &(*is_pinned as i64)],
)
}
pub fn records(
&self,
tx: &Transaction,
app_browser_window_tab_id: &i64,
) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_tab_id`,
`is_pinned` FROM `app_browser_window_tab_label`
WHERE `app_browser_window_tab_id` = ?",
)?;
let result = stmt.query_map([app_browser_window_tab_id], |row| {
Ok(Table {
id: row.get(0)?,
// app_browser_window_tab_id: row.get(1)?, not in use
is_pinned: row.get(2)?,
})
})?;
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_label` WHERE `id` = ?",
[id],
)
}
/* not in use
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
tx.last_insert_rowid()
} */
}