sea_query IntoCondition

This commit is contained in:
Chris Tsang 2021-05-25 22:30:17 +08:00
parent 68f11ea987
commit e216974fa3
3 changed files with 8 additions and 22 deletions

View File

@ -70,7 +70,7 @@ pub trait EntityTrait: EntityName {
/// .to_string(), /// .to_string(),
/// [ /// [
/// r#"SELECT "cake_filling"."cake_id", "cake_filling"."filling_id" FROM "cake_filling""#, /// 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(" ") /// ].join(" ")
/// ); /// );
/// ``` /// ```

View File

@ -116,7 +116,7 @@ mod tests {
"SELECT `cake`.`id` AS `A_id`, `cake`.`name` AS `A_name`,", "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`", "`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`", "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(" ") ].join(" ")
); );
} }

View File

@ -1,12 +1,8 @@
use crate::{ColumnTrait, Identity, IntoSimpleExpr, RelationDef}; use crate::{ColumnTrait, Identity, IntoSimpleExpr, RelationDef};
use sea_query::{Alias, Expr, SelectExpr, SelectStatement, SimpleExpr}; use sea_query::{Alias, Expr, IntoCondition, SelectExpr, SelectStatement, SimpleExpr};
pub use sea_query::{Condition, JoinType, Order}; pub use sea_query::{Condition, JoinType, Order};
use std::rc::Rc; use std::rc::Rc;
pub trait IntoCondition {
fn into_condition(self) -> Condition;
}
pub trait QueryHelper: Sized { pub trait QueryHelper: Sized {
fn query(&mut self) -> &mut SelectStatement; fn query(&mut self) -> &mut SelectStatement;
@ -71,10 +67,10 @@ pub trait QueryHelper: Sized {
/// .filter(cake::Column::Id.eq(5)) /// .filter(cake::Column::Id.eq(5))
/// .build(MysqlQueryBuilder) /// .build(MysqlQueryBuilder)
/// .to_string(), /// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 AND (`cake`.`id` = 5)" /// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 AND `cake`.`id` = 5"
/// ); /// );
/// ``` /// ```
/// ///
/// Add a condition tree. /// Add a condition tree.
/// ``` /// ```
/// use sea_orm::{Condition, ColumnTrait, EntityTrait, QueryHelper, tests_cfg::cake, sea_query::MysqlQueryBuilder}; /// use sea_orm::{Condition, ColumnTrait, EntityTrait, QueryHelper, tests_cfg::cake, sea_query::MysqlQueryBuilder};
@ -92,7 +88,9 @@ pub trait QueryHelper: Sized {
/// ); /// );
/// ``` /// ```
fn filter<F>(mut self, filter: F) -> Self fn filter<F>(mut self, filter: F) -> Self
where F: IntoCondition { where
F: IntoCondition,
{
self.query().cond_where(filter.into_condition()); self.query().cond_where(filter.into_condition());
self self
} }
@ -216,18 +214,6 @@ 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 { fn join_condition(rel: RelationDef) -> SimpleExpr {
let from_tbl = rel.from_tbl.clone(); let from_tbl = rel.from_tbl.clone();
let to_tbl = rel.to_tbl.clone(); let to_tbl = rel.to_tbl.clone();