pub trait QueryTrait {
type QueryStatement: StatementBuilder;
// 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
Required Associated Typesยง
Sourcetype QueryStatement: StatementBuilder
type QueryStatement: StatementBuilder
The underlying sea_query statement type this builder produces.
Required Methodsยง
Sourcefn query(&mut self) -> &mut Self::QueryStatement
fn query(&mut self) -> &mut Self::QueryStatement
Mutable access to the underlying statement.
Sourcefn as_query(&self) -> &Self::QueryStatement
fn as_query(&self) -> &Self::QueryStatement
Shared access to the underlying statement.
Sourcefn into_query(self) -> Self::QueryStatement
fn into_query(self) -> Self::QueryStatement
Consume the builder and return the underlying statement.
Provided Methodsยง
Sourcefn build(&self, db_backend: DbBackend) -> Statement
fn build(&self, db_backend: DbBackend) -> Statement
Render the query for db_backend as a Statement (SQL + bound
parameters). Useful for inspecting generated SQL in tests.
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::{DbBackend, entity::*, query::*, tests_cfg::cake};
assert_eq!(
cake::Entity::find()
.apply_if(Some(3), |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"#
);Dyn Compatibilityยง
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".