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