Yoda/src/app/browser/database.rs

52 lines
1.3 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 fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
2024-11-16 15:39:42 +02:00
`app_id` INTEGER NOT NULL,
FOREIGN KEY (`app_id`) REFERENCES `app`(`id`)
)",
[],
)
2024-10-04 21:23:38 +03:00
}
2024-11-13 11:45:33 +02:00
pub fn insert(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-11-13 11:45:33 +02:00
pub fn select(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
let result = stmt.query_map([app_id], |row| {
Ok(Table {
id: row.get(0)?,
// app_id: row.get(1)?, not in use
})
})?;
2024-10-04 21:23:38 +03:00
let mut records = Vec::new();
2024-10-04 21:23:38 +03:00
for record in result {
let table = record?;
records.push(table);
2024-10-04 21:23:38 +03:00
}
Ok(records)
}
2024-10-04 21:23:38 +03:00
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])
}
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
2024-10-04 21:23:38 +03:00
}