Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion sea-orm-sync/src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{ActiveValue, ActiveValue::*};
use crate::{
ColumnTrait, Condition, ConnectionTrait, DbBackend, DeleteResult, EntityName, EntityTrait,
IdenStatic, Iterable, PrimaryKeyArity, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter,
Related, RelatedSelfVia, RelationDef, RelationTrait, Value,
Related, RelatedSelfVia, RelationDef, RelationTrait, UpdateResult, Value,
error::*,
query::{
clear_key_on_active_model, column_tuple_in_condition, get_key_from_active_model,
Expand Down Expand Up @@ -338,6 +338,17 @@ pub trait ActiveModelTrait: Clone + Debug {
Self::after_save(model, db, false)
}

/// Similar to [`update`], but without returning
/// It also won't execute [`ActiveModelTrait::after_save`]
fn update_without_returning<'a, C>(self, db: &'a C) -> Result<UpdateResult, DbErr>
where
Self: ActiveModelBehavior,
C: ConnectionTrait,
{
let am = ActiveModelBehavior::before_save(self, db, false)?;
Self::Entity::update_without_returning(am).exec_without_returning(db)
}

/// Insert the model if primary key is `NotSet`, update otherwise.
/// Only works if the entity has auto increment primary key.
fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>
Expand Down
24 changes: 24 additions & 0 deletions sea-orm-sync/src/executor/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ impl<A> ValidatedUpdateOne<A>
where
A: ActiveModelTrait,
{
/// Execute an UPDATE operation on an ActiveModel without returning the updated model
pub fn exec_without_returning<C>(self, db: &C) -> Result<UpdateResult, DbErr>
where
C: ConnectionTrait,
{
Updater::new(self.query)
// If nothing is updated, return RecordNotUpdated error
.check_record_exists()
.exec(db)
}

/// Execute an UPDATE operation on an ActiveModel
pub fn exec<C>(self, db: &C) -> Result<<A::Entity as EntityTrait>::Model, DbErr>
where
Expand All @@ -37,6 +48,14 @@ impl<A> UpdateOne<A>
where
A: ActiveModelTrait,
{
/// Execute an UPDATE operation on an ActiveModel without returning the updated model
pub fn exec_without_returning<C>(self, db: &C) -> Result<UpdateResult, DbErr>
where
C: ConnectionTrait,
{
self.0?.exec_without_returning(db)
}

/// Execute an UPDATE operation on an ActiveModel
pub fn exec<C>(self, db: &C) -> Result<<A::Entity as EntityTrait>::Model, DbErr>
where
Expand Down Expand Up @@ -77,6 +96,11 @@ impl Updater {
}
}

fn check_record_exists(mut self) -> Self {
self.check_record_exists = true;
self
}

/// Execute an update operation
pub fn exec<C>(self, db: &C) -> Result<UpdateResult, DbErr>
where
Expand Down
15 changes: 14 additions & 1 deletion src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{ActiveValue, ActiveValue::*};
use crate::{
ColumnTrait, Condition, ConnectionTrait, DbBackend, DeleteResult, EntityName, EntityTrait,
IdenStatic, Iterable, PrimaryKeyArity, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter,
Related, RelatedSelfVia, RelationDef, RelationTrait, Value,
Related, RelatedSelfVia, RelationDef, RelationTrait, UpdateResult, Value,
error::*,
query::{
clear_key_on_active_model, column_tuple_in_condition, get_key_from_active_model,
Expand Down Expand Up @@ -345,6 +345,19 @@ pub trait ActiveModelTrait: Clone + Debug {
Self::after_save(model, db, false).await
}

/// Similar to [`update`], but without returning
/// It also won't execute [`ActiveModelTrait::after_save`]
async fn update_without_returning<'a, C>(self, db: &'a C) -> Result<UpdateResult, DbErr>
where
Self: ActiveModelBehavior,
C: ConnectionTrait,
{
let am = ActiveModelBehavior::before_save(self, db, false).await?;
Self::Entity::update_without_returning(am)
.exec_without_returning(db)
.await
}

/// Insert the model if primary key is `NotSet`, update otherwise.
/// Only works if the entity has auto increment primary key.
async fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>
Expand Down
25 changes: 25 additions & 0 deletions src/executor/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,18 @@ impl<A> ValidatedUpdateOne<A>
where
A: ActiveModelTrait,
{
/// Execute an UPDATE operation on an ActiveModel without returning the updated model
pub async fn exec_without_returning<C>(self, db: &C) -> Result<UpdateResult, DbErr>
where
C: ConnectionTrait,
{
Updater::new(self.query)
// If nothing is updated, return RecordNotUpdated error
.check_record_exists()
.exec(db)
.await
}

/// Execute an UPDATE operation on an ActiveModel
pub async fn exec<C>(self, db: &C) -> Result<<A::Entity as EntityTrait>::Model, DbErr>
where
Expand All @@ -39,6 +51,14 @@ impl<A> UpdateOne<A>
where
A: ActiveModelTrait,
{
/// Execute an UPDATE operation on an ActiveModel without returning the updated model
pub async fn exec_without_returning<C>(self, db: &C) -> Result<UpdateResult, DbErr>
where
C: ConnectionTrait,
{
self.0?.exec_without_returning(db).await
}

/// Execute an UPDATE operation on an ActiveModel
pub async fn exec<C>(self, db: &C) -> Result<<A::Entity as EntityTrait>::Model, DbErr>
where
Expand Down Expand Up @@ -81,6 +101,11 @@ impl Updater {
}
}

fn check_record_exists(mut self) -> Self {
self.check_record_exists = true;
self
}

/// Execute an update operation
pub async fn exec<C>(self, db: &C) -> Result<UpdateResult, DbErr>
where
Expand Down
Loading