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_window_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_tab`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -15,19 +16,20 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
FOREIGN KEY (`app_browser_window_id`) REFERENCES `app_browser_window`(`id`)
)",
[],
)
)?)
}
pub fn insert(tx: &Transaction, app_browser_window_id: i64) -> Result<usize, Error> {
pub fn insert(tx: &Transaction, app_browser_window_id: i64) -> Result<i64> {
tx.execute(
"INSERT INTO `app_browser_window_tab` (
`app_browser_window_id`
) VALUES (?)",
[app_browser_window_id],
)
)?;
Ok(tx.last_insert_rowid())
}
pub fn select(tx: &Transaction, app_browser_window_id: i64) -> Result<Vec<Table>, Error> {
pub fn select(tx: &Transaction, app_browser_window_id: i64) -> Result<Vec<Table>> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_id` FROM `app_browser_window_tab`
@ -51,10 +53,6 @@ pub fn select(tx: &Transaction, app_browser_window_id: i64) -> Result<Vec<Table>
Ok(records)
}
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `app_browser_window_tab` 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_tab` WHERE `id` = ?", [id])?)
}

View file

@ -7,6 +7,7 @@ use super::{Action as TabAction, BrowserAction, WindowAction};
use crate::Profile;
use action::Action;
use adw::TabPage;
use anyhow::Result;
use client::Client;
use gtk::{
prelude::{ActionExt, ActionMapExt, BoxExt},
@ -164,26 +165,12 @@ impl Item {
}
}
pub fn clean(
&self,
transaction: &Transaction,
app_browser_window_tab_id: i64,
) -> Result<(), String> {
match database::select(transaction, app_browser_window_tab_id) {
Ok(records) => {
for record in records {
match database::delete(transaction, record.id) {
Ok(_) => {
// Delegate clean action to the item childs
self.page.clean(transaction, record.id)?;
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
pub fn clean(&self, transaction: &Transaction, app_browser_window_tab_id: i64) -> Result<()> {
for record in database::select(transaction, app_browser_window_tab_id)? {
database::delete(transaction, record.id)?;
// Delegate clean action to the item childs
self.page.clean(transaction, record.id)?;
}
Ok(())
}
@ -192,32 +179,23 @@ impl Item {
transaction: &Transaction,
app_browser_window_tab_id: i64,
page_position: i32,
) -> Result<(), String> {
match database::insert(
) -> Result<()> {
let id = database::insert(
transaction,
app_browser_window_tab_id,
page_position,
self.tab_page.is_pinned(),
self.tab_page.is_selected(),
) {
Ok(_) => {
// Delegate save action to childs
let id = database::last_insert_id(transaction);
self.page.save(transaction, id)?;
}
Err(e) => return Err(e.to_string()),
}
)?;
self.page.save(transaction, id)?;
Ok(())
}
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
pub fn migrate(tx: &Transaction) -> Result<()> {
// Migrate self components
if let Err(e) = database::init(tx) {
return Err(e.to_string());
}
database::init(tx)?;
// Delegate migration to childs
page::migrate(tx)?;
@ -231,9 +209,6 @@ pub fn migrate(tx: &Transaction) -> Result<(), String> {
pub fn restore(
transaction: &Transaction,
app_browser_window_tab_id: i64,
) -> Result<Vec<database::Table>, String> {
match database::select(transaction, app_browser_window_tab_id) {
Ok(records) => Ok(records),
Err(e) => Err(e.to_string()),
}
) -> Result<Vec<database::Table>> {
database::select(transaction, app_browser_window_tab_id)
}

View file

@ -1,4 +1,5 @@
use sqlite::{Error, Transaction};
use anyhow::Result;
use sqlite::Transaction;
pub struct Table {
pub id: i64,
@ -8,8 +9,8 @@ pub struct Table {
pub is_selected: bool,
}
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_tab_item`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -21,7 +22,7 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
FOREIGN KEY (`app_browser_window_tab_id`) REFERENCES `app_browser_window_tab` (`id`)
)",
[],
)
)?)
}
pub fn insert(
@ -30,7 +31,7 @@ pub fn insert(
page_position: i32,
is_pinned: bool,
is_selected: bool,
) -> Result<usize, Error> {
) -> Result<i64> {
tx.execute(
"INSERT INTO `app_browser_window_tab_item` (
`app_browser_window_tab_id`,
@ -44,10 +45,11 @@ pub fn insert(
is_pinned as i64,
is_selected as i64,
],
)
)?;
Ok(tx.last_insert_rowid())
}
pub fn select(tx: &Transaction, app_browser_window_tab_id: i64) -> Result<Vec<Table>, Error> {
pub fn select(tx: &Transaction, app_browser_window_tab_id: i64) -> Result<Vec<Table>> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_tab_id`,
@ -79,13 +81,9 @@ pub fn select(tx: &Transaction, app_browser_window_tab_id: i64) -> Result<Vec<Ta
Ok(records)
}
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute(
pub fn delete(tx: &Transaction, id: i64) -> Result<usize> {
Ok(tx.execute(
"DELETE FROM `app_browser_window_tab_item` WHERE `id` = ?",
[id],
)
}
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
)?)
}

View file

@ -7,6 +7,7 @@ mod search;
use super::{Action as ItemAction, BrowserAction, Profile, TabAction, WindowAction};
use adw::TabPage;
use anyhow::Result;
use content::Content;
use error::Error;
use input::Input;
@ -104,20 +105,11 @@ impl Page {
&self,
transaction: &Transaction,
app_browser_window_tab_item_id: i64,
) -> Result<(), String> {
match database::select(transaction, app_browser_window_tab_item_id) {
Ok(records) => {
for record in records {
match database::delete(transaction, record.id) {
Ok(_) => {
// Delegate clean action to the item childs
self.navigation.clean(transaction, &record.id)?;
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
) -> Result<()> {
for record in database::select(transaction, app_browser_window_tab_item_id)? {
database::delete(transaction, record.id)?;
// Delegate clean action to the item childs
self.navigation.clean(transaction, &record.id)?;
}
Ok(())
}
@ -127,25 +119,20 @@ impl Page {
&self,
transaction: &Transaction,
app_browser_window_tab_item_id: i64,
) -> Result<(), String> {
) -> Result<()> {
// Begin page restore
match database::select(transaction, app_browser_window_tab_item_id) {
Ok(records) => {
for record in records {
// Restore `Self`
if let Some(title) = record.title {
self.set_title(title.as_str());
}
self.set_needs_attention(record.is_needs_attention);
// Restore child components
self.navigation.restore(transaction, &record.id)?;
// Make initial page history snap using `navigation` values restored
if let Some(uri) = self.navigation.uri() {
self.profile.history.memory.request.set(uri);
}
}
for record in database::select(transaction, app_browser_window_tab_item_id)? {
// Restore `Self`
if let Some(title) = record.title {
self.set_title(title.as_str());
}
self.set_needs_attention(record.is_needs_attention);
// Restore child components
self.navigation.restore(transaction, &record.id)?;
// Make initial page history snap using `navigation` values restored
if let Some(uri) = self.navigation.uri() {
self.profile.history.memory.request.set(uri);
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
@ -155,10 +142,10 @@ impl Page {
&self,
transaction: &Transaction,
app_browser_window_tab_item_id: i64,
) -> Result<(), String> {
) -> Result<()> {
// Keep value in memory until operation complete
let title = self.tab_page.title();
match database::insert(
let id = database::insert(
transaction,
app_browser_window_tab_item_id,
self.tab_page.needs_attention(),
@ -166,15 +153,9 @@ impl Page {
true => None,
false => Some(title.as_str()),
},
) {
Ok(_) => {
let id = database::last_insert_id(transaction);
// Delegate save action to childs
self.navigation.save(transaction, &id)?;
}
Err(e) => return Err(e.to_string()),
}
)?;
// Delegate save action to childs
self.navigation.save(transaction, &id)?;
Ok(())
}
@ -198,11 +179,9 @@ impl Page {
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
pub fn migrate(tx: &Transaction) -> Result<()> {
// Migrate self components
if let Err(e) = database::init(tx) {
return Err(e.to_string());
}
database::init(tx)?;
// Delegate migration to childs
navigation::migrate(tx)?;

View file

@ -1,4 +1,5 @@
use sqlite::{Error, Transaction};
use anyhow::Result;
use sqlite::Transaction;
pub struct Table {
pub id: i64,
@ -7,8 +8,8 @@ pub struct Table {
pub title: Option<String>,
}
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_tab_item_page`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -19,7 +20,7 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
FOREIGN KEY (`app_browser_window_tab_item_id`) REFERENCES `app_browser_window_tab_item` (`id`)
)",
[],
)
)?)
}
pub fn insert(
@ -27,7 +28,7 @@ pub fn insert(
app_browser_window_tab_item_id: i64,
is_needs_attention: bool,
title: Option<&str>,
) -> Result<usize, Error> {
) -> Result<i64> {
tx.execute(
"INSERT INTO `app_browser_window_tab_item_page` (
`app_browser_window_tab_item_id`,
@ -35,10 +36,11 @@ pub fn insert(
`title`
) VALUES (?, ?, ?)",
(app_browser_window_tab_item_id, is_needs_attention, title),
)
)?;
Ok(tx.last_insert_rowid())
}
pub fn select(tx: &Transaction, app_browser_window_tab_item_id: i64) -> Result<Vec<Table>, Error> {
pub fn select(tx: &Transaction, app_browser_window_tab_item_id: i64) -> Result<Vec<Table>> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_tab_item_id`,
@ -67,13 +69,9 @@ pub fn select(tx: &Transaction, app_browser_window_tab_item_id: i64) -> Result<V
Ok(records)
}
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute(
pub fn delete(tx: &Transaction, id: i64) -> Result<usize> {
Ok(tx.execute(
"DELETE FROM `app_browser_window_tab_item_page` WHERE `id` = ?",
[id],
)
}
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
)?)
}

View file

@ -6,6 +6,7 @@ mod reload;
mod request;
use super::{ItemAction, Profile, TabAction, WindowAction};
use anyhow::Result;
use bookmark::Bookmark;
use gtk::{
glib::{GString, Uri},
@ -72,22 +73,12 @@ impl Navigation {
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_id: &i64,
) -> Result<(), String> {
match database::select(transaction, app_browser_window_tab_item_page_id) {
Ok(records) => {
for record in records {
match database::delete(transaction, &record.id) {
Ok(_) => {
// Delegate clean action to the item childs
self.request.clean(transaction, &record.id)?;
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
) -> Result<()> {
for record in database::select(transaction, app_browser_window_tab_item_page_id)? {
database::delete(transaction, &record.id)?;
// Delegate clean action to the item childs
self.request.clean(transaction, &record.id)?;
}
Ok(())
}
@ -95,17 +86,11 @@ impl Navigation {
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_id: &i64,
) -> Result<(), String> {
match database::select(transaction, app_browser_window_tab_item_page_id) {
Ok(records) => {
for record in records {
// Delegate restore action to the item childs
self.request.restore(transaction, &record.id)?;
}
}
Err(e) => return Err(e.to_string()),
) -> Result<()> {
for record in database::select(transaction, app_browser_window_tab_item_page_id)? {
// Delegate restore action to the item childs
self.request.restore(transaction, &record.id)?;
}
Ok(())
}
@ -113,17 +98,10 @@ impl Navigation {
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_id: &i64,
) -> Result<(), String> {
match database::insert(transaction, app_browser_window_tab_item_page_id) {
Ok(_) => {
let id = database::last_insert_id(transaction);
// Delegate save action to childs
self.request.save(transaction, &id)?;
}
Err(e) => return Err(e.to_string()),
}
) -> Result<()> {
let id = database::insert(transaction, app_browser_window_tab_item_page_id)?;
// Delegate save action to childs
self.request.save(transaction, &id)?;
Ok(())
}
@ -173,11 +151,9 @@ impl Navigation {
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
pub fn migrate(tx: &Transaction) -> Result<()> {
// Migrate self components
if let Err(e) = database::init(tx) {
return Err(e.to_string());
}
database::init(tx)?;
// Delegate migration to childs
request::migrate(tx)?;

View file

@ -48,7 +48,7 @@ impl Bookmark for Button {
}
fn update(&self, profile: &Profile, request: &Entry) {
let has_bookmark = profile.bookmark.get(&request.text()).is_ok();
let has_bookmark = profile.bookmark.get(&request.text()).is_some();
self.set_icon_name(icon_name(has_bookmark));
self.set_tooltip_text(Some(tooltip_text(has_bookmark)));
}

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_window_tab_item_page_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_tab_item_page_navigation`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -15,22 +16,20 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
FOREIGN KEY (`app_browser_window_tab_item_page_id`) REFERENCES `app_browser_window_tab_item_page`(`id`)
)",
[],
)
)?)
}
pub fn insert(tx: &Transaction, app_browser_window_tab_item_page_id: &i64) -> Result<usize, Error> {
pub fn insert(tx: &Transaction, app_browser_window_tab_item_page_id: &i64) -> Result<i64> {
tx.execute(
"INSERT INTO `app_browser_window_tab_item_page_navigation` (
`app_browser_window_tab_item_page_id`
) VALUES (?)",
[app_browser_window_tab_item_page_id],
)
)?;
Ok(tx.last_insert_rowid())
}
pub fn select(
tx: &Transaction,
app_browser_window_tab_item_page_id: &i64,
) -> Result<Vec<Table>, Error> {
pub fn select(tx: &Transaction, app_browser_window_tab_item_page_id: &i64) -> Result<Vec<Table>> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_tab_item_page_id`
@ -55,13 +54,9 @@ pub fn select(
Ok(records)
}
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
tx.execute(
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize> {
Ok(tx.execute(
"DELETE FROM `app_browser_window_tab_item_page_navigation` WHERE `id` = ?",
[id],
)
}
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
)?)
}

View file

@ -3,15 +3,15 @@ mod identity;
mod primary_icon;
mod search;
use adw::{prelude::AdwDialogExt, AlertDialog};
use primary_icon::PrimaryIcon;
use super::{ItemAction, Profile};
use adw::{prelude::AdwDialogExt, AlertDialog};
use anyhow::Result;
use gtk::{
glib::{gformat, GString, Uri, UriFlags},
prelude::{EditableExt, EntryExt, WidgetExt},
Entry, EntryIconPosition, StateFlags,
};
use primary_icon::PrimaryIcon;
use sqlite::Transaction;
use std::{cell::Cell, rc::Rc};
@ -29,19 +29,19 @@ pub trait Request {
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String>;
) -> Result<()>;
fn restore(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String>;
) -> Result<()>;
fn save(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String>;
) -> Result<()>;
fn update_primary_icon(&self, profile: &Profile);
fn update_secondary_icon(&self);
@ -147,22 +147,13 @@ impl Request for Entry {
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String> {
match database::select(transaction, app_browser_window_tab_item_page_navigation_id) {
Ok(records) => {
for record in records {
match database::delete(transaction, &record.id) {
Ok(_) => {
// Delegate clean action to the item childs
// nothing yet..
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
) -> Result<()> {
for record in database::select(transaction, app_browser_window_tab_item_page_navigation_id)?
{
database::delete(transaction, &record.id)?;
// Delegate clean action to the item childs
// nothing yet..
}
Ok(())
}
@ -170,21 +161,15 @@ impl Request for Entry {
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String> {
match database::select(transaction, app_browser_window_tab_item_page_navigation_id) {
Ok(records) => {
for record in records {
if let Some(text) = record.text {
self.set_text(&text);
}
// Delegate restore action to the item childs
// nothing yet..
}
) -> Result<()> {
for record in database::select(transaction, app_browser_window_tab_item_page_navigation_id)?
{
if let Some(text) = record.text {
self.set_text(&text);
}
Err(e) => return Err(e.to_string()),
// Delegate restore action to the item childs
// nothing yet..
}
Ok(())
}
@ -192,27 +177,19 @@ impl Request for Entry {
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String> {
) -> Result<()> {
// Keep value in memory until operation complete
let text = self.text();
match database::insert(
let _id = database::insert(
transaction,
app_browser_window_tab_item_page_navigation_id,
match text.is_empty() {
true => None,
false => Some(text.as_str()),
},
) {
Ok(_) => {
// let id = database::last_insert_id(transaction);
// Delegate save action to childs
// nothing yet..
}
Err(e) => return Err(e.to_string()),
}
)?;
// Delegate save action to childs
// nothing yet..
Ok(())
}
@ -369,11 +346,9 @@ impl Request for Entry {
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
pub fn migrate(tx: &Transaction) -> Result<()> {
// Migrate self components
if let Err(e) = database::init(tx) {
return Err(e.to_string());
}
database::init(tx)?;
// Delegate migration to childs
// nothing yet..

View file

@ -1,4 +1,5 @@
use sqlite::{Error, Transaction};
use anyhow::Result;
use sqlite::Transaction;
pub struct Table {
pub id: i64,
@ -6,8 +7,8 @@ pub struct Table {
pub text: Option<String>, // can be stored as NULL
}
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_tab_item_page_navigation_request`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -17,27 +18,28 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
FOREIGN KEY (`app_browser_window_tab_item_page_navigation_id`) REFERENCES `app_browser_window_tab_item_page_navigation`(`id`)
)",
[],
)
)?)
}
pub fn insert(
tx: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
text: Option<&str>,
) -> Result<usize, Error> {
) -> Result<i64> {
tx.execute(
"INSERT INTO `app_browser_window_tab_item_page_navigation_request` (
`app_browser_window_tab_item_page_navigation_id`,
`text`
) VALUES (?, ?)",
(app_browser_window_tab_item_page_navigation_id, text),
)
)?;
Ok(tx.last_insert_rowid())
}
pub fn select(
tx: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<Vec<Table>, Error> {
) -> Result<Vec<Table>> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_tab_item_page_navigation_id`,
@ -64,14 +66,9 @@ pub fn select(
Ok(records)
}
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
tx.execute(
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize> {
Ok(tx.execute(
"DELETE FROM `app_browser_window_tab_item_page_navigation_request` WHERE `id` = ?",
[id],
)
)?)
}
/* not in use
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
} */