sea_orm::entity

Enum ActiveValue

Source
pub enum ActiveValue<V>
where V: Into<Value>,
{ Set(V), Unchanged(V), NotSet, }
Expand description

Defines a stateful value used in ActiveModel.

There are three possible state represented by three enum variants.

The stateful value is useful when constructing UPDATE SQL statement, see an example below.

ยงExamples

use sea_orm::tests_cfg::{cake, fruit};
use sea_orm::{entity::*, query::*, DbBackend};

// The code snipped below does an UPDATE operation on a `ActiveValue`
assert_eq!(
    Update::one(fruit::ActiveModel {
        id: ActiveValue::set(1),
        name: ActiveValue::set("Orange".to_owned()),
        cake_id: ActiveValue::not_set(),
    })
    .build(DbBackend::Postgres)
    .to_string(),
    r#"UPDATE "fruit" SET "name" = 'Orange' WHERE "fruit"."id" = 1"#
);

Variantsยง

ยง

Set(V)

A defined Value actively being set

ยง

Unchanged(V)

A defined Value remain unchanged

ยง

NotSet

An undefined Value

Implementationsยง

Sourceยง

impl<V> ActiveValue<V>
where V: Into<Value>,

Source

pub fn set(value: V) -> Self

Create an ActiveValue::Set

Source

pub fn is_set(&self) -> bool

Check if the ActiveValue is ActiveValue::Set

Source

pub fn unchanged(value: V) -> Self

Source

pub fn is_unchanged(&self) -> bool

Source

pub fn not_set() -> Self

Source

pub fn is_not_set(&self) -> bool

Source

pub fn take(&mut self) -> Option<V>

Get the mutable value an ActiveValue also setting itself to ActiveValue::NotSet

Source

pub fn unwrap(self) -> V

Get an owned value of the ActiveValue

ยงPanics

Panics if it is ActiveValue::NotSet

Source

pub fn into_value(self) -> Option<Value>

Check if a Value exists or not

Source

pub fn into_wrapped_value(self) -> ActiveValue<Value>

Wrap the Value into a ActiveValue<Value>

Source

pub fn reset(&mut self)

Reset the value from ActiveValue::Unchanged to ActiveValue::Set, leaving ActiveValue::NotSet untouched.

Source

pub fn set_if_not_equals(&mut self, value: V)
where V: PartialEq,

Set(value), except when self.is_unchanged() and value equals the current Unchanged value.

This is useful when you have an Unchanged value from the database, then update it using this method, and then use .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 to see whether are any changes that need to be saved to the database.

ยงExamples
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"));
Source

pub fn try_as_ref(&self) -> Option<&V>

Get the inner value, unless self is NotSet.

Thereโ€™s also a panicking version: ActiveValue::as_ref.

ยงExamples
assert_eq!(ActiveValue::Unchanged(42).try_as_ref(), Some(&42));
assert_eq!(ActiveValue::Set(42).try_as_ref(), Some(&42));
assert_eq!(ActiveValue::NotSet.try_as_ref(), None::<&i32>);

Trait Implementationsยง

Sourceยง

impl<V> AsRef<V> for ActiveValue<V>
where V: Into<Value>,

Sourceยง

fn as_ref(&self) -> &V

ยงPanics

Panics if it is ActiveValue::NotSet.

See ActiveValue::try_as_ref for a fallible non-panicking version.

Sourceยง

impl<V> Clone for ActiveValue<V>
where V: Into<Value> + Clone,

Sourceยง

fn clone(&self) -> ActiveValue<V>

Returns a copy of the value. Read more
1.0.0 ยท Sourceยง

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Sourceยง

impl<V> Debug for ActiveValue<V>
where V: Into<Value> + Debug,

Sourceยง

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Sourceยง

impl<V> Default for ActiveValue<V>
where V: Into<Value>,

Sourceยง

impl<V> From<ActiveValue<V>> for ActiveValue<Option<V>>
where V: Into<Value> + Nullable,

Sourceยง

fn from(value: ActiveValue<V>) -> Self

Converts to this type from the input type.
Sourceยง

impl<V> PartialEq for ActiveValue<V>
where V: Into<Value> + PartialEq,

Sourceยง

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementationsยง

ยง

impl<V> Freeze for ActiveValue<V>
where V: Freeze,

ยง

impl<V> RefUnwindSafe for ActiveValue<V>
where V: RefUnwindSafe,

ยง

impl<V> Send for ActiveValue<V>
where V: Send,

ยง

impl<V> Sync for ActiveValue<V>
where V: Sync,

ยง

impl<V> Unpin for ActiveValue<V>
where V: Unpin,

ยง

impl<V> UnwindSafe for ActiveValue<V>
where V: UnwindSafe,

Blanket Implementationsยง

Sourceยง

impl<T> Any for T
where T: 'static + ?Sized,

Sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Sourceยง

impl<T> Borrow<T> for T
where T: ?Sized,

Sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Sourceยง

impl<T> BorrowMut<T> for T
where T: ?Sized,

Sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Sourceยง

impl<T> CloneToUninit for T
where T: Clone,

Sourceยง

unsafe fn clone_to_uninit(&self, dst: *mut T)

๐Ÿ”ฌThis is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Sourceยง

impl<T> From<T> for T

Sourceยง

fn from(t: T) -> T

Returns the argument unchanged.

Sourceยง

impl<T> Instrument for T

Sourceยง

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Sourceยง

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Sourceยง

impl<T, U> Into<U> for T
where U: From<T>,

Sourceยง

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Sourceยง

impl<T> IntoEither for T

Sourceยง

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Sourceยง

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Sourceยง

impl<T> Same for T

Sourceยง

type Output = T

Should always be Self
Sourceยง

impl<T> ToOwned for T
where T: Clone,

Sourceยง

type Owned = T

The resulting type after obtaining ownership.
Sourceยง

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Sourceยง

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Sourceยง

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Sourceยง

type Error = Infallible

The type returned in the event of a conversion error.
Sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Sourceยง

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Sourceยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Sourceยง

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Sourceยง

fn vzip(self) -> V

Sourceยง

impl<T> WithSubscriber for T

Sourceยง

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Sourceยง

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more