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 /// Appends this value to `buf` as a SQL literal of this dialect.
36 ///
37 /// Used where a statement cannot take bound parameters, such as the body
38 /// of a `CREATE VIEW`. Returns `false`, having written nothing, when the
39 /// value has no literal form; the default knows none.
40 #[inline]
41 fn write_literal(&self, buf: &mut crate::prelude::String) -> bool {
42 let _ = buf;
43 false
44 }
45}
46
47// Implement SQLParam for common types
48// impl<T: SQLParam> SQLParam for Option<T> {}
49// impl<T: SQLParam> SQLParam for Vec<T> {}
50// impl<T: SQLParam> SQLParam for Box<[T]> {}
51// impl<T: SQLParam> SQLParam for Rc<T> {}
52// impl<T: SQLParam> SQLParam for Arc<T> {}
53// impl<T: SQLParam> SQLParam for RefCell<T> {}
54// impl<'a, T: SQLParam> SQLParam for Cow<'a, T> {}
55// impl<T: SQLParam> SQLParam for &[T] {}
56// impl<T: SQLParam> SQLParam for &T {}
57// impl<const N: usize, T: SQLParam> SQLParam for [T; N] {}