This commit is contained in:
Billy Chan 2021-08-29 19:39:01 +08:00
parent 3bada7fbc3
commit 752cc4a44e
No known key found for this signature in database
GPG Key ID: A2D690CAC7DF3CC7
3 changed files with 26 additions and 14 deletions

View File

@ -1,11 +1,21 @@
use super::{ColumnTrait, IdenStatic, Iterable};
use crate::TryGetable;
use sea_query::IntoValueTuple;
use std::fmt::{Debug, Display};
use std::{
fmt::{Debug, Display},
str::FromStr,
};
//LINT: composite primary key cannot auto increment
pub trait PrimaryKeyTrait: IdenStatic + Iterable {
type ValueType: Sized + Default + Debug + Display + PartialEq + IntoValueTuple + TryGetable;
type ValueType: Sized
+ Default
+ Debug
+ Display
+ PartialEq
+ IntoValueTuple
+ TryGetable
+ FromStr;
fn auto_increment() -> bool;
}

View File

@ -1,4 +1,5 @@
use crate::TryGetable;
use std::str::FromStr;
#[derive(Debug)]
pub struct ExecResult {
@ -22,14 +23,15 @@ pub(crate) enum ExecResultHolder {
impl ExecResult {
pub fn last_insert_id<T>(&self) -> T
where
T: TryGetable + Default,
T: TryGetable + Default + FromStr,
{
match &self.result {
#[cfg(feature = "sqlx-mysql")]
ExecResultHolder::SqlxMySql(result) => {
// result.last_insert_id()
T::default()
}
ExecResultHolder::SqlxMySql(result) => result
.last_insert_id()
.to_string()
.parse()
.unwrap_or_default(),
#[cfg(feature = "sqlx-postgres")]
ExecResultHolder::SqlxPostgres(result) => {
res.try_get("", "last_insert_id").unwrap_or_default()
@ -40,15 +42,15 @@ impl ExecResult {
if last_insert_rowid < 0 {
panic!("negative last_insert_rowid")
} else {
// last_insert_rowid
T::default()
last_insert_rowid.to_string().parse().unwrap_or_default()
}
}
#[cfg(feature = "mock")]
ExecResultHolder::Mock(result) => {
// result.last_insert_id
T::default()
}
ExecResultHolder::Mock(result) => result
.last_insert_id
.to_string()
.parse()
.unwrap_or_default(),
}
}

View File

@ -1,4 +1,4 @@
use sea_orm::{entity::*, DbConn, InsertResult};
use sea_orm::{entity::*, DbConn};
pub use super::common::bakery_chain::*;