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 /// Creates an `EXISTS(subquery)` expression.
21 pub fn exists(subquery: impl Into<Query>) -> Expr {
22 Expr::Exists(ExprExists {
23 subquery: Box::new(subquery.into()),
24 })
25 }
26
27 /// Creates a `NOT EXISTS(subquery)` expression.
28 pub fn not_exists(subquery: impl Into<Query>) -> Expr {
29 Expr::not(Expr::exists(subquery))
30 }
31}
32
33impl From<ExprExists> for Expr {
34 fn from(value: ExprExists) -> Self {
35 Self::Exists(value)
36 }
37}