Hotfix - Postgres types

This commit is contained in:
Billy Chan 2021-08-06 12:16:18 +08:00
parent 9b61e22ba9
commit d5447bca88
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
3 changed files with 62 additions and 18 deletions

View File

@ -1,4 +1,4 @@
use crate::DbErr;
use crate::{debug_print, DbErr};
use chrono::NaiveDateTime;
use serde_json::Value as Json;
use std::fmt;
@ -92,7 +92,10 @@ macro_rules! try_getable_all {
use sqlx::Row;
match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
}
}
#[cfg(feature = "sqlx-postgres")]
@ -100,7 +103,10 @@ macro_rules! try_getable_all {
use sqlx::Row;
match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
}
}
#[cfg(feature = "sqlx-sqlite")]
@ -108,13 +114,19 @@ macro_rules! try_getable_all {
use sqlx::Row;
match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
}
}
#[cfg(feature = "mock")]
QueryResultRow::Mock(row) => match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
},
}
}
@ -159,7 +171,10 @@ macro_rules! try_getable_unsigned {
use sqlx::Row;
match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
}
}
#[cfg(feature = "sqlx-postgres")]
@ -171,13 +186,19 @@ macro_rules! try_getable_unsigned {
use sqlx::Row;
match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
}
}
#[cfg(feature = "mock")]
QueryResultRow::Mock(row) => match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
},
}
}
@ -220,7 +241,10 @@ macro_rules! try_getable_mysql {
use sqlx::Row;
match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
}
}
#[cfg(feature = "sqlx-postgres")]
@ -234,7 +258,10 @@ macro_rules! try_getable_mysql {
#[cfg(feature = "mock")]
QueryResultRow::Mock(row) => match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
},
}
}
@ -309,7 +336,10 @@ impl TryGetable for Option<Decimal> {
use sqlx::Row;
match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
}
}
#[cfg(feature = "sqlx-postgres")]
@ -317,7 +347,10 @@ impl TryGetable for Option<Decimal> {
use sqlx::Row;
match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
}
}
#[cfg(feature = "sqlx-sqlite")]
@ -325,13 +358,19 @@ impl TryGetable for Option<Decimal> {
let result: Result<Decimal, _> = TryGetable::try_get(res, pre, col);
match result {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
}
}
#[cfg(feature = "mock")]
QueryResultRow::Mock(row) => match row.try_get(column.as_str()) {
Ok(v) => Ok(Some(v)),
Err(_) => Ok(None),
Err(e) => {
debug_print!("{:#?}", e.to_string());
Ok(None)
}
},
}
}

View File

@ -316,7 +316,7 @@ pub async fn group_by() {
#[derive(Debug, FromQueryResult)]
struct SelectResult {
name: String,
number_orders: Option<i32>,
number_orders: Option<i64>,
total_spent: Option<Decimal>,
min_spent: Option<Decimal>,
max_spent: Option<Decimal>,

View File

@ -130,10 +130,15 @@ async fn init_setup(db: &DatabaseConnection) {
}
async fn find_baker_least_sales(db: &DatabaseConnection) -> Option<baker::Model> {
#[cfg(feature = "sqlx-postgres")]
type Type = i64;
#[cfg(not(feature = "sqlx-postgres"))]
type Type = Decimal;
#[derive(Debug, FromQueryResult)]
struct SelectResult {
id: i32,
cakes_sold_opt: Option<Decimal>,
cakes_sold_opt: Option<Type>,
}
#[derive(Debug)]
@ -174,13 +179,13 @@ async fn find_baker_least_sales(db: &DatabaseConnection) -> Option<baker::Model>
.into_iter()
.map(|b| LeastSalesBakerResult {
id: b.id.clone(),
cakes_sold: b.cakes_sold_opt.unwrap_or(dec!(0)),
cakes_sold: b.cakes_sold_opt.unwrap_or_default().into(),
})
.collect();
results.sort_by(|a, b| b.cakes_sold.cmp(&a.cakes_sold));
Baker::find_by_id(results.last().unwrap().id)
Baker::find_by_id(results.last().unwrap().id as i64)
.one(db)
.await
.unwrap()