From a016cdeedaaf6862343b895c74d0be44c9fa2e9a Mon Sep 17 00:00:00 2001 From: Chris Tsang Date: Mon, 24 May 2021 22:07:02 +0800 Subject: [PATCH] `filter` accepts Condition --- src/entity/base.rs | 2 +- src/query/combine.rs | 2 +- src/query/helper.rs | 40 +++++++++++++++++++++++++++++----------- 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/entity/base.rs b/src/entity/base.rs index 4283a27f..84a11ea2 100644 --- a/src/entity/base.rs +++ b/src/entity/base.rs @@ -70,7 +70,7 @@ pub trait EntityTrait: EntityName { /// .to_string(), /// [ /// r#"SELECT "cake_filling"."cake_id", "cake_filling"."filling_id" FROM "cake_filling""#, - /// r#"WHERE "cake_filling"."cake_id" = 2 AND "cake_filling"."filling_id" = 3"#, + /// r#"WHERE "cake_filling"."cake_id" = 2 AND ("cake_filling"."filling_id" = 3)"#, /// ].join(" ") /// ); /// ``` diff --git a/src/query/combine.rs b/src/query/combine.rs index 757fa3ee..8de78c1f 100644 --- a/src/query/combine.rs +++ b/src/query/combine.rs @@ -116,7 +116,7 @@ mod tests { "SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,", "`fruit`.`id` AS `B_id`, `fruit`.`name` AS `B_name`, `fruit`.`cake_id` AS `B_cake_id`", "FROM `cake` LEFT JOIN `fruit` ON `cake`.`id` = `fruit`.`cake_id`", - "WHERE `cake`.`id` = 1 AND `fruit`.`id` = 2", + "WHERE `cake`.`id` = 1 AND (`fruit`.`id` = 2)", ].join(" ") ); } diff --git a/src/query/helper.rs b/src/query/helper.rs index 264b6ba0..ccc38dfb 100644 --- a/src/query/helper.rs +++ b/src/query/helper.rs @@ -3,6 +3,10 @@ use sea_query::{Alias, Expr, SelectExpr, SelectStatement, SimpleExpr}; pub use sea_query::{Condition, JoinType, Order}; use std::rc::Rc; +pub trait IntoCondition { + fn into_condition(self) -> Condition; +} + pub trait QueryHelper: Sized { fn query(&mut self) -> &mut SelectStatement; @@ -63,31 +67,33 @@ pub trait QueryHelper: Sized { /// /// assert_eq!( /// cake::Entity::find() + /// .filter(cake::Column::Id.eq(4)) /// .filter(cake::Column::Id.eq(5)) /// .build(MysqlQueryBuilder) /// .to_string(), - /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 5" + /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 AND (`cake`.`id` = 5)" /// ); /// ``` - fn filter(mut self, expr: SimpleExpr) -> Self { - self.query().and_where(expr); - self - } - - /// Add a condition tree. This can be called once only. + /// + /// Add a condition tree. /// ``` /// use sea_orm::{Condition, ColumnTrait, EntityTrait, QueryHelper, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// /// assert_eq!( /// cake::Entity::find() - /// .condition(Condition::any().add(cake::Column::Id.eq(5))) + /// .filter( + /// Condition::any() + /// .add(cake::Column::Id.eq(4)) + /// .add(cake::Column::Id.eq(5)) + /// ) /// .build(MysqlQueryBuilder) /// .to_string(), - /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 5" + /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 OR `cake`.`id` = 5" /// ); /// ``` - fn condition(mut self, cond: Condition) -> Self { - self.query().cond_where(cond); + fn filter(mut self, filter: F) -> Self + where F: IntoCondition { + self.query().cond_where(filter.into_condition()); self } @@ -210,6 +216,18 @@ pub trait QueryHelper: Sized { } } +impl IntoCondition for SimpleExpr { + fn into_condition(self) -> Condition { + Condition::all().add(self) + } +} + +impl IntoCondition for Condition { + fn into_condition(self) -> Condition { + self + } +} + fn join_condition(rel: RelationDef) -> SimpleExpr { let from_tbl = rel.from_tbl.clone(); let to_tbl = rel.to_tbl.clone();