Skip to content

Commit

Permalink
Use NaiveDatetime for placed_at
Browse files Browse the repository at this point in the history
  • Loading branch information
samsamai committed Jul 9, 2021
1 parent e2cb2de commit 5bc913a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 13 deletions.
21 changes: 15 additions & 6 deletions src/executor/query.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::DbErr;
use chrono::NaiveDateTime;
use std::fmt;

#[derive(Debug)]
Expand Down Expand Up @@ -56,12 +57,14 @@ macro_rules! try_getable_all {
#[cfg(feature = "sqlx-mysql")]
QueryResultRow::SqlxMySql(row) => {
use sqlx::Row;
row.try_get(column.as_str()).map_err(crate::sqlx_error_to_query_err)
row.try_get(column.as_str())
.map_err(crate::sqlx_error_to_query_err)
}
#[cfg(feature = "sqlx-sqlite")]
QueryResultRow::SqlxSqlite(row) => {
use sqlx::Row;
row.try_get(column.as_str()).map_err(crate::sqlx_error_to_query_err)
row.try_get(column.as_str())
.map_err(crate::sqlx_error_to_query_err)
}
#[cfg(feature = "mock")]
QueryResultRow::Mock(row) => Ok(row.try_get(column.as_str())?),
Expand Down Expand Up @@ -109,7 +112,8 @@ macro_rules! try_getable_mysql {
#[cfg(feature = "sqlx-mysql")]
QueryResultRow::SqlxMySql(row) => {
use sqlx::Row;
row.try_get(column.as_str()).map_err(crate::sqlx_error_to_query_err)
row.try_get(column.as_str())
.map_err(crate::sqlx_error_to_query_err)
}
#[cfg(feature = "sqlx-sqlite")]
QueryResultRow::SqlxSqlite(_) => {
Expand Down Expand Up @@ -160,6 +164,7 @@ try_getable_mysql!(u64);
try_getable_all!(f32);
try_getable_all!(f64);
try_getable_all!(String);
try_getable_all!(NaiveDateTime);

#[cfg(feature = "with-rust_decimal")]
use rust_decimal::Decimal;
Expand All @@ -172,14 +177,18 @@ impl TryGetable for Decimal {
#[cfg(feature = "sqlx-mysql")]
QueryResultRow::SqlxMySql(row) => {
use sqlx::Row;
row.try_get(column.as_str()).map_err(crate::sqlx_error_to_query_err)
row.try_get(column.as_str())
.map_err(crate::sqlx_error_to_query_err)
}
#[cfg(feature = "sqlx-sqlite")]
QueryResultRow::SqlxSqlite(row) => {
use sqlx::Row;
let val: f64 = row.try_get(column.as_str()).map_err(crate::sqlx_error_to_query_err)?;
let val: f64 = row
.try_get(column.as_str())
.map_err(crate::sqlx_error_to_query_err)?;
use rust_decimal::prelude::FromPrimitive;
Decimal::from_f64(val).ok_or_else(|| DbErr::Query("Failed to convert f64 into Decimal".to_owned()))
Decimal::from_f64(val)
.ok_or_else(|| DbErr::Query("Failed to convert f64 into Decimal".to_owned()))
}
#[cfg(feature = "mock")]
QueryResultRow::Mock(row) => Ok(row.try_get(column.as_str())?),
Expand Down
1 change: 1 addition & 0 deletions tests/bakery_chain/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct Model {
pub total: Decimal,
pub bakery_id: Option<i32>,
pub customer_id: Option<i32>,
pub placed_at: Option<NaiveDateTime>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
Expand Down
3 changes: 2 additions & 1 deletion tests/crud/create_lineitem.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use super::*;
use chrono::offset::Utc;
use rust_decimal_macros::dec;

pub async fn test_create_lineitem(db: &DbConn) {
Expand Down Expand Up @@ -64,7 +65,7 @@ pub async fn test_create_lineitem(db: &DbConn) {
let order_1 = order::ActiveModel {
bakery_id: Set(Some(bakery_insert_res.last_insert_id as i32)),
customer_id: Set(Some(customer_insert_res.last_insert_id as i32)),
placed_at: Set("placeholder".to_string()),
placed_at: Set(Some(Utc::now().naive_utc())),
..Default::default()
};
let order_insert_res: InsertResult = Order::insert(order_1)
Expand Down
3 changes: 2 additions & 1 deletion tests/crud/create_order.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub use super::*;
use chrono::offset::Utc;
use rust_decimal_macros::dec;

pub async fn test_create_order(db: &DbConn) {
Expand Down Expand Up @@ -65,7 +66,7 @@ pub async fn test_create_order(db: &DbConn) {
bakery_id: Set(Some(bakery_insert_res.last_insert_id as i32)),
customer_id: Set(Some(customer_insert_res.last_insert_id as i32)),
total: Set(dec!(15.10)),
placed_at: Set("placeholder".to_string()),
placed_at: Set(Some(Utc::now().naive_utc())),
..Default::default()
};
let order_insert_res: InsertResult = Order::insert(order_1)
Expand Down
6 changes: 1 addition & 5 deletions tests/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ pub async fn create_order_table(db: &DbConn) -> Result<ExecResult, DbErr> {
.integer()
.not_null(),
)
.col(
ColumnDef::new(order::Column::PlacedAt)
.date_time()
.not_null(),
)
.col(ColumnDef::new(order::Column::PlacedAt).date_time())
.foreign_key(
ForeignKey::create()
.name("FK_order_bakery")
Expand Down

0 comments on commit 5bc913a

Please sign in to comment.