Make Val looks like enum variants
This commit is contained in:
parent
b818138724
commit
b148c3906d
@ -17,7 +17,7 @@ pub async fn all_about_operation(db: &Database) -> Result<(), ExecErr> {
|
||||
|
||||
pub async fn insert_and_update(db: &Database) -> Result<(), ExecErr> {
|
||||
let pear = fruit::ActiveModel {
|
||||
name: Val::set("pear".to_owned()),
|
||||
name: Set("pear".to_owned()),
|
||||
..Default::default()
|
||||
};
|
||||
let res = Fruit::insert(pear).exec(db).await?;
|
||||
@ -34,7 +34,7 @@ pub async fn insert_and_update(db: &Database) -> Result<(), ExecErr> {
|
||||
println!("Pear: {:?}\n", pear);
|
||||
|
||||
let mut pear: fruit::ActiveModel = pear.unwrap().into();
|
||||
pear.name = Val::set("Sweet pear".to_owned());
|
||||
pear.name = Set("Sweet pear".to_owned());
|
||||
|
||||
let res = Fruit::update(pear).exec(db).await?;
|
||||
|
||||
@ -46,7 +46,7 @@ pub async fn insert_and_update(db: &Database) -> Result<(), ExecErr> {
|
||||
|
||||
pub async fn save_active_model(db: &Database) -> Result<(), ExecErr> {
|
||||
let banana = fruit::ActiveModel {
|
||||
name: Val::set("banana".to_owned()),
|
||||
name: Set("banana".to_owned()),
|
||||
..Default::default()
|
||||
};
|
||||
let mut banana = banana.save(db).await?;
|
||||
@ -54,7 +54,7 @@ pub async fn save_active_model(db: &Database) -> Result<(), ExecErr> {
|
||||
println!();
|
||||
println!("Inserted: {:?}\n", banana);
|
||||
|
||||
banana.name = Val::set("banana banana".to_owned());
|
||||
banana.name = Set("banana banana".to_owned());
|
||||
|
||||
let banana = banana.save(db).await?;
|
||||
|
||||
@ -79,8 +79,8 @@ mod form {
|
||||
|
||||
async fn save_custom_active_model(db: &Database) -> Result<(), ExecErr> {
|
||||
let pineapple = form::ActiveModel {
|
||||
id: Val::unset(),
|
||||
name: Val::set("pineapple".to_owned()),
|
||||
id: Unset(None),
|
||||
name: Set("pineapple".to_owned()),
|
||||
};
|
||||
|
||||
let pineapple = pineapple.save(db).await?;
|
||||
|
@ -10,7 +10,21 @@ where
|
||||
state: ActiveValueState,
|
||||
}
|
||||
|
||||
pub type Val<V> = ActiveValue<V>;
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Set<V>(v: V) -> ActiveValue<V>
|
||||
where
|
||||
V: Into<Value> + Default,
|
||||
{
|
||||
ActiveValue::set(v)
|
||||
}
|
||||
|
||||
#[allow(non_snake_case)]
|
||||
pub fn Unset<V>(_: Option<bool>) -> ActiveValue<V>
|
||||
where
|
||||
V: Into<Value> + Default,
|
||||
{
|
||||
ActiveValue::unset()
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum ActiveValueState {
|
||||
|
@ -106,7 +106,7 @@ pub trait EntityTrait: EntityName {
|
||||
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder};
|
||||
///
|
||||
/// let apple = cake::ActiveModel {
|
||||
/// name: Val::set("Apple Pie".to_owned()),
|
||||
/// name: Set("Apple Pie".to_owned()),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
/// assert_eq!(
|
||||
@ -121,11 +121,11 @@ pub trait EntityTrait: EntityName {
|
||||
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder};
|
||||
///
|
||||
/// let apple = cake::ActiveModel {
|
||||
/// name: Val::set("Apple Pie".to_owned()),
|
||||
/// name: Set("Apple Pie".to_owned()),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
/// let orange = cake::ActiveModel {
|
||||
/// name: Val::set("Orange Scone".to_owned()),
|
||||
/// name: Set("Orange Scone".to_owned()),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
/// assert_eq!(
|
||||
@ -153,7 +153,7 @@ pub trait EntityTrait: EntityName {
|
||||
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder};
|
||||
///
|
||||
/// let apple = cake::ActiveModel {
|
||||
/// name: Val::set("Apple Pie".to_owned()),
|
||||
/// name: Set("Apple Pie".to_owned()),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
/// assert_eq!(
|
||||
@ -174,11 +174,11 @@ pub trait EntityTrait: EntityName {
|
||||
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::PostgresQueryBuilder};
|
||||
///
|
||||
/// let apple = cake::ActiveModel {
|
||||
/// name: Val::set("Apple Pie".to_owned()),
|
||||
/// name: Set("Apple Pie".to_owned()),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
/// let orange = cake::ActiveModel {
|
||||
/// name: Val::set("Orange Scone".to_owned()),
|
||||
/// name: Set("Orange Scone".to_owned()),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
/// assert_eq!(
|
||||
@ -200,8 +200,8 @@ pub trait EntityTrait: EntityName {
|
||||
/// use sea_orm::{entity::*, query::*, tests_cfg::fruit, sea_query::PostgresQueryBuilder};
|
||||
///
|
||||
/// let orange = fruit::ActiveModel {
|
||||
/// id: Val::set(1),
|
||||
/// name: Val::set("Orange".to_owned()),
|
||||
/// id: Set(1),
|
||||
/// name: Set("Orange".to_owned()),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
/// assert_eq!(
|
||||
|
@ -95,7 +95,7 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::tests_cfg::cake;
|
||||
use crate::{Insert, QueryTrait, Val};
|
||||
use crate::{Insert, QueryTrait, ActiveValue};
|
||||
use sea_query::PostgresQueryBuilder;
|
||||
|
||||
#[test]
|
||||
@ -103,8 +103,8 @@ mod tests {
|
||||
assert_eq!(
|
||||
Insert::<cake::ActiveModel>::new()
|
||||
.one(cake::ActiveModel {
|
||||
id: Val::unset(),
|
||||
name: Val::set("Apple Pie".to_owned()),
|
||||
id: ActiveValue::unset(),
|
||||
name: ActiveValue::set("Apple Pie".to_owned()),
|
||||
})
|
||||
.build(PostgresQueryBuilder)
|
||||
.to_string(),
|
||||
@ -117,8 +117,8 @@ mod tests {
|
||||
assert_eq!(
|
||||
Insert::<cake::ActiveModel>::new()
|
||||
.one(cake::ActiveModel {
|
||||
id: Val::set(1),
|
||||
name: Val::set("Apple Pie".to_owned()),
|
||||
id: ActiveValue::set(1),
|
||||
name: ActiveValue::set("Apple Pie".to_owned()),
|
||||
})
|
||||
.build(PostgresQueryBuilder)
|
||||
.to_string(),
|
||||
@ -164,12 +164,12 @@ mod tests {
|
||||
#[should_panic(expected = "columns mismatch")]
|
||||
fn insert_5() {
|
||||
let apple = cake::ActiveModel {
|
||||
name: Val::set("Apple".to_owned()),
|
||||
name: ActiveValue::set("Apple".to_owned()),
|
||||
..Default::default()
|
||||
};
|
||||
let orange = cake::ActiveModel {
|
||||
id: Val::set(2),
|
||||
name: Val::set("Orange".to_owned()),
|
||||
id: ActiveValue::set(2),
|
||||
name: ActiveValue::set("Orange".to_owned()),
|
||||
};
|
||||
assert_eq!(
|
||||
Insert::<cake::ActiveModel>::new()
|
||||
|
@ -86,15 +86,15 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::tests_cfg::{cake, fruit};
|
||||
use crate::{QueryTrait, Update, Val};
|
||||
use crate::{QueryTrait, Update, ActiveValue};
|
||||
use sea_query::PostgresQueryBuilder;
|
||||
|
||||
#[test]
|
||||
fn update_1() {
|
||||
assert_eq!(
|
||||
Update::<cake::ActiveModel>::new(cake::ActiveModel {
|
||||
id: Val::set(1),
|
||||
name: Val::set("Apple Pie".to_owned()),
|
||||
id: ActiveValue::set(1),
|
||||
name: ActiveValue::set("Apple Pie".to_owned()),
|
||||
})
|
||||
.build(PostgresQueryBuilder)
|
||||
.to_string(),
|
||||
@ -106,9 +106,9 @@ mod tests {
|
||||
fn update_2() {
|
||||
assert_eq!(
|
||||
Update::<fruit::ActiveModel>::new(fruit::ActiveModel {
|
||||
id: Val::set(1),
|
||||
name: Val::set("Orange".to_owned()),
|
||||
cake_id: Val::unset(),
|
||||
id: ActiveValue::set(1),
|
||||
name: ActiveValue::set("Orange".to_owned()),
|
||||
cake_id: ActiveValue::unset(),
|
||||
})
|
||||
.build(PostgresQueryBuilder)
|
||||
.to_string(),
|
||||
@ -120,9 +120,9 @@ mod tests {
|
||||
fn update_3() {
|
||||
assert_eq!(
|
||||
Update::<fruit::ActiveModel>::new(fruit::ActiveModel {
|
||||
id: Val::set(2),
|
||||
name: Val::unchanged("Apple".to_owned()),
|
||||
cake_id: Val::set(Some(3)),
|
||||
id: ActiveValue::set(2),
|
||||
name: ActiveValue::unchanged("Apple".to_owned()),
|
||||
cake_id: ActiveValue::set(Some(3)),
|
||||
})
|
||||
.build(PostgresQueryBuilder)
|
||||
.to_string(),
|
||||
|
Loading…
x
Reference in New Issue
Block a user