pub trait QueryFilter: Sized {
type QueryStatement: ConditionalStatement;
// Required method
fn query(&mut self) -> &mut Self::QueryStatement;
// Provided methods
fn filter<F>(self, filter: F) -> Self
where F: IntoCondition { ... }
fn belongs_to<M>(self, model: &M) -> Self
where M: ModelTrait { ... }
fn belongs_to_tbl_alias<M>(self, model: &M, tbl_alias: &str) -> Self
where M: ModelTrait { ... }
}
Expand description
Perform a FILTER opertation on a statement
Required Associated Types§
Required Methods§
Sourcefn query(&mut self) -> &mut Self::QueryStatement
fn query(&mut self) -> &mut Self::QueryStatement
Add the query to perform a FILTER on
Provided Methods§
Sourcefn filter<F>(self, filter: F) -> Selfwhere
F: IntoCondition,
fn filter<F>(self, filter: F) -> Selfwhere
F: IntoCondition,
Add an AND WHERE expression
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.filter(cake::Column::Id.eq(4))
.filter(cake::Column::Id.eq(5))
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 AND `cake`.`id` = 5"
);
Add a condition tree.
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.filter(
Condition::any()
.add(cake::Column::Id.eq(4))
.add(cake::Column::Id.eq(5))
)
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` = 4 OR `cake`.`id` = 5"
);
Like above, but using the IN
operator.
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.filter(cake::Column::Id.is_in([4, 5]))
.build(DbBackend::MySql)
.to_string(),
"SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`id` IN (4, 5)"
);
Like above, but using the ANY
operator. Postgres only.
use sea_orm::sea_query::{extension::postgres::PgFunc, Expr};
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.filter(Expr::col((cake::Entity, cake::Column::Id)).eq(PgFunc::any(vec![4, 5])))
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT "cake"."id", "cake"."name" FROM "cake" WHERE "cake"."id" = ANY(ARRAY [4,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%'"
);
A slightly more complex example.
use sea_orm::{entity::*, query::*, tests_cfg::cake, sea_query::Expr, DbBackend};
assert_eq!(
cake::Entity::find()
.filter(
Condition::all()
.add(
Condition::all()
.not()
.add(Expr::val(1).eq(1))
.add(Expr::val(2).eq(2))
)
.add(
Condition::any()
.add(Expr::val(3).eq(3))
.add(Expr::val(4).eq(4))
)
)
.build(DbBackend::Postgres)
.to_string(),
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"
);
Sourcefn belongs_to<M>(self, model: &M) -> Selfwhere
M: ModelTrait,
fn belongs_to<M>(self, model: &M) -> Selfwhere
M: ModelTrait,
Apply a where condition using the model’s primary key
Sourcefn belongs_to_tbl_alias<M>(self, model: &M, tbl_alias: &str) -> Selfwhere
M: ModelTrait,
fn belongs_to_tbl_alias<M>(self, model: &M, tbl_alias: &str) -> Selfwhere
M: ModelTrait,
Perform a check to determine table belongs to a Model through it’s name alias
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.