Delete and example
This commit is contained in:
parent
162f6f4331
commit
5069f92001
@ -120,6 +120,10 @@ UPDATE `fruit` SET `name` = 'banana banana' WHERE `fruit`.`id` = 22
|
||||
|
||||
Updated: ActiveModel { id: ActiveValue { value: 22, state: Unchanged }, name: ActiveValue { value: "banana banana", state: Set }, cake_id: ActiveValue { value: None, state: Unchanged } }
|
||||
|
||||
DELETE FROM `fruit` WHERE `fruit`.`id` = 22
|
||||
|
||||
Deleted: DeleteResult { rows_affected: 1 }
|
||||
|
||||
===== =====
|
||||
|
||||
INSERT INTO `fruit` (`name`) VALUES ('pineapple')
|
||||
|
@ -61,6 +61,11 @@ pub async fn save_active_model(db: &Database) -> Result<(), ExecErr> {
|
||||
println!();
|
||||
println!("Updated: {:?}\n", banana);
|
||||
|
||||
let result = banana.delete(db).await?;
|
||||
|
||||
println!();
|
||||
println!("Deleted: {:?}\n", result);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -40,6 +40,10 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
|
||||
pub async fn save(self, db: &sea_orm::Database) -> Result<Self, sea_orm::ExecErr> {
|
||||
sea_orm::save_active_model::<Self, Entity>(self, db).await
|
||||
}
|
||||
|
||||
pub async fn delete(self, db: &sea_orm::Database) -> Result<sea_orm::DeleteResult, sea_orm::ExecErr> {
|
||||
sea_orm::delete_active_model::<Self, Entity>(self, db).await
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ActiveModel {
|
||||
@ -56,7 +60,7 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
|
||||
}
|
||||
}
|
||||
|
||||
impl sea_orm::IntoActiveModel<ActiveModel> for Model {
|
||||
impl sea_orm::IntoActiveModel<ActiveModel> for <Entity as EntityTrait>::Model {
|
||||
fn into_active_model(self) -> ActiveModel {
|
||||
self.into()
|
||||
}
|
||||
|
71
src/connector/delete.rs
Normal file
71
src/connector/delete.rs
Normal file
@ -0,0 +1,71 @@
|
||||
use crate::{
|
||||
ActiveModelTrait, Connection, Database, DeleteMany, DeleteOne, EntityTrait, ExecErr, Statement,
|
||||
};
|
||||
use sea_query::{DeleteStatement, QueryBuilder};
|
||||
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<A>
|
||||
where
|
||||
A: ActiveModelTrait,
|
||||
{
|
||||
pub fn exec(
|
||||
self,
|
||||
db: &'a Database,
|
||||
) -> impl Future<Output = Result<DeleteResult, ExecErr>> + 'a {
|
||||
// so that self is dropped before entering await
|
||||
exec_delete_only(self.query, db)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, E> DeleteMany<E>
|
||||
where
|
||||
E: EntityTrait,
|
||||
{
|
||||
pub fn exec(
|
||||
self,
|
||||
db: &'a Database,
|
||||
) -> impl Future<Output = Result<DeleteResult, ExecErr>> + '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 build<B>(&self, builder: B) -> Statement
|
||||
where
|
||||
B: QueryBuilder,
|
||||
{
|
||||
self.query.build(builder).into()
|
||||
}
|
||||
|
||||
pub fn exec(self, db: &Database) -> impl Future<Output = Result<DeleteResult, ExecErr>> + '_ {
|
||||
let builder = db.get_query_builder_backend();
|
||||
exec_delete(self.build(builder), db)
|
||||
}
|
||||
}
|
||||
|
||||
async fn exec_delete_only(query: DeleteStatement, db: &Database) -> Result<DeleteResult, ExecErr> {
|
||||
Deleter::new(query).exec(db).await
|
||||
}
|
||||
|
||||
// Only Statement impl Send
|
||||
async fn exec_delete(statement: Statement, db: &Database) -> Result<DeleteResult, ExecErr> {
|
||||
let result = db.get_connection().execute(statement).await?;
|
||||
Ok(DeleteResult {
|
||||
rows_affected: result.rows_affected(),
|
||||
})
|
||||
}
|
@ -1,9 +1,11 @@
|
||||
mod delete;
|
||||
mod executor;
|
||||
mod insert;
|
||||
mod paginator;
|
||||
mod select;
|
||||
mod update;
|
||||
|
||||
pub use delete::*;
|
||||
pub use executor::*;
|
||||
pub use insert::*;
|
||||
pub use paginator::*;
|
||||
|
@ -1,4 +1,4 @@
|
||||
use crate::{Database, EntityTrait, ExecErr, Iterable, PrimaryKeyToColumn, Value};
|
||||
use crate::{Database, DeleteResult, EntityTrait, ExecErr, Iterable, PrimaryKeyToColumn, Value};
|
||||
use std::fmt::Debug;
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
@ -268,3 +268,12 @@ where
|
||||
let exec = E::update(am).exec(db);
|
||||
exec.await
|
||||
}
|
||||
|
||||
pub async fn delete_active_model<A, E>(am: A, db: &Database) -> Result<DeleteResult, ExecErr>
|
||||
where
|
||||
A: ActiveModelTrait<Entity = E>,
|
||||
E: EntityTrait,
|
||||
{
|
||||
let exec = E::delete(am).exec(db);
|
||||
exec.await
|
||||
}
|
||||
|
@ -1,7 +1,7 @@
|
||||
use crate::{
|
||||
ActiveModelTrait, ColumnTrait, FromQueryResult, Insert, ModelTrait, OneOrManyActiveModel,
|
||||
PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, RelationBuilder, RelationTrait, RelationType,
|
||||
Select, Update, UpdateOne,
|
||||
ActiveModelTrait, ColumnTrait, Delete, DeleteOne, FromQueryResult, Insert, ModelTrait,
|
||||
OneOrManyActiveModel, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, RelationBuilder,
|
||||
RelationTrait, RelationType, Select, Update, UpdateOne,
|
||||
};
|
||||
use sea_query::{Iden, IntoValueTuple};
|
||||
use std::fmt::Debug;
|
||||
@ -217,4 +217,25 @@ pub trait EntityTrait: EntityName {
|
||||
{
|
||||
Update::one(model)
|
||||
}
|
||||
|
||||
/// ```
|
||||
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit, sea_query::PostgresQueryBuilder};
|
||||
///
|
||||
/// let orange = fruit::ActiveModel {
|
||||
/// id: Set(3),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
/// assert_eq!(
|
||||
/// fruit::Entity::delete(orange)
|
||||
/// .build(PostgresQueryBuilder)
|
||||
/// .to_string(),
|
||||
/// r#"DELETE FROM "fruit" WHERE "fruit"."id" = 3"#,
|
||||
/// );
|
||||
/// ```
|
||||
fn delete<A>(model: A) -> DeleteOne<A>
|
||||
where
|
||||
A: ActiveModelTrait<Entity = Self>,
|
||||
{
|
||||
Delete::one(model)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user