drizzle_core/
param.rs

1mod owned;
2pub use owned::*;
3
4use crate::prelude::*;
5use crate::{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 fn new(placeholder: Placeholder, value: Option<Cow<'a, V>>) -> Self {
19        Self { placeholder, value }
20    }
21}
22
23impl<'a, V: SQLParam> From<OwnedParam<V>> for Param<'a, 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<'a, V: SQLParam> From<Placeholder> for Param<'a, V> {
42    fn from(value: Placeholder) -> Self {
43        Self {
44            placeholder: value,
45            value: None,
46        }
47    }
48}
49
50impl<'a, T: SQLParam> Param<'a, T> {
51    /// Creates a new parameter with a positional placeholder
52    pub const fn positional(value: T) -> Self {
53        Self {
54            placeholder: Placeholder::positional(),
55            value: Some(Cow::Owned(value)),
56        }
57    }
58
59    /// Creates a new parameter with a specific placeholder and no value
60    pub const fn from_placeholder(placeholder: Placeholder) -> Self {
61        Self {
62            placeholder,
63            value: None,
64        }
65    }
66
67    /// Creates a new parameter with a named placeholder (colon style)
68    pub const fn named(name: &'static str, value: T) -> Self {
69        Self {
70            placeholder: Placeholder::colon(name),
71            value: Some(Cow::Owned(value)),
72        }
73    }
74
75    /// Creates a new parameter with a specific placeholder
76    pub const fn with_placeholder(placeholder: Placeholder, value: T) -> Self {
77        Self {
78            placeholder,
79            value: Some(Cow::Owned(value)),
80        }
81    }
82}
83
84#[derive(Debug, Clone)]
85pub struct ParamBind<'a, V: SQLParam> {
86    pub name: &'a str,
87    pub value: V,
88}
89
90impl<'a, V: SQLParam> ParamBind<'a, V> {
91    pub const fn new(name: &'a str, value: V) -> Self {
92        Self { name, value }
93    }
94}