Yoda/src/app/browser/database.rs

60 lines
1.5 KiB
Rust
Raw Normal View History

2024-10-06 00:43:35 +03:00
use sqlite::{Error, Transaction};
2024-10-04 21:23:38 +03:00
pub struct Table {
pub id: i64,
2024-10-05 15:34:46 +03:00
// pub app_id: i64, not in use
2024-10-04 21:23:38 +03:00
}
pub struct Database {
2024-10-06 00:43:35 +03:00
// nothing yet..
2024-10-04 21:23:38 +03:00
}
impl Database {
pub fn new() -> Self {
Self {}
}
pub fn init(tx: &Transaction) -> Result<usize, Error> {
2024-10-06 00:43:35 +03:00
tx.execute(
2024-10-04 21:23:38 +03:00
"CREATE TABLE IF NOT EXISTS `app_browser`
(
2024-10-05 03:31:28 +03:00
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
2024-10-05 03:27:09 +03:00
`app_id` INTEGER NOT NULL
2024-10-04 21:23:38 +03:00
)",
[],
)
2024-10-04 21:23:38 +03:00
}
2024-10-06 00:43:35 +03:00
pub fn add(&self, tx: &Transaction, app_id: &i64) -> Result<usize, Error> {
tx.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id])
2024-10-04 21:23:38 +03:00
}
2024-10-06 00:43:35 +03:00
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` = ?")?;
2024-10-04 21:23:38 +03:00
2024-10-06 00:43:35 +03:00
let result = stmt.query_map([app_id], |row| {
2024-10-04 21:23:38 +03:00
Ok(Table {
id: row.get(0)?,
2024-10-05 15:34:46 +03:00
// app_id: row.get(1)?, not in use
2024-10-04 21:23:38 +03:00
})
})?;
let mut records = Vec::new();
for record in result {
let table = record?;
records.push(table);
}
Ok(records)
}
2024-10-06 00:43:35 +03:00
pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])
2024-10-04 21:23:38 +03:00
}
2024-10-06 00:43:35 +03:00
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
tx.last_insert_rowid()
2024-10-04 21:23:38 +03:00
}
}