Skip to content

Commit

Permalink
Revert breaking changes
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Sep 9, 2021
1 parent 558223a commit ac9dc5a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
2 changes: 1 addition & 1 deletion src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ pub trait ActiveModelTrait: Clone + Debug {
}

async fn update(self, db: &DatabaseConnection) -> Result<Self, DbErr> {
let exec = Self::Entity::update(self).prepare_filters().exec(db);
let exec = Self::Entity::update(self).exec(db);
exec.await
}

Expand Down
33 changes: 15 additions & 18 deletions src/query/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl Update {
/// id: ActiveValue::set(1),
/// name: ActiveValue::set("Apple Pie".to_owned()),
/// })
/// .filter(cake::Column::Id.eq(1))
/// .build(DbBackend::Postgres)
/// .to_string(),
/// r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#,
Expand All @@ -48,12 +47,13 @@ impl Update {
E: EntityTrait,
A: ActiveModelTrait<Entity = E>,
{
let myself = UpdateOne {
let mut myself = UpdateOne {
query: UpdateStatement::new()
.table(A::Entity::default().table_ref())
.to_owned(),
model,
};
myself = myself.prepare_filters();
myself.prepare_values()
}

Expand Down Expand Up @@ -86,19 +86,6 @@ impl<A> UpdateOne<A>
where
A: ActiveModelTrait,
{
pub(crate) fn prepare_values(mut self) -> Self {
for col in <A::Entity as EntityTrait>::Column::iter() {
if <A::Entity as EntityTrait>::PrimaryKey::from_column(col).is_some() {
continue;
}
let av = self.model.get(col);
if av.is_set() {
self.query.value(col, av.unwrap());
}
}
self
}

pub(crate) fn prepare_filters(mut self) -> Self {
for key in <A::Entity as EntityTrait>::PrimaryKey::iter() {
let col = key.into_column();
Expand All @@ -111,6 +98,19 @@ where
}
self
}

pub(crate) fn prepare_values(mut self) -> Self {
for col in <A::Entity as EntityTrait>::Column::iter() {
if <A::Entity as EntityTrait>::PrimaryKey::from_column(col).is_some() {
continue;
}
let av = self.model.get(col);
if av.is_set() {
self.query.value(col, av.unwrap());
}
}
self
}
}

impl<A> QueryFilter for UpdateOne<A>
Expand Down Expand Up @@ -199,7 +199,6 @@ mod tests {
id: ActiveValue::set(1),
name: ActiveValue::set("Apple Pie".to_owned()),
})
.filter(cake::Column::Id.eq(1))
.build(DbBackend::Postgres)
.to_string(),
r#"UPDATE "cake" SET "name" = 'Apple Pie' WHERE "cake"."id" = 1"#,
Expand All @@ -214,7 +213,6 @@ mod tests {
name: ActiveValue::set("Orange".to_owned()),
cake_id: ActiveValue::unset(),
})
.filter(fruit::Column::Id.eq(1))
.build(DbBackend::Postgres)
.to_string(),
r#"UPDATE "fruit" SET "name" = 'Orange' WHERE "fruit"."id" = 1"#,
Expand All @@ -241,7 +239,6 @@ mod tests {
assert_eq!(
Update::many(fruit::Entity)
.col_expr(fruit::Column::CakeId, Expr::value(Value::Int(None)))
.filter(fruit::Column::Id.eq(2))
.build(DbBackend::Postgres)
.to_string(),
r#"UPDATE "fruit" SET "cake_id" = NULL WHERE "fruit"."id" = 2"#,
Expand Down

0 comments on commit ac9dc5a

Please sign in to comment.