Skip to main content

drizzle_core/traits/
param.rs

1use crate::dialect::Dialect;
2
3/// A marker trait for types that can be used as SQL parameters.
4///
5/// This trait is used as a bound on the parameter type in SQL fragments.
6/// It ensures type safety when building SQL queries with parameters.
7#[diagnostic::on_unimplemented(
8    message = "`{Self}` is not a SQL parameter type",
9    label = "use a dialect-specific value type (e.g., SQLiteValue, PostgresValue)"
10)]
11pub trait SQLParam: Clone + core::fmt::Debug {
12    /// The SQL dialect for this parameter type
13    const DIALECT: Dialect;
14
15    /// Type-level dialect marker for compile-time dispatch.
16    ///
17    /// Used by [`crate::row::SQLTypeToRust`] to select dialect-specific type mappings
18    /// and by [`crate::dialect::DialectTypes`] to resolve conceptual SQL types to
19    /// dialect-native markers.
20    type DialectMarker: crate::dialect::DialectTypes;
21
22    /// Converts a numeric `LIMIT`/`OFFSET` value into a bindable parameter.
23    ///
24    /// Dialects that return `Some` render `.limit(n)` / `.offset(n)` as a
25    /// bound parameter instead of a numeric literal, keeping the generated
26    /// SQL text stable across pagination values so statement caches can hit.
27    /// The default (`None`) keeps the numeric-literal rendering.
28    #[inline]
29    #[must_use]
30    fn pagination_param(value: usize) -> Option<Self> {
31        let _ = value;
32        None
33    }
34}
35
36// Implement SQLParam for common types
37// impl<T: SQLParam> SQLParam for Option<T> {}
38// impl<T: SQLParam> SQLParam for Vec<T> {}
39// impl<T: SQLParam> SQLParam for Box<[T]> {}
40// impl<T: SQLParam> SQLParam for Rc<T> {}
41// impl<T: SQLParam> SQLParam for Arc<T> {}
42// impl<T: SQLParam> SQLParam for RefCell<T> {}
43// impl<'a, T: SQLParam> SQLParam for Cow<'a, T> {}
44// impl<T: SQLParam> SQLParam for &[T] {}
45// impl<T: SQLParam> SQLParam for &T {}
46// impl<const N: usize, T: SQLParam> SQLParam for [T; N] {}