Use TryGetableMany
This commit is contained in:
parent
d664985ea9
commit
e72d8a9e04
@ -38,13 +38,12 @@ where
|
|||||||
let mut query = self.query;
|
let mut query = self.query;
|
||||||
#[cfg(feature = "sqlx-postgres")]
|
#[cfg(feature = "sqlx-postgres")]
|
||||||
if let DatabaseConnection::SqlxPostgresPoolConnection(_) = db {
|
if let DatabaseConnection::SqlxPostgresPoolConnection(_) = db {
|
||||||
use crate::Iterable;
|
use crate::{sea_query::Query, Iterable};
|
||||||
use sea_query::{Alias, Expr, Query};
|
if <A::Entity as EntityTrait>::PrimaryKey::iter().count() > 0 {
|
||||||
for key in <A::Entity as EntityTrait>::PrimaryKey::iter() {
|
|
||||||
query.returning(
|
query.returning(
|
||||||
Query::select()
|
Query::select()
|
||||||
.expr_as(Expr::col(key), Alias::new("last_insert_id"))
|
.columns(<A::Entity as EntityTrait>::PrimaryKey::iter())
|
||||||
.to_owned(),
|
.take(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -83,12 +82,17 @@ async fn exec_insert<A>(
|
|||||||
where
|
where
|
||||||
A: ActiveModelTrait,
|
A: ActiveModelTrait,
|
||||||
{
|
{
|
||||||
type ValueTypeOf<A> = <<<A as ActiveModelTrait>::Entity as EntityTrait>::PrimaryKey as PrimaryKeyTrait>::ValueType;
|
type PrimaryKey<A> = <<A as ActiveModelTrait>::Entity as EntityTrait>::PrimaryKey;
|
||||||
|
type ValueTypeOf<A> = <PrimaryKey<A> as PrimaryKeyTrait>::ValueType;
|
||||||
let last_insert_id = match db {
|
let last_insert_id = match db {
|
||||||
#[cfg(feature = "sqlx-postgres")]
|
#[cfg(feature = "sqlx-postgres")]
|
||||||
DatabaseConnection::SqlxPostgresPoolConnection(conn) => {
|
DatabaseConnection::SqlxPostgresPoolConnection(conn) => {
|
||||||
|
use crate::{sea_query::Iden, Iterable};
|
||||||
|
let cols = PrimaryKey::<A>::iter()
|
||||||
|
.map(|col| col.to_string())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
let res = conn.query_one(statement).await?.unwrap();
|
let res = conn.query_one(statement).await?.unwrap();
|
||||||
res.try_get_many("", "last_insert_id").unwrap_or_default()
|
res.try_get_many("", cols.as_ref()).unwrap_or_default()
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
let last_insert_id = db.execute(statement).await?.last_insert_id();
|
let last_insert_id = db.execute(statement).await?.last_insert_id();
|
||||||
|
@ -44,6 +44,13 @@ impl QueryResult {
|
|||||||
{
|
{
|
||||||
Ok(T::try_get(self, pre, col)?)
|
Ok(T::try_get(self, pre, col)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn try_get_many<T>(&self, pre: &str, cols: &[String]) -> Result<T, DbErr>
|
||||||
|
where
|
||||||
|
T: TryGetableMany,
|
||||||
|
{
|
||||||
|
Ok(T::try_get_many(self, pre, cols)?)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for QueryResultRow {
|
impl fmt::Debug for QueryResultRow {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user