linq_rs_sql 0.1.0

A typed SQL query builder — compile-time checked columns, bound parameters, no driver and no dependencies
Documentation
//! [`Column`] trait + per-SQL-type operator extension traits.
//!
//! Operators are grouped by which `SqlType` they apply to:
//!
//! - [`ExprExt`]  — universal: `.eq`, `.ne`, `.is_null`
//! - [`IntOps`]   — `Integer`: `.lt`, `.lte`, `.gt`, `.gte`
//! - [`FloatOps`] — `Float`:   `.lt`, `.lte`, `.gt`, `.gte`
//! - [`TextOps`]  — `Text`:    `.lt`, `.lte`, `.gt`, `.gte`, `.like`
//! - [`BoolOps`]  — `Boolean`: `.and`, `.or`
//! - free function [`crate::not`] — `Boolean`: `NOT expr`
//!
//! All extension traits have blanket impls keyed on `Expr<SqlType = ...>`,
//! so column types, primitive literals, and intermediate expression nodes
//! all pick up the same operators automatically.

use crate::expr::{And, Eq, Expr, Gt, GtEq, IsNull, Like, Lt, LtEq, NotEq, Or};
use crate::types::{Boolean, Float, Integer, Text};

/// A SQL column. The zero-sized types generated by [`crate::table!`]
/// implement this trait.
///
/// Implementors must also implement [`Expr`] — that's where the column
/// declares its SQL type (`Integer`, `Text`, …) and how it writes itself
/// (`sql.push_str(C::NAME)`).
pub trait Column: Expr + Sized {
    /// The table marker type this column belongs to. Used by
    /// [`Selection`](crate::Selection) to ensure all selected columns come from
    /// the same table.
    type Table;
    /// The SQL identifier (column name) for this column.
    const NAME: &'static str;
}

// ═══════════════════════════════════════════════════════════════════════════
// Universal: ExprExt
// ═══════════════════════════════════════════════════════════════════════════

/// Equality and NULL-test operators applicable to expressions of any
/// `SqlType`. Blanket-implemented for every `Expr + Sized`.
pub trait ExprExt: Expr + Sized {
    /// `self = rhs`. RHS must have the same `SqlType` as `self`.
    fn eq<R>(self, rhs: R) -> Eq<Self, R>
    where
        R: Expr<SqlType = Self::SqlType>,
    {
        Eq {
            left: self,
            right: rhs,
        }
    }

    /// `self != rhs`.
    fn ne<R>(self, rhs: R) -> NotEq<Self, R>
    where
        R: Expr<SqlType = Self::SqlType>,
    {
        NotEq {
            left: self,
            right: rhs,
        }
    }

    /// `self IS NULL`.
    ///
    /// Consumes `self` like every other builder method here; clippy's
    /// `wrong_self_convention` lint expects `is_*` methods to take `&self`
    /// or `&mut self`, but the consumer-of-self convention is consistent
    /// across this trait.
    #[allow(clippy::wrong_self_convention)]
    fn is_null(self) -> IsNull<Self> {
        IsNull { inner: self }
    }
}

impl<E: Expr + Sized> ExprExt for E {}

// ═══════════════════════════════════════════════════════════════════════════
// IntOps — ordering on Integer
// ═══════════════════════════════════════════════════════════════════════════

/// Ordering operators for `Integer` expressions.
pub trait IntOps: Expr<SqlType = Integer> + Sized {
    /// `self < rhs`.
    fn lt<R: Expr<SqlType = Integer>>(self, rhs: R) -> Lt<Self, R> {
        Lt {
            left: self,
            right: rhs,
        }
    }
    /// `self <= rhs`.
    fn lte<R: Expr<SqlType = Integer>>(self, rhs: R) -> LtEq<Self, R> {
        LtEq {
            left: self,
            right: rhs,
        }
    }
    /// `self > rhs`.
    fn gt<R: Expr<SqlType = Integer>>(self, rhs: R) -> Gt<Self, R> {
        Gt {
            left: self,
            right: rhs,
        }
    }
    /// `self >= rhs`.
    fn gte<R: Expr<SqlType = Integer>>(self, rhs: R) -> GtEq<Self, R> {
        GtEq {
            left: self,
            right: rhs,
        }
    }
}
impl<E> IntOps for E where E: Expr<SqlType = Integer> + Sized {}

