40 lines
1.0 KiB
Rust
40 lines
1.0 KiB
Rust
use rusqlite::{Params, Row, Transaction};
|
|
|
|
#[derive(Debug)]
|
|
pub enum Error {
|
|
SqlError(rusqlite::Error),
|
|
DoubleBooking(String),
|
|
InvalidDrawer(String),
|
|
InvalidAmount(String),
|
|
InvalidPayee(String),
|
|
DoubleSpendingError(String),
|
|
Testing(String),
|
|
}
|
|
|
|
pub type CustomResult<T, E = Error> = Result<T, E>;
|
|
|
|
pub struct TransactionHelper<'a> {
|
|
tx: &'a Transaction<'a>,
|
|
}
|
|
|
|
impl TransactionHelper<'_> {
|
|
pub fn new<'a>(tx: &'a Transaction) -> TransactionHelper<'a> {
|
|
TransactionHelper { tx }
|
|
}
|
|
|
|
pub fn execute<P: Params>(&self, sql: &str, params: P) -> CustomResult<usize> {
|
|
self.tx.execute(sql, params).map_err(Error::SqlError)
|
|
}
|
|
|
|
pub fn query_row<T, P, F>(&self, sql: &str, params: P, f: F) -> CustomResult<T>
|
|
where
|
|
P: Params,
|
|
F: FnOnce(&Row<'_>) -> rusqlite::Result<T>, {
|
|
self.tx.query_row(sql, params, f).map_err(Error::SqlError)
|
|
}
|
|
//
|
|
// pub fn commit(&self) -> CustomResult<()> {
|
|
// self.tx.commit().map_err(Error::SqlError)
|
|
// }
|
|
}
|