sea_orm/entity/
active_value.rs1use crate::Value;
2use sea_query::Nullable;
3use std::fmt::Debug;
4
5pub use ActiveValue::{NotSet, Set, Unchanged};
6
7#[derive(Clone, Debug)]
55pub enum ActiveValue<V>
56where
57 V: Into<Value>,
58{
59 Set(V),
67 Unchanged(V),
76 NotSet,
86}
87
88#[deprecated(
90 since = "0.5.0",
91 note = "Please use [`ActiveValue::NotSet`] or [`NotSet`]"
92)]
93#[allow(non_snake_case)]
94pub fn Unset<V>(_: Option<bool>) -> ActiveValue<V>
95where
96 V: Into<Value>,
97{
98 ActiveValue::not_set()
99}
100
101pub trait IntoActiveValue<V>
103where
104 V: Into<Value>,
105{
106 fn into_active_value(self) -> ActiveValue<V>;
108}
109
110impl<V> IntoActiveValue<Option<V>> for Option<V>
111where
112 V: IntoActiveValue<V> + Into<Value> + Nullable,
113{
114 fn into_active_value(self) -> ActiveValue<Option<V>> {
115 match self {
116 Some(value) => Set(Some(value)),
117 None => NotSet,
118 }
119 }
120}
121
122impl<V> IntoActiveValue<Option<V>> for Option<Option<V>>
123where
124 V: IntoActiveValue<V> + Into<Value> + Nullable,
125{
126 fn into_active_value(self) -> ActiveValue<Option<V>> {
127 match self {
128 Some(value) => Set(value),
129 None => NotSet,
130 }
131 }
132}
133
134macro_rules! impl_into_active_value {
135 ($ty: ty) => {
136 impl IntoActiveValue<$ty> for $ty {
137 fn into_active_value(self) -> ActiveValue<$ty> {
138 Set(self)
139 }
140 }
141 };
142}
143
144impl_into_active_value!(bool);
145impl_into_active_value!(i8);
146impl_into_active_value!(i16);
147impl_into_active_value!(i32);
148impl_into_active_value!(i64);
149impl_into_active_value!(u8);
150impl_into_active_value!(u16);
151impl_into_active_value!(u32);
152impl_into_active_value!(u64);
153impl_into_active_value!(f32);
154impl_into_active_value!(f64);
155impl_into_active_value!(&'static str);
156impl_into_active_value!(String);
157impl_into_active_value!(Vec<u8>);
158
159#[cfg(feature = "with-json")]
160#[cfg_attr(docsrs, doc(cfg(feature = "with-json")))]
161impl_into_active_value!(crate::prelude::Json);
162
163#[cfg(feature = "with-chrono")]
164#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
165impl_into_active_value!(crate::prelude::Date);
166
167#[cfg(feature = "with-chrono")]
168#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
169impl_into_active_value!(crate::prelude::Time);
170
171#[cfg(feature = "with-chrono")]
172#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
173impl_into_active_value!(crate::prelude::DateTime);
174
175#[cfg(feature = "with-chrono")]
176#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
177impl_into_active_value!(crate::prelude::DateTimeWithTimeZone);
178
179#[cfg(feature = "with-chrono")]
180#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
181impl_into_active_value!(crate::prelude::DateTimeUtc);
182
183#[cfg(feature = "with-chrono")]
184#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
185impl_into_active_value!(crate::prelude::DateTimeLocal);
186
187#[cfg(feature = "with-rust_decimal")]
188#[cfg_attr(docsrs, doc(cfg(feature = "with-rust_decimal")))]
189impl_into_active_value!(crate::prelude::Decimal);
190
191#[cfg(feature = "with-bigdecimal")]
192#[cfg_attr(docsrs, doc(cfg(feature = "with-bigdecimal")))]
193impl_into_active_value!(crate::prelude::BigDecimal);
194
195#[cfg(feature = "with-uuid")]
196#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))]
197impl_into_active_value!(crate::prelude::Uuid);
198
199#[cfg(feature = "with-time")]
200#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
201impl_into_active_value!(crate::prelude::TimeDate);
202
203#[cfg(feature = "with-time")]
204#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
205impl_into_active_value!(crate::prelude::TimeTime);
206
207#[cfg(feature = "with-time")]
208#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
209impl_into_active_value!(crate::prelude::TimeDateTime);
210
211#[cfg(feature = "with-time")]
212#[cfg_attr(docsrs, doc(cfg(feature = "with-time")))]
213impl_into_active_value!(crate::prelude::TimeDateTimeWithTimeZone);
214
215#[cfg(feature = "with-ipnetwork")]
216#[cfg_attr(docsrs, doc(cfg(feature = "with-ipnetwork")))]
217impl_into_active_value!(crate::prelude::IpNetwork);
218
219impl<V> Default for ActiveValue<V>
220where
221 V: Into<Value>,
222{
223 fn default() -> Self {
225 Self::NotSet
226 }
227}
228
229impl<V> ActiveValue<V>
230where
231 V: Into<Value>,
232{
233 pub fn set(value: V) -> Self {
235 Self::Set(value)
236 }
237
238 pub fn is_set(&self) -> bool {
240 matches!(self, Self::Set(_))
241 }
242
243 pub fn unchanged(value: V) -> Self {
245 Self::Unchanged(value)
246 }
247
248 pub fn is_unchanged(&self) -> bool {
250 matches!(self, Self::Unchanged(_))
251 }
252
253 pub fn not_set() -> Self {
255 Self::default()
256 }
257
258 pub fn is_not_set(&self) -> bool {
260 matches!(self, Self::NotSet)
261 }
262
263 pub fn take(&mut self) -> Option<V> {
265 match std::mem::take(self) {
266 ActiveValue::Set(value) | ActiveValue::Unchanged(value) => Some(value),
267 ActiveValue::NotSet => None,
268 }
269 }
270
271 pub fn unwrap(self) -> V {
277 match self {
278 ActiveValue::Set(value) | ActiveValue::Unchanged(value) => value,
279 ActiveValue::NotSet => panic!("Cannot unwrap ActiveValue::NotSet"),
280 }
281 }
282
283 pub fn into_value(self) -> Option<Value> {
285 match self {
286 ActiveValue::Set(value) | ActiveValue::Unchanged(value) => Some(value.into()),
287 ActiveValue::NotSet => None,
288 }
289 }
290
291 pub fn into_wrapped_value(self) -> ActiveValue<Value> {
293 match self {
294 Self::Set(value) => ActiveValue::set(value.into()),
295 Self::Unchanged(value) => ActiveValue::unchanged(value.into()),
296 Self::NotSet => ActiveValue::not_set(),
297 }
298 }
299
300 pub fn reset(&mut self) {
303 *self = match self.take() {
304 Some(value) => ActiveValue::Set(value),
305 None => ActiveValue::NotSet,
306 };
307 }
308
309 pub fn set_if_not_equals(&mut self, value: V)
337 where
338 V: PartialEq,
339 {
340 match self {
341 ActiveValue::Unchanged(current) if &value == current => {}
342 _ => *self = ActiveValue::Set(value),
343 }
344 }
345
346 pub fn try_as_ref(&self) -> Option<&V> {
360 match self {
361 ActiveValue::Set(value) | ActiveValue::Unchanged(value) => Some(value),
362 ActiveValue::NotSet => None,
363 }
364 }
365}
366
367impl<V> std::convert::AsRef<V> for ActiveValue<V>
368where
369 V: Into<Value>,
370{
371 fn as_ref(&self) -> &V {
377 match self {
378 ActiveValue::Set(value) | ActiveValue::Unchanged(value) => value,
379 ActiveValue::NotSet => panic!("Cannot borrow ActiveValue::NotSet"),
380 }
381 }
382}
383
384impl<V> PartialEq for ActiveValue<V>
385where
386 V: Into<Value> + std::cmp::PartialEq,
387{
388 fn eq(&self, other: &Self) -> bool {
389 match (self, other) {
390 (ActiveValue::Set(l), ActiveValue::Set(r)) => l == r,
391 (ActiveValue::Unchanged(l), ActiveValue::Unchanged(r)) => l == r,
392 (ActiveValue::NotSet, ActiveValue::NotSet) => true,
393 _ => false,
394 }
395 }
396}
397
398impl<V> From<ActiveValue<V>> for ActiveValue<Option<V>>
399where
400 V: Into<Value> + Nullable,
401{
402 fn from(value: ActiveValue<V>) -> Self {
403 match value {
404 ActiveValue::Set(value) => ActiveValue::set(Some(value)),
405 ActiveValue::Unchanged(value) => ActiveValue::unchanged(Some(value)),
406 ActiveValue::NotSet => ActiveValue::not_set(),
407 }
408 }
409}