Skip to main content

drizzle_core/traits/
param.rs

1use crate::dialect::Dialect;
2
3/// A driver's value type: what bound parameters hold (`SQLiteValue`,
4/// `PostgresValue`, `MySQLValue`, ...).
5///
6/// Every [`SQL`](crate::SQL) fragment is generic over one, and it fixes the
7/// dialect: placeholder syntax, identifier quoting, and type mappings.
8#[diagnostic::on_unimplemented(
9    message = "`{Self}` is not a SQL parameter type",
10    label = "use a dialect-specific value type (e.g., SQLiteValue, PostgresValue)"
11)]
12pub trait SQLParam: Clone + core::fmt::Debug {
13    /// The dialect, at runtime.
14    const DIALECT: Dialect;
15
16    /// The dialect, as a type ([`SQLiteDialect`](crate::SQLiteDialect),
17    /// [`PostgresDialect`](crate::PostgresDialect) or
18    /// [`MySQLDialect`](crate::MySQLDialect)).
19    ///
20    /// Selects type mappings ([`SQLTypeToRust`](crate::row::SQLTypeToRust),
21    /// [`DialectTypes`](crate::dialect::DialectTypes)) and dialect-only
22    /// features ([`DialectSupports`](crate::DialectSupports)).
23    type DialectMarker: crate::dialect::DialectTypes;
24
25    /// Converts a `LIMIT`/`OFFSET` value into a parameter, or returns `None`
26    /// to write it as a literal (the default).
27    ///
28    /// Binding keeps the SQL text the same across pages, so a cached
29    /// prepared statement can be reused.
30    #[inline]
31    #[must_use]
32    fn pagination_param(value: usize) -> Option<Self> {
33        let _ = value;
34        None
35    }
36
37    /// Appends this value to `buf` as a SQL literal of this dialect.
38    ///
39    /// Used where a statement cannot take bound parameters, such as the body
40    /// of a `CREATE VIEW`. Returns `false`, having written nothing, when the
41    /// value has no literal form; the default knows none.
42    #[inline]
43    fn write_literal(&self, buf: &mut crate::prelude::String) -> bool {
44        let _ = buf;
45        false
46    }
47}
48
49// Implement SQLParam for common types
50// impl<T: SQLParam> SQLParam for Option<T> {}
51// impl<T: SQLParam> SQLParam for Vec<T> {}
52// impl<T: SQLParam> SQLParam for Box<[T]> {}
53// impl<T: SQLParam> SQLParam for Rc<T> {}
54// impl<T: SQLParam> SQLParam for Arc<T> {}
55// impl<T: SQLParam> SQLParam for RefCell<T> {}
56// impl<'a, T: SQLParam> SQLParam for Cow<'a, T> {}
57// impl<T: SQLParam> SQLParam for &[T] {}
58// impl<T: SQLParam> SQLParam for &T {}
59// impl<const N: usize, T: SQLParam> SQLParam for [T; N] {}