implement sqlite transactions

This commit is contained in:
yggverse 2024-10-06 00:43:35 +03:00
parent d5101a6465
commit 271acd50ed
8 changed files with 213 additions and 119 deletions

View file

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

View file

@ -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(),

View file

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