mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
implement sqlite transactions
This commit is contained in:
parent
d5101a6465
commit
271acd50ed
8 changed files with 213 additions and 119 deletions
|
|
@ -5,6 +5,7 @@ mod window;
|
|||
|
||||
use database::Database;
|
||||
use header::Header;
|
||||
use sqlite::Transaction;
|
||||
use widget::Widget;
|
||||
use window::Window;
|
||||
|
||||
|
|
@ -13,7 +14,10 @@ use gtk::{
|
|||
prelude::{ActionMapExt, GtkWindowExt},
|
||||
ApplicationWindow,
|
||||
};
|
||||
use std::{path::PathBuf, sync::Arc};
|
||||
use std::{
|
||||
path::PathBuf,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
pub struct Browser {
|
||||
// Extras
|
||||
|
|
@ -28,7 +32,7 @@ impl Browser {
|
|||
// Construct
|
||||
pub fn new(
|
||||
// Extras
|
||||
profile_database_connection: Arc<sqlite::Connection>,
|
||||
profile_database_connection: Arc<RwLock<sqlite::Connection>>,
|
||||
profile_path: PathBuf,
|
||||
// Actions
|
||||
action_tool_debug: Arc<SimpleAction>,
|
||||
|
|
@ -45,9 +49,27 @@ impl Browser {
|
|||
action_tab_pin: Arc<SimpleAction>,
|
||||
) -> Browser {
|
||||
// Init database
|
||||
let database = match Database::init(profile_database_connection.clone()) {
|
||||
Ok(database) => Arc::new(database),
|
||||
Err(error) => panic!("{error}"), // @TODO
|
||||
let database = {
|
||||
// Init writable database connection
|
||||
let mut connection = match profile_database_connection.write() {
|
||||
Ok(connection) => connection,
|
||||
Err(error) => todo!("{error}"),
|
||||
};
|
||||
|
||||
// Init new transaction
|
||||
let transaction = match connection.transaction() {
|
||||
Ok(transaction) => transaction,
|
||||
Err(error) => todo!("{error}"),
|
||||
};
|
||||
|
||||
// Init database structure
|
||||
match Database::init(&transaction) {
|
||||
Ok(database) => match transaction.commit() {
|
||||
Ok(_) => Arc::new(database),
|
||||
Err(error) => todo!("{error}"),
|
||||
},
|
||||
Err(error) => todo!("{error}"),
|
||||
}
|
||||
};
|
||||
|
||||
// Init components
|
||||
|
|
@ -227,50 +249,54 @@ impl Browser {
|
|||
}
|
||||
|
||||
// Actions
|
||||
pub fn clean(&self, app_id: &i64) {
|
||||
match self.database.records(app_id) {
|
||||
pub fn clean(&self, tx: &Transaction, app_id: &i64) {
|
||||
match self.database.records(tx, app_id) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
match self.database.delete(&record.id) {
|
||||
match self.database.delete(tx, &record.id) {
|
||||
Ok(_) => {
|
||||
// Delegate clean action to childs
|
||||
// @TODO
|
||||
// self.header.clean(record.id);
|
||||
// self.main.clean(record.id);
|
||||
self.widget.clean(&record.id);
|
||||
|
||||
self.widget.clean(tx, &record.id);
|
||||
}
|
||||
Err(error) => panic!("{error}"), // @TODO
|
||||
Err(error) => todo!("{error}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(error) => panic!("{error}"), // @TODO
|
||||
Err(error) => todo!("{error}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn restore(&self, app_id: &i64) {
|
||||
match self.database.records(app_id) {
|
||||
pub fn restore(&self, tx: &Transaction, app_id: &i64) {
|
||||
match self.database.records(tx, app_id) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
// Delegate restore action to childs
|
||||
// @TODO
|
||||
// self.header.restore(record.id);
|
||||
// self.window.restore(record.id);
|
||||
self.widget.restore(&record.id);
|
||||
|
||||
self.widget.restore(tx, &record.id);
|
||||
}
|
||||
}
|
||||
Err(error) => panic!("{error}"), // @TODO
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self, app_id: &i64) {
|
||||
match self.database.add(app_id) {
|
||||
pub fn save(&self, tx: &Transaction, app_id: &i64) {
|
||||
match self.database.add(tx, app_id) {
|
||||
Ok(_) => {
|
||||
// Delegate save action to childs
|
||||
let id = self.database.last_insert_id();
|
||||
let id = self.database.last_insert_id(tx);
|
||||
|
||||
// @TODO
|
||||
// self.header.save(id);
|
||||
// self.window.save(id);
|
||||
self.widget.save(&id);
|
||||
|
||||
self.widget.save(tx, &id);
|
||||
}
|
||||
Err(error) => panic!("{error}"), // @TODO
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use sqlite::{Connection, Error};
|
||||
use std::sync::Arc;
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
|
|
@ -7,12 +6,12 @@ pub struct Table {
|
|||
}
|
||||
|
||||
pub struct Database {
|
||||
connection: Arc<Connection>,
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn init(connection: Arc<Connection>) -> Result<Database, Error> {
|
||||
connection.execute(
|
||||
pub fn init(tx: &Transaction) -> Result<Database, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `app_browser`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
|
|
@ -21,20 +20,17 @@ impl Database {
|
|||
[],
|
||||
)?;
|
||||
|
||||
Ok(Self { connection })
|
||||
Ok(Self {})
|
||||
}
|
||||
|
||||
pub fn add(&self, app_id: &i64) -> Result<usize, Error> {
|
||||
self.connection
|
||||
.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id])
|
||||
pub fn add(&self, tx: &Transaction, app_id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id])
|
||||
}
|
||||
|
||||
pub fn records(&self, app_id: &i64) -> Result<Vec<Table>, Error> {
|
||||
let mut statement = self
|
||||
.connection
|
||||
.prepare("SELECT `id`, `app_id` FROM `app_browser` WHERE `app_id` = ?")?;
|
||||
pub fn records(&self, 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 = statement.query_map([app_id], |row| {
|
||||
let result = stmt.query_map([app_id], |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
// app_id: row.get(1)?, not in use
|
||||
|
|
@ -51,12 +47,11 @@ impl Database {
|
|||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn delete(&self, id: &i64) -> Result<usize, Error> {
|
||||
self.connection
|
||||
.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])
|
||||
pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
||||
pub fn last_insert_id(&self) -> i64 {
|
||||
self.connection.last_insert_rowid()
|
||||
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,8 @@ mod database;
|
|||
use database::Database;
|
||||
|
||||
use gtk::{prelude::GtkWindowExt, ApplicationWindow, Box, HeaderBar};
|
||||
use std::sync::Arc;
|
||||
use sqlite::Transaction;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
// Default options
|
||||
const DEFAULT_HEIGHT: i32 = 480;
|
||||
|
|
@ -18,14 +19,32 @@ pub struct Widget {
|
|||
impl Widget {
|
||||
// Construct
|
||||
pub fn new(
|
||||
profile_database_connection: Arc<sqlite::Connection>,
|
||||
profile_database_connection: Arc<RwLock<sqlite::Connection>>,
|
||||
titlebar: &HeaderBar,
|
||||
child: &Box,
|
||||
) -> Self {
|
||||
// Init database
|
||||
let database = match Database::init(profile_database_connection) {
|
||||
Ok(database) => Arc::new(database),
|
||||
Err(error) => panic!("{error}"), // @TODO
|
||||
let database = {
|
||||
// Init writable database connection
|
||||
let mut connection = match profile_database_connection.write() {
|
||||
Ok(connection) => connection,
|
||||
Err(error) => todo!("{error}"),
|
||||
};
|
||||
|
||||
// Init new transaction
|
||||
let transaction = match connection.transaction() {
|
||||
Ok(transaction) => transaction,
|
||||
Err(error) => todo!("{error}"),
|
||||
};
|
||||
|
||||
// Init database structure
|
||||
match Database::init(&transaction) {
|
||||
Ok(database) => match transaction.commit() {
|
||||
Ok(_) => Arc::new(database),
|
||||
Err(error) => todo!("{error}"),
|
||||
},
|
||||
Err(error) => todo!("{error}"),
|
||||
}
|
||||
};
|
||||
|
||||
// Init GTK
|
||||
|
|
@ -45,11 +64,11 @@ impl Widget {
|
|||
}
|
||||
|
||||
// Actions
|
||||
pub fn clean(&self, app_browser_id: &i64) {
|
||||
match self.database.records(app_browser_id) {
|
||||
pub fn clean(&self, tx: &Transaction, app_browser_id: &i64) {
|
||||
match self.database.records(tx, app_browser_id) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
match self.database.delete(&record.id) {
|
||||
match self.database.delete(tx, &record.id) {
|
||||
Ok(_) => {
|
||||
// Delegate clean action to childs
|
||||
// nothing yet..
|
||||
|
|
@ -62,8 +81,8 @@ impl Widget {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn restore(&self, app_browser_id: &i64) {
|
||||
match self.database.records(app_browser_id) {
|
||||
pub fn restore(&self, tx: &Transaction, app_browser_id: &i64) {
|
||||
match self.database.records(tx, app_browser_id) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
// Restore widget
|
||||
|
|
@ -79,8 +98,9 @@ impl Widget {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn save(&self, app_browser_id: &i64) {
|
||||
pub fn save(&self, tx: &Transaction, app_browser_id: &i64) {
|
||||
match self.database.add(
|
||||
tx,
|
||||
app_browser_id,
|
||||
&self.application_window.default_width(),
|
||||
&self.application_window.default_height(),
|
||||
|
|
|
|||
|
|
@ -1,5 +1,4 @@
|
|||
use sqlite::{Connection, Error};
|
||||
use std::sync::Arc;
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
|
|
@ -10,12 +9,12 @@ pub struct Table {
|
|||
}
|
||||
|
||||
pub struct Database {
|
||||
connection: Arc<Connection>,
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn init(connection: Arc<Connection>) -> Result<Database, Error> {
|
||||
connection.execute(
|
||||
pub fn init(tx: &Transaction) -> Result<Database, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `app_browser_widget`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
|
|
@ -27,17 +26,18 @@ impl Database {
|
|||
[],
|
||||
)?;
|
||||
|
||||
Ok(Self { connection })
|
||||
Ok(Self {})
|
||||
}
|
||||
|
||||
pub fn add(
|
||||
&self,
|
||||
tx: &Transaction,
|
||||
app_browser_id: &i64,
|
||||
default_width: &i32,
|
||||
default_height: &i32,
|
||||
is_maximized: &bool,
|
||||
) -> Result<usize, Error> {
|
||||
self.connection.execute(
|
||||
tx.execute(
|
||||
"INSERT INTO `app_browser_widget` (
|
||||
`app_browser_id`,
|
||||
`default_width`,
|
||||
|
|
@ -56,8 +56,8 @@ impl Database {
|
|||
)
|
||||
}
|
||||
|
||||
pub fn records(&self, app_browser_id: &i64) -> Result<Vec<Table>, Error> {
|
||||
let mut statement = self.connection.prepare(
|
||||
pub fn records(&self, tx: &Transaction, app_browser_id: &i64) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`,
|
||||
`app_browser_id`,
|
||||
`default_width`,
|
||||
|
|
@ -65,7 +65,7 @@ impl Database {
|
|||
`is_maximized` FROM `app_browser_widget` WHERE `app_browser_id` = ?",
|
||||
)?;
|
||||
|
||||
let result = statement.query_map([app_browser_id], |row| {
|
||||
let result = stmt.query_map([app_browser_id], |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
// app_browser_id: row.get(1)?, not in use
|
||||
|
|
@ -85,13 +85,12 @@ impl Database {
|
|||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn delete(&self, id: &i64) -> Result<usize, Error> {
|
||||
self.connection
|
||||
.execute("DELETE FROM `app_browser_widget` WHERE `id` = ?", [id])
|
||||
pub fn delete(&self, 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) -> i64 {
|
||||
self.connection.last_insert_rowid()
|
||||
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
} */
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,38 +1,33 @@
|
|||
use sqlite::{Connection, Error};
|
||||
use std::sync::Arc;
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
// pub time: i64,
|
||||
}
|
||||
|
||||
pub struct Database {
|
||||
connection: Arc<Connection>,
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn init(connection: Arc<Connection>) -> Result<Database, Error> {
|
||||
connection.execute(
|
||||
pub fn init(tx: &Transaction) -> Result<Database, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `app`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`time` INTEGER NOT NULL DEFAULT (UNIXEPOCH('NOW'))
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
|
||||
Ok(Self { connection })
|
||||
Ok(Self {})
|
||||
}
|
||||
|
||||
pub fn add(&self) -> Result<usize, Error> {
|
||||
self.connection
|
||||
.execute("INSERT INTO `app` DEFAULT VALUES", [])
|
||||
pub fn add(&self, tx: &Transaction) -> Result<usize, Error> {
|
||||
tx.execute("INSERT INTO `app` DEFAULT VALUES", [])
|
||||
}
|
||||
|
||||
pub fn records(&self) -> Result<Vec<Table>, Error> {
|
||||
let mut statement = self.connection.prepare("SELECT `id` FROM `app`")?;
|
||||
|
||||
let result = statement.query_map([], |row| Ok(Table { id: row.get(0)? }))?;
|
||||
pub fn records(&self, 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)? }))?;
|
||||
|
||||
let mut records = Vec::new();
|
||||
|
||||
|
|
@ -44,12 +39,11 @@ impl Database {
|
|||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn delete(&self, id: &i64) -> Result<usize, Error> {
|
||||
self.connection
|
||||
.execute("DELETE FROM `app` WHERE `id` = ?", [id])
|
||||
pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `app` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
||||
pub fn last_insert_id(&self) -> i64 {
|
||||
self.connection.last_insert_rowid()
|
||||
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue