2024-11-13 04:35:01 +02:00
|
|
|
use gtk::glib::DateTime;
|
2024-11-13 08:03:48 +02:00
|
|
|
use sqlite::{Connection, Error, Transaction};
|
|
|
|
use std::{rc::Rc, sync::RwLock};
|
2024-11-13 04:35:01 +02:00
|
|
|
|
2024-11-13 08:03:48 +02:00
|
|
|
#[derive(Debug)]
|
2024-11-13 03:40:29 +02:00
|
|
|
pub struct Table {
|
|
|
|
pub id: i64,
|
2024-11-13 08:03:48 +02:00
|
|
|
pub is_active: bool,
|
2024-11-13 04:35:01 +02:00
|
|
|
pub time: DateTime,
|
|
|
|
pub name: Option<String>,
|
2024-11-13 03:40:29 +02:00
|
|
|
}
|
2024-11-08 07:46:25 +02:00
|
|
|
|
2024-11-13 08:03:48 +02:00
|
|
|
pub struct Database {
|
|
|
|
pub connection: Rc<RwLock<Connection>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Database {
|
|
|
|
// Constructors
|
|
|
|
|
|
|
|
/// Create new `Self`
|
|
|
|
pub fn new(connection: Rc<RwLock<Connection>>) -> Self {
|
|
|
|
Self { connection }
|
|
|
|
}
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
|
|
|
/// Get all records
|
|
|
|
pub fn records(&self) -> Vec<Table> {
|
|
|
|
let binding = self.connection.read().unwrap();
|
|
|
|
let tx = binding.unchecked_transaction().unwrap();
|
|
|
|
records(&tx).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get selected profile record if exist
|
|
|
|
pub fn selected(&self) -> Option<Table> {
|
2024-11-13 08:07:32 +02:00
|
|
|
for record in self.records() {
|
2024-11-13 08:03:48 +02:00
|
|
|
if record.is_active {
|
|
|
|
return Some(record);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-13 08:08:54 +02:00
|
|
|
// Low-level DB API
|
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub fn init(tx: &Transaction) -> Result<usize, Error> {
|
|
|
|
tx.execute(
|
|
|
|
"CREATE TABLE IF NOT EXISTS `profile`
|
|
|
|
(
|
2024-11-13 08:03:48 +02:00
|
|
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
|
|
|
`is_active` INTEGER NOT NULL,
|
|
|
|
`time` INTEGER NOT NULL,
|
|
|
|
`name` VARCHAR(255)
|
2024-11-13 03:40:29 +02:00
|
|
|
)",
|
|
|
|
[],
|
|
|
|
)
|
|
|
|
}
|
2024-11-08 07:46:25 +02:00
|
|
|
|
2024-11-13 04:48:13 +02:00
|
|
|
pub fn add(
|
|
|
|
tx: &Transaction,
|
2024-11-13 08:03:48 +02:00
|
|
|
is_active: bool,
|
2024-11-13 04:48:13 +02:00
|
|
|
time: &DateTime,
|
|
|
|
name: Option<&str>,
|
|
|
|
) -> Result<usize, Error> {
|
2024-11-13 08:03:48 +02:00
|
|
|
tx.execute("INSERT INTO `profile`", (is_active, time.to_unix(), name))
|
2024-11-08 07:46:25 +02:00
|
|
|
}
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub fn records(tx: &Transaction) -> Result<Vec<Table>, Error> {
|
2024-11-13 08:03:48 +02:00
|
|
|
let mut stmt = tx.prepare("SELECT `id`, `is_active`, `time`, `name` FROM `profile`")?;
|
2024-11-13 04:35:01 +02:00
|
|
|
let result = stmt.query_map([], |row| {
|
|
|
|
Ok(Table {
|
|
|
|
id: row.get(0)?,
|
2024-11-13 08:03:48 +02:00
|
|
|
is_active: row.get(1)?,
|
2024-11-13 04:48:13 +02:00
|
|
|
time: DateTime::from_unix_local(row.get(2)?).unwrap(),
|
|
|
|
name: row.get(3)?,
|
2024-11-13 04:35:01 +02:00
|
|
|
})
|
|
|
|
})?;
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
let mut records = Vec::new();
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
for record in result {
|
|
|
|
let table = record?;
|
|
|
|
records.push(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(records)
|
|
|
|
}
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
|
|
|
tx.execute("DELETE FROM `profile` WHERE `id` = ?", [id])
|
|
|
|
}
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
pub fn last_insert_id(tx: &Transaction) -> i64 {
|
|
|
|
tx.last_insert_rowid()
|
2024-11-12 12:30:12 +02:00
|
|
|
}
|