From 151eb1d0743a6f34004187443325d42c37b58ae7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Sat, 28 Oct 2023 22:15:43 +0800 Subject: [PATCH] revert: Again. Refs: 9bac6e91ca9df04ccd8368906e1613cfc5b96218 --- src/database/proxy.rs | 2 +- src/executor/insert.rs | 43 +++++++++++++++++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/database/proxy.rs b/src/database/proxy.rs index f2cacb252..0d069c658 100644 --- a/src/database/proxy.rs +++ b/src/database/proxy.rs @@ -136,7 +136,7 @@ impl From for ProxyExecResult { #[cfg(feature = "sqlx-mysql")] ExecResultHolder::SqlxMySql(result) => { ProxyExecResult::Inserted(vec![ProxyExecResultIdType::Integer( - result.last_insert_id().unwrap_or(0), + result.last_insert_id(), )]) } #[cfg(feature = "sqlx-postgres")] diff --git a/src/executor/insert.rs b/src/executor/insert.rs index 2f6aa74ce..bfd0692a6 100644 --- a/src/executor/insert.rs +++ b/src/executor/insert.rs @@ -1,7 +1,7 @@ use crate::{ - error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, Insert, IntoActiveModel, - Iterable, PrimaryKeyToColumn, PrimaryKeyTrait, SelectModel, SelectorRaw, Statement, TryFromU64, - TryInsert, + error::*, ActiveModelTrait, ColumnTrait, ConnectionTrait, EntityTrait, ExecResultHolder, + Insert, IntoActiveModel, Iterable, PrimaryKeyToColumn, PrimaryKeyTrait, ProxyExecResult, + ProxyExecResultIdType, SelectModel, SelectorRaw, Statement, TryFromU64, TryInsert, }; use sea_query::{Expr, FromValueTuple, Iden, InsertStatement, IntoColumnRef, Query, ValueTuple}; use std::{future::Future, marker::PhantomData}; @@ -241,8 +241,41 @@ where if res.rows_affected() == 0 { return Err(DbErr::RecordNotInserted); } - let last_insert_id = res.last_insert_id(); - ValueTypeOf::::try_from_u64(last_insert_id).map_err(|_| DbErr::UnpackInsertId)? + match res.result { + ExecResultHolder::Proxy(val) => match val { + ProxyExecResult::Empty | ProxyExecResult::Conflicted => { + return Err(DbErr::RecordNotInserted); + } + ProxyExecResult::Inserted(val) => { + match val.last().ok_or(DbErr::UnpackInsertId)? { + ProxyExecResultIdType::Integer(val) => { + ValueTypeOf::::try_from_u64(*val) + .map_err(|_| DbErr::UnpackInsertId)? + } + ProxyExecResultIdType::String(val) => { + ValueTypeOf::::from_value_tuple(ValueTuple::One( + sea_query::Value::String(Some(Box::new(val.to_owned()))), + )) + } + ProxyExecResultIdType::Uuid(val) => { + ValueTypeOf::::from_value_tuple(ValueTuple::One( + sea_query::Value::Uuid(Some(Box::new(val.to_owned()))), + )) + } + ProxyExecResultIdType::Bytes(val) => { + ValueTypeOf::::from_value_tuple(ValueTuple::One( + sea_query::Value::Bytes(Some(Box::new(val.to_owned()))), + )) + } + } + } + }, + _ => { + let last_insert_id = res.last_insert_id(); + ValueTypeOf::::try_from_u64(last_insert_id) + .map_err(|_| DbErr::UnpackInsertId)? + } + } } };