qraft-core 0.1.2

Core type system, query model, decoding, and SQL lowering primitives for qraft.
Documentation
//! Bound parameter expressions and literal value metadata.

use std::{borrow::Cow, marker::PhantomData};

use crate::{expression::Expression, lower::LowerCtx, param::ParamValue, ty::TypeMeta};

/// Runtime type tags used for bound literals and `NULL` values.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TypeId {
    Bool,
    Integer,
    BigInt,
    UInt,
    UBigInt,
    Float,
    Double,
    Blob,
    Text,
}

/// Literal value variants accepted by `Bound`.
#[derive(Debug, Clone)]
pub enum Value<'a> {
    Bool(bool),
    Integer(i32),
    BigInt(i64),
    UInt(u32),
    UBigInt(u64),
    Float(f32),
    Double(f64),
    Blob(Cow<'a, [u8]>),
    Text(Cow<'a, str>),
    Null(TypeId),
}

/// A typed bound parameter expression.
pub struct Bound<'e, T: TypeMeta> {
    /// Parameter payload stored in the query's parameter buffer.
    pub value: ParamValue<'e>,
    marker: PhantomData<T>,
}

impl<'e, T: TypeMeta> Bound<'e, T> {
    /// Wraps a parameter value in a typed expression.
    pub fn new(value: ParamValue<'e>) -> Self {
        Self {
            value,
            marker: PhantomData,
        }
    }
}

#[qraft_expression_macro::as_expression]
impl<'e, T: TypeMeta> Expression for Bound<'e, T> {
    type Type = T;

    fn lower(&self, ctx: &mut LowerCtx) -> usize {
        let param = self.value.clone().into_param(ctx.data);
        ctx.lower_param(param)
    }
}