toasty_core/stmt/
expr_exists.rs

1use crate::stmt::{Expr, Query};
2
3/// Tests whether a subquery returns any rows.
4///
5/// Returns `true` if the subquery produces at least one row.
6///
7/// # Examples
8///
9/// ```text
10/// exists(subquery)      // returns `true` if subquery has results
11/// not_exists(subquery)  // returns `true` if subquery has no results
12/// ```
13#[derive(Debug, Clone, PartialEq)]
14pub struct ExprExists {
15    /// The subquery to check.
16    pub subquery: Box<Query>,
17}
18
19impl Expr {
20    pub fn exists(subquery: impl Into<Query>) -> Expr {
21        Expr::Exists(ExprExists {
22            subquery: Box::new(subquery.into()),
23        })
24    }
25
26    pub fn not_exists(subquery: impl Into<Query>) -> Expr {
27        Expr::not(Expr::exists(subquery))
28    }
29}
30
31impl From<ExprExists> for Expr {
32    fn from(value: ExprExists) -> Self {
33        Self::Exists(value)
34    }
35}