diff --git a/Cargo.toml b/Cargo.toml index 88bdfccc..f8388030 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ async-stream = { version = "^0.3" } chrono = { version = "^0", optional = true } futures = { version = "^0.3" } futures-util = { version = "^0.3" } +rust_decimal = { version = "^1", optional = true } sea-query = { version = "^0.12" } sea-orm-macros = { path = "sea-orm-macros", optional = true } serde = { version = "^1.0", features = [ "derive" ] } @@ -45,6 +46,7 @@ serde_json = { version = "^1", optional = true } [dev-dependencies] async-std = { version = "^1.9", features = [ "attributes" ] } maplit = { version = "^1" } +rust_decimal_macros = { version = "^1" } sea-orm = { path = ".", features = ["sqlx-sqlite", "sqlx-json", "sqlx-chrono", "sqlx-decimal", "runtime-async-std-native-tls"] } [features] @@ -54,7 +56,7 @@ macros = [ "sea-orm-macros" ] mock = [] with-json = [ "serde_json", "sea-query/with-json" ] with-chrono = [ "chrono", "sea-query/with-chrono" ] -with-rust_decimal = [ "sea-query/with-rust_decimal" ] +with-rust_decimal = [ "rust_decimal", "sea-query/with-rust_decimal" ] sqlx-dep = [ "sqlx" ] sqlx-json = [ "sqlx/json", "with-json" ] sqlx-chrono = [ "sqlx/chrono", "with-chrono" ] diff --git a/src/executor/query.rs b/src/executor/query.rs index ef43318a..e10e7553 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -1,5 +1,4 @@ use crate::DbErr; -use rust_decimal::prelude::*; use std::fmt; #[derive(Debug)] @@ -47,25 +46,6 @@ impl fmt::Debug for QueryResultRow { } // TryGetable // -impl TryGetable for Decimal { - fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result { - let column = format!("{}{}", pre, col); - match &res.row { - #[cfg(feature = "sqlx-mysql")] - QueryResultRow::SqlxMySql(row) => { - use sqlx::Row; - row.try_get(column.as_str()) - .map_err(crate::sqlx_error_to_query_err) - } - #[cfg(feature = "sqlx-sqlite")] - QueryResultRow::SqlxSqlite(_) => { - panic!("{} unsupported by sqlx-sqlite", stringify!($type)) - } - #[cfg(feature = "mock")] - QueryResultRow::Mock(row) => Ok(row.try_get(column.as_str())?), - } - } -} macro_rules! try_getable_all { ( $type: ty ) => { @@ -76,14 +56,12 @@ 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())?), @@ -131,8 +109,7 @@ 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(_) => { @@ -183,3 +160,5 @@ try_getable_mysql!(u64); try_getable_all!(f32); try_getable_all!(f64); try_getable_all!(String); +#[cfg(feature = "with-rust_decimal")] +try_getable_mysql!(rust_decimal::Decimal);