diff --git a/src/entity/column.rs b/src/entity/column.rs index 865eccae..d1910f1d 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -1,5 +1,5 @@ use crate::{EntityName, IdenStatic, Iterable}; -use sea_query::{DynIden, Expr, SeaRc, SelectStatement, SimpleExpr, Value}; +use sea_query::{Alias, BinOper, DynIden, Expr, SeaRc, SelectStatement, SimpleExpr, Value}; use std::str::FromStr; /// Defines a Column for an Entity @@ -67,13 +67,18 @@ pub enum ColumnType { } macro_rules! bind_oper { - ( $op: ident ) => { + ( $op: ident, $bin_op: ident ) => { #[allow(missing_docs)] fn $op(&self, v: V) -> SimpleExpr where V: Into, { - Expr::tbl(self.entity_name(), *self).$op(v) + let val = Expr::val(v); + let expr = match self.def().get_column_type().get_enum_name() { + Some(enum_name) => val.as_enum(Alias::new(enum_name)), + None => val.into(), + }; + Expr::tbl(self.entity_name(), *self).binary(BinOper::$bin_op, expr) } }; } @@ -130,12 +135,12 @@ pub trait ColumnTrait: IdenStatic + Iterable + FromStr { (self.entity_name(), SeaRc::new(*self) as DynIden) } - bind_oper!(eq); - bind_oper!(ne); - bind_oper!(gt); - bind_oper!(gte); - bind_oper!(lt); - bind_oper!(lte); + bind_oper!(eq, Equal); + bind_oper!(ne, NotEqual); + bind_oper!(gt, GreaterThan); + bind_oper!(gte, GreaterThanOrEqual); + bind_oper!(lt, SmallerThan); + bind_oper!(lte, SmallerThanOrEqual); /// ``` /// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend}; diff --git a/tests/active_enum_tests.rs b/tests/active_enum_tests.rs index ca9bf7d9..aaad419b 100644 --- a/tests/active_enum_tests.rs +++ b/tests/active_enum_tests.rs @@ -30,8 +30,9 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> { .insert(db) .await?; + let model = Entity::find().one(db).await?.unwrap(); assert_eq!( - Entity::find().one(db).await?.unwrap(), + model, Model { id: 1, category: None, @@ -39,6 +40,17 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> { tea: None, } ); + assert_eq!( + model, + Entity::find() + .filter(Column::Id.is_not_null()) + .filter(Column::Category.is_null()) + .filter(Column::Color.is_null()) + .filter(Column::Tea.is_null()) + .one(db) + .await? + .unwrap() + ); let am = ActiveModel { category: Set(Some(Category::Big)), @@ -49,8 +61,9 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> { .save(db) .await?; + let model = Entity::find().one(db).await?.unwrap(); assert_eq!( - Entity::find().one(db).await?.unwrap(), + model, Model { id: 1, category: Some(Category::Big), @@ -58,6 +71,17 @@ pub async fn insert_active_enum(db: &DatabaseConnection) -> Result<(), DbErr> { tea: Some(Tea::EverydayTea), } ); + assert_eq!( + model, + Entity::find() + .filter(Column::Id.eq(1)) + .filter(Column::Category.eq(Category::Big)) + .filter(Column::Color.eq(Color::Black)) + .filter(Column::Tea.eq(Tea::EverydayTea)) + .one(db) + .await? + .unwrap() + ); let res = am.delete(db).await?;