Struct sea_query::query::Condition[][src]

pub struct Condition { /* fields omitted */ }
Expand description

Represents the value of an Condition::any or Condition::all: a set of disjunctive or conjunctive conditions.

Implementations

Add a condition to the set.

If it’s an Condition::any, it will be separated from the others by an " OR " in the query. If it’s an Condition::all, it will be separated by an " AND ".

Add an optional condition to the set.

Shorthand for if o.is_some() { self.add(o) }

Examples

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

let query = Query::select()
    .column(Glyph::Image)
    .from(Glyph::Table)
    .cond_where(
        Cond::all()
            .add_option(Some(Expr::tbl(Glyph::Table, Glyph::Image).like("A%")))
            .add_option(None::<SimpleExpr>)
    )
    .to_owned();

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

Create a condition that is true if any of the conditions is true.

Examples

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

let query = Query::select()
    .column(Glyph::Image)
    .from(Glyph::Table)
    .cond_where(
        Cond::any()
            .add(Expr::tbl(Glyph::Table, Glyph::Aspect).is_in(vec![3, 4]))
            .add(Expr::tbl(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) OR `glyph`.`image` LIKE 'A%'"#
);

Create a condition that is false if any of the conditions is false.

Examples

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

let query = Query::select()
    .column(Glyph::Image)
    .from(Glyph::Table)
    .cond_where(
        Cond::all()
            .add(Expr::tbl(Glyph::Table, Glyph::Aspect).is_in(vec![3, 4]))
            .add(Expr::tbl(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%'"#
);

Negates a condition.

Examples

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

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

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

More Examples

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

let query = Query::select()
    .column(Glyph::Id)
    .cond_where(
        Cond::all()
            .add(
                Cond::all()
                    .not()
                    .add(Expr::val(1).eq(1))
                    .add(Expr::val(2).eq(2))
            )
            .add(
                Cond::any()
                    .add(Expr::val(3).eq(3))
                    .add(Expr::val(4).eq(4))
            )
    )
    .to_owned();

assert_eq!(
    query.to_string(MysqlQueryBuilder),
    r#"SELECT `id` WHERE (NOT (1 = 1 AND 2 = 2)) AND (3 = 3 OR 4 = 4)"#
);

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.