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,4 +1,5 @@
use sqlite::{Connection, Error, Transaction};
use anyhow::Result;
use sqlite::{Connection, Transaction};
use std::{rc::Rc, sync::RwLock};
#[derive(Clone)]
@ -18,7 +19,7 @@ impl Database {
// Constructors
/// Create new `Self`
pub fn init(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self, Error> {
pub fn init(connection: &Rc<RwLock<Connection>>, profile_id: &Rc<i64>) -> Result<Self> {
let mut writable = connection.write().unwrap(); // @TODO handle
let tx = writable.transaction()?;
@ -38,7 +39,7 @@ impl Database {
// Getters
/// Get records from database
pub fn records(&self) -> Result<Vec<Row>, Error> {
pub fn records(&self) -> Result<Vec<Row>> {
let readable = self.connection.read().unwrap(); // @TODO handle
let tx = readable.unchecked_transaction()?;
select(&tx, *self.profile_id)
@ -48,30 +49,19 @@ impl Database {
/// Create new record in database
/// * return last insert ID on success
pub fn add(&self, query: String, is_default: bool) -> Result<i64, Error> {
// Begin new transaction
pub fn add(&self, query: String, is_default: bool) -> Result<i64> {
let mut writable = self.connection.write().unwrap(); // @TODO handle
let tx = writable.transaction()?;
// Create new record
if is_default {
// make sure only one default provider in set
reset(&tx, *self.profile_id, !is_default)?;
}
insert(&tx, *self.profile_id, query, is_default)?;
// Hold insert ID for result
let id = last_insert_id(&tx);
// Done
match tx.commit() {
Ok(_) => Ok(id),
Err(e) => Err(e),
}
let id = insert(&tx, *self.profile_id, query, is_default)?;
tx.commit()?;
Ok(id)
}
/// Delete record from database
pub fn delete(&self, id: i64) -> Result<(), Error> {
pub fn delete(&self, id: i64) -> Result<()> {
// Begin new transaction
let mut writable = self.connection.write().unwrap(); // @TODO
let tx = writable.transaction()?;
@ -100,11 +90,12 @@ impl Database {
}
// Done
tx.commit()
tx.commit()?;
Ok(())
}
/// Delete record from database
pub fn set_default(&self, id: i64) -> Result<(), Error> {
pub fn set_default(&self, id: i64) -> Result<()> {
// Begin new transaction
let mut writable = self.connection.write().unwrap(); // @TODO
let tx = writable.transaction()?;
@ -114,14 +105,15 @@ impl Database {
// Delete record by ID
set_default(&tx, *self.profile_id, id, true)?;
tx.commit()
tx.commit()?;
Ok(())
}
}
// Low-level DB API
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 `profile_search`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
@ -132,15 +124,10 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
FOREIGN KEY (`profile_id`) REFERENCES `profile` (`id`)
)",
[],
)
)?)
}
fn insert(
tx: &Transaction,
profile_id: i64,
query: String,
is_default: bool,
) -> Result<usize, Error> {
fn insert(tx: &Transaction, profile_id: i64, query: String, is_default: bool) -> Result<i64> {
tx.execute(
"INSERT INTO `profile_search` (
`profile_id`,
@ -148,10 +135,11 @@ fn insert(
`query`
) VALUES (?, ?, ?)",
(profile_id, is_default, query),
)
)?;
Ok(tx.last_insert_rowid())
}
fn select(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>, Error> {
fn select(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>> {
let mut stmt = tx.prepare(
"SELECT `id`, `profile_id`, `is_default`, `query`
FROM `profile_search`
@ -177,35 +165,26 @@ fn select(tx: &Transaction, profile_id: i64) -> Result<Vec<Row>, Error> {
Ok(records)
}
fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `profile_search` WHERE `id` = ?", [id])
fn delete(tx: &Transaction, id: i64) -> Result<usize> {
Ok(tx.execute("DELETE FROM `profile_search` WHERE `id` = ?", [id])?)
}
fn reset(tx: &Transaction, profile_id: i64, is_default: bool) -> Result<usize, Error> {
tx.execute(
fn reset(tx: &Transaction, profile_id: i64, is_default: bool) -> Result<usize> {
Ok(tx.execute(
"UPDATE `profile_search` SET `is_default` = ? WHERE `profile_id` = ?",
(is_default, profile_id),
)
)?)
}
fn set_default(
tx: &Transaction,
profile_id: i64,
id: i64,
is_default: bool,
) -> Result<usize, Error> {
tx.execute(
fn set_default(tx: &Transaction, profile_id: i64, id: i64, is_default: bool) -> Result<usize> {
Ok(tx.execute(
"UPDATE `profile_search` SET `is_default` = ? WHERE `profile_id` = ? AND `id` = ?",
(is_default, profile_id, id),
)
}
fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
)?)
}
/// Init default search providers list for given profile
fn add_defaults(tx: &Transaction, profile_id: i64) -> Result<(), Error> {
fn add_defaults(tx: &Transaction, profile_id: i64) -> Result<()> {
for (provider, is_default) in &[
("gemini://tlgs.one/search/search", true),
("gemini://kennedy.gemi.dev/search", false),

View file

@ -1,16 +0,0 @@
use std::fmt::{Display, Formatter, Result};
#[derive(Debug)]
pub enum Error {
Database(sqlite::Error),
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
Self::Database(e) => {
write!(f, "Database error: {e}")
}
}
}
}