use anyhow crate, return id on insert

This commit is contained in:
yggverse 2025-03-07 18:14:37 +02:00
parent e859b97d79
commit 5effd63575
42 changed files with 496 additions and 1164 deletions

View file

@ -1,12 +1,13 @@
use sqlite::{Error, Transaction};
use anyhow::Result;
use sqlite::Transaction;
pub struct Table {
pub id: i64,
// pub app_browser_id: i64, not in use
}
pub fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute(
pub fn init(tx: &Transaction) -> Result<usize> {
Ok(tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -15,17 +16,18 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
FOREIGN KEY (`app_browser_id`) REFERENCES `app_browser`(`id`)
)",
[],
)
)?)
}
pub fn insert(tx: &Transaction, app_browser_id: i64) -> Result<usize, Error> {
pub fn insert(tx: &Transaction, app_browser_id: i64) -> Result<i64> {
tx.execute(
"INSERT INTO `app_browser_window` (`app_browser_id`) VALUES (?)",
[app_browser_id],
)
)?;
Ok(tx.last_insert_rowid())
}
pub fn select(tx: &Transaction, app_browser_id: i64) -> Result<Vec<Table>, Error> {
pub fn select(tx: &Transaction, app_browser_id: i64) -> Result<Vec<Table>> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_id` FROM `app_browser_window`
@ -49,10 +51,6 @@ pub fn select(tx: &Transaction, app_browser_id: i64) -> Result<Vec<Table>, Error
Ok(records)
}
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `app_browser_window` WHERE `id` = ?", [id])
}
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
pub fn delete(tx: &Transaction, id: i64) -> Result<usize> {
Ok(tx.execute("DELETE FROM `app_browser_window` WHERE `id` = ?", [id])?)
}