use crate::{
error::*, ConnectionTrait, DeleteResult, EntityTrait, Iterable, PrimaryKeyToColumn, Value,
};
use async_trait::async_trait;
use sea_query::{Nullable, ValueTuple};
use std::fmt::Debug;
pub use ActiveValue::NotSet;
#[derive(Clone, Debug)]
pub enum ActiveValue<V>
where
V: Into<Value>,
{
Set(V),
Unchanged(V),
NotSet,
}
#[allow(non_snake_case)]
pub fn Set<V>(v: V) -> ActiveValue<V>
where
V: Into<Value>,
{
ActiveValue::set(v)
}
#[deprecated(
since = "0.5.0",
note = "Please use [`ActiveValue::NotSet`] or [`NotSet`]"
)]
#[allow(non_snake_case)]
pub fn Unset<V>(_: Option<bool>) -> ActiveValue<V>
where
V: Into<Value>,
{
ActiveValue::not_set()
}
#[allow(non_snake_case)]
pub fn Unchanged<V>(value: V) -> ActiveValue<V>
where
V: Into<Value>,
{
ActiveValue::unchanged(value)
}
#[async_trait]
pub trait ActiveModelTrait: Clone + Debug {
type Entity: EntityTrait;
fn take(&mut self, c: <Self::Entity as EntityTrait>::Column) -> ActiveValue<Value>;
fn get(&self, c: <Self::Entity as EntityTrait>::Column) -> ActiveValue<Value>;
fn set(&mut self, c: <Self::Entity as EntityTrait>::Column, v: Value);
fn not_set(&mut self, c: <Self::Entity as EntityTrait>::Column);
fn is_not_set(&self, c: <Self::Entity as EntityTrait>::Column) -> bool;
fn default() -> Self;
#[allow(clippy::question_mark)]
fn get_primary_key_value(&self) -> Option<ValueTuple> {
let mut cols = <Self::Entity as EntityTrait>::PrimaryKey::iter();
macro_rules! next {
() => {
if let Some(col) = cols.next() {
if let Some(val) = self.get(col.into_column()).into_value() {
val
} else {
return None;
}
} else {
return None;
}
};
}
match <Self::Entity as EntityTrait>::PrimaryKey::iter().count() {
1 => {
let s1 = next!();
Some(ValueTuple::One(s1))
}
2 => {
let s1 = next!();
let s2 = next!();
Some(ValueTuple::Two(s1, s2))
}
3 => {
let s1 = next!();
let s2 = next!();
let s3 = next!();
Some(ValueTuple::Three(s1, s2, s3))
}
_ => panic!("The arity cannot be larger than 3"),
}
}
async fn insert<'a, C>(self, db: &'a C) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>
where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait<'a>,
{
let am = ActiveModelBehavior::before_save(self, true)?;
let model = <Self::Entity as EntityTrait>::insert(am)
.exec_with_returning(db)
.await?;
Self::after_save(model, true)
}
async fn update<'a, C>(self, db: &'a C) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>
where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait<'a>,
{
let am = ActiveModelBehavior::before_save(self, false)?;
let model: <Self::Entity as EntityTrait>::Model = Self::Entity::update(am).exec(db).await?;
Self::after_save(model, false)
}
async fn save<'a, C>(self, db: &'a C) -> Result<Self, DbErr>
where
<Self::Entity as EntityTrait>::Model: IntoActiveModel<Self>,
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait<'a>,
{
let mut is_update = true;
for key in <Self::Entity as EntityTrait>::PrimaryKey::iter() {
let col = key.into_column();
if self.is_not_set(col) {
is_update = false;
break;
}
}
let res = if !is_update {
self.insert(db).await
} else {
self.update(db).await
}?;
Ok(res.into_active_model())
}
async fn delete<'a, C>(self, db: &'a C) -> Result<DeleteResult, DbErr>
where
Self: ActiveModelBehavior + 'a,
C: ConnectionTrait<'a>,
{
let am = ActiveModelBehavior::before_delete(self)?;
let am_clone = am.clone();
let delete_res = Self::Entity::delete(am).exec(db).await?;
ActiveModelBehavior::after_delete(am_clone)?;
Ok(delete_res)
}
}
#[allow(unused_variables)]
pub trait ActiveModelBehavior: ActiveModelTrait {
fn new() -> Self {
<Self as ActiveModelTrait>::default()
}
fn before_save(self, insert: bool) -> Result<Self, DbErr> {
Ok(self)
}
fn after_save(
model: <Self::Entity as EntityTrait>::Model,
insert: bool,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr> {
Ok(model)
}
fn before_delete(self) -> Result<Self, DbErr> {
Ok(self)
}
fn after_delete(self) -> Result<Self, DbErr> {
Ok(self)
}
}
pub trait IntoActiveModel<A>
where
A: ActiveModelTrait,
{
fn into_active_model(self) -> A;
}
impl<A> IntoActiveModel<A> for A
where
A: ActiveModelTrait,
{
fn into_active_model(self) -> A {
self
}
}
pub trait IntoActiveValue<V>
where
V: Into<Value>,
{
fn into_active_value(self) -> ActiveValue<V>;
}
macro_rules! impl_into_active_value {
($ty: ty, $fn: ident) => {
impl IntoActiveValue<$ty> for $ty {
fn into_active_value(self) -> ActiveValue<$ty> {
$fn(self)
}
}
impl IntoActiveValue<Option<$ty>> for Option<$ty> {
fn into_active_value(self) -> ActiveValue<Option<$ty>> {
match self {
Some(value) => Set(Some(value)),
None => NotSet,
}
}
}
impl IntoActiveValue<Option<$ty>> for Option<Option<$ty>> {
fn into_active_value(self) -> ActiveValue<Option<$ty>> {
match self {
Some(value) => Set(value),
None => NotSet,
}
}
}
};
}
impl_into_active_value!(bool, Set);
impl_into_active_value!(i8, Set);
impl_into_active_value!(i16, Set);
impl_into_active_value!(i32, Set);
impl_into_active_value!(i64, Set);
impl_into_active_value!(u8, Set);
impl_into_active_value!(u16, Set);
impl_into_active_value!(u32, Set);
impl_into_active_value!(u64, Set);
impl_into_active_value!(f32, Set);
impl_into_active_value!(f64, Set);
impl_into_active_value!(&'static str, Set);
impl_into_active_value!(String, Set);
#[cfg(feature = "with-json")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-json")))]
impl_into_active_value!(crate::prelude::Json, Set);
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
impl_into_active_value!(crate::prelude::Date, Set);
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
impl_into_active_value!(crate::prelude::Time, Set);
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
impl_into_active_value!(crate::prelude::DateTime, Set);
#[cfg(feature = "with-chrono")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-chrono")))]
impl_into_active_value!(crate::prelude::DateTimeWithTimeZone, Set);
#[cfg(feature = "with-rust_decimal")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-rust_decimal")))]
impl_into_active_value!(crate::prelude::Decimal, Set);
#[cfg(feature = "with-uuid")]
#[cfg_attr(docsrs, doc(cfg(feature = "with-uuid")))]
impl_into_active_value!(crate::prelude::Uuid, Set);
impl<V> Default for ActiveValue<V>
where
V: Into<Value>,
{
fn default() -> Self {
Self::NotSet
}
}
impl<V> ActiveValue<V>
where
V: Into<Value>,
{
pub fn set(value: V) -> Self {
Self::Set(value)
}
pub fn is_set(&self) -> bool {
matches!(self, Self::Set(_))
}
pub fn unchanged(value: V) -> Self {
Self::Unchanged(value)
}
pub fn is_unchanged(&self) -> bool {
matches!(self, Self::Unchanged(_))
}
pub fn not_set() -> Self {
Self::default()
}
pub fn is_not_set(&self) -> bool {
matches!(self, Self::NotSet)
}
pub fn take(&mut self) -> Option<V> {
match std::mem::take(self) {
ActiveValue::Set(value) | ActiveValue::Unchanged(value) => Some(value),
ActiveValue::NotSet => None,
}
}
pub fn unwrap(self) -> V {
match self {
ActiveValue::Set(value) | ActiveValue::Unchanged(value) => value,
ActiveValue::NotSet => panic!("Cannot unwrap ActiveValue::NotSet"),
}
}
pub fn into_value(self) -> Option<Value> {
match self {
ActiveValue::Set(value) | ActiveValue::Unchanged(value) => Some(value.into()),
ActiveValue::NotSet => None,
}
}
pub fn into_wrapped_value(self) -> ActiveValue<Value> {
match self {
Self::Set(value) => ActiveValue::set(value.into()),
Self::Unchanged(value) => ActiveValue::unchanged(value.into()),
Self::NotSet => ActiveValue::not_set(),
}
}
}
impl<V> std::convert::AsRef<V> for ActiveValue<V>
where
V: Into<Value>,
{
fn as_ref(&self) -> &V {
match self {
ActiveValue::Set(value) | ActiveValue::Unchanged(value) => value,
ActiveValue::NotSet => panic!("Cannot borrow ActiveValue::NotSet"),
}
}
}
impl<V> PartialEq for ActiveValue<V>
where
V: Into<Value> + std::cmp::PartialEq,
{
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(ActiveValue::Set(l), ActiveValue::Set(r)) => l == r,
(ActiveValue::Unchanged(l), ActiveValue::Unchanged(r)) => l == r,
(ActiveValue::NotSet, ActiveValue::NotSet) => true,
_ => false,
}
}
}
impl<V> From<ActiveValue<V>> for ActiveValue<Option<V>>
where
V: Into<Value> + Nullable,
{
fn from(value: ActiveValue<V>) -> Self {
match value {
ActiveValue::Set(value) => ActiveValue::set(Some(value)),
ActiveValue::Unchanged(value) => ActiveValue::unchanged(Some(value)),
ActiveValue::NotSet => ActiveValue::not_set(),
}
}
}
#[cfg(test)]
mod tests {
use crate::tests_cfg::*;
#[test]
#[cfg(feature = "macros")]
fn test_derive_into_active_model_1() {
use crate::entity::*;
mod my_fruit {
pub use super::fruit::*;
use crate as sea_orm;
use crate::entity::prelude::*;
#[derive(DeriveIntoActiveModel)]
pub struct NewFruit {
pub name: String,
pub cake_id: i32,
}
}
assert_eq!(
my_fruit::NewFruit {
name: "Apple".to_owned(),
cake_id: 1,
}
.into_active_model(),
fruit::ActiveModel {
id: NotSet,
name: Set("Apple".to_owned()),
cake_id: Set(Some(1)),
}
);
}
#[test]
#[cfg(feature = "macros")]
fn test_derive_into_active_model_2() {
use crate::entity::*;
mod my_fruit {
pub use super::fruit::*;
use crate as sea_orm;
use crate::entity::prelude::*;
#[derive(DeriveIntoActiveModel)]
pub struct UpdateFruit {
pub cake_id: Option<Option<i32>>,
}
}
assert_eq!(
my_fruit::UpdateFruit {
cake_id: Some(Some(1)),
}
.into_active_model(),
fruit::ActiveModel {
id: NotSet,
name: NotSet,
cake_id: Set(Some(1)),
}
);
assert_eq!(
my_fruit::UpdateFruit {
cake_id: Some(None),
}
.into_active_model(),
fruit::ActiveModel {
id: NotSet,
name: NotSet,
cake_id: Set(None),
}
);
assert_eq!(
my_fruit::UpdateFruit { cake_id: None }.into_active_model(),
fruit::ActiveModel {
id: NotSet,
name: NotSet,
cake_id: NotSet,
}
);
}
}