// ═══════════════════════════════════════════════════════════════════════════
// FloatOps — ordering on Float
// ═══════════════════════════════════════════════════════════════════════════

/// Ordering operators for `Float` expressions.
pub trait FloatOps: Expr<SqlType = Float> + Sized {
    /// `self < rhs`.
    fn lt<R: Expr<SqlType = Float>>(self, rhs: R) -> Lt<Self, R> {
        Lt {
            left: self,
            right: rhs,
        }
    }
    /// `self <= rhs`.
    fn lte<R: Expr<SqlType = Float>>(self, rhs: R) -> LtEq<Self, R> {
        LtEq {
            left: self,
            right: rhs,
        }
    }
    /// `self > rhs`.
    fn gt<R: Expr<SqlType = Float>>(self, rhs: R) -> Gt<Self, R> {
        Gt {
            left: self,
            right: rhs,
        }
    }
    /// `self >= rhs`.
    fn gte<R: Expr<SqlType = Float>>(self, rhs: R) -> GtEq<Self, R> {
        GtEq {
            left: self,
            right: rhs,
        }
    }
}
impl<E> FloatOps for E where E: Expr<SqlType = Float> + Sized {}

// ═══════════════════════════════════════════════════════════════════════════
// TextOps — lexicographic ordering + LIKE
// ═══════════════════════════════════════════════════════════════════════════

/// Text-specific operators: lexicographic comparison and `LIKE`.
pub trait TextOps: Expr<SqlType = Text> + Sized {
    /// `self < rhs` — lexicographic.
    fn lt<R: Expr<SqlType = Text>>(self, rhs: R) -> Lt<Self, R> {
        Lt {
            left: self,
            right: rhs,
        }
    }
    /// `self <= rhs` — lexicographic.
    fn lte<R: Expr<SqlType = Text>>(self, rhs: R) -> LtEq<Self, R> {
        LtEq {
            left: self,
            right: rhs,
        }
    }
    /// `self > rhs` — lexicographic.
    fn gt<R: Expr<SqlType = Text>>(self, rhs: R) -> Gt<Self, R> {
        Gt {
            left: self,
            right: rhs,
        }
    }
    /// `self >= rhs` — lexicographic.
    fn gte<R: Expr<SqlType = Text>>(self, rhs: R) -> GtEq<Self, R> {
        GtEq {
            left: self,
            right: rhs,
        }
    }
    /// `self LIKE pattern` — SQL `%`/`_` glob match.
    fn like<R: Expr<SqlType = Text>>(self, pattern: R) -> Like<Self, R> {
        Like {
            left: self,
            right: pattern,
        }
    }
}
impl<E> TextOps for E where E: Expr<SqlType = Text> + Sized {}

// ═══════════════════════════════════════════════════════════════════════════
// BoolOps — logical AND / OR
// ═══════════════════════════════════════════════════════════════════════════

/// Logical AND / OR for `Boolean` expressions. (NOT lives at the module
/// level as the free function [`crate::not`].)
pub trait BoolOps: Expr<SqlType = Boolean> + Sized {
    /// `self AND rhs`.
    fn and<R: Expr<SqlType = Boolean>>(self, rhs: R) -> And<Self, R> {
        And {
            left: self,
            right: rhs,
        }
    }
    /// `self OR rhs`.
    fn or<R: Expr<SqlType = Boolean>>(self, rhs: R) -> Or<Self, R> {
        Or {
            left: self,
            right: rhs,
        }
    }
}
impl<E> BoolOps for E where E: Expr<SqlType = Boolean> + Sized {}