diff options
Diffstat (limited to 'src/db')
| -rw-r--r-- | src/db/categories.rs | 19 | ||||
| -rw-r--r-- | src/db/incomes.rs | 42 | ||||
| -rw-r--r-- | src/db/jobs.rs | 6 | ||||
| -rw-r--r-- | src/db/mod.rs | 15 | ||||
| -rw-r--r-- | src/db/payments.rs | 78 | ||||
| -rw-r--r-- | src/db/users.rs | 15 | ||||
| -rw-r--r-- | src/db/utils.rs | 13 |
7 files changed, 79 insertions, 109 deletions
diff --git a/src/db/categories.rs b/src/db/categories.rs index 31cb3d0..abcba59 100644 --- a/src/db/categories.rs +++ b/src/db/categories.rs @@ -1,4 +1,4 @@ -use tokio_rusqlite::{named_params, Connection, Row}; +use tokio_rusqlite::{Connection, Row, named_params}; use crate::db::utils; use crate::model::category::{Category, Create, Update}; @@ -26,11 +26,8 @@ pub async fn list(conn: &Connection) -> Vec<Category> { .call(move |conn| { let mut stmt = conn.prepare(query)?; - let users = stmt - .query_map([], row_to_category)? - .collect::<Result<Vec<Category>, _>>()?; - - Ok(users) + stmt.query_map([], row_to_category)? + .collect::<Result<Vec<Category>, _>>() }) .await; @@ -76,7 +73,7 @@ pub async fn get(conn: &Connection, id: i64) -> Option<Category> { pub async fn create(conn: &Connection, c: Create) -> Option<i64> { let query = r#"INSERT INTO categories(name, color) VALUES (:name, :color)"#; - let res = conn + let res: Result<_, tokio_rusqlite::Error<rusqlite::Error>> = conn .call(move |conn| { conn.execute( query, @@ -107,10 +104,10 @@ pub async fn update(conn: &Connection, id: i64, c: Update) -> bool { let res = conn .call(move |conn| { - Ok(conn.execute( + conn.execute( query, named_params![":name": c.name, ":color": c.color, ":id": id], - )?) + ) }) .await; @@ -126,7 +123,7 @@ pub async fn update(conn: &Connection, id: i64, c: Update) -> bool { pub async fn delete(conn: &Connection, id: i64) -> bool { let res = conn .call(move |conn| { - Ok(conn.execute( + conn.execute( r#" UPDATE categories @@ -136,7 +133,7 @@ pub async fn delete(conn: &Connection, id: i64) -> bool { id = :id "#, named_params![":id": id], - )?) + ) }) .await; diff --git a/src/db/incomes.rs b/src/db/incomes.rs index d33cbcb..11ebb86 100644 --- a/src/db/incomes.rs +++ b/src/db/incomes.rs @@ -1,7 +1,7 @@ use chrono::NaiveDate; use std::collections::HashMap; use std::iter::FromIterator; -use tokio_rusqlite::{named_params, Connection, Row}; +use tokio_rusqlite::{Connection, Row, named_params}; use crate::db::utils; use crate::model::income::{Create, Form, Stat, Table, Update}; @@ -75,14 +75,12 @@ pub async fn list(conn: &Connection, page: i64, per_page: i64) -> Vec<Table> { let res = conn.call(move |conn| { let mut stmt = conn.prepare(query)?; - let incomes = stmt + stmt .query_map( named_params![":limit": per_page, ":offset": (page - 1) * per_page], row_to_table )? - .collect::<Result<Vec<Table>, _>>()?; - - Ok(incomes) + .collect::<Result<Vec<Table>, _>>() }) .await; @@ -164,7 +162,7 @@ pub async fn create(conn: &Connection, i: Create) -> Option<i64> { VALUES (:user_id, :date, :amount) "#; - let res = conn + let res: Result<_, tokio_rusqlite::Error<rusqlite::Error>> = conn .call(move |conn| { conn.execute( query, @@ -205,14 +203,11 @@ pub async fn defined_at( .call(move |conn| { let mut stmt = conn.prepare(query)?; - let incomes = stmt - .query_map( - named_params![":user_id": user_id, ":date": date], - |row| row.get(0), - )? - .collect::<Result<Vec<i64>, _>>()?; - - Ok(incomes) + stmt.query_map( + named_params![":user_id": user_id, ":date": date], + |row| row.get(0), + )? + .collect::<Result<Vec<i64>, _>>() }) .await; @@ -238,7 +233,7 @@ pub async fn update(conn: &Connection, id: i64, i: Update) -> bool { let res = conn .call(move |conn| { - Ok(conn.execute( + conn.execute( query, named_params![ ":user_id": i.user_id, @@ -246,7 +241,7 @@ pub async fn update(conn: &Connection, id: i64, i: Update) -> bool { ":amount": i.amount, ":id": id ], - )?) + ) }) .await; @@ -263,7 +258,7 @@ pub async fn delete(conn: &Connection, id: i64) -> bool { let query = r#"UPDATE incomes SET deleted_at = datetime() WHERE id = :id"#; let res = conn - .call(move |conn| Ok(conn.execute(query, named_params![":id": id])?)) + .call(move |conn| conn.execute(query, named_params![":id": id])) .await; match res { @@ -317,10 +312,8 @@ pub async fn cumulative( let res = conn .call(move |conn| { let mut stmt = conn.prepare(&cumulative_query(from))?; - let incomes = stmt - .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))? - .collect::<Result<Vec<(i64, i64)>, _>>()?; - Ok(incomes) + stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))? + .collect::<Result<Vec<(i64, i64)>, _>>() }) .await; @@ -470,11 +463,8 @@ pub async fn total_each_month(conn: &Connection) -> Vec<Stat> { let res = conn .call(move |conn| { let mut stmt = conn.prepare(query)?; - let stats = stmt - .query_map([], row_to_stat)? - .collect::<Result<Vec<Stat>, _>>()?; - - Ok(stats) + stmt.query_map([], row_to_stat)? + .collect::<Result<Vec<Stat>, _>>() }) .await; diff --git a/src/db/jobs.rs b/src/db/jobs.rs index 0080339..ac75233 100644 --- a/src/db/jobs.rs +++ b/src/db/jobs.rs @@ -1,4 +1,4 @@ -use tokio_rusqlite::{named_params, Connection}; +use tokio_rusqlite::{Connection, named_params}; use crate::model::job::Job; @@ -15,7 +15,7 @@ pub async fn should_run(conn: &Connection, job: Job) -> bool { let res = conn .call(move |conn| { let mut stmt = conn.prepare(&query)?; - Ok(stmt.exists([job.to_string()])?) + stmt.exists([job.to_string()]) }) .await; @@ -37,7 +37,7 @@ pub async fn actualize_last_execution(conn: &Connection, job: Job) { let res = conn .call(move |conn| { - Ok(conn.execute(query, named_params![":name": job.to_string()])?) + conn.execute(query, named_params![":name": job.to_string()]) }) .await; diff --git a/src/db/mod.rs b/src/db/mod.rs index 4894e95..c444995 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -1,5 +1,5 @@ use anyhow::{Error, Result}; -use rusqlite_migration::{Migrations, M}; +use rusqlite_migration::{M, Migrations}; use tokio_rusqlite::Connection; pub mod categories; @@ -30,13 +30,7 @@ async fn apply_migrations(conn: &Connection) -> Result<()> { M::up(include_str!("migrations/06-remove-weekly-report-job.sql")), ]); - Ok(conn - .call(move |conn| { - migrations.to_latest(conn).map_err(|migration_err| { - tokio_rusqlite::Error::Other(Box::new(migration_err)) - }) - }) - .await?) + Ok(conn.call(move |conn| migrations.to_latest(conn)).await?) } async fn set_pragma( @@ -47,9 +41,6 @@ async fn set_pragma( let key = key.into(); let value = value.into(); Ok(conn - .call(move |conn| { - conn.pragma_update(None, &key, &value) - .map_err(tokio_rusqlite::Error::Rusqlite) - }) + .call(move |conn| conn.pragma_update(None, &key, &value)) .await?) } diff --git a/src/db/payments.rs b/src/db/payments.rs index 23b4d2f..a46db05 100644 --- a/src/db/payments.rs +++ b/src/db/payments.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::iter::FromIterator; use tokio_rusqlite::{ - named_params, params_from_iter, types::ToSql, Connection, Row, + Connection, Row, named_params, params_from_iter, types::ToSql, }; use crate::db::utils; @@ -121,10 +121,8 @@ pub async fn list_for_table( let res = conn .call(move |conn| { let mut stmt = conn.prepare(&query)?; - let payments = stmt - .query_map(params_from_iter(params), row_to_table)? - .collect::<Result<Vec<payment::Table>, _>>()?; - Ok(payments) + stmt.query_map(params_from_iter(params), row_to_table)? + .collect::<Result<Vec<payment::Table>, _>>() }) .await; @@ -177,17 +175,17 @@ fn complete_name( query: &mut String, params: &mut Vec<Box<dyn ToSql + Send>>, ) { - if let Some(name) = name { - if !name.is_empty() { - query.push_str( - format!( - "AND {} LIKE ?", - utils::format_key_for_search("payments.name") - ) - .as_str(), - ); - params.push(Box::new(text::format_search(&name))); - } + if let Some(name) = name + && !name.is_empty() + { + query.push_str( + format!( + "AND {} LIKE ?", + utils::format_key_for_search("payments.name") + ) + .as_str(), + ); + params.push(Box::new(text::format_search(&name))); } } @@ -196,11 +194,11 @@ fn complete_cost( query: &mut String, params: &mut Vec<Box<dyn ToSql + Send>>, ) { - if let Some(cost) = cost { - if !cost.is_empty() { - query.push_str("AND payments.cost = ?"); - params.push(Box::new(cost)) - } + if let Some(cost) = cost + && !cost.is_empty() + { + query.push_str("AND payments.cost = ?"); + params.push(Box::new(cost)) } } @@ -232,11 +230,11 @@ fn complete_date( query: &mut String, params: &mut Vec<Box<dyn ToSql + Send>>, ) { - if let Some(date) = date { - if !date.is_empty() { - query.push_str(format!("AND {name_and_op} ?").as_str()); - params.push(Box::new(date)); - } + if let Some(date) = date + && !date.is_empty() + { + query.push_str(format!("AND {name_and_op} ?").as_str()); + params.push(Box::new(date)); } } @@ -259,10 +257,8 @@ pub async fn list_for_stats(conn: &Connection) -> Vec<payment::Stat> { let res = conn .call(move |conn| { let mut stmt = conn.prepare(query)?; - let payments = stmt - .query_map([], row_to_stat)? - .collect::<Result<Vec<payment::Stat>, _>>()?; - Ok(payments) + stmt.query_map([], row_to_stat)? + .collect::<Result<Vec<payment::Stat>, _>>() }) .await; @@ -350,7 +346,7 @@ pub async fn create(conn: &Connection, p: payment::Create) -> Option<i64> { VALUES (:name, :cost, :user_id, :category_id, :date, :frequency) "#; - let res = conn + let res: Result<_, tokio_rusqlite::Error<rusqlite::Error>> = conn .call(move |conn| { conn.execute( query, @@ -391,7 +387,7 @@ pub async fn update(conn: &Connection, id: i64, p: payment::Update) -> bool { let res = conn .call(move |conn| { - Ok(conn.execute( + conn.execute( query, named_params![ ":name": p.name, @@ -401,7 +397,7 @@ pub async fn update(conn: &Connection, id: i64, p: payment::Update) -> bool { ":date": p.date, ":id": id ], - )?) + ) }) .await; @@ -418,7 +414,7 @@ pub async fn delete(conn: &Connection, id: i64) -> bool { let query = r#"UPDATE payments SET deleted_at = datetime() WHERE id = :id"#; let res = conn - .call(move |conn| Ok(conn.execute(query, named_params![":id": id])?)) + .call(move |conn| conn.execute(query, named_params![":id": id])) .await; match res { @@ -448,7 +444,7 @@ pub async fn search_category( let payment_name_closure = payment_name.clone(); - let res = conn + let res: Result<_, tokio_rusqlite::Error<rusqlite::Error>> = conn .call(move |conn| { let mut stmt = conn.prepare(&query)?; let mut iter = stmt.query_map( @@ -461,7 +457,7 @@ pub async fn search_category( match res { Ok(category) => Some(category), - Err(tokio_rusqlite::Error::Rusqlite( + Err(tokio_rusqlite::Error::Error( rusqlite::Error::QueryReturnedNoRows, )) => None, Err(err) => { @@ -488,7 +484,7 @@ pub async fn is_category_used(conn: &Connection, category_id: i64) -> bool { let res = conn .call(move |conn| { let mut stmt = conn.prepare(query)?; - Ok(stmt.exists(named_params![":category_id": category_id])?) + stmt.exists(named_params![":category_id": category_id]) }) .await; @@ -527,10 +523,8 @@ pub async fn repartition(conn: &Connection) -> HashMap<i64, i64> { let res = conn .call(move |conn| { let mut stmt = conn.prepare(query)?; - let payments = stmt - .query_map([], |row| Ok((row.get(0)?, row.get(1)?)))? - .collect::<Result<Vec<(i64, i64)>, _>>()?; - Ok(payments) + stmt.query_map([], |row| Ok((row.get(0)?, row.get(1)?)))? + .collect::<Result<Vec<(i64, i64)>, _>>() }) .await; @@ -559,7 +553,7 @@ pub async fn create_monthly_payments(conn: &Connection) { AND deleted_at IS NULL "#; - let res = conn.call(move |conn| Ok(conn.execute(query, [])?)).await; + let res = conn.call(move |conn| conn.execute(query, [])).await; match res { Ok(_) => (), diff --git a/src/db/users.rs b/src/db/users.rs index 8b21ff4..7af3543 100644 --- a/src/db/users.rs +++ b/src/db/users.rs @@ -1,4 +1,4 @@ -use tokio_rusqlite::{named_params, Connection, Row}; +use tokio_rusqlite::{Connection, Row, named_params}; use crate::db::utils; use crate::model::user::User; @@ -18,11 +18,8 @@ pub async fn list(conn: &Connection) -> Vec<User> { .call(move |conn| { let mut stmt = conn.prepare(query)?; - let users = stmt - .query_map([], row_to_user)? - .collect::<Result<Vec<User>, _>>()?; - - Ok(users) + stmt.query_map([], row_to_user)? + .collect::<Result<Vec<User>, _>>() }) .await; @@ -51,13 +48,13 @@ pub async fn set_login_token( let res = conn .call(move |conn| { - Ok(conn.execute( + conn.execute( query, named_params![ ":login_token": login_token, ":email": email ], - )?) + ) }) .await; @@ -80,7 +77,7 @@ pub async fn remove_login_token(conn: &Connection, id: i64) -> bool { "#; let res = conn - .call(move |conn| Ok(conn.execute(query, named_params![":id": id])?)) + .call(move |conn| conn.execute(query, named_params![":id": id])) .await; match res { diff --git a/src/db/utils.rs b/src/db/utils.rs index 8f8a31d..7c359ab 100644 --- a/src/db/utils.rs +++ b/src/db/utils.rs @@ -1,17 +1,18 @@ pub fn format_key_for_search(value: &str) -> String { // Lower doesn’t work on accentuated letters, hence the need to remove manually accents for // uppercase letters as well. - format!("replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(lower({}), 'à', 'a'), 'â', 'a'), 'ç', 'c'), 'è', 'e'), 'é', 'e'), 'ê', 'e'), 'ë', 'e'), 'î', 'i'), 'ï', 'i'), 'ô', 'o'), 'ù', 'u'), 'û', 'u'), 'ü', 'u'), 'À', 'A'), 'Â', 'A'), 'Ç', 'C'), 'È', 'E'), 'É', 'E'), 'Ê', 'E'), 'Ë', 'E'), 'Î', 'I'), 'Ï', 'I'), 'Ô', 'O'), 'Ù', 'U'), 'Û', 'U'), 'Ü', 'U')", value) + format!( + "replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(lower({}), 'à', 'a'), 'â', 'a'), 'ç', 'c'), 'è', 'e'), 'é', 'e'), 'ê', 'e'), 'ë', 'e'), 'î', 'i'), 'ï', 'i'), 'ô', 'o'), 'ù', 'u'), 'û', 'u'), 'ü', 'u'), 'À', 'A'), 'Â', 'A'), 'Ç', 'C'), 'È', 'E'), 'É', 'E'), 'Ê', 'E'), 'Ë', 'E'), 'Î', 'I'), 'Ï', 'I'), 'Ô', 'O'), 'Ù', 'U'), 'Û', 'U'), 'Ü', 'U')", + value + ) } pub fn one<A, I: Iterator<Item = Result<A, rusqlite::Error>>>( mut iter: I, -) -> Result<A, tokio_rusqlite::Error> { +) -> Result<A, rusqlite::Error> { match iter.next() { Some(Ok(user)) => Ok(user), - Some(Err(err)) => Err(tokio_rusqlite::Error::Rusqlite(err)), - None => Err(tokio_rusqlite::Error::Rusqlite( - rusqlite::Error::QueryReturnedNoRows, - )), + Some(Err(err)) => Err(err), + None => Err(rusqlite::Error::QueryReturnedNoRows), } } |
