pub trait QueryTrait {
type QueryStatement: QueryStatementBuilder;
// Required methods
fn query(&mut self) -> &mut Self::QueryStatement;
fn as_query(&self) -> &Self::QueryStatement;
fn into_query(self) -> Self::QueryStatement;
// Provided methods
fn build(&self, db_backend: DbBackend) -> Statement { ... }
fn apply_if<T, F>(self, val: Option<T>, if_some: F) -> Self
where Self: Sized,
F: FnOnce(Self, T) -> Self { ... }
}
Expand description
A Trait for any type performing queries on a Model or ActiveModel
Required Associated Typesยง
Sourcetype QueryStatement: QueryStatementBuilder
type QueryStatement: QueryStatementBuilder
Constrain the QueryStatement to QueryStatementBuilder trait
Required Methodsยง
Sourcefn query(&mut self) -> &mut Self::QueryStatement
fn query(&mut self) -> &mut Self::QueryStatement
Get a mutable ref to the query builder
Sourcefn as_query(&self) -> &Self::QueryStatement
fn as_query(&self) -> &Self::QueryStatement
Get an immutable ref to the query builder
Sourcefn into_query(self) -> Self::QueryStatement
fn into_query(self) -> Self::QueryStatement
Take ownership of the query builder
Provided Methodsยง
Sourcefn apply_if<T, F>(self, val: Option<T>, if_some: F) -> Self
fn apply_if<T, F>(self, val: Option<T>, if_some: F) -> Self
Apply an operation on the QueryTrait::QueryStatement if the given Option<T>
is Some(_)
ยงExample
use sea_orm::{entity::*, query::*, tests_cfg::cake, DbBackend};
assert_eq!(
cake::Entity::find()
.apply_if(Some(3), |mut query, v| {
query.filter(cake::Column::Id.eq(v))
})
.apply_if(Some(100), QuerySelect::limit)
.apply_if(None, QuerySelect::offset::<Option<u64>>) // no-op
.build(DbBackend::Postgres)
.to_string(),
r#"SELECT "cake"."id", "cake"."name" FROM "cake" WHERE "cake"."id" = 3 LIMIT 100"#
);