diff --git a/examples/async-std/src/example_cake_filling.rs b/examples/async-std/src/example_cake_filling.rs index c24e7296..4fa188bc 100644 --- a/examples/async-std/src/example_cake_filling.rs +++ b/examples/async-std/src/example_cake_filling.rs @@ -28,7 +28,7 @@ pub enum PrimaryKey { } impl PrimaryKeyTrait for PrimaryKey { - type ValueType = i32; + type ValueType = (i32, i32); fn auto_increment() -> bool { false diff --git a/sea-orm-codegen/tests/entity/cake_filling.rs b/sea-orm-codegen/tests/entity/cake_filling.rs index 227740c1..d100fa8c 100644 --- a/sea-orm-codegen/tests/entity/cake_filling.rs +++ b/sea-orm-codegen/tests/entity/cake_filling.rs @@ -30,7 +30,7 @@ pub enum PrimaryKey { } impl PrimaryKeyTrait for PrimaryKey { - type ValueType = i32; + type ValueType = (i32, i32); fn auto_increment() -> bool { false diff --git a/src/entity/active_model.rs b/src/entity/active_model.rs index 612e285d..f823411c 100644 --- a/src/entity/active_model.rs +++ b/src/entity/active_model.rs @@ -225,16 +225,14 @@ where if ::auto_increment() && res.last_insert_id != ::ValueType::default() { - let last_insert_id = res.last_insert_id.to_string(); let find = E::find_by_id(res.last_insert_id).one(db); let found = find.await; let model: Option = found?; match model { Some(model) => Ok(model.into_active_model()), None => Err(DbErr::Exec(format!( - "Failed to find inserted item: {} {}", + "Failed to find inserted item: {}", E::default().to_string(), - last_insert_id ))), } } else { diff --git a/src/entity/primary_key.rs b/src/entity/primary_key.rs index 9702b8a1..b9c381d4 100644 --- a/src/entity/primary_key.rs +++ b/src/entity/primary_key.rs @@ -1,17 +1,16 @@ use super::{ColumnTrait, IdenStatic, Iterable}; -use crate::{TryFromU64, TryGetable}; +use crate::{TryFromU64, TryGetableMany}; use sea_query::IntoValueTuple; -use std::fmt::{Debug, Display}; +use std::fmt::Debug; //LINT: composite primary key cannot auto increment pub trait PrimaryKeyTrait: IdenStatic + Iterable { type ValueType: Sized + Default + Debug - + Display + PartialEq + IntoValueTuple - + TryGetable + + TryGetableMany + TryFromU64; fn auto_increment() -> bool; diff --git a/src/executor/query.rs b/src/executor/query.rs index 2d9d4db0..88d6a157 100644 --- a/src/executor/query.rs +++ b/src/executor/query.rs @@ -17,10 +17,8 @@ pub(crate) enum QueryResultRow { Mock(crate::MockRow), } -pub trait TryGetable { - fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result - where - Self: Sized; +pub trait TryGetable: Sized { + fn try_get(res: &QueryResult, pre: &str, col: &str) -> Result; } // QueryResult // @@ -406,6 +404,72 @@ impl TryGetable for Option { #[cfg(feature = "with-uuid")] try_getable_all!(uuid::Uuid); +// TryGetableMany // + +pub trait TryGetableMany: Sized { + fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result; +} + +impl TryGetableMany for T +where + T: TryGetable, +{ + fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result { + let expect_len = 1; + if cols.len() < expect_len { + return Err(DbErr::Query(format!( + "Expect {} column names supplied but got slice of length {}", + expect_len, + cols.len() + ))); + } + T::try_get(res, pre, &cols[0]) + } +} + +impl TryGetableMany for (T, T) +where + T: TryGetable, +{ + fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result { + let expect_len = 2; + if cols.len() < expect_len { + return Err(DbErr::Query(format!( + "Expect {} column names supplied but got slice of length {}", + expect_len, + cols.len() + ))); + } + Ok(( + T::try_get(res, pre, &cols[0])?, + T::try_get(res, pre, &cols[1])?, + )) + } +} + +impl TryGetableMany for (T, T, T) +where + T: TryGetable, +{ + fn try_get_many(res: &QueryResult, pre: &str, cols: &[String]) -> Result { + let expect_len = 3; + if cols.len() < expect_len { + return Err(DbErr::Query(format!( + "Expect {} column names supplied but got slice of length {}", + expect_len, + cols.len() + ))); + } + Ok(( + T::try_get(res, pre, &cols[0])?, + T::try_get(res, pre, &cols[1])?, + T::try_get(res, pre, &cols[2])?, + )) + } +} + +// TryFromU64 // + pub trait TryFromU64: Sized { fn try_from_u64(n: u64) -> Result; } diff --git a/src/tests_cfg/cake_filling.rs b/src/tests_cfg/cake_filling.rs index 4336d2fa..ed6d0af0 100644 --- a/src/tests_cfg/cake_filling.rs +++ b/src/tests_cfg/cake_filling.rs @@ -29,7 +29,7 @@ pub enum PrimaryKey { } impl PrimaryKeyTrait for PrimaryKey { - type ValueType = i32; + type ValueType = (i32, i32); fn auto_increment() -> bool { false diff --git a/src/tests_cfg/cake_filling_price.rs b/src/tests_cfg/cake_filling_price.rs index bd2bc8eb..e820ae0f 100644 --- a/src/tests_cfg/cake_filling_price.rs +++ b/src/tests_cfg/cake_filling_price.rs @@ -35,7 +35,7 @@ pub enum PrimaryKey { } impl PrimaryKeyTrait for PrimaryKey { - type ValueType = i32; + type ValueType = (i32, i32); fn auto_increment() -> bool { false diff --git a/tests/common/bakery_chain/cakes_bakers.rs b/tests/common/bakery_chain/cakes_bakers.rs index 853cf6da..98a25682 100644 --- a/tests/common/bakery_chain/cakes_bakers.rs +++ b/tests/common/bakery_chain/cakes_bakers.rs @@ -28,7 +28,7 @@ pub enum PrimaryKey { } impl PrimaryKeyTrait for PrimaryKey { - type ValueType = i32; + type ValueType = (i32, i32); fn auto_increment() -> bool { false