Skip to main content

drizzle_core/param/
mod.rs

1mod owned;
2pub use owned::*;
3
4use crate::prelude::*;
5use crate::{placeholder::Placeholder, traits::SQLParam};
6
7/// A SQL parameter that associates a value with a placeholder.
8/// Designed to be const-friendly and zero-cost when possible.
9#[derive(Debug, Clone)]
10pub struct Param<'a, V: SQLParam> {
11    /// The placeholder to use in the SQL
12    pub placeholder: Placeholder,
13    /// The value to bind
14    pub value: Option<Cow<'a, V>>,
15}
16
17impl<'a, V: SQLParam> Param<'a, V> {
18    pub const fn new(placeholder: Placeholder, value: Option<Cow<'a, V>>) -> Self {
19        Self { placeholder, value }
20    }
21}
22
23impl<V: SQLParam> From<OwnedParam<V>> for Param<'_, V> {
24    fn from(value: OwnedParam<V>) -> Self {
25        Self {
26            placeholder: value.placeholder,
27            value: value.value.map(|v| Cow::Owned(v)),
28        }
29    }
30}
31
32impl<'a, V: SQLParam> From<&'a OwnedParam<V>> for Param<'a, V> {
33    fn from(value: &'a OwnedParam<V>) -> Self {
34        Self {
35            placeholder: value.placeholder,
36            value: value.value.as_ref().map(|v| Cow::Borrowed(v)),
37        }
38    }
39}
40
41impl<V: SQLParam> From<Placeholder> for Param<'_, V> {
42    fn from(value: Placeholder) -> Self {
43        Self {
44            placeholder: value,
45            value: None,
46        }
47    }
48}
49
50impl<T: SQLParam> Param<'_, T> {
51    /// Creates a new parameter with a positional placeholder
52    pub const fn positional(value: T) -> Self {
53        Self {
54            placeholder: Placeholder::anonymous(),
55            value: Some(Cow::Owned(value)),
56        }
57    }
58
59    /// Creates a new parameter with a specific placeholder and no value
60    #[must_use]
61    pub const fn from_placeholder(placeholder: Placeholder) -> Self {
62        Self {
63            placeholder,
64            value: None,
65        }
66    }
67
68    /// Creates a new parameter with a named placeholder
69    pub const fn named(name: &'static str, value: T) -> Self {
70        Self {
71            placeholder: Placeholder::named(name),
72            value: Some(Cow::Owned(value)),
73        }
74    }
75
76    /// Creates a new parameter with a specific placeholder
77    pub const fn with_placeholder(placeholder: Placeholder, value: T) -> Self {
78        Self {
79            placeholder,
80            value: Some(Cow::Owned(value)),
81        }
82    }
83}
84
85#[derive(Debug, Clone)]
86pub struct ParamBind<'a, V: SQLParam> {
87    pub name: &'a str,
88    pub value: V,
89}
90
91impl<'a, V: SQLParam> ParamBind<'a, V> {
92    pub const fn new(name: &'a str, value: V) -> Self {
93        Self { name, value }
94    }
95
96    /// Creates a new positional parameter binding.
97    pub const fn positional(value: V) -> Self {
98        Self { name: "", value }
99    }
100}
101
102/// A typed collection of parameter bindings.
103#[derive(Debug, Clone)]
104pub struct ParamSet<'a, V: SQLParam, const N: usize> {
105    binds: [ParamBind<'a, V>; N],
106}
107
108impl<'a, V: SQLParam, const N: usize> ParamSet<'a, V, N> {
109    pub const fn new(binds: [ParamBind<'a, V>; N]) -> Self {
110        Self { binds }
111    }
112}
113
114impl<'a, V: SQLParam, const N: usize> From<[ParamBind<'a, V>; N]> for ParamSet<'a, V, N> {
115    fn from(value: [ParamBind<'a, V>; N]) -> Self {
116        Self::new(value)
117    }
118}
119
120impl<'a, V: SQLParam, const N: usize> IntoIterator for ParamSet<'a, V, N> {
121    type Item = ParamBind<'a, V>;
122    type IntoIter = core::array::IntoIter<ParamBind<'a, V>, N>;
123
124    fn into_iter(self) -> Self::IntoIter {
125        self.binds.into_iter()
126    }
127}