Use rows_affected
when DB does not support returning
This commit is contained in:
parent
a465d1ebac
commit
03207fbda9
@ -58,8 +58,8 @@ pub enum DbErr {
|
|||||||
/// None of the records are being inserted into the database,
|
/// None of the records are being inserted into the database,
|
||||||
/// if you insert with upsert expression that means
|
/// if you insert with upsert expression that means
|
||||||
/// all of them conflict with existing records in the database
|
/// all of them conflict with existing records in the database
|
||||||
#[error("RecordNotInserted Error: {0}")]
|
#[error("None of the records are being inserted")]
|
||||||
RecordNotInserted(String),
|
RecordNotInserted,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Runtime error
|
/// Runtime error
|
||||||
|
@ -52,7 +52,7 @@ impl ExecResult {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get the number of rows affedted by the operation
|
/// Get the number of rows affected by the operation
|
||||||
pub fn rows_affected(&self) -> u64 {
|
pub fn rows_affected(&self) -> u64 {
|
||||||
match &self.result {
|
match &self.result {
|
||||||
#[cfg(feature = "sqlx-mysql")]
|
#[cfg(feature = "sqlx-mysql")]
|
||||||
|
@ -140,18 +140,17 @@ where
|
|||||||
|
|
||||||
let last_insert_id = match (primary_key, db.support_returning()) {
|
let last_insert_id = match (primary_key, db.support_returning()) {
|
||||||
(Some(value_tuple), _) => {
|
(Some(value_tuple), _) => {
|
||||||
db.execute(statement).await?;
|
let res = db.execute(statement).await?;
|
||||||
|
if res.rows_affected() == 0 {
|
||||||
|
return Err(DbErr::RecordNotInserted);
|
||||||
|
}
|
||||||
FromValueTuple::from_value_tuple(value_tuple)
|
FromValueTuple::from_value_tuple(value_tuple)
|
||||||
}
|
}
|
||||||
(None, true) => {
|
(None, true) => {
|
||||||
let mut rows = db.query_all(statement).await?;
|
let mut rows = db.query_all(statement).await?;
|
||||||
let row = match rows.pop() {
|
let row = match rows.pop() {
|
||||||
Some(row) => row,
|
Some(row) => row,
|
||||||
None => {
|
None => return Err(DbErr::RecordNotInserted),
|
||||||
return Err(DbErr::RecordNotInserted(
|
|
||||||
"None of the records are being inserted".to_owned(),
|
|
||||||
))
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let cols = PrimaryKey::<A>::iter()
|
let cols = PrimaryKey::<A>::iter()
|
||||||
.map(|col| col.to_string())
|
.map(|col| col.to_string())
|
||||||
@ -161,6 +160,9 @@ where
|
|||||||
}
|
}
|
||||||
(None, false) => {
|
(None, false) => {
|
||||||
let res = db.execute(statement).await?;
|
let res = db.execute(statement).await?;
|
||||||
|
if res.rows_affected() == 0 {
|
||||||
|
return Err(DbErr::RecordNotInserted);
|
||||||
|
}
|
||||||
let last_insert_id = res.last_insert_id();
|
let last_insert_id = res.last_insert_id();
|
||||||
ValueTypeOf::<A>::try_from_u64(last_insert_id).map_err(|_| DbErr::UnpackInsertId)?
|
ValueTypeOf::<A>::try_from_u64(last_insert_id).map_err(|_| DbErr::UnpackInsertId)?
|
||||||
}
|
}
|
||||||
|
@ -54,12 +54,7 @@ pub async fn create_insert_default(db: &DatabaseConnection) -> Result<(), DbErr>
|
|||||||
.exec(db)
|
.exec(db)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(res.err(), Some(DbErr::RecordNotInserted));
|
||||||
res.err(),
|
|
||||||
Some(DbErr::RecordNotInserted(
|
|
||||||
"None of the records are being inserted".to_owned()
|
|
||||||
))
|
|
||||||
);
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user