sea_orm::query

Trait ConditionalStatement

Source
pub trait ConditionalStatement {
    // Required method
    fn cond_where<C>(&mut self, condition: C) -> &mut Self
       where C: IntoCondition;

    // Provided methods
    fn and_where(&mut self, other: SimpleExpr) -> &mut Self { ... }
    fn and_where_option(&mut self, other: Option<SimpleExpr>) -> &mut Self { ... }
}

Required Methods§

Source

fn cond_where<C>(&mut self, condition: C) -> &mut Self
where C: IntoCondition,

Where condition, expressed with any and all. Calling cond_where multiple times will conjoin them. Calling or_where after cond_where will panic.

§Examples
use sea_query::{*, tests_cfg::*};

let query = Query::select()
    .column(Glyph::Image)
    .from(Glyph::Table)
    .cond_where(
        Cond::all()
            .add(Expr::col((Glyph::Table, Glyph::Aspect)).is_in([3, 4]))
            .add(Cond::any()
                .add(Expr::col((Glyph::Table, Glyph::Image)).like("A%"))
                .add(Expr::col((Glyph::Table, Glyph::Image)).like("B%"))
            )
    )
    .to_owned();

assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "image" FROM "glyph" WHERE "glyph"."aspect" IN (3, 4) AND ("glyph"."image" LIKE 'A%' OR "glyph"."image" LIKE 'B%')"#
);

Using macro

use sea_query::{*, tests_cfg::*};

let query = Query::select()
    .column(Glyph::Image)
    .from(Glyph::Table)
    .cond_where(
        all![
            Expr::col((Glyph::Table, Glyph::Aspect)).is_in([3, 4]),
            any![
                Expr::col((Glyph::Table, Glyph::Image)).like("A%"),
                Expr::col((Glyph::Table, Glyph::Image)).like("B%"),
            ]
        ])
    .to_owned();

assert_eq!(
    query.to_string(PostgresQueryBuilder),
    r#"SELECT "image" FROM "glyph" WHERE "glyph"."aspect" IN (3, 4) AND ("glyph"."image" LIKE 'A%' OR "glyph"."image" LIKE 'B%')"#
);

Calling multiple times; the following two are equivalent:

use sea_query::{tests_cfg::*, *};

assert_eq!(
    Query::select()
        .column(Glyph::Id)
        .from(Glyph::Table)
        .cond_where(Expr::col(Glyph::Id).eq(1))
        .cond_where(any![Expr::col(Glyph::Id).eq(2), Expr::col(Glyph::Id).eq(3)])
        .to_owned()
        .to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "glyph" WHERE "id" = 1 AND ("id" = 2 OR "id" = 3)"#
);

assert_eq!(
    Query::select()
        .column(Glyph::Id)
        .from(Glyph::Table)
        .cond_where(any![Expr::col(Glyph::Id).eq(2), Expr::col(Glyph::Id).eq(3)])
        .cond_where(Expr::col(Glyph::Id).eq(1))
        .to_owned()
        .to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "glyph" WHERE ("id" = 2 OR "id" = 3) AND "id" = 1"#
);

Calling multiple times; will be ANDed togother

use sea_query::{tests_cfg::*, *};

assert_eq!(
    Query::select()
        .column(Glyph::Id)
        .from(Glyph::Table)
        .cond_where(any![Expr::col(Glyph::Id).eq(1), Expr::col(Glyph::Id).eq(2)])
        .cond_where(any![Expr::col(Glyph::Id).eq(3), Expr::col(Glyph::Id).eq(4)])
        .to_owned()
        .to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "glyph" WHERE ("id" = 1 OR "id" = 2) AND ("id" = 3 OR "id" = 4)"#
);

assert_eq!(
    Query::select()
        .column(Glyph::Id)
        .from(Glyph::Table)
        .cond_where(all![Expr::col(Glyph::Id).eq(1), Expr::col(Glyph::Id).eq(2)])
        .cond_where(all![Expr::col(Glyph::Id).eq(3), Expr::col(Glyph::Id).eq(4)])
        .to_owned()
        .to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "glyph" WHERE "id" = 1 AND "id" = 2 AND "id" = 3 AND "id" = 4"#
);

Some more test cases involving negation

use sea_query::{tests_cfg::*, *};

assert_eq!(
    Query::select()
        .column(Glyph::Id)
        .from(Glyph::Table)
        .cond_where(
            Cond::all()
                .not()
                .add(Expr::col(Glyph::Id).eq(1))
                .add(Expr::col(Glyph::Id).eq(2)),
        )
        .cond_where(
            Cond::all()
                .add(Expr::col(Glyph::Id).eq(3))
                .add(Expr::col(Glyph::Id).eq(4)),
        )
        .to_owned()
        .to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "glyph" WHERE (NOT ("id" = 1 AND "id" = 2)) AND ("id" = 3 AND "id" = 4)"#
);

assert_eq!(
    Query::select()
        .column(Glyph::Id)
        .from(Glyph::Table)
        .cond_where(
            Cond::all()
                .add(Expr::col(Glyph::Id).eq(3))
                .add(Expr::col(Glyph::Id).eq(4)),
        )
        .cond_where(
            Cond::all()
                .not()
                .add(Expr::col(Glyph::Id).eq(1))
                .add(Expr::col(Glyph::Id).eq(2)),
        )
        .to_owned()
        .to_string(PostgresQueryBuilder),
    r#"SELECT "id" FROM "glyph" WHERE "id" = 3 AND "id" = 4 AND (NOT ("id" = 1 AND "id" = 2))"#
);

Provided Methods§

Source

fn and_where(&mut self, other: SimpleExpr) -> &mut Self

And where condition. Calling or_where after and_where will panic.

§Examples
use sea_query::{*, tests_cfg::*};

let query = Query::select()
    .column(Glyph::Image)
    .from(Glyph::Table)
    .and_where(Expr::col((Glyph::Table, Glyph::Aspect)).is_in([3, 4]))
    .and_where(Expr::col((Glyph::Table, Glyph::Image)).like("A%"))
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `image` FROM `glyph` WHERE `glyph`.`aspect` IN (3, 4) AND `glyph`.`image` LIKE 'A%'"#
);
Source

fn and_where_option(&mut self, other: Option<SimpleExpr>) -> &mut Self

Optional and where, short hand for if c.is_some() q.and_where(c).

use sea_query::{tests_cfg::*, *};

let query = Query::select()
    .column(Glyph::Image)
    .from(Glyph::Table)
    .and_where(Expr::col(Glyph::Aspect).is_in([3, 4]))
    .and_where_option(Some(Expr::col(Glyph::Image).like("A%")))
    .and_where_option(None)
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `image` FROM `glyph` WHERE `aspect` IN (3, 4) AND `image` LIKE 'A%'"#
);

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§