toasty_core/stmt/
expr_in_subquery.rs

1use crate::stmt::{Node, Visit, VisitMut};
2
3use super::{Expr, Query};
4
5/// Tests whether a value is in the results of a subquery.
6///
7/// Returns `true` if `expr` matches any row returned by `query`.
8///
9/// # Examples
10///
11/// ```text
12/// in_subquery(x, select(...))  // returns `true` if `x` is in the subquery results
13/// ```
14#[derive(Debug, Clone, PartialEq)]
15pub struct ExprInSubquery {
16    /// The value to search for.
17    pub expr: Box<Expr>,
18
19    /// The subquery to search within.
20    pub query: Box<Query>,
21}
22
23impl Expr {
24    pub fn in_subquery(lhs: impl Into<Self>, rhs: impl Into<Query>) -> Self {
25        ExprInSubquery {
26            expr: Box::new(lhs.into()),
27            query: Box::new(rhs.into()),
28        }
29        .into()
30    }
31
32    pub fn is_in_subquery(&self) -> bool {
33        matches!(self, Self::InSubquery(_))
34    }
35}
36
37impl Node for ExprInSubquery {
38    fn visit<V: Visit>(&self, mut visit: V) {
39        visit.visit_expr_in_subquery(self);
40    }
41
42    fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
43        visit.visit_expr_in_subquery_mut(self);
44    }
45}
46
47impl From<ExprInSubquery> for Expr {
48    fn from(value: ExprInSubquery) -> Self {
49        Self::InSubquery(value)
50    }
51}