toasty_core/stmt/expr_is_null.rs
1use super::Expr;
2
3/// Tests whether an expression is null.
4///
5/// Returns `true` if the expression evaluates to null, or the opposite when
6/// `negated` is set. The flag makes `IS NOT NULL` a predicate in its own
7/// right rather than the negation of `IS NULL`: statement normalization
8/// attaches variant guards to the predicate before expanding the negated
9/// form to `not(is_null(..))`, so the guards end up outside the negation.
10///
11/// # Examples
12///
13/// ```text
14/// is_null(x) // returns `true` if x is null
15/// is_not_null(x) // returns `true` if x is not null
16/// ```
17#[derive(Debug, Clone, PartialEq)]
18pub struct ExprIsNull {
19 /// The expression to check for null.
20 pub expr: Box<Expr>,
21
22 /// Whether the check is `IS NOT NULL`.
23 pub negated: bool,
24}
25
26impl Expr {
27 /// Creates an `IS NULL` expression.
28 pub fn is_null(expr: impl Into<Self>) -> Self {
29 ExprIsNull {
30 expr: Box::new(expr.into()),
31 negated: false,
32 }
33 .into()
34 }
35
36 /// Creates an `IS NOT NULL` expression.
37 pub fn is_not_null(expr: impl Into<Self>) -> Self {
38 ExprIsNull {
39 expr: Box::new(expr.into()),
40 negated: true,
41 }
42 .into()
43 }
44}
45
46impl From<ExprIsNull> for Expr {
47 fn from(value: ExprIsNull) -> Self {
48 Self::IsNull(value)
49 }
50}