mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 00:55:28 +00:00
draft db actions
This commit is contained in:
parent
ad3f6083e1
commit
17dedbd36e
3 changed files with 52 additions and 31 deletions
|
|
@ -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 std::sync::Arc;
|
||||
|
||||
const DEBUG: bool = true; // @TODO
|
||||
|
||||
enum Table {
|
||||
Id,
|
||||
Time,
|
||||
}
|
||||
|
||||
pub struct Database {
|
||||
connection: Arc<sqlite::Connection>,
|
||||
connection: Arc<Connection>,
|
||||
}
|
||||
|
||||
impl Database {
|
||||
// Construct new application DB
|
||||
pub fn init(connection: Arc<Connection>) -> Database {
|
||||
// Init app table
|
||||
if let Err(error) = connection.execute(
|
||||
|
|
@ -22,18 +23,38 @@ impl Database {
|
|||
)",
|
||||
[],
|
||||
) {
|
||||
panic!("{error}");
|
||||
panic!("{error}"); // @TODO
|
||||
}
|
||||
|
||||
// Return struct
|
||||
Self { connection }
|
||||
}
|
||||
|
||||
pub fn add(&self) -> i64 {
|
||||
if let Err(error) = self.connection.execute("INSERT INTO `app`", []) {
|
||||
panic!("{error}");
|
||||
}
|
||||
pub fn add(&self) -> Result<usize, String> {
|
||||
return match self.connection.execute("INSERT INTO `app`", []) {
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue