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 {
|
2024-10-07 19:54:28 +03:00
|
|
|
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-07 19:54:28 +03:00
|
|
|
)
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
|
|
|
|
2024-10-07 20:34:48 +03:00
|
|
|
pub fn add(tx: &Transaction, app_id: &i64) -> Result<usize, Error> {
|
2024-10-06 00:43:35 +03:00
|
|
|
tx.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id])
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
|
|
|
|
2024-10-07 20:34:48 +03:00
|
|
|
pub fn records(tx: &Transaction, app_id: &i64) -> Result<Vec<Table>, Error> {
|
2024-10-06 00:43:35 +03:00
|
|
|
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-07 20:34:48 +03:00
|
|
|
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
2024-10-06 00:43:35 +03:00
|
|
|
tx.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
|
|
|
|
2024-10-07 20:34:48 +03:00
|
|
|
pub fn last_insert_id(tx: &Transaction) -> i64 {
|
2024-10-06 00:43:35 +03:00
|
|
|
tx.last_insert_rowid()
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
|
|
|
}
|