use crate::{ error::*, ActiveModelTrait, DatabaseConnection, DeleteMany, DeleteOne, EntityTrait, Statement, }; use sea_query::DeleteStatement; use std::future::Future; #[derive(Clone, Debug)] pub struct Deleter { query: DeleteStatement, } #[derive(Clone, Debug)] pub struct DeleteResult { pub rows_affected: u64, } impl<'a, A: 'a> DeleteOne where A: ActiveModelTrait, { pub fn exec( self, db: &'a DatabaseConnection, ) -> impl Future> + 'a { // so that self is dropped before entering await exec_delete_only(self.query, db) } } impl<'a, E> DeleteMany where E: EntityTrait, { pub fn exec( self, db: &'a DatabaseConnection, ) -> impl Future> + 'a { // so that self is dropped before entering await exec_delete_only(self.query, db) } } impl Deleter { pub fn new(query: DeleteStatement) -> Self { Self { query } } pub fn exec( self, db: &DatabaseConnection, ) -> impl Future> + '_ { let builder = db.get_query_builder_backend(); exec_delete(builder.build(&self.query), db) } } async fn exec_delete_only( query: DeleteStatement, db: &DatabaseConnection, ) -> Result { Deleter::new(query).exec(db).await } // Only Statement impl Send async fn exec_delete( statement: Statement, db: &DatabaseConnection, ) -> Result { let result = db.execute(statement).await?; Ok(DeleteResult { rows_affected: result.rows_affected(), }) }