Yoda/src/app/browser/window/tab/database.rs

74 lines
2.0 KiB
Rust
Raw Normal View History

2024-10-06 18:20:36 +03:00
use sqlite::{Error, Transaction};
pub struct Table {
pub id: i64,
// pub app_browser_window_id: i64, not in use
2024-10-06 20:47:08 +03:00
pub is_current: bool,
2024-10-06 18:20:36 +03:00
}
pub struct Database {
// nothing yet..
}
impl Database {
pub fn init(tx: &Transaction) -> Result<usize, Error> {
2024-10-06 18:20:36 +03:00
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
2024-10-06 20:47:08 +03:00
`app_browser_window_id` INTEGER NOT NULL,
`is_current` INTEGER NOT NULL
2024-10-06 18:20:36 +03:00
)",
[],
)
2024-10-06 18:20:36 +03:00
}
2024-10-06 20:47:08 +03:00
pub fn add(
tx: &Transaction,
app_browser_window_id: &i64,
is_current: &bool,
) -> Result<usize, Error> {
2024-10-06 18:20:36 +03:00
tx.execute(
2024-10-06 20:47:08 +03:00
"INSERT INTO `app_browser_window_tab` (
`app_browser_window_id`,
`is_current`
) VALUES (?,?)",
[app_browser_window_id, &(*is_current as i64)],
2024-10-06 18:20:36 +03:00
)
}
2024-10-07 20:34:48 +03:00
pub fn records(tx: &Transaction, app_browser_window_id: &i64) -> Result<Vec<Table>, Error> {
2024-10-06 20:47:08 +03:00
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_id`,
`is_current` FROM `app_browser_window_tab`
WHERE `app_browser_window_id` = ?",
)?;
2024-10-06 18:20:36 +03:00
let result = stmt.query_map([app_browser_window_id], |row| {
Ok(Table {
id: row.get(0)?,
// app_browser_window_id: row.get(1)?, not in use
2024-10-06 20:47:08 +03:00
is_current: row.get(2)?,
2024-10-06 18:20:36 +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 18:20:36 +03:00
tx.execute("DELETE FROM `app_browser_window_tab` WHERE `id` = ?", [id])
}
2024-10-07 20:34:48 +03:00
pub fn last_insert_id(tx: &Transaction) -> i64 {
2024-10-06 18:20:36 +03:00
tx.last_insert_rowid()
2024-10-07 04:38:22 +03:00
}
2024-10-06 18:20:36 +03:00
}