Merge pull request #7 from SeaQL/filter_condition
`filter` accepts Condition
This commit is contained in:
commit
68f11ea987
@ -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(" ")
|
||||
/// );
|
||||
/// ```
|
||||
|
@ -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(" ")
|
||||
);
|
||||
}
|
||||
|
@ -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<F>(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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user