Refactor
This commit is contained in:
parent
f50dc1dd1c
commit
07d5f781ca
@ -1,7 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, ExecResult, Insert,
|
error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, Insert, IntoActiveModel,
|
||||||
IntoActiveModel, Iterable, PrimaryKeyTrait, QueryResult, SelectModel, SelectorRaw, Statement,
|
Iterable, PrimaryKeyTrait, SelectModel, SelectorRaw, Statement, TryFromU64,
|
||||||
TryFromU64,
|
|
||||||
};
|
};
|
||||||
use sea_query::{Expr, FromValueTuple, Iden, InsertStatement, IntoColumnRef, Query, ValueTuple};
|
use sea_query::{Expr, FromValueTuple, Iden, InsertStatement, IntoColumnRef, Query, ValueTuple};
|
||||||
use std::{future::Future, marker::PhantomData};
|
use std::{future::Future, marker::PhantomData};
|
||||||
@ -139,48 +138,37 @@ where
|
|||||||
type PrimaryKey<A> = <<A as ActiveModelTrait>::Entity as EntityTrait>::PrimaryKey;
|
type PrimaryKey<A> = <<A as ActiveModelTrait>::Entity as EntityTrait>::PrimaryKey;
|
||||||
type ValueTypeOf<A> = <PrimaryKey<A> as PrimaryKeyTrait>::ValueType;
|
type ValueTypeOf<A> = <PrimaryKey<A> as PrimaryKeyTrait>::ValueType;
|
||||||
|
|
||||||
enum QueryOrExecResult {
|
let last_insert_id = if db.support_returning() {
|
||||||
Query(QueryResult),
|
|
||||||
Exec(ExecResult),
|
|
||||||
}
|
|
||||||
|
|
||||||
let insert_result = if db.support_returning() {
|
|
||||||
let mut rows = db.query_all(statement).await?;
|
let mut rows = db.query_all(statement).await?;
|
||||||
if rows.is_empty() {
|
let row = match rows.pop() {
|
||||||
|
Some(row) => row,
|
||||||
|
None => {
|
||||||
return Err(DbErr::RecordNotInserted(
|
return Err(DbErr::RecordNotInserted(
|
||||||
"None of the records are being inserted".to_owned(),
|
"None of the records are being inserted".to_owned(),
|
||||||
));
|
))
|
||||||
}
|
}
|
||||||
QueryOrExecResult::Query(rows.remove(rows.len() - 1))
|
|
||||||
} else {
|
|
||||||
QueryOrExecResult::Exec(db.execute(statement).await?)
|
|
||||||
};
|
};
|
||||||
|
match primary_key {
|
||||||
let last_insert_id = match primary_key {
|
|
||||||
Some(value_tuple) => Ok(FromValueTuple::from_value_tuple(value_tuple)),
|
Some(value_tuple) => Ok(FromValueTuple::from_value_tuple(value_tuple)),
|
||||||
None => {
|
None => {
|
||||||
if db.support_returning() {
|
|
||||||
match insert_result {
|
|
||||||
QueryOrExecResult::Query(row) => {
|
|
||||||
let cols = PrimaryKey::<A>::iter()
|
let cols = PrimaryKey::<A>::iter()
|
||||||
.map(|col| col.to_string())
|
.map(|col| col.to_string())
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
row.try_get_many("", cols.as_ref())
|
row.try_get_many("", cols.as_ref())
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match insert_result {
|
let res = db.execute(statement).await?;
|
||||||
QueryOrExecResult::Exec(res) => {
|
match primary_key {
|
||||||
|
Some(value_tuple) => Ok(FromValueTuple::from_value_tuple(value_tuple)),
|
||||||
|
None => {
|
||||||
let last_insert_id = res.last_insert_id();
|
let last_insert_id = res.last_insert_id();
|
||||||
ValueTypeOf::<A>::try_from_u64(last_insert_id)
|
ValueTypeOf::<A>::try_from_u64(last_insert_id)
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.map_err(|_| DbErr::UnpackInsertId)?;
|
.map_err(|_| DbErr::UnpackInsertId)?;
|
||||||
|
|
||||||
Ok(InsertResult { last_insert_id })
|
Ok(InsertResult { last_insert_id })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user