2024-10-04 21:23:38 +03:00
|
|
|
use sqlite::{Connection, Error};
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
pub struct Table {
|
|
|
|
pub id: i64,
|
|
|
|
pub app_id: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Database {
|
|
|
|
connection: Arc<Connection>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Database {
|
|
|
|
pub fn init(connection: Arc<Connection>) -> Result<Database, Error> {
|
|
|
|
connection.execute(
|
|
|
|
"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
|
|
|
)",
|
|
|
|
[],
|
|
|
|
)?;
|
|
|
|
|
|
|
|
Ok(Self { connection })
|
|
|
|
}
|
|
|
|
|
2024-10-05 04:54:08 +03:00
|
|
|
pub fn add(&self, app_id: &i64) -> Result<usize, Error> {
|
2024-10-05 03:27:09 +03:00
|
|
|
self.connection
|
|
|
|
.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id])
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
|
|
|
|
2024-10-05 04:54:08 +03:00
|
|
|
pub fn records(&self, app_id: &i64) -> Result<Vec<Table>, Error> {
|
2024-10-05 03:27:09 +03:00
|
|
|
let mut statement = self
|
|
|
|
.connection
|
2024-10-05 05:05:18 +03:00
|
|
|
.prepare("SELECT `id`, `app_id` FROM `app_browser` WHERE `app_id` = ?")?;
|
2024-10-04 21:23:38 +03:00
|
|
|
|
|
|
|
let result = statement.query_map([app_id], |row| {
|
|
|
|
Ok(Table {
|
|
|
|
id: row.get(0)?,
|
|
|
|
app_id: row.get(1)?,
|
|
|
|
})
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let mut records = Vec::new();
|
|
|
|
|
|
|
|
for record in result {
|
|
|
|
let table = record?;
|
|
|
|
records.push(table);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(records)
|
|
|
|
}
|
|
|
|
|
2024-10-05 04:54:08 +03:00
|
|
|
pub fn delete(&self, id: &i64) -> Result<usize, Error> {
|
2024-10-04 21:23:38 +03:00
|
|
|
self.connection
|
|
|
|
.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn last_insert_id(&self) -> i64 {
|
|
|
|
self.connection.last_insert_rowid()
|
|
|
|
}
|
|
|
|
}
|