1use crate::backend::Backend;
2use crate::deserialize::{self, FromSql, FromSqlRef, Queryable};
3#[cfg(feature = "std")]
4use crate::query_builder::bind_collector::RawBytesBindCollector;
5use crate::serialize::{self, Output, ToSql};
6use crate::sql_types::{
7 self, BigInt, Binary, Bool, Double, Float, Integer, SingleValue, SmallInt, Text,
8};
9use alloc::borrow::Cow;
10use alloc::borrow::ToOwned;
11use alloc::boxed::Box;
12use alloc::fmt;
13use alloc::string::String;
14use alloc::vec::Vec;
15
16#[allow(dead_code)]
17mod foreign_impls {
18 use super::*;
19 use crate::deserialize::FromSqlRow;
20
21 #[derive(AsExpression, FromSqlRow)]
22 #[diesel(foreign_derive)]
23 #[diesel(sql_type = Bool)]
24 struct BoolProxy(bool);
25
26 #[derive(FromSqlRow)]
27 #[cfg_attr(
28 any(feature = "mysql_backend", feature = "mariadb_backend"),
29 derive(AsExpression)
30 )]
31 #[diesel(foreign_derive)]
32 #[cfg_attr(any(feature = "mysql_backend", feature = "mariadb_backend"), diesel(sql_type = crate::sql_types::TinyInt))]
33 struct I8Proxy(i8);
34
35 #[derive(AsExpression, FromSqlRow)]
36 #[diesel(foreign_derive)]
37 #[diesel(sql_type = SmallInt)]
38 struct I16Proxy(i16);
39
40 #[derive(AsExpression, FromSqlRow)]
41 #[diesel(foreign_derive)]
42 #[diesel(sql_type = Integer)]
43 struct I32Proxy(i32);
44
45 #[derive(AsExpression, FromSqlRow)]
46 #[diesel(foreign_derive)]
47 #[diesel(sql_type = BigInt)]
48 struct I64Proxy(i64);
49
50 #[derive(FromSqlRow)]
51 #[cfg_attr(
52 any(
53 feature = "mysql_backend",
54 feature = "postgres_backend",
55 feature = "mariadb_backend"
56 ),
57 derive(AsExpression)
58 )]
59 #[diesel(foreign_derive)]
60 #[cfg_attr(
61 any(feature = "mysql_backend", feature = "mariadb_backend"),
62 diesel(sql_type = crate::sql_types::Unsigned<crate::sql_types::TinyInt>)
63 )]
64 #[cfg_attr(feature = "postgres_backend", diesel(foreign_derive, sql_type = crate::sql_types::CChar))]
65 struct U8Proxy(u8);
66
67 #[derive(FromSqlRow)]
68 #[cfg_attr(
69 any(feature = "mysql_backend", feature = "mariadb_backend"),
70 derive(AsExpression)
71 )]
72 #[diesel(foreign_derive)]
73 #[cfg_attr(any(feature = "mysql_backend", feature = "mariadb_backend"), diesel(sql_type = crate::sql_types::Unsigned<SmallInt>))]
74 struct U16Proxy(u16);
75
76 #[derive(FromSqlRow)]
77 #[cfg_attr(
78 any(
79 feature = "mysql_backend",
80 feature = "mariadb_backend",
81 feature = "postgres_backend"
82 ),
83 derive(AsExpression)
84 )]
85 #[diesel(foreign_derive)]
86 #[cfg_attr(any(feature = "mysql_backend", feature = "mariadb_backend"), diesel(sql_type = crate::sql_types::Unsigned<Integer>))]
87 #[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::sql_types::Oid))]
88 struct U32Proxy(u32);
89
90 #[derive(FromSqlRow)]
91 #[cfg_attr(
92 any(feature = "mysql_backend", feature = "mariadb_backend"),
93 derive(AsExpression)
94 )]
95 #[diesel(foreign_derive)]
96 #[cfg_attr(any(feature = "mysql_backend", feature = "mariadb_backend"), diesel(sql_type = crate::sql_types::Unsigned<BigInt>))]
97 struct U64Proxy(u64);
98
99 #[derive(AsExpression, FromSqlRow)]
100 #[diesel(foreign_derive)]
101 #[diesel(sql_type = Float)]
102 struct F32Proxy(f32);
103
104 #[derive(AsExpression, FromSqlRow)]
105 #[diesel(foreign_derive)]
106 #[diesel(sql_type = Double)]
107 struct F64Proxy(f64);
108
109 #[derive(AsExpression, FromSqlRow)]
110 #[diesel(foreign_derive)]
111 #[diesel(sql_type = Text)]
112 #[cfg_attr(feature = "__sqlite-shared", diesel(sql_type = crate::sql_types::Date))]
113 #[cfg_attr(feature = "__sqlite-shared", diesel(sql_type = crate::sql_types::Time))]
114 #[cfg_attr(feature = "__sqlite-shared", diesel(sql_type = crate::sql_types::Timestamp))]
115 #[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::sql_types::Citext))]
116 #[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::pg::sql_types::Bpchar))]
117 struct StringProxy(String);
118
119 #[derive(AsExpression)]
120 #[diesel(foreign_derive, not_sized)]
121 #[diesel(sql_type = Text)]
122 #[cfg_attr(feature = "__sqlite-shared", diesel(sql_type = crate::sql_types::Date))]
123 #[cfg_attr(feature = "__sqlite-shared", diesel(sql_type = crate::sql_types::Time))]
124 #[cfg_attr(feature = "__sqlite-shared", diesel(sql_type = crate::sql_types::Timestamp))]
125 #[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::sql_types::Citext))]
126 #[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::pg::sql_types::Bpchar))]
127 struct StrProxy(str);
128
129 #[derive(FromSqlRow)]
130 #[diesel(foreign_derive)]
131 struct VecProxy<T>(Vec<T>);
132
133 #[derive(AsExpression)]
134 #[diesel(foreign_derive)]
135 #[diesel(sql_type = Binary)]
136 struct BinaryVecProxy(Vec<u8>);
137
138 #[derive(AsExpression)]
139 #[diesel(foreign_derive, not_sized)]
140 #[diesel(sql_type = Binary)]
141 struct BinarySliceProxy([u8]);
142
143 #[derive(AsExpression)]
144 #[diesel(foreign_derive)]
145 #[diesel(sql_type = Binary)]
146 struct BinaryArrayProxy<const N: usize>([u8; N]);
147}
148
149#[diagnostic::do_not_recommend]
150impl<ST, DB> FromSql<ST, DB> for String
151where
152 DB: Backend,
153 for<'a> &'a str: FromSqlRef<'a, ST, DB>,
154{
155 fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
156 <&str as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(|v| v.to_owned())
157 }
158}
159
160#[cfg(feature = "std")]
161impl<DB> ToSql<sql_types::Text, DB> for str
162where
163 for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>,
164{
165 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
166 use alloc::boxed::Box;
167 use std::io::Write;
168
169 out.write_all(self.as_bytes())
170 .map(|_| crate::serialize::IsNull::No)
171 .map_err(|e| Box::new(e) as Box<dyn core::error::Error + Send + Sync>)
172 }
173}
174
175impl<DB> ToSql<sql_types::Text, DB> for String
176where
177 DB: Backend,
178 str: ToSql<sql_types::Text, DB>,
179{
180 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
181 (self as &str).to_sql(out)
182 }
183}
184
185impl<ST, DB> FromSql<ST, DB> for Vec<u8>
186where
187 DB: Backend,
188 for<'a> &'a [u8]: FromSqlRef<'a, ST, DB>,
189{
190 fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
191 <&[u8] as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(|v| v.to_owned())
192 }
193}
194
195impl<DB> ToSql<sql_types::Binary, DB> for Vec<u8>
196where
197 DB: Backend,
198 [u8]: ToSql<sql_types::Binary, DB>,
199{
200 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
201 (self as &[u8]).to_sql(out)
202 }
203}
204
205impl<DB, const N: usize> ToSql<sql_types::Binary, DB> for [u8; N]
206where
207 DB: Backend,
208 [u8]: ToSql<sql_types::Binary, DB>,
209{
210 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
211 self.as_slice().to_sql(out)
212 }
213}
214
215#[cfg(feature = "std")]
216impl<DB> ToSql<sql_types::Binary, DB> for [u8]
217where
218 for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>,
219{
220 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
221 use alloc::boxed::Box;
222 use std::io::Write;
223
224 out.write_all(self)
225 .map(|_| crate::serialize::IsNull::No)
226 .map_err(|e| Box::new(e) as Box<dyn core::error::Error + Send + Sync>)
227 }
228}
229
230impl<'a, T: ?Sized, ST, DB> ToSql<ST, DB> for Cow<'a, T>
231where
232 T: 'a + ToOwned + ToSql<ST, DB>,
233 DB: Backend,
234 Self: fmt::Debug,
235{
236 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
237 ToSql::<ST, DB>::to_sql(&**self, out)
238 }
239}
240
241impl<'a, T: ?Sized, ST, DB> FromSql<ST, DB> for Cow<'a, T>
242where
243 T: 'a + ToOwned,
244 DB: Backend,
245 T::Owned: FromSql<ST, DB>,
246{
247 fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
248 T::Owned::from_sql(bytes).map(Cow::Owned)
249 }
250}
251
252impl<'a, T: ?Sized, ST, DB> Queryable<ST, DB> for Cow<'a, T>
253where
254 T: 'a + ToOwned,
255 ST: SingleValue,
256 DB: Backend,
257 Self: FromSql<ST, DB>,
258{
259 type Row = Self;
260
261 fn build(row: Self::Row) -> deserialize::Result<Self> {
262 Ok(row)
263 }
264}
265
266use crate::expression::bound::Bound;
267use crate::expression::{AsExpression, Expression, TypedExpressionType};
268use sql_types::SqlType;
269
270impl<'a, T: ?Sized, ST> AsExpression<ST> for Cow<'a, T>
271where
272 T: 'a + ToOwned,
273 Bound<ST, Cow<'a, T>>: Expression<SqlType = ST>,
274 ST: SqlType + TypedExpressionType,
275{
276 type Expression = Bound<ST, Self>;
277
278 fn as_expression(self) -> Self::Expression {
279 Bound::new(self)
280 }
281}
282
283impl<'a, 'b, T: ?Sized, ST> AsExpression<ST> for &'b Cow<'a, T>
284where
285 T: 'a + ToOwned,
286 Bound<ST, &'b T>: Expression<SqlType = ST>,
287 ST: SqlType + TypedExpressionType,
288{
289 type Expression = Bound<ST, &'b T>;
290
291 fn as_expression(self) -> Self::Expression {
292 Bound::new(&**self)
293 }
294}
295
296impl<T: ?Sized, ST, DB> ToSql<ST, DB> for Box<T>
297where
298 T: ToSql<ST, DB>,
299 DB: Backend,
300 Self: fmt::Debug,
301{
302 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
303 ToSql::<ST, DB>::to_sql(&**self, out)
304 }
305}
306
307impl<T, ST, DB> FromSql<ST, DB> for Box<T>
308where
309 DB: Backend,
310 T: FromSql<ST, DB>,
311{
312 fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
313 T::from_sql(bytes).map(Box::from)
314 }
315}
316
317impl<T: ?Sized, ST, DB> Queryable<ST, DB> for Box<T>
318where
319 ST: SingleValue,
320 DB: Backend,
321 Self: FromSql<ST, DB>,
322{
323 type Row = Self;
324
325 fn build(row: Self::Row) -> deserialize::Result<Self> {
326 Ok(row)
327 }
328}
329
330impl<T: ?Sized, ST, DB> ToSql<ST, DB> for alloc::rc::Rc<T>
331where
332 T: ToSql<ST, DB>,
333 DB: Backend,
334 Self: fmt::Debug,
335{
336 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
337 ToSql::<ST, DB>::to_sql(&**self, out)
338 }
339}
340
341impl<T, ST, DB> FromSql<ST, DB> for alloc::rc::Rc<T>
342where
343 DB: Backend,
344 T: FromSql<ST, DB>,
345{
346 fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
347 T::from_sql(bytes).map(alloc::rc::Rc::from)
348 }
349}
350
351impl<T: ?Sized, ST, DB> Queryable<ST, DB> for alloc::rc::Rc<T>
352where
353 ST: SingleValue,
354 DB: Backend,
355 Self: FromSql<ST, DB>,
356{
357 type Row = Self;
358
359 fn build(row: Self::Row) -> deserialize::Result<Self> {
360 Ok(row)
361 }
362}
363
364impl<T: ?Sized, ST, DB> ToSql<ST, DB> for alloc::sync::Arc<T>
365where
366 T: ToSql<ST, DB>,
367 DB: Backend,
368 Self: fmt::Debug,
369{
370 fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result {
371 ToSql::<ST, DB>::to_sql(&**self, out)
372 }
373}
374
375impl<T, ST, DB> FromSql<ST, DB> for alloc::sync::Arc<T>
376where
377 DB: Backend,
378 T: FromSql<ST, DB>,
379{
380 fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
381 T::from_sql(bytes).map(alloc::sync::Arc::from)
382 }
383}
384
385impl<T: ?Sized, ST, DB> Queryable<ST, DB> for alloc::sync::Arc<T>
386where
387 ST: SingleValue,
388 DB: Backend,
389 Self: FromSql<ST, DB>,
390{
391 type Row = Self;
392
393 fn build(row: Self::Row) -> deserialize::Result<Self> {
394 Ok(row)
395 }
396}
397
398impl<ST, DB> FromSql<ST, DB> for Box<str>
406where
407 DB: Backend,
408 for<'a> &'a str: FromSqlRef<'a, ST, DB>,
409{
410 fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
411 <&str as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(Box::from)
412 }
413}
414
415impl<ST, DB> FromSql<ST, DB> for alloc::rc::Rc<str>
416where
417 DB: Backend,
418 for<'a> &'a str: FromSqlRef<'a, ST, DB>,
419{
420 fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
421 <&str as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(alloc::rc::Rc::from)
422 }
423}
424
425impl<ST, DB> FromSql<ST, DB> for alloc::sync::Arc<str>
426where
427 DB: Backend,
428 for<'a> &'a str: FromSqlRef<'a, ST, DB>,
429{
430 fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
431 <&str as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(alloc::sync::Arc::from)
432 }
433}
434
435impl<ST, DB> FromSql<ST, DB> for Box<[u8]>
436where
437 DB: Backend,
438 for<'a> &'a [u8]: FromSqlRef<'a, ST, DB>,
439{
440 fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
441 <&[u8] as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(Box::from)
442 }
443}
444
445impl<ST, DB> FromSql<ST, DB> for alloc::rc::Rc<[u8]>
446where
447 DB: Backend,
448 for<'a> &'a [u8]: FromSqlRef<'a, ST, DB>,
449{
450 fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
451 <&[u8] as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(alloc::rc::Rc::from)
452 }
453}
454
455impl<ST, DB> FromSql<ST, DB> for alloc::sync::Arc<[u8]>
456where
457 DB: Backend,
458 for<'a> &'a [u8]: FromSqlRef<'a, ST, DB>,
459{
460 fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> {
461 <&[u8] as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(alloc::sync::Arc::from)
462 }
463}