pub struct Condition { /* private fields */ }Expand description
Represents the value of an Condition::any or Condition::all: a set of disjunctive or conjunctive conditions.
Implementations§
Source§impl Condition
impl Condition
Sourcepub fn is_trivially_true(&self) -> bool
pub fn is_trivially_true(&self) -> bool
Whether the condition holds for every row, whatever the row holds:
it folds to TRUE, as an empty Condition::all does, or x NOT IN (),
or an OR with a TRUE branch.
A condition that only might hold everywhere (col = col) is not
trivially true; what is not a constant is left unknown.
use sql_builder::*;
assert!(Cond::all().is_trivially_true());
assert!(Cond::any().add(Expr::col("id").eq(1)).add(Expr::val(true)).is_trivially_true());
assert!(!Cond::all().add(Expr::col("id").eq(1)).is_trivially_true());Source§impl Condition
impl Condition
Sourcepub fn add<C>(self, condition: C) -> Self
pub fn add<C>(self, condition: C) -> Self
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 ".
use sql_builder::*;
let statement = Query::select()
.column("id")
.from("glyph")
.cond_where(
Cond::all()
.add(Expr::col("aspect").eq(0).into_condition().not())
.add(Expr::col("id").eq(0).into_condition().not()),
)
.to_string(PostgresQueryBuilder).unwrap();
assert_eq!(
statement,
r#"SELECT "id" FROM "glyph" WHERE (NOT "aspect" = 0) AND (NOT "id" = 0)"#
);Sourcepub fn add_option<C>(self, other: Option<C>) -> Self
pub fn add_option<C>(self, other: Option<C>) -> Self
Add an optional condition to the set.
Shorthand for if o.is_some() { self.add(o) }
§Examples
use sql_builder::*;
let query = Query::select()
.column("image")
.from("glyph")
.cond_where(
Cond::all()
.add_option(Some(Expr::col(("glyph", "image")).like("A%")))
.add_option(None::<Expr>),
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
r#"SELECT `image` FROM `glyph` WHERE `glyph`.`image` LIKE 'A%'"#
);Sourcepub fn any() -> Condition
pub fn any() -> Condition
Create a condition that is true if any of the conditions is true.
§Examples
use sql_builder::*;
let query = Query::select()
.column("image")
.from("glyph")
.cond_where(
Cond::any()
.add(Expr::col(("glyph", "aspect")).is_in([3, 4]))
.add(Expr::col(("glyph", "image")).like("A%"))
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
r#"SELECT `image` FROM `glyph` WHERE `glyph`.`aspect` IN (3, 4) OR `glyph`.`image` LIKE 'A%'"#
);Sourcepub fn all() -> Condition
pub fn all() -> Condition
Create a condition that is false if any of the conditions is false.
§Examples
use sql_builder::*;
let query = Query::select()
.column("image")
.from("glyph")
.cond_where(
Cond::all()
.add(Expr::col(("glyph", "aspect")).is_in([3, 4]))
.add(Expr::col(("glyph", "image")).like("A%"))
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
r#"SELECT `image` FROM `glyph` WHERE `glyph`.`aspect` IN (3, 4) AND `glyph`.`image` LIKE 'A%'"#
);Sourcepub fn not(self) -> Self
pub fn not(self) -> Self
Negates a condition.
§Examples
use sql_builder::*;
let query = Query::select()
.column("image")
.from("glyph")
.cond_where(
Cond::all()
.not()
.add(Expr::col(("glyph", "aspect")).is_in([3, 4]))
.add(Expr::col(("glyph", "image")).like("A%"))
)
.to_owned();
assert_eq!(
query.to_string(MysqlQueryBuilder).unwrap(),
r#"SELECT `image` FROM `glyph` WHERE NOT (`glyph`.`aspect` IN (3, 4) AND `glyph`.`image` LIKE 'A%')"#
);§More Examples
use sql_builder::*;
let query = Query::select()
.column("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).unwrap(),
r#"SELECT `id` WHERE (NOT (1 = 1 AND 2 = 2)) AND (3 = 3 OR 4 = 4)"#
);Trait Implementations§
impl StructuralPartialEq for Condition
Auto Trait Implementations§
impl Forget for Condition
impl Freeze for Condition
impl Move for Condition
impl RefUnwindSafe for Condition
impl Send for Condition
impl Sync for Condition
impl Unpin for Condition
impl UnsafeUnpin for Condition
impl UnwindSafe for Condition
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ExprTrait for T
impl<T> ExprTrait for T
Source§fn as_enum<N>(self, type_name: N) -> Exprwhere
N: IntoIden,
fn as_enum<N>(self, type_name: N) -> Exprwhere
N: IntoIden,
AS enum expression. Read moreSource§fn cast_as<N>(self, type_name: N) -> Exprwhere
N: IntoIden,
fn cast_as<N>(self, type_name: N) -> Exprwhere
N: IntoIden,
CAST AS expression. Read moreSource§fn count_distinct(self) -> Expr
fn count_distinct(self) -> Expr
COUNT function with the DISTINCT modifier. Read moreSource§fn equals<C>(self, col: C) -> Exprwhere
C: IntoColumnRef,
fn equals<C>(self, col: C) -> Exprwhere
C: IntoColumnRef,
Source§fn in_subquery(self, sel: SelectStatement) -> Expr
fn in_subquery(self, sel: SelectStatement) -> Expr
IN sub-query expression. Read moreSource§fn in_tuples<V, I>(self, v: I) -> Exprwhere
V: IntoValueTuple,
I: IntoIterator<Item = V>,
fn in_tuples<V, I>(self, v: I) -> Exprwhere
V: IntoValueTuple,
I: IntoIterator<Item = V>,
IN sub expression. Read moreSource§fn is_not_null(self) -> Expr
fn is_not_null(self) -> Expr
IS NOT NULL expression. Read moreSource§fn left_shift<R>(self, right: R) -> Expr
fn left_shift<R>(self, right: R) -> Expr
Source§fn not_between<A, B>(self, a: A, b: B) -> Expr
fn not_between<A, B>(self, a: A, b: B) -> Expr
NOT BETWEEN expression. Read moreSource§fn not_equals<C>(self, col: C) -> Exprwhere
C: IntoColumnRef,
fn not_equals<C>(self, col: C) -> Exprwhere
C: IntoColumnRef,
Source§fn not_in_subquery(self, sel: SelectStatement) -> Expr
fn not_in_subquery(self, sel: SelectStatement) -> Expr
NOT IN sub-query expression. Read moreSource§fn not_like<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
fn not_like<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
NOT LIKE expression. Read moreSource§fn right_shift<R>(self, right: R) -> Expr
fn right_shift<R>(self, right: R) -> Expr
Source§impl<T> IntoCondition for T
impl<T> IntoCondition for T
fn into_condition(self) -> Condition
Source§impl<T> PgExpr for Twhere
T: ExprTrait,
impl<T> PgExpr for Twhere
T: ExprTrait,
Source§fn concatenate<T>(self, right: T) -> Expr
fn concatenate<T>(self, right: T) -> Expr
||) expression. Read moreSource§fn matches<T>(self, expr: T) -> Expr
fn matches<T>(self, expr: T) -> Expr
@@) expression. Read moreSource§fn contains<T>(self, expr: T) -> Expr
fn contains<T>(self, expr: T) -> Expr
@>) expression. Read moreSource§fn contained<T>(self, expr: T) -> Expr
fn contained<T>(self, expr: T) -> Expr
<@) expression. Read moreSource§fn ilike<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
fn ilike<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
ILIKE expression. Read moreSource§fn not_ilike<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
fn not_ilike<L>(self, like: L) -> Exprwhere
L: IntoLikeExpr,
NOT ILIKE expressionSource§fn get_json_field<T>(self, right: T) -> Expr
fn get_json_field<T>(self, right: T) -> Expr
->). Read moreSource§fn cast_json_field<T>(self, right: T) -> Expr
fn cast_json_field<T>(self, right: T) -> Expr
->>). Read more