drizzle_core/
conversions.rs

1use crate::prelude::*;
2use crate::{
3    Token,
4    sql::SQL,
5    traits::{SQLColumnInfo, SQLParam, SQLTableInfo},
6};
7
8#[cfg(feature = "uuid")]
9use uuid::Uuid;
10
11pub trait ToSQL<'a, V: SQLParam> {
12    fn to_sql(&self) -> SQL<'a, V>;
13    fn alias(&self, alias: &'static str) -> SQL<'a, V> {
14        self.to_sql().alias(alias)
15    }
16}
17
18impl<'a, T, V> From<&T> for SQL<'a, V>
19where
20    T: ToSQL<'a, V>,
21    V: SQLParam,
22{
23    fn from(value: &T) -> Self {
24        value.to_sql()
25    }
26}
27
28impl<'a, V: SQLParam, T> ToSQL<'a, V> for &T
29where
30    T: ToSQL<'a, V>,
31{
32    fn to_sql(&self) -> SQL<'a, V> {
33        (**self).to_sql()
34    }
35}
36
37impl<'a, V: SQLParam + 'a> ToSQL<'a, V> for () {
38    fn to_sql(&self) -> SQL<'a, V> {
39        SQL::empty()
40    }
41}
42
43impl<'a, V, T> ToSQL<'a, V> for Vec<T>
44where
45    V: SQLParam + 'a,
46    T: ToSQL<'a, V>,
47{
48    fn to_sql(&self) -> SQL<'a, V> {
49        SQL::join(self.iter().map(ToSQL::to_sql), Token::COMMA)
50    }
51}
52
53impl<'a, V, T> ToSQL<'a, V> for &'a [T]
54where
55    V: SQLParam + 'a,
56    T: ToSQL<'a, V>,
57{
58    fn to_sql(&self) -> SQL<'a, V> {
59        SQL::join(self.iter().map(ToSQL::to_sql), Token::COMMA)
60    }
61}
62
63impl<'a, V, T, const N: usize> ToSQL<'a, V> for [T; N]
64where
65    V: SQLParam + 'a,
66    T: ToSQL<'a, V>,
67{
68    fn to_sql(&self) -> SQL<'a, V> {
69        SQL::join(self.iter().map(ToSQL::to_sql), Token::COMMA)
70    }
71}
72
73// Implement ToSQL for SQLTableInfo and SQLColumnInfo trait objects
74impl<'a, V: SQLParam + 'a> ToSQL<'a, V> for &'static dyn SQLTableInfo {
75    fn to_sql(&self) -> SQL<'a, V> {
76        SQL::table(*self)
77    }
78}
79
80impl<'a, V: SQLParam + 'a> ToSQL<'a, V> for &'static dyn SQLColumnInfo {
81    fn to_sql(&self) -> SQL<'a, V> {
82        SQL::column(*self)
83    }
84}
85
86impl<'a, V: SQLParam + 'a> ToSQL<'a, V> for Box<[&'static dyn SQLColumnInfo]> {
87    fn to_sql(&self) -> SQL<'a, V> {
88        SQL::join(self.iter().map(|&v| SQL::column(v)), Token::COMMA)
89    }
90}
91
92impl<'a, V: SQLParam + 'a> ToSQL<'a, V> for Box<[&'static dyn SQLTableInfo]> {
93    fn to_sql(&self) -> SQL<'a, V> {
94        SQL::join(self.iter().map(|&v| SQL::table(v)), Token::COMMA)
95    }
96}
97
98// Implement ToSQL for primitive types
99impl<'a, V> ToSQL<'a, V> for &'a str
100where
101    V: SQLParam + 'a,
102    V: From<&'a str>,
103    V: Into<Cow<'a, V>>,
104{
105    fn to_sql(&self) -> SQL<'a, V> {
106        SQL::param(V::from(self))
107    }
108}
109
110impl<'a, V> ToSQL<'a, V> for String
111where
112    V: SQLParam + 'a,
113    V: From<String>,
114    V: Into<Cow<'a, V>>,
115{
116    fn to_sql(&self) -> SQL<'a, V> {
117        SQL::param(V::from(self.clone()))
118    }
119}
120
121impl<'a, V> ToSQL<'a, V> for i8
122where
123    V: SQLParam + 'a + From<i8>,
124    V: Into<Cow<'a, V>>,
125{
126    fn to_sql(&self) -> SQL<'a, V> {
127        SQL::param(V::from(*self))
128    }
129}
130
131impl<'a, V> ToSQL<'a, V> for i16
132where
133    V: SQLParam + 'a + From<i16>,
134    V: Into<Cow<'a, V>>,
135{
136    fn to_sql(&self) -> SQL<'a, V> {
137        SQL::param(V::from(*self))
138    }
139}
140
141impl<'a, V> ToSQL<'a, V> for i32
142where
143    V: SQLParam + 'a + From<i32>,
144    V: Into<Cow<'a, V>>,
145{
146    fn to_sql(&self) -> SQL<'a, V> {
147        SQL::param(V::from(*self))
148    }
149}
150
151impl<'a, V> ToSQL<'a, V> for i64
152where
153    V: SQLParam + 'a + From<i64>,
154    V: Into<Cow<'a, V>>,
155{
156    fn to_sql(&self) -> SQL<'a, V> {
157        SQL::param(V::from(*self))
158    }
159}
160
161impl<'a, V> ToSQL<'a, V> for f32
162where
163    V: SQLParam + 'a + From<f32>,
164    V: Into<Cow<'a, V>>,
165{
166    fn to_sql(&self) -> SQL<'a, V> {
167        SQL::param(V::from(*self))
168    }
169}
170
171impl<'a, V> ToSQL<'a, V> for f64
172where
173    V: SQLParam + 'a + From<f64>,
174    V: Into<Cow<'a, V>>,
175{
176    fn to_sql(&self) -> SQL<'a, V> {
177        SQL::param(V::from(*self))
178    }
179}
180
181impl<'a, V> ToSQL<'a, V> for bool
182where
183    V: SQLParam + 'a + From<bool>,
184    V: Into<Cow<'a, V>>,
185{
186    fn to_sql(&self) -> SQL<'a, V> {
187        SQL::param(V::from(*self))
188    }
189}
190
191impl<'a, V> ToSQL<'a, V> for u8
192where
193    V: SQLParam + 'a + From<u8>,
194    V: Into<Cow<'a, V>>,
195{
196    fn to_sql(&self) -> SQL<'a, V> {
197        SQL::param(V::from(*self))
198    }
199}
200
201impl<'a, V> ToSQL<'a, V> for u16
202where
203    V: SQLParam + 'a + From<u16>,
204    V: Into<Cow<'a, V>>,
205{
206    fn to_sql(&self) -> SQL<'a, V> {
207        SQL::param(V::from(*self))
208    }
209}
210
211impl<'a, V> ToSQL<'a, V> for u32
212where
213    V: SQLParam + 'a + From<u32>,
214    V: Into<Cow<'a, V>>,
215{
216    fn to_sql(&self) -> SQL<'a, V> {
217        SQL::param(V::from(*self))
218    }
219}
220
221impl<'a, V> ToSQL<'a, V> for u64
222where
223    V: SQLParam + 'a + From<u64>,
224    V: Into<Cow<'a, V>>,
225{
226    fn to_sql(&self) -> SQL<'a, V> {
227        SQL::param(V::from(*self))
228    }
229}
230
231impl<'a, V> ToSQL<'a, V> for isize
232where
233    V: SQLParam + 'a + From<isize>,
234    V: Into<Cow<'a, V>>,
235{
236    fn to_sql(&self) -> SQL<'a, V> {
237        SQL::param(V::from(*self))
238    }
239}
240
241impl<'a, V> ToSQL<'a, V> for usize
242where
243    V: SQLParam + 'a + From<usize>,
244    V: Into<Cow<'a, V>>,
245{
246    fn to_sql(&self) -> SQL<'a, V> {
247        SQL::param(V::from(*self))
248    }
249}
250
251impl<'a, V, T> ToSQL<'a, V> for Option<T>
252where
253    V: SQLParam + 'a,
254    T: ToSQL<'a, V>,
255{
256    fn to_sql(&self) -> SQL<'a, V> {
257        match self {
258            Some(value) => value.to_sql(), // Let the inner type handle parameterization
259            None => SQL::raw("NULL"),      // NULL is a keyword, use raw
260        }
261    }
262}
263
264#[cfg(feature = "uuid")]
265impl<'a, V> ToSQL<'a, V> for Uuid
266where
267    V: SQLParam + 'a,
268    V: From<Uuid>,
269    V: Into<Cow<'a, V>>,
270{
271    fn to_sql(&self) -> SQL<'a, V> {
272        SQL::param(V::from(*self))
273    }
274}