QueryFilter

Trait QueryFilter 

Source
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§

Source

fn query(&mut self) -> &mut Self::QueryStatement

Add the query to perform a FILTER on

Provided Methods§

Source

fn filter<F>(self, filter: F) -> Self
where 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"
);
Source

fn filter_mut<F>(&mut self, filter: F)
where F: IntoCondition,

Like Self::filter, but without consuming self

Source

fn belongs_to<M>(self, model: &M) -> Self
where 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(" ")
);
Source

fn belongs_to_tbl_alias<M>(self, model: &M, tbl_alias: &str) -> Self
where 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.

Implementors§

Source§

impl<A> QueryFilter for ValidatedUpdateOne<A>

Source§

impl<E> QueryFilter for DeleteMany<E>
where E: EntityTrait,

Source§

impl<E> QueryFilter for Select<E>
where E: EntityTrait,

Source§

impl<E> QueryFilter for UpdateMany<E>
where E: EntityTrait,

Source§

impl<E> QueryFilter for ValidatedDeleteOne<E>
where E: EntityTrait,

Source§

impl<E, F> QueryFilter for SelectTwo<E, F>
where E: EntityTrait, F: EntityTrait,

Source§

impl<E, F> QueryFilter for SelectTwoMany<E, F>
where E: EntityTrait, F: EntityTrait,

Source§

impl<E, F, G, H, I, J, TOP> QueryFilter for SelectSix<E, F, G, H, I, J, TOP>

Source§

impl<E, F, G, H, I, TOP> QueryFilter for SelectFive<E, F, G, H, I, TOP>

Source§

impl<E, F, G, H, TOP> QueryFilter for SelectFour<E, F, G, H, TOP>

Source§

impl<E, F, G, TOP> QueryFilter for SelectThree<E, F, G, TOP>