Skip to content

Commit aa9d87d

Browse files
committed
Update without returning
1 parent 9f4c4e2 commit aa9d87d

4 files changed

Lines changed: 75 additions & 2 deletions

File tree

sea-orm-sync/src/entity/active_model.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{ActiveValue, ActiveValue::*};
22
use crate::{
33
ColumnTrait, Condition, ConnectionTrait, DbBackend, DeleteResult, EntityName, EntityTrait,
44
IdenStatic, Iterable, PrimaryKeyArity, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter,
5-
Related, RelatedSelfVia, RelationDef, RelationTrait, Value,
5+
Related, RelatedSelfVia, RelationDef, RelationTrait, UpdateResult, Value,
66
error::*,
77
query::{
88
clear_key_on_active_model, column_tuple_in_condition, get_key_from_active_model,
@@ -338,6 +338,17 @@ pub trait ActiveModelTrait: Clone + Debug {
338338
Self::after_save(model, db, false)
339339
}
340340

341+
/// Similar to [`update`], but without returning
342+
/// It also won't execute [`ActiveModelTrait::after_save`]
343+
fn update_without_returning<'a, C>(self, db: &'a C) -> Result<UpdateResult, DbErr>
344+
where
345+
Self: ActiveModelBehavior,
346+
C: ConnectionTrait,
347+
{
348+
let am = ActiveModelBehavior::before_save(self, db, false)?;
349+
Self::Entity::update_without_returning(am).exec_without_returning(db)
350+
}
351+
341352
/// Insert the model if primary key is `NotSet`, update otherwise.
342353
/// Only works if the entity has auto increment primary key.
343354
fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>

sea-orm-sync/src/executor/update.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ impl<A> ValidatedUpdateOne<A>
2323
where
2424
A: ActiveModelTrait,
2525
{
26+
/// Execute an UPDATE operation on an ActiveModel without returning the updated model
27+
pub fn exec_without_returning<C>(self, db: &C) -> Result<UpdateResult, DbErr>
28+
where
29+
C: ConnectionTrait,
30+
{
31+
Updater::new(self.query)
32+
// If nothing is updated, return RecordNotUpdated error
33+
.check_record_exists()
34+
.exec(db)
35+
}
36+
2637
/// Execute an UPDATE operation on an ActiveModel
2738
pub fn exec<C>(self, db: &C) -> Result<<A::Entity as EntityTrait>::Model, DbErr>
2839
where
@@ -37,6 +48,14 @@ impl<A> UpdateOne<A>
3748
where
3849
A: ActiveModelTrait,
3950
{
51+
/// Execute an UPDATE operation on an ActiveModel without returning the updated model
52+
pub fn exec_without_returning<C>(self, db: &C) -> Result<UpdateResult, DbErr>
53+
where
54+
C: ConnectionTrait,
55+
{
56+
self.0?.exec_without_returning(db)
57+
}
58+
4059
/// Execute an UPDATE operation on an ActiveModel
4160
pub fn exec<C>(self, db: &C) -> Result<<A::Entity as EntityTrait>::Model, DbErr>
4261
where
@@ -77,6 +96,11 @@ impl Updater {
7796
}
7897
}
7998

99+
fn check_record_exists(mut self) -> Self {
100+
self.check_record_exists = true;
101+
self
102+
}
103+
80104
/// Execute an update operation
81105
pub fn exec<C>(self, db: &C) -> Result<UpdateResult, DbErr>
82106
where

src/entity/active_model.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{ActiveValue, ActiveValue::*};
22
use crate::{
33
ColumnTrait, Condition, ConnectionTrait, DbBackend, DeleteResult, EntityName, EntityTrait,
44
IdenStatic, Iterable, PrimaryKeyArity, PrimaryKeyToColumn, PrimaryKeyTrait, QueryFilter,
5-
Related, RelatedSelfVia, RelationDef, RelationTrait, Value,
5+
Related, RelatedSelfVia, RelationDef, RelationTrait, UpdateResult, Value,
66
error::*,
77
query::{
88
clear_key_on_active_model, column_tuple_in_condition, get_key_from_active_model,
@@ -345,6 +345,19 @@ pub trait ActiveModelTrait: Clone + Debug {
345345
Self::after_save(model, db, false).await
346346
}
347347

348+
/// Similar to [`update`], but without returning
349+
/// It also won't execute [`ActiveModelTrait::after_save`]
350+
async fn update_without_returning<'a, C>(self, db: &'a C) -> Result<UpdateResult, DbErr>
351+
where
352+
Self: ActiveModelBehavior,
353+
C: ConnectionTrait,
354+
{
355+
let am = ActiveModelBehavior::before_save(self, db, false).await?;
356+
Self::Entity::update_without_returning(am)
357+
.exec_without_returning(db)
358+
.await
359+
}
360+
348361
/// Insert the model if primary key is `NotSet`, update otherwise.
349362
/// Only works if the entity has auto increment primary key.
350363
async fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>

src/executor/update.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ impl<A> ValidatedUpdateOne<A>
2323
where
2424
A: ActiveModelTrait,
2525
{
26+
/// Execute an UPDATE operation on an ActiveModel without returning the updated model
27+
pub async fn exec_without_returning<C>(self, db: &C) -> Result<UpdateResult, DbErr>
28+
where
29+
C: ConnectionTrait,
30+
{
31+
Updater::new(self.query)
32+
// If nothing is updated, return RecordNotUpdated error
33+
.check_record_exists()
34+
.exec(db)
35+
.await
36+
}
37+
2638
/// Execute an UPDATE operation on an ActiveModel
2739
pub async fn exec<C>(self, db: &C) -> Result<<A::Entity as EntityTrait>::Model, DbErr>
2840
where
@@ -39,6 +51,14 @@ impl<A> UpdateOne<A>
3951
where
4052
A: ActiveModelTrait,
4153
{
54+
/// Execute an UPDATE operation on an ActiveModel without returning the updated model
55+
pub async fn exec_without_returning<C>(self, db: &C) -> Result<UpdateResult, DbErr>
56+
where
57+
C: ConnectionTrait,
58+
{
59+
self.0?.exec_without_returning(db).await
60+
}
61+
4262
/// Execute an UPDATE operation on an ActiveModel
4363
pub async fn exec<C>(self, db: &C) -> Result<<A::Entity as EntityTrait>::Model, DbErr>
4464
where
@@ -81,6 +101,11 @@ impl Updater {
81101
}
82102
}
83103

104+
fn check_record_exists(mut self) -> Self {
105+
self.check_record_exists = true;
106+
self
107+
}
108+
84109
/// Execute an update operation
85110
pub async fn exec<C>(self, db: &C) -> Result<UpdateResult, DbErr>
86111
where

0 commit comments

Comments
 (0)