From 5daa9abd38fde08debde86cadada198e935b34cb Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Sat, 23 Oct 2021 15:41:16 +0800 Subject: [PATCH] #267 Added `is_null` and `is_not_null` --- src/entity/column.rs | 13 ++++++++----- src/query/helper.rs | 12 ++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/entity/column.rs b/src/entity/column.rs index b7ce2820..423e3e3b 100644 --- a/src/entity/column.rs +++ b/src/entity/column.rs @@ -47,8 +47,9 @@ macro_rules! bind_oper { }; } -macro_rules! bind_agg_func { +macro_rules! bind_func_no_params { ( $func: ident ) => { + /// See also SeaQuery's method with same name. fn $func(&self) -> SimpleExpr { Expr::tbl(self.entity_name(), *self).$func() } @@ -213,10 +214,12 @@ pub trait ColumnTrait: IdenStatic + Iterable + FromStr { Expr::tbl(self.entity_name(), *self).like(&pattern) } - bind_agg_func!(max); - bind_agg_func!(min); - bind_agg_func!(sum); - bind_agg_func!(count); + bind_func_no_params!(max); + bind_func_no_params!(min); + bind_func_no_params!(sum); + bind_func_no_params!(count); + bind_func_no_params!(is_null); + bind_func_no_params!(is_not_null); fn if_null(&self, v: V) -> SimpleExpr where diff --git a/src/query/helper.rs b/src/query/helper.rs index 7efe0ca1..bf762fc9 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -340,6 +340,18 @@ pub trait QueryFilter: Sized { /// r#"SELECT "cake"."id", "cake"."name" FROM "cake" WHERE (NOT (1 = 1 AND 2 = 2)) AND (3 = 3 OR 4 = 4)"# /// ); /// ``` + /// Use a sea_query expression + /// ``` + /// use sea_orm::{entity::*, query::*, sea_query::Expr, tests_cfg::fruit, DbBackend}; + /// + /// assert_eq!( + /// fruit::Entity::find() + /// .filter(Expr::col(fruit::Column::CakeId).is_null()) + /// .build(DbBackend::MySql) + /// .to_string(), + /// "SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` WHERE `cake_id` IS NULL" + /// ); + /// ``` fn filter(mut self, filter: F) -> Self where F: IntoCondition,