drizzle-core 0.2.0

A type-safe SQL query builder for Rust
Documentation
use crate::dialect::Dialect;

/// A marker trait for types that can be used as SQL parameters.
///
/// This trait is used as a bound on the parameter type in SQL fragments.
/// It ensures type safety when building SQL queries with parameters.
#[diagnostic::on_unimplemented(
    message = "`{Self}` is not a SQL parameter type",
    label = "use a dialect-specific value type (e.g., SQLiteValue, PostgresValue)"
)]
pub trait SQLParam: Clone + core::fmt::Debug {
    /// The SQL dialect for this parameter type
    const DIALECT: Dialect;

    /// Type-level dialect marker for compile-time dispatch.
    ///
    /// Used by [`crate::row::SQLTypeToRust`] to select dialect-specific type mappings
    /// and by [`crate::dialect::DialectTypes`] to resolve conceptual SQL types to
    /// dialect-native markers.
    type DialectMarker: crate::dialect::DialectTypes;

    /// Converts a numeric `LIMIT`/`OFFSET` value into a bindable parameter.
    ///
    /// Dialects that return `Some` render `.limit(n)` / `.offset(n)` as a
    /// bound parameter instead of a numeric literal, keeping the generated
    /// SQL text stable across pagination values so statement caches can hit.
    /// The default (`None`) keeps the numeric-literal rendering.
    #[inline]
    #[must_use]
    fn pagination_param(value: usize) -> Option<Self> {
        let _ = value;
        None
    }

    /// Appends this value to `buf` as a SQL literal of this dialect.
    ///
    /// Used where a statement cannot take bound parameters, such as the body
    /// of a `CREATE VIEW`. Returns `false`, having written nothing, when the
    /// value has no literal form; the default knows none.
    #[inline]
    fn write_literal(&self, buf: &mut crate::prelude::String) -> bool {
        let _ = buf;
        false
    }
}

// Implement SQLParam for common types
// impl<T: SQLParam> SQLParam for Option<T> {}
// impl<T: SQLParam> SQLParam for Vec<T> {}
// impl<T: SQLParam> SQLParam for Box<[T]> {}
// impl<T: SQLParam> SQLParam for Rc<T> {}
// impl<T: SQLParam> SQLParam for Arc<T> {}
// impl<T: SQLParam> SQLParam for RefCell<T> {}
// impl<'a, T: SQLParam> SQLParam for Cow<'a, T> {}
// impl<T: SQLParam> SQLParam for &[T] {}
// impl<T: SQLParam> SQLParam for &T {}
// impl<const N: usize, T: SQLParam> SQLParam for [T; N] {}