pub struct CaseBuilder<'a, V: SQLParam, T: DataType, N: Nullability, A: AggregateKind, S = ()> { /* private fields */ }Expand description
A CASE builder with at least one branch.
T is the result type set by the first branch and N the nullability of
the results so far. S records the tables read so far: WHEN conditions
are only scope-checked (a NULL condition falls through to the next
branch), while THEN results can make the result NULL.
Implementations§
Source§impl<'a, V, T, N, A, S> CaseBuilder<'a, V, T, N, A, S>
impl<'a, V, T, N, A, S> CaseBuilder<'a, V, T, N, A, S>
Sourcepub fn when<C, R>(
self,
condition: C,
result: R,
) -> CaseBuilder<'a, V, T, <N as Nullability>::Or<R::Nullable>, <<A as AggregateKind>::Or<C::Aggregate> as AggregateKind>::Or<R::Aggregate>, (S, (ScopeOnly<C::Sources>, R::Sources))>where
C: Expr<'a, V>,
R: Expr<'a, V>,
C::SQLType: BooleanLike,
T: Compatible<R::SQLType>,
N: Nullability,
R::Nullable: Nullability,
A: AggregateKind,
C::Aggregate: AggregateKind,
R::Aggregate: AggregateKind,
pub fn when<C, R>(
self,
condition: C,
result: R,
) -> CaseBuilder<'a, V, T, <N as Nullability>::Or<R::Nullable>, <<A as AggregateKind>::Or<C::Aggregate> as AggregateKind>::Or<R::Aggregate>, (S, (ScopeOnly<C::Sources>, R::Sources))>where
C: Expr<'a, V>,
R: Expr<'a, V>,
C::SQLType: BooleanLike,
T: Compatible<R::SQLType>,
N: Nullability,
R::Nullable: Nullability,
A: AggregateKind,
C::Aggregate: AggregateKind,
R::Aggregate: AggregateKind,
Adds another WHEN condition THEN result branch.
condition must be boolean and result must have a type compatible with
the first branch. The result becomes nullable if this branch’s result is.
Sourcepub fn end(self) -> SQLExpr<'a, V, T, Null, A, S>
pub fn end(self) -> SQLExpr<'a, V, T, Null, A, S>
Finishes the expression without ELSE (... END).
Rows that match no branch give NULL, so the result is always nullable.
§Examples
let label = case().when(users.active, "active").end();
assert_eq!(label.sql(), r#"CASE WHEN "users"."active" THEN ? END"#);Sourcepub fn else<D>(
self,
default: D,
) -> SQLExpr<'a, V, T, <N as Nullability>::Or<D::Nullable>, <A as AggregateKind>::Or<D::Aggregate>, (S, D::Sources)>where
D: Expr<'a, V>,
T: Compatible<D::SQLType>,
N: Nullability,
D::Nullable: Nullability,
A: AggregateKind,
D::Aggregate: AggregateKind,
pub fn else<D>(
self,
default: D,
) -> SQLExpr<'a, V, T, <N as Nullability>::Or<D::Nullable>, <A as AggregateKind>::Or<D::Aggregate>, (S, D::Sources)>where
D: Expr<'a, V>,
T: Compatible<D::SQLType>,
N: Nullability,
D::Nullable: Nullability,
A: AggregateKind,
D::Aggregate: AggregateKind,
Finishes the expression with a default (... ELSE default END).
default must have a type compatible with the first branch. The result is
nullable if any branch result or the default is.
§Examples
let label = case().when(users.active, "active").r#else("inactive");
assert_eq!(label.sql(), r#"CASE WHEN "users"."active" THEN ? ELSE ? END"#);