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    /// Creates an `IN (subquery)` expression: `lhs IN (SELECT ...)`.
25    pub fn in_subquery(lhs: impl Into<Self>, rhs: impl Into<Query>) -> Self {
26        ExprInSubquery {
27            expr: Box::new(lhs.into()),
28            query: Box::new(rhs.into()),
29        }
30        .into()
31    }
32
33    /// Returns `true` if this expression is an `IN (subquery)` check.
34    pub fn is_in_subquery(&self) -> bool {
35        matches!(self, Self::InSubquery(_))
36    }
37}
38
39impl Node for ExprInSubquery {
40    fn visit<V: Visit>(&self, mut visit: V) {
41        visit.visit_expr_in_subquery(self);
42    }
43
44    fn visit_mut<V: VisitMut>(&mut self, mut visit: V) {
45        visit.visit_expr_in_subquery_mut(self);
46    }
47}
48
49impl From<ExprInSubquery> for Expr {
50    fn from(value: ExprInSubquery) -> Self {
51        Self::InSubquery(value)
52    }
53}