diff --git a/src/query/update.rs b/src/query/update.rs index 2f805511..fec7757c 100644 --- a/src/query/update.rs +++ b/src/query/update.rs @@ -86,7 +86,7 @@ impl UpdateOne where A: ActiveModelTrait, { - pub(crate) fn prepare_filters(mut self) -> Self { + fn prepare_filters(mut self) -> Self { for key in ::PrimaryKey::iter() { let col = key.into_column(); let av = self.model.get(col); @@ -99,7 +99,7 @@ where self } - pub(crate) fn prepare_values(mut self) -> Self { + fn prepare_values(mut self) -> Self { for col in ::Column::iter() { if ::PrimaryKey::from_column(col).is_some() { continue; @@ -177,6 +177,19 @@ impl UpdateMany where E: EntityTrait, { + pub fn set(mut self, model: A) -> Self + where + A: ActiveModelTrait, + { + for col in E::Column::iter() { + let av = model.get(col); + if av.is_set() { + self.query.value(col, av.unwrap()); + } + } + self + } + pub fn col_expr(mut self, col: T, expr: SimpleExpr) -> Self where T: IntoIden, @@ -244,4 +257,35 @@ mod tests { r#"UPDATE "fruit" SET "cake_id" = NULL WHERE "fruit"."id" = 2"#, ); } + + #[test] + fn update_5() { + assert_eq!( + Update::many(fruit::Entity) + .set(fruit::ActiveModel { + name: ActiveValue::set("Apple".to_owned()), + cake_id: ActiveValue::set(Some(3)), + ..Default::default() + }) + .filter(fruit::Column::Id.eq(2)) + .build(DbBackend::Postgres) + .to_string(), + r#"UPDATE "fruit" SET "name" = 'Apple', "cake_id" = 3 WHERE "fruit"."id" = 2"#, + ); + } + + #[test] + fn update_6() { + assert_eq!( + Update::many(fruit::Entity) + .set(fruit::ActiveModel { + id: ActiveValue::set(3), + ..Default::default() + }) + .filter(fruit::Column::Id.eq(2)) + .build(DbBackend::Postgres) + .to_string(), + r#"UPDATE "fruit" SET "id" = 3 WHERE "fruit"."id" = 2"#, + ); + } }