mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-02-05 16:04:15 +00:00
draft db actions
This commit is contained in:
parent
ad3f6083e1
commit
17dedbd36e
24
src/app.rs
24
src/app.rs
@ -113,4 +113,28 @@ impl App {
|
|||||||
pub fn run(&self) -> ExitCode {
|
pub fn run(&self) -> ExitCode {
|
||||||
self.app.run()
|
self.app.run()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn save(&self) {
|
||||||
|
// Cleanup previous record
|
||||||
|
match self.database.clean() {
|
||||||
|
Ok(_) => {
|
||||||
|
// Delegate clean action to children components
|
||||||
|
// self.browser.clean(app_id) @TODO
|
||||||
|
// ..
|
||||||
|
|
||||||
|
// Create new record
|
||||||
|
match self.database.add() {
|
||||||
|
Ok(_) => {
|
||||||
|
// let app_id = self.database.last_insert_id();
|
||||||
|
|
||||||
|
// Delegate save action to children components
|
||||||
|
// self.browser.save(app_id) @TODO
|
||||||
|
// ..
|
||||||
|
}
|
||||||
|
Err(error) => panic!("{error}"), // @TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Err(error) => panic!("{error}"), // @TODO
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,24 +0,0 @@
|
|||||||
/* @TODO
|
|
||||||
use std::sync::Arc;
|
|
||||||
|
|
||||||
pub struct Browser {
|
|
||||||
connection: Arc<sqlite::Connection>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Browser {
|
|
||||||
// Construct new browser DB (connection)
|
|
||||||
pub fn new(connection: Arc<sqlite::Connection>) -> Browser {
|
|
||||||
let this = Self { connection };
|
|
||||||
this.init();
|
|
||||||
this
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create browser table in DB if not exist yet
|
|
||||||
fn init(&self) {}
|
|
||||||
|
|
||||||
// Save active browser session to DB
|
|
||||||
fn save(&self) {}
|
|
||||||
|
|
||||||
// Restore previous browser session from DB
|
|
||||||
fn restore(&self) {}
|
|
||||||
}*/
|
|
@ -1,17 +1,18 @@
|
|||||||
use sqlite::Connection;
|
use sqlite::Connection;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
const DEBUG: bool = true; // @TODO
|
||||||
|
|
||||||
enum Table {
|
enum Table {
|
||||||
Id,
|
Id,
|
||||||
Time,
|
Time,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
connection: Arc<sqlite::Connection>,
|
connection: Arc<Connection>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
// Construct new application DB
|
|
||||||
pub fn init(connection: Arc<Connection>) -> Database {
|
pub fn init(connection: Arc<Connection>) -> Database {
|
||||||
// Init app table
|
// Init app table
|
||||||
if let Err(error) = connection.execute(
|
if let Err(error) = connection.execute(
|
||||||
@ -22,18 +23,38 @@ impl Database {
|
|||||||
)",
|
)",
|
||||||
[],
|
[],
|
||||||
) {
|
) {
|
||||||
panic!("{error}");
|
panic!("{error}"); // @TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return struct
|
// Return struct
|
||||||
Self { connection }
|
Self { connection }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn add(&self) -> i64 {
|
pub fn add(&self) -> Result<usize, String> {
|
||||||
if let Err(error) = self.connection.execute("INSERT INTO `app`", []) {
|
return match self.connection.execute("INSERT INTO `app`", []) {
|
||||||
panic!("{error}");
|
Ok(total) => {
|
||||||
}
|
if DEBUG {
|
||||||
|
println!("Inserted {total} row to `app` table");
|
||||||
|
}
|
||||||
|
Ok(total)
|
||||||
|
}
|
||||||
|
Err(error) => Err(error.to_string()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn clean(&self) -> Result<usize, String> {
|
||||||
|
return match self.connection.execute("DELETE FROM `app`", []) {
|
||||||
|
Ok(total) => {
|
||||||
|
if DEBUG {
|
||||||
|
println!("Deleted {total} rows from `app` table");
|
||||||
|
}
|
||||||
|
Ok(total)
|
||||||
|
}
|
||||||
|
Err(error) => Err(error.to_string()),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn last_insert_id(&self) -> i64 {
|
||||||
self.connection.last_insert_rowid()
|
self.connection.last_insert_rowid()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user