qraft-core 0.1.2

Core type system, query model, decoding, and SQL lowering primitives for qraft.
Documentation
//! `exists (...)` predicates backed by scalar subqueries.

use crate::{
    Bool, Query, TypeMeta,
    expression::{Expression, Scalar},
    lower::LowerCtx,
};

/// An `exists` or `not exists` predicate over a scalar subquery.
pub struct Exists {
    query: Query,
    negated: bool,
}

#[qraft_expression_macro::as_expression]
impl Expression for Exists {
    type Type = Bool;

    fn lower(&self, ctx: &mut LowerCtx) -> usize {
        let inner = ctx.lower_subquery_ref(&self.query);
        ctx.lower_exists(self.negated, inner)
    }
}

/// Builds an `exists (...)` predicate from a scalar query.
pub fn exists<T>(query: Scalar<T>) -> Exists
where
    T: TypeMeta,
{
    Exists {
        query: query.inner,
        negated: false,
    }
}

/// Builds a `not exists (...)` predicate from a scalar query.
pub fn not_exists<T>(query: Scalar<T>) -> Exists
where
    T: TypeMeta,
{
    Exists {
        query: query.inner,
        negated: true,
    }
}