mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-03-31 16:45:27 +00:00
draft database features
This commit is contained in:
parent
227cfb30a1
commit
3dffcb029a
5 changed files with 305 additions and 14 deletions
64
src/app/browser/window/database.rs
Normal file
64
src/app/browser/window/database.rs
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
// pub app_browser_id: i64, not in use
|
||||
}
|
||||
|
||||
pub struct Database {
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn init(tx: &Transaction) -> Result<Database, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `app_browser_window`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`app_browser_id` INTEGER NOT NULL
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
|
||||
Ok(Self {})
|
||||
}
|
||||
|
||||
pub fn add(&self, tx: &Transaction, app_browser_id: &i64) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"INSERT INTO `app_browser_window` (`app_browser_id`) VALUES (?)",
|
||||
[app_browser_id],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn records(&self, tx: &Transaction, app_browser_id: &i64) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`,
|
||||
`app_browser_id` FROM `app_browser_window`
|
||||
WHERE `app_browser_id` = ?",
|
||||
)?;
|
||||
|
||||
let result = stmt.query_map([app_browser_id], |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
// app_browser_id: row.get(1)?, not in use
|
||||
})
|
||||
})?;
|
||||
|
||||
let mut records = Vec::new();
|
||||
|
||||
for record in result {
|
||||
let table = record?;
|
||||
records.push(table);
|
||||
}
|
||||
|
||||
Ok(records)
|
||||
}
|
||||
|
||||
pub fn delete(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `app_browser_window` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
||||
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue