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,
|
||||
/// if you insert with upsert expression that means
|
||||
/// all of them conflict with existing records in the database
|
||||
#[error("RecordNotInserted Error: {0}")]
|
||||
RecordNotInserted(String),
|
||||
#[error("None of the records are being inserted")]
|
||||
RecordNotInserted,
|
||||
}
|
||||
|
||||
/// 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 {
|
||||
match &self.result {
|
||||
#[cfg(feature = "sqlx-mysql")]
|
||||
|
@ -140,18 +140,17 @@ where
|
||||
|
||||
let last_insert_id = match (primary_key, db.support_returning()) {
|
||||
(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)
|
||||
}
|
||||
(None, true) => {
|
||||
let mut rows = db.query_all(statement).await?;
|
||||
let row = match rows.pop() {
|
||||
Some(row) => row,
|
||||
None => {
|
||||
return Err(DbErr::RecordNotInserted(
|
||||
"None of the records are being inserted".to_owned(),
|
||||
))
|
||||
}
|
||||
None => return Err(DbErr::RecordNotInserted),
|
||||
};
|
||||
let cols = PrimaryKey::<A>::iter()
|
||||
.map(|col| col.to_string())
|
||||
@ -161,6 +160,9 @@ where
|
||||
}
|
||||
(None, false) => {
|
||||
let res = db.execute(statement).await?;
|
||||
if res.rows_affected() == 0 {
|
||||
return Err(DbErr::RecordNotInserted);
|
||||
}
|
||||
let last_insert_id = res.last_insert_id();
|
||||
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)
|
||||
.await;
|
||||
|
||||
assert_eq!(
|
||||
res.err(),
|
||||
Some(DbErr::RecordNotInserted(
|
||||
"None of the records are being inserted".to_owned()
|
||||
))
|
||||
);
|
||||
assert_eq!(res.err(), Some(DbErr::RecordNotInserted));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user