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 filter_mut<F>(&mut self, filter: F)
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::{DbBackend, entity::*, query::*, tests_cfg::cake};
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::{DbBackend, entity::*, query::*, tests_cfg::cake};
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::{DbBackend, entity::*, query::*, tests_cfg::cake};
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::{DbBackend, entity::*, query::*, tests_cfg::cake};
assert_eq!(
cake::Entity::find()
.filter(cake::Column::Id.eq_any([4, 5]))
.build(DbBackend::Postgres),
Statement::from_sql_and_values(
DbBackend::Postgres,
r#"SELECT "cake"."id", "cake"."name" FROM "cake" WHERE "cake"."id" = ANY($1)"#,
[vec![4, 5].into()]
)
);Add a runtime-built condition tree.
use sea_orm::{DbBackend, entity::*, query::*, tests_cfg::cake};
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::{DbBackend, entity::*, query::*, tests_cfg::cake};
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, ExprTrait}, 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, ExprTrait}, 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 filter_mut<F>(&mut self, filter: F)where
F: IntoCondition,
fn filter_mut<F>(&mut self, filter: F)where
F: IntoCondition,
Like Self::filter, but without consuming self
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
assert_eq!(
fruit::Entity::find()
.left_join(cake::Entity)
.belongs_to(&cake::Model {
id: 12,
name: "".into(),
})
.build(DbBackend::MySql)
.to_string(),
[
"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`",
"LEFT JOIN `cake` ON `fruit`.`cake_id` = `cake`.`id`",
"WHERE `cake`.`id` = 12",
]
.join(" ")
);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,
Like belongs_to, but via a table alias
assert_eq!(
fruit::Entity::find()
.join_as(JoinType::LeftJoin, fruit::Relation::Cake.def(), "puff")
.belongs_to_tbl_alias(
&cake::Model {
id: 12,
name: "".into(),
},
"puff"
)
.build(DbBackend::MySql)
.to_string(),
[
"SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit`",
"LEFT JOIN `cake` AS `puff` ON `fruit`.`cake_id` = `puff`.`id`",
"WHERE `puff`.`id` = 12",
]
.join(" ")
);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.