Trait sea_query::query::ConditionalStatement
source · [−]pub trait ConditionalStatement {
fn cond_where<C>(&mut self, condition: C) -> &mut Self
where
C: IntoCondition;
fn and_where(&mut self, other: SimpleExpr) -> &mut Self { ... }
fn and_where_option(&mut self, other: Option<SimpleExpr>) -> &mut Self { ... }
fn or_where(&mut self, other: SimpleExpr) -> &mut Self { ... }
}
Required methods
fn cond_where<C>(&mut self, condition: C) -> &mut Self where
C: IntoCondition,
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::tbl(Glyph::Table, Glyph::Aspect).is_in(vec![3, 4]))
.add(Cond::any()
.add(Expr::tbl(Glyph::Table, Glyph::Image).like("A%"))
.add(Expr::tbl(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::tbl(Glyph::Table, Glyph::Aspect).is_in(vec![3, 4]),
any![
Expr::tbl(Glyph::Table, Glyph::Image).like("A%"),
Expr::tbl(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
use sea_query::{tests_cfg::*, *};
assert_eq!(
Query::select()
.cond_where(Cond::all().add(Expr::col(Glyph::Id).eq(1)))
.cond_where(
Cond::any()
.add(Expr::col(Glyph::Id).eq(2))
.add(Expr::col(Glyph::Id).eq(3)),
)
.to_owned()
.to_string(PostgresQueryBuilder),
r#"SELECT WHERE "id" = 1 AND ("id" = 2 OR "id" = 3)"#
);
Calling multiple times
use sea_query::{tests_cfg::*, *};
assert_eq!(
Query::select()
.cond_where(
Cond::any()
.add(Expr::col(Glyph::Id).eq(1))
.add(Expr::col(Glyph::Id).eq(2)),
)
.cond_where(Expr::col(Glyph::Id).eq(3))
.cond_where(Expr::col(Glyph::Id).eq(4))
.to_owned()
.to_string(PostgresQueryBuilder),
r#"SELECT WHERE "id" = 1 OR "id" = 2 OR "id" = 3 OR "id" = 4"#
);
Provided methods
fn and_where(&mut self, other: SimpleExpr) -> &mut Self
fn and_where(&mut self, other: SimpleExpr) -> &mut Self
And where condition. This cannot be mixed with ConditionalStatement::or_where
.
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::tbl(Glyph::Table, Glyph::Aspect).is_in(vec![3, 4]))
.and_where(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%'"#
);
fn and_where_option(&mut self, other: Option<SimpleExpr>) -> &mut Self
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(vec![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%'"#
);
fn or_where(&mut self, other: SimpleExpr) -> &mut Self
fn or_where(&mut self, other: SimpleExpr) -> &mut Self
👎 Deprecated since 0.12.0:
Please use [ConditionalStatement::cond_where
]. Calling or_where
after and_where
will panic.
Or where condition. This cannot be mixed with ConditionalStatement::and_where
.
Calling or_where
after and_where
will panic.