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

86 lines
2.4 KiB
Rust
Raw Normal View History

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