Add ActiveValue::set_if_not_equals()
This commit is contained in:
parent
145bfa02a9
commit
b2b0a404e4
@ -849,6 +849,43 @@ where
|
||||
None => ActiveValue::NotSet,
|
||||
};
|
||||
}
|
||||
|
||||
/// `Set(value)`, except when [`self.is_unchanged()`][ActiveValue#method.is_unchanged]
|
||||
/// and `value` equals the current [Unchanged][ActiveValue::Unchanged] value.
|
||||
///
|
||||
/// This is useful when you have an [Unchanged][ActiveValue::Unchanged] value from the database,
|
||||
/// then update it using this method,
|
||||
/// and then use [`.is_unchanged()`][ActiveValue#method.is_unchanged] to see whether it has *actually* changed.
|
||||
///
|
||||
/// The same nice effect applies to the entire `ActiveModel`.
|
||||
/// You can now meaningfully use [ActiveModelTrait::is_changed][ActiveModelTrait#method.is_changed]
|
||||
/// to see whether are any changes that need to be saved to the database.
|
||||
///
|
||||
/// ## Examples
|
||||
///
|
||||
/// ```
|
||||
/// # use sea_orm::ActiveValue;
|
||||
/// #
|
||||
/// let mut value = ActiveValue::Unchanged("old");
|
||||
///
|
||||
/// // This wouldn't be the case if we used plain `value = Set("old");`
|
||||
/// value.set_if_not_equals("old");
|
||||
/// assert!(value.is_unchanged());
|
||||
///
|
||||
/// // Only when we change the actual `&str` value, it becomes `Set`
|
||||
/// value.set_if_not_equals("new");
|
||||
/// assert_eq!(value.is_unchanged(), false);
|
||||
/// assert_eq!(value, ActiveValue::Set("new"));
|
||||
/// ```
|
||||
pub fn set_if_not_equals(&mut self, value: V)
|
||||
where
|
||||
V: PartialEq,
|
||||
{
|
||||
match self {
|
||||
ActiveValue::Unchanged(current) if &value == current => {}
|
||||
_ => *self = ActiveValue::Set(value),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> std::convert::AsRef<V> for ActiveValue<V>
|
||||
|
Loading…
x
Reference in New Issue
Block a user