Skip to content

Commit

Permalink
Add example usage of Condition
Browse files Browse the repository at this point in the history
  • Loading branch information
tyt2y3 committed Sep 29, 2021
1 parent 689e007 commit bbd09ce
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/query/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,48 @@ pub trait QueryFilter: Sized {
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 OR `cake`.`id` = 5"
/// );
/// ```
///
/// Add a runtime-built condition tree.
/// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
/// struct Input {
/// name: Option<String>,
/// }
/// let input = Input { name: Some("cheese".to_owned()) };
///
/// let mut conditions = Condition::all();
/// if let Some(name) = input.name {
/// conditions = conditions.add(cake::Column::Name.contains(&name));
/// }
///
/// assert_eq!(
/// cake::Entity::find()
/// .filter(conditions)
/// .build(DbBackend::MySql)
/// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%cheese%'"
/// );
/// ```
///
/// Add a runtime-built condition tree, functional-way.
/// ```
/// use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
/// struct Input {
/// name: Option<String>,
/// }
/// let input = Input { name: Some("cheese".to_owned()) };
///
/// assert_eq!(
/// cake::Entity::find()
/// .filter(
/// Condition::all()
/// .add_option(input.name.map(|n| cake::Column::Name.contains(&n)))
/// )
/// .build(DbBackend::MySql)
/// .to_string(),
/// "SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%cheese%'"
/// );
/// ```
fn filter<F>(mut self, filter: F) -> Self
where
F: IntoCondition,
Expand Down

0 comments on commit bbd09ce

Please sign in to comment.