use anyhow::Result;
use sqlite::Transaction;
pub struct Table {
pub id: i64,
}
pub fn init(tx: &Transaction) -> Result<usize> {
Ok(tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_id` INTEGER NOT NULL,
FOREIGN KEY (`app_id`) REFERENCES `app`(`id`)
)",
[],
)?)
}
pub fn insert(tx: &Transaction, app_id: i64) -> Result<i64> {
tx.execute("INSERT INTO `app_browser` (`app_id`) VALUES (?)", [app_id])?;
Ok(tx.last_insert_rowid())
}
pub fn select(tx: &Transaction, app_id: i64) -> Result<Vec<Table>> {
let mut stmt = tx.prepare("SELECT `id`, `app_id` FROM `app_browser` WHERE `app_id` = ?")?;
let result = stmt.query_map([app_id], |row| {
Ok(Table {
id: row.get(0)?,
})
})?;
let mut records = Vec::new();
for record in result {
let table = record?;
records.push(table);
}
Ok(records)
}
pub fn delete(tx: &Transaction, id: i64) -> Result<usize> {
Ok(tx.execute("DELETE FROM `app_browser` WHERE `id` = ?", [id])?)
}