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 } }
|
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')
|
INSERT INTO `fruit` (`name`) VALUES ('pineapple')
|
||||||
|
@ -61,6 +61,11 @@ pub async fn save_active_model(db: &Database) -> Result<(), ExecErr> {
|
|||||||
println!();
|
println!();
|
||||||
println!("Updated: {:?}\n", banana);
|
println!("Updated: {:?}\n", banana);
|
||||||
|
|
||||||
|
let result = banana.delete(db).await?;
|
||||||
|
|
||||||
|
println!();
|
||||||
|
println!("Deleted: {:?}\n", result);
|
||||||
|
|
||||||
Ok(())
|
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> {
|
pub async fn save(self, db: &sea_orm::Database) -> Result<Self, sea_orm::ExecErr> {
|
||||||
sea_orm::save_active_model::<Self, Entity>(self, db).await
|
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 {
|
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 {
|
fn into_active_model(self) -> ActiveModel {
|
||||||
self.into()
|
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 executor;
|
||||||
mod insert;
|
mod insert;
|
||||||
mod paginator;
|
mod paginator;
|
||||||
mod select;
|
mod select;
|
||||||
mod update;
|
mod update;
|
||||||
|
|
||||||
|
pub use delete::*;
|
||||||
pub use executor::*;
|
pub use executor::*;
|
||||||
pub use insert::*;
|
pub use insert::*;
|
||||||
pub use paginator::*;
|
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;
|
use std::fmt::Debug;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
@ -268,3 +268,12 @@ where
|
|||||||
let exec = E::update(am).exec(db);
|
let exec = E::update(am).exec(db);
|
||||||
exec.await
|
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::{
|
use crate::{
|
||||||
ActiveModelTrait, ColumnTrait, FromQueryResult, Insert, ModelTrait, OneOrManyActiveModel,
|
ActiveModelTrait, ColumnTrait, Delete, DeleteOne, FromQueryResult, Insert, ModelTrait,
|
||||||
PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, RelationBuilder, RelationTrait, RelationType,
|
OneOrManyActiveModel, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter, RelationBuilder,
|
||||||
Select, Update, UpdateOne,
|
RelationTrait, RelationType, Select, Update, UpdateOne,
|
||||||
};
|
};
|
||||||
use sea_query::{Iden, IntoValueTuple};
|
use sea_query::{Iden, IntoValueTuple};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
@ -217,4 +217,25 @@ pub trait EntityTrait: EntityName {
|
|||||||
{
|
{
|
||||||
Update::one(model)
|
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