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 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`
|
2025-01-12 04:02:41 +02:00
|
|
|
pub fn build(connection: &Rc<RwLock<Connection>>) -> Self {
|
|
|
|
Self {
|
|
|
|
connection: connection.clone(),
|
|
|
|
}
|
2024-11-13 08:03:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Getters
|
|
|
|
|
|
|
|
/// Get all records
|
2024-11-22 15:51:03 +02:00
|
|
|
pub fn records(&self) -> Result<Vec<Table>, Error> {
|
2024-11-13 08:10:54 +02:00
|
|
|
let readable = self.connection.read().unwrap();
|
2024-11-22 15:51:03 +02:00
|
|
|
let tx = readable.unchecked_transaction()?;
|
|
|
|
select(&tx)
|
2024-11-13 08:03:48 +02:00
|
|
|
}
|
|
|
|
|
2024-11-14 06:00:41 +02:00
|
|
|
/// Get active profile record if exist
|
2024-11-22 15:51:03 +02:00
|
|
|
pub fn active(&self) -> Result<Option<Table>, Error> {
|
|
|
|
let records = self.records()?;
|
|
|
|
Ok(records.into_iter().find(|record| record.is_active))
|
2024-11-13 08:03:48 +02:00
|
|
|
}
|
2024-11-13 08:32:49 +02:00
|
|
|
|
|
|
|
// Setters
|
|
|
|
|
2024-11-13 12:55:36 +02:00
|
|
|
/// Create new record in `Self` database connected
|
2024-11-22 15:51:03 +02:00
|
|
|
pub fn add(&self, is_active: bool, time: DateTime, name: Option<String>) -> Result<i64, Error> {
|
2024-11-13 11:36:48 +02:00
|
|
|
// Begin new transaction
|
2024-11-13 08:32:49 +02:00
|
|
|
let mut writable = self.connection.write().unwrap();
|
2024-11-22 15:51:03 +02:00
|
|
|
let tx = writable.transaction()?;
|
2024-11-13 08:32:49 +02:00
|
|
|
|
2024-11-13 11:36:48 +02:00
|
|
|
// New record has active status
|
|
|
|
if is_active {
|
|
|
|
// Deactivate other records as only one profile should be active
|
2024-11-22 15:51:03 +02:00
|
|
|
for record in select(&tx)? {
|
|
|
|
update(&tx, record.id, false, record.time, record.name)?;
|
2024-11-13 11:36:48 +02:00
|
|
|
}
|
|
|
|
}
|
2024-11-13 08:32:49 +02:00
|
|
|
|
2024-11-13 11:36:48 +02:00
|
|
|
// Create new record
|
2024-11-22 15:51:03 +02:00
|
|
|
insert(&tx, is_active, time, name)?;
|
2024-11-13 11:36:48 +02:00
|
|
|
|
|
|
|
// Hold insert ID for result
|
2024-11-13 08:32:49 +02:00
|
|
|
let id = last_insert_id(&tx);
|
|
|
|
|
2024-11-13 11:36:48 +02:00
|
|
|
// Done
|
2024-11-22 15:51:03 +02:00
|
|
|
tx.commit()?;
|
2024-11-13 12:55:36 +02:00
|
|
|
|
2024-11-22 15:51:03 +02:00
|
|
|
Ok(id)
|
|
|
|
}
|
2024-11-13 08:03:48 +02:00
|
|
|
}
|
|
|
|
|
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 11:36:48 +02:00
|
|
|
pub fn insert(
|
|
|
|
tx: &Transaction,
|
|
|
|
is_active: bool,
|
2024-11-13 12:55:36 +02:00
|
|
|
time: DateTime,
|
2024-11-14 04:24:32 +02:00
|
|
|
name: Option<String>,
|
2024-11-13 11:36:48 +02:00
|
|
|
) -> Result<usize, Error> {
|
|
|
|
tx.execute(
|
|
|
|
"INSERT INTO `profile` (
|
|
|
|
`is_active`,
|
|
|
|
`time`,
|
|
|
|
`name`
|
|
|
|
) VALUES (?, ?, ?)",
|
|
|
|
(is_active, time.to_unix(), name),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn update(
|
2024-11-13 04:48:13 +02:00
|
|
|
tx: &Transaction,
|
2024-11-13 11:36:48 +02:00
|
|
|
id: i64,
|
2024-11-13 08:03:48 +02:00
|
|
|
is_active: bool,
|
2024-11-13 12:55:36 +02:00
|
|
|
time: DateTime,
|
2024-11-14 04:24:32 +02:00
|
|
|
name: Option<String>,
|
2024-11-13 04:48:13 +02:00
|
|
|
) -> Result<usize, Error> {
|
2024-11-13 11:36:48 +02:00
|
|
|
tx.execute(
|
2024-11-13 12:55:36 +02:00
|
|
|
"UPDATE `profile` SET `is_active` = ?, `time` = ?, `name` = ? WHERE `id` = ?",
|
2024-11-13 11:36:48 +02:00
|
|
|
(is_active, time.to_unix(), name, id),
|
|
|
|
)
|
2024-11-08 07:46:25 +02:00
|
|
|
}
|
2024-11-12 12:30:12 +02:00
|
|
|
|
2024-11-13 11:36:48 +02:00
|
|
|
pub fn select(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 last_insert_id(tx: &Transaction) -> i64 {
|
|
|
|
tx.last_insert_rowid()
|
2024-11-12 12:30:12 +02:00
|
|
|
}
|