1use super::{OwnedSQLiteValue, SQLiteValue};
4use crate::prelude::*;
5use drizzle_core::{error::DrizzleError, sql::SQL, traits::ToSQL};
6
7#[cfg(feature = "uuid")]
8use uuid::Uuid;
9
10impl<'a> ToSQL<'a, Self> for SQLiteValue<'a> {
15 fn to_sql(&self) -> SQL<'a, Self> {
16 SQL::param(self.clone())
17 }
18}
19
20impl From<OwnedSQLiteValue> for SQLiteValue<'_> {
25 fn from(value: OwnedSQLiteValue) -> Self {
26 match value {
27 OwnedSQLiteValue::Integer(f) => SQLiteValue::Integer(f),
28 OwnedSQLiteValue::Real(r) => SQLiteValue::Real(r),
29 OwnedSQLiteValue::Text(v) => SQLiteValue::Text(Cow::Owned(v)),
30 OwnedSQLiteValue::Blob(v) => SQLiteValue::Blob(Cow::Owned(v.into())),
31 OwnedSQLiteValue::Null => SQLiteValue::Null,
32 }
33 }
34}
35
36impl<'a> From<&'a OwnedSQLiteValue> for SQLiteValue<'a> {
37 fn from(value: &'a OwnedSQLiteValue) -> Self {
38 match value {
39 OwnedSQLiteValue::Integer(f) => SQLiteValue::Integer(*f),
40 OwnedSQLiteValue::Real(r) => SQLiteValue::Real(*r),
41 OwnedSQLiteValue::Text(v) => SQLiteValue::Text(Cow::Borrowed(v)),
42 OwnedSQLiteValue::Blob(v) => SQLiteValue::Blob(Cow::Borrowed(v)),
43 OwnedSQLiteValue::Null => SQLiteValue::Null,
44 }
45 }
46}
47
48impl<'a> From<&'a Self> for SQLiteValue<'a> {
49 fn from(value: &'a Self) -> Self {
50 match value {
51 SQLiteValue::Integer(f) => SQLiteValue::Integer(*f),
52 SQLiteValue::Real(r) => SQLiteValue::Real(*r),
53 SQLiteValue::Text(v) => SQLiteValue::Text(Cow::Borrowed(v)),
54 SQLiteValue::Blob(v) => SQLiteValue::Blob(Cow::Borrowed(v)),
55 SQLiteValue::Null => SQLiteValue::Null,
56 }
57 }
58}
59
60impl<'a> From<Cow<'a, Self>> for SQLiteValue<'a> {
61 fn from(value: Cow<'a, Self>) -> Self {
62 match value {
63 Cow::Borrowed(r) => r.into(),
64 Cow::Owned(o) => o,
65 }
66 }
67}
68
69impl<'a> From<&'a Cow<'a, Self>> for SQLiteValue<'a> {
70 fn from(value: &'a Cow<'a, Self>) -> Self {
71 match value {
72 Cow::Borrowed(r) => (*r).into(),
73 Cow::Owned(o) => o.into(),
74 }
75 }
76}
77
78macro_rules! impl_from_lossless_int_for_sqlite_value {
85 ($($ty:ty),* $(,)?) => {
86 $(
87 impl<'a> From<$ty> for SQLiteValue<'a> {
88 #[inline]
89 fn from(value: $ty) -> Self {
90 SQLiteValue::Integer(i64::from(value))
91 }
92 }
93
94 impl<'a> From<&$ty> for SQLiteValue<'a> {
95 #[inline]
96 fn from(value: &$ty) -> Self {
97 SQLiteValue::Integer(i64::from(*value))
98 }
99 }
100 )*
101 };
102}
103
104impl_from_lossless_int_for_sqlite_value!(i8, i16, i32, u8, u16, u32, bool);
106
107impl From<i64> for SQLiteValue<'_> {
109 #[inline]
110 fn from(value: i64) -> Self {
111 SQLiteValue::Integer(value)
112 }
113}
114
115impl From<&i64> for SQLiteValue<'_> {
116 #[inline]
117 fn from(value: &i64) -> Self {
118 SQLiteValue::Integer(*value)
119 }
120}
121
122impl From<u64> for SQLiteValue<'_> {
126 #[inline]
127 fn from(value: u64) -> Self {
128 SQLiteValue::Integer(value.cast_signed())
129 }
130}
131
132impl From<&u64> for SQLiteValue<'_> {
133 #[inline]
134 fn from(value: &u64) -> Self {
135 SQLiteValue::Integer(value.cast_signed())
136 }
137}
138
139macro_rules! impl_from_pointer_int_for_sqlite_value {
143 ($($ty:ty),* $(,)?) => {
144 $(
145 impl<'a> From<$ty> for SQLiteValue<'a> {
146 #[inline]
147 fn from(value: $ty) -> Self {
148 SQLiteValue::Integer(i64::try_from(value).unwrap_or(i64::MAX))
149 }
150 }
151
152 impl<'a> From<&$ty> for SQLiteValue<'a> {
153 #[inline]
154 fn from(value: &$ty) -> Self {
155 SQLiteValue::Integer(i64::try_from(*value).unwrap_or(i64::MAX))
156 }
157 }
158 )*
159 };
160}
161
162impl_from_pointer_int_for_sqlite_value!(isize, usize);
163
164impl From<f32> for SQLiteValue<'_> {
166 #[inline]
167 fn from(value: f32) -> Self {
168 SQLiteValue::Real(f64::from(value))
169 }
170}
171
172impl From<&f32> for SQLiteValue<'_> {
173 #[inline]
174 fn from(value: &f32) -> Self {
175 SQLiteValue::Real(f64::from(*value))
176 }
177}
178
179impl From<f64> for SQLiteValue<'_> {
181 #[inline]
182 fn from(value: f64) -> Self {
183 SQLiteValue::Real(value)
184 }
185}
186
187impl From<&f64> for SQLiteValue<'_> {
188 #[inline]
189 fn from(value: &f64) -> Self {
190 SQLiteValue::Real(*value)
191 }
192}
193
194impl<'a> From<&'a str> for SQLiteValue<'a> {
197 fn from(value: &'a str) -> Self {
198 SQLiteValue::Text(Cow::Borrowed(value))
199 }
200}
201
202impl<'a> From<Cow<'a, str>> for SQLiteValue<'a> {
203 fn from(value: Cow<'a, str>) -> Self {
204 SQLiteValue::Text(value)
205 }
206}
207
208impl From<String> for SQLiteValue<'_> {
209 fn from(value: String) -> Self {
210 SQLiteValue::Text(Cow::Owned(value))
211 }
212}
213
214impl<'a> From<&'a String> for SQLiteValue<'a> {
215 fn from(value: &'a String) -> Self {
216 SQLiteValue::Text(Cow::Borrowed(value))
217 }
218}
219
220impl From<Box<String>> for SQLiteValue<'_> {
221 fn from(value: Box<String>) -> Self {
222 SQLiteValue::Text(Cow::Owned(*value))
223 }
224}
225
226impl<'a> From<&'a Box<String>> for SQLiteValue<'a> {
227 fn from(value: &'a Box<String>) -> Self {
228 SQLiteValue::Text(Cow::Borrowed(value.as_str()))
229 }
230}
231
232impl From<Rc<String>> for SQLiteValue<'_> {
233 fn from(value: Rc<String>) -> Self {
234 SQLiteValue::Text(Cow::Owned(value.as_ref().clone()))
235 }
236}
237
238impl<'a> From<&'a Rc<String>> for SQLiteValue<'a> {
239 fn from(value: &'a Rc<String>) -> Self {
240 SQLiteValue::Text(Cow::Borrowed(value.as_str()))
241 }
242}
243
244impl From<Arc<String>> for SQLiteValue<'_> {
245 fn from(value: Arc<String>) -> Self {
246 SQLiteValue::Text(Cow::Owned(value.as_ref().clone()))
247 }
248}
249
250impl<'a> From<&'a Arc<String>> for SQLiteValue<'a> {
251 fn from(value: &'a Arc<String>) -> Self {
252 SQLiteValue::Text(Cow::Borrowed(value.as_str()))
253 }
254}
255
256impl From<Box<str>> for SQLiteValue<'_> {
257 fn from(value: Box<str>) -> Self {
258 SQLiteValue::Text(Cow::Owned(value.into()))
259 }
260}
261
262impl<'a> From<&'a Box<str>> for SQLiteValue<'a> {
263 fn from(value: &'a Box<str>) -> Self {
264 SQLiteValue::Text(Cow::Borrowed(value.as_ref()))
265 }
266}
267
268impl From<Rc<str>> for SQLiteValue<'_> {
269 fn from(value: Rc<str>) -> Self {
270 SQLiteValue::Text(Cow::Owned(value.as_ref().to_string()))
271 }
272}
273
274impl<'a> From<&'a Rc<str>> for SQLiteValue<'a> {
275 fn from(value: &'a Rc<str>) -> Self {
276 SQLiteValue::Text(Cow::Borrowed(value.as_ref()))
277 }
278}
279
280impl From<Arc<str>> for SQLiteValue<'_> {
281 fn from(value: Arc<str>) -> Self {
282 SQLiteValue::Text(Cow::Owned(value.as_ref().to_string()))
283 }
284}
285
286impl<'a> From<&'a Arc<str>> for SQLiteValue<'a> {
287 fn from(value: &'a Arc<str>) -> Self {
288 SQLiteValue::Text(Cow::Borrowed(value.as_ref()))
289 }
290}
291
292#[cfg(feature = "arrayvec")]
295impl<const N: usize> From<arrayvec::ArrayString<N>> for SQLiteValue<'_> {
296 fn from(value: arrayvec::ArrayString<N>) -> Self {
297 SQLiteValue::Text(Cow::Owned(value.to_string()))
298 }
299}
300
301#[cfg(feature = "arrayvec")]
302impl<const N: usize> From<&arrayvec::ArrayString<N>> for SQLiteValue<'_> {
303 fn from(value: &arrayvec::ArrayString<N>) -> Self {
304 SQLiteValue::Text(Cow::Owned(String::from(value.as_str())))
305 }
306}
307
308impl From<compact_str::CompactString> for SQLiteValue<'_> {
309 fn from(value: compact_str::CompactString) -> Self {
310 SQLiteValue::Text(Cow::Owned(value.to_string()))
311 }
312}
313
314impl<'a> From<&'a compact_str::CompactString> for SQLiteValue<'a> {
315 fn from(value: &'a compact_str::CompactString) -> Self {
316 SQLiteValue::Text(Cow::Borrowed(value.as_str()))
317 }
318}
319
320impl<'a> From<&'a [u8]> for SQLiteValue<'a> {
323 fn from(value: &'a [u8]) -> Self {
324 SQLiteValue::Blob(Cow::Borrowed(value))
325 }
326}
327
328impl<'a> From<Cow<'a, [u8]>> for SQLiteValue<'a> {
329 fn from(value: Cow<'a, [u8]>) -> Self {
330 SQLiteValue::Blob(value)
331 }
332}
333
334impl From<Vec<u8>> for SQLiteValue<'_> {
335 fn from(value: Vec<u8>) -> Self {
336 SQLiteValue::Blob(Cow::Owned(value))
337 }
338}
339
340impl From<Box<Vec<u8>>> for SQLiteValue<'_> {
341 fn from(value: Box<Vec<u8>>) -> Self {
342 SQLiteValue::Blob(Cow::Owned(*value))
343 }
344}
345
346impl<'a> From<&'a Box<Vec<u8>>> for SQLiteValue<'a> {
347 fn from(value: &'a Box<Vec<u8>>) -> Self {
348 SQLiteValue::Blob(Cow::Borrowed(value.as_slice()))
349 }
350}
351
352impl From<Rc<Vec<u8>>> for SQLiteValue<'_> {
353 fn from(value: Rc<Vec<u8>>) -> Self {
354 SQLiteValue::Blob(Cow::Owned(value.as_ref().clone()))
355 }
356}
357
358impl<'a> From<&'a Rc<Vec<u8>>> for SQLiteValue<'a> {
359 fn from(value: &'a Rc<Vec<u8>>) -> Self {
360 SQLiteValue::Blob(Cow::Borrowed(value.as_slice()))
361 }
362}
363
364impl From<Arc<Vec<u8>>> for SQLiteValue<'_> {
365 fn from(value: Arc<Vec<u8>>) -> Self {
366 SQLiteValue::Blob(Cow::Owned(value.as_ref().clone()))
367 }
368}
369
370impl<'a> From<&'a Arc<Vec<u8>>> for SQLiteValue<'a> {
371 fn from(value: &'a Arc<Vec<u8>>) -> Self {
372 SQLiteValue::Blob(Cow::Borrowed(value.as_slice()))
373 }
374}
375
376#[cfg(feature = "arrayvec")]
379impl<const N: usize> From<arrayvec::ArrayVec<u8, N>> for SQLiteValue<'_> {
380 fn from(value: arrayvec::ArrayVec<u8, N>) -> Self {
381 SQLiteValue::Blob(Cow::Owned(value.to_vec()))
382 }
383}
384
385#[cfg(feature = "arrayvec")]
386impl<const N: usize> From<&arrayvec::ArrayVec<u8, N>> for SQLiteValue<'_> {
387 fn from(value: &arrayvec::ArrayVec<u8, N>) -> Self {
388 SQLiteValue::Blob(Cow::Owned(value.to_vec()))
389 }
390}
391
392#[cfg(feature = "bytes")]
393impl From<bytes::Bytes> for SQLiteValue<'_> {
394 fn from(value: bytes::Bytes) -> Self {
395 SQLiteValue::Blob(Cow::Owned(value.to_vec()))
396 }
397}
398
399#[cfg(feature = "bytes")]
400impl<'a> From<&'a bytes::Bytes> for SQLiteValue<'a> {
401 fn from(value: &'a bytes::Bytes) -> Self {
402 SQLiteValue::Blob(Cow::Owned(value.to_vec()))
403 }
404}
405
406#[cfg(feature = "bytes")]
407impl From<bytes::BytesMut> for SQLiteValue<'_> {
408 fn from(value: bytes::BytesMut) -> Self {
409 SQLiteValue::Blob(Cow::Owned(value.to_vec()))
410 }
411}
412
413#[cfg(feature = "bytes")]
414impl<'a> From<&'a bytes::BytesMut> for SQLiteValue<'a> {
415 fn from(value: &'a bytes::BytesMut) -> Self {
416 SQLiteValue::Blob(Cow::Owned(value.to_vec()))
417 }
418}
419
420#[cfg(feature = "smallvec")]
421impl<const N: usize> From<smallvec::SmallVec<[u8; N]>> for SQLiteValue<'_> {
422 fn from(value: smallvec::SmallVec<[u8; N]>) -> Self {
423 SQLiteValue::Blob(Cow::Owned(value.into_vec()))
424 }
425}
426
427#[cfg(feature = "smallvec")]
428impl<const N: usize> From<&smallvec::SmallVec<[u8; N]>> for SQLiteValue<'_> {
429 fn from(value: &smallvec::SmallVec<[u8; N]>) -> Self {
430 SQLiteValue::Blob(Cow::Owned(value.to_vec()))
431 }
432}
433
434#[cfg(feature = "chrono")]
437impl From<chrono::NaiveDate> for SQLiteValue<'_> {
438 fn from(value: chrono::NaiveDate) -> Self {
439 SQLiteValue::Text(Cow::Owned(value.to_string()))
440 }
441}
442
443#[cfg(feature = "chrono")]
444impl From<&chrono::NaiveDate> for SQLiteValue<'_> {
445 fn from(value: &chrono::NaiveDate) -> Self {
446 SQLiteValue::Text(Cow::Owned(value.to_string()))
447 }
448}
449
450#[cfg(feature = "chrono")]
451impl From<chrono::NaiveTime> for SQLiteValue<'_> {
452 fn from(value: chrono::NaiveTime) -> Self {
453 SQLiteValue::Text(Cow::Owned(value.to_string()))
454 }
455}
456
457#[cfg(feature = "chrono")]
458impl From<&chrono::NaiveTime> for SQLiteValue<'_> {
459 fn from(value: &chrono::NaiveTime) -> Self {
460 SQLiteValue::Text(Cow::Owned(value.to_string()))
461 }
462}
463
464#[cfg(feature = "chrono")]
465impl From<chrono::NaiveDateTime> for SQLiteValue<'_> {
466 fn from(value: chrono::NaiveDateTime) -> Self {
467 SQLiteValue::Text(Cow::Owned(value.to_string()))
468 }
469}
470
471#[cfg(feature = "chrono")]
472impl From<&chrono::NaiveDateTime> for SQLiteValue<'_> {
473 fn from(value: &chrono::NaiveDateTime) -> Self {
474 SQLiteValue::Text(Cow::Owned(value.to_string()))
475 }
476}
477
478#[cfg(feature = "chrono")]
479impl From<chrono::DateTime<chrono::FixedOffset>> for SQLiteValue<'_> {
480 fn from(value: chrono::DateTime<chrono::FixedOffset>) -> Self {
481 SQLiteValue::Text(Cow::Owned(value.to_rfc3339()))
482 }
483}
484
485#[cfg(feature = "chrono")]
486impl From<&chrono::DateTime<chrono::FixedOffset>> for SQLiteValue<'_> {
487 fn from(value: &chrono::DateTime<chrono::FixedOffset>) -> Self {
488 SQLiteValue::Text(Cow::Owned(value.to_rfc3339()))
489 }
490}
491
492#[cfg(feature = "chrono")]
493impl From<chrono::DateTime<chrono::Utc>> for SQLiteValue<'_> {
494 fn from(value: chrono::DateTime<chrono::Utc>) -> Self {
495 SQLiteValue::Text(Cow::Owned(value.to_rfc3339()))
496 }
497}
498
499#[cfg(feature = "chrono")]
500impl From<&chrono::DateTime<chrono::Utc>> for SQLiteValue<'_> {
501 fn from(value: &chrono::DateTime<chrono::Utc>) -> Self {
502 SQLiteValue::Text(Cow::Owned(value.to_rfc3339()))
503 }
504}
505
506#[cfg(feature = "chrono")]
507impl From<chrono::Duration> for SQLiteValue<'_> {
508 fn from(value: chrono::Duration) -> Self {
509 SQLiteValue::Text(Cow::Owned(value.to_string()))
510 }
511}
512
513#[cfg(feature = "chrono")]
514impl From<&chrono::Duration> for SQLiteValue<'_> {
515 fn from(value: &chrono::Duration) -> Self {
516 SQLiteValue::Text(Cow::Owned(value.to_string()))
517 }
518}
519
520#[cfg(feature = "time")]
523impl From<time::Date> for SQLiteValue<'_> {
524 fn from(value: time::Date) -> Self {
525 SQLiteValue::Text(Cow::Owned(
526 value
527 .format(&time::format_description::well_known::Iso8601::DATE)
528 .unwrap_or_else(|_| value.to_string()),
529 ))
530 }
531}
532
533#[cfg(feature = "time")]
534impl From<&time::Date> for SQLiteValue<'_> {
535 fn from(value: &time::Date) -> Self {
536 SQLiteValue::Text(Cow::Owned(
537 value
538 .format(&time::format_description::well_known::Iso8601::DATE)
539 .unwrap_or_else(|_| value.to_string()),
540 ))
541 }
542}
543
544#[cfg(feature = "time")]
547impl From<time::Time> for SQLiteValue<'_> {
548 fn from(value: time::Time) -> Self {
549 SQLiteValue::Text(Cow::Owned(
550 value
551 .format(time::macros::format_description!(
552 "[hour]:[minute]:[second].[subsecond]"
553 ))
554 .unwrap_or_else(|_| value.to_string()),
555 ))
556 }
557}
558
559#[cfg(feature = "time")]
560impl From<&time::Time> for SQLiteValue<'_> {
561 fn from(value: &time::Time) -> Self {
562 Self::from(*value)
563 }
564}
565
566#[cfg(feature = "time")]
567impl From<time::PrimitiveDateTime> for SQLiteValue<'_> {
568 fn from(value: time::PrimitiveDateTime) -> Self {
569 SQLiteValue::Text(Cow::Owned(
570 value
571 .format(&time::format_description::well_known::Iso8601::DATE_TIME)
572 .unwrap_or_else(|_| value.to_string()),
573 ))
574 }
575}
576
577#[cfg(feature = "time")]
578impl From<&time::PrimitiveDateTime> for SQLiteValue<'_> {
579 fn from(value: &time::PrimitiveDateTime) -> Self {
580 SQLiteValue::Text(Cow::Owned(
581 value
582 .format(&time::format_description::well_known::Iso8601::DATE_TIME)
583 .unwrap_or_else(|_| value.to_string()),
584 ))
585 }
586}
587
588#[cfg(feature = "time")]
589impl From<time::OffsetDateTime> for SQLiteValue<'_> {
590 fn from(value: time::OffsetDateTime) -> Self {
591 SQLiteValue::Text(Cow::Owned(
592 value
593 .format(&time::format_description::well_known::Rfc3339)
594 .unwrap_or_else(|_| value.to_string()),
595 ))
596 }
597}
598
599#[cfg(feature = "time")]
600impl From<&time::OffsetDateTime> for SQLiteValue<'_> {
601 fn from(value: &time::OffsetDateTime) -> Self {
602 SQLiteValue::Text(Cow::Owned(
603 value
604 .format(&time::format_description::well_known::Rfc3339)
605 .unwrap_or_else(|_| value.to_string()),
606 ))
607 }
608}
609
610#[cfg(feature = "time")]
611impl From<time::Duration> for SQLiteValue<'_> {
612 fn from(value: time::Duration) -> Self {
613 SQLiteValue::Text(Cow::Owned(format!("{}s", value.whole_seconds())))
614 }
615}
616
617#[cfg(feature = "time")]
618impl From<&time::Duration> for SQLiteValue<'_> {
619 fn from(value: &time::Duration) -> Self {
620 SQLiteValue::Text(Cow::Owned(format!("{}s", value.whole_seconds())))
621 }
622}
623
624#[cfg(feature = "jiff")]
631impl From<jiff::civil::Date> for SQLiteValue<'_> {
632 fn from(value: jiff::civil::Date) -> Self {
633 SQLiteValue::Text(Cow::Owned(value.to_string()))
634 }
635}
636
637#[cfg(feature = "jiff")]
638impl From<&jiff::civil::Date> for SQLiteValue<'_> {
639 fn from(value: &jiff::civil::Date) -> Self {
640 Self::from(*value)
641 }
642}
643
644#[cfg(feature = "jiff")]
645impl From<jiff::civil::Time> for SQLiteValue<'_> {
646 fn from(value: jiff::civil::Time) -> Self {
647 SQLiteValue::Text(Cow::Owned(value.to_string()))
648 }
649}
650
651#[cfg(feature = "jiff")]
652impl From<&jiff::civil::Time> for SQLiteValue<'_> {
653 fn from(value: &jiff::civil::Time) -> Self {
654 Self::from(*value)
655 }
656}
657
658#[cfg(feature = "jiff")]
659impl From<jiff::civil::DateTime> for SQLiteValue<'_> {
660 fn from(value: jiff::civil::DateTime) -> Self {
661 SQLiteValue::Text(Cow::Owned(format!("{} {}", value.date(), value.time())))
662 }
663}
664
665#[cfg(feature = "jiff")]
666impl From<&jiff::civil::DateTime> for SQLiteValue<'_> {
667 fn from(value: &jiff::civil::DateTime) -> Self {
668 Self::from(*value)
669 }
670}
671
672#[cfg(feature = "jiff")]
673impl From<jiff::Timestamp> for SQLiteValue<'_> {
674 fn from(value: jiff::Timestamp) -> Self {
675 SQLiteValue::Text(Cow::Owned(value.to_string()))
676 }
677}
678
679#[cfg(feature = "jiff")]
680impl From<&jiff::Timestamp> for SQLiteValue<'_> {
681 fn from(value: &jiff::Timestamp) -> Self {
682 Self::from(*value)
683 }
684}
685
686#[cfg(feature = "rust-decimal")]
689impl From<rust_decimal::Decimal> for SQLiteValue<'_> {
690 fn from(value: rust_decimal::Decimal) -> Self {
691 SQLiteValue::Text(Cow::Owned(value.to_string()))
692 }
693}
694
695#[cfg(feature = "rust-decimal")]
696impl From<&rust_decimal::Decimal> for SQLiteValue<'_> {
697 fn from(value: &rust_decimal::Decimal) -> Self {
698 SQLiteValue::Text(Cow::Owned(value.to_string()))
699 }
700}
701
702#[cfg(feature = "serde")]
705impl From<serde_json::Value> for SQLiteValue<'_> {
706 fn from(value: serde_json::Value) -> Self {
707 SQLiteValue::Text(Cow::Owned(value.to_string()))
708 }
709}
710
711#[cfg(feature = "serde")]
712impl From<&serde_json::Value> for SQLiteValue<'_> {
713 fn from(value: &serde_json::Value) -> Self {
714 SQLiteValue::Text(Cow::Owned(value.to_string()))
715 }
716}
717
718#[cfg(feature = "uuid")]
721impl From<Uuid> for SQLiteValue<'_> {
722 fn from(value: Uuid) -> Self {
723 SQLiteValue::Blob(Cow::Owned(value.as_bytes().to_vec()))
724 }
725}
726
727#[cfg(feature = "uuid")]
728impl<'a> From<&'a Uuid> for SQLiteValue<'a> {
729 fn from(value: &'a Uuid) -> Self {
730 SQLiteValue::Blob(Cow::Borrowed(value.as_bytes()))
731 }
732}
733
734impl<T> From<Option<T>> for SQLiteValue<'_>
743where
744 T: TryInto<Self>,
745{
746 fn from(value: Option<T>) -> Self {
747 value.map_or(SQLiteValue::Null, |v| {
748 v.try_into().unwrap_or_else(|_| {
749 panic!(
750 "could not convert a `{}` to a SQLite value",
751 core::any::type_name::<T>()
752 )
753 })
754 })
755 }
756}
757
758impl<'a> From<SQLiteValue<'a>> for Cow<'a, SQLiteValue<'a>> {
760 fn from(value: SQLiteValue<'a>) -> Self {
761 Cow::Owned(value)
762 }
763}
764
765impl<'a> From<&'a SQLiteValue<'a>> for Cow<'a, SQLiteValue<'a>> {
766 fn from(value: &'a SQLiteValue<'a>) -> Self {
767 Cow::Borrowed(value)
768 }
769}
770
771macro_rules! impl_try_from_sqlite_value {
778 ($($ty:ty),* $(,)?) => {
779 $(
780 impl<'a> TryFrom<SQLiteValue<'a>> for $ty {
781 type Error = DrizzleError;
782
783 #[inline]
784 fn try_from(value: SQLiteValue<'a>) -> Result<Self, Self::Error> {
785 value.convert()
786 }
787 }
788 )*
789 };
790}
791
792impl_try_from_sqlite_value!(
793 i8,
794 i16,
795 i32,
796 i64,
797 isize,
798 u8,
799 u16,
800 u32,
801 u64,
802 usize,
803 f32,
804 f64,
805 bool,
806 String,
807 Box<String>,
808 Rc<String>,
809 Arc<String>,
810 Box<str>,
811 Rc<str>,
812 Arc<str>,
813 Box<Vec<u8>>,
814 Rc<Vec<u8>>,
815 Arc<Vec<u8>>,
816 Vec<u8>,
817 compact_str::CompactString,
818);
819
820#[cfg(feature = "uuid")]
821impl_try_from_sqlite_value!(Uuid);
822
823#[cfg(feature = "chrono")]
824impl_try_from_sqlite_value!(
825 chrono::NaiveDate,
826 chrono::NaiveTime,
827 chrono::NaiveDateTime,
828 chrono::DateTime<chrono::FixedOffset>,
829 chrono::DateTime<chrono::Utc>,
830 chrono::Duration,
831);
832
833#[cfg(feature = "time")]
834impl_try_from_sqlite_value!(
835 time::Date,
836 time::Time,
837 time::PrimitiveDateTime,
838 time::OffsetDateTime,
839 time::Duration,
840);
841
842#[cfg(feature = "rust-decimal")]
843impl_try_from_sqlite_value!(rust_decimal::Decimal);
844
845#[cfg(feature = "serde")]
846impl_try_from_sqlite_value!(serde_json::Value);
847
848#[cfg(feature = "bytes")]
849impl_try_from_sqlite_value!(bytes::Bytes, bytes::BytesMut);
850
851#[cfg(feature = "smallvec")]
852impl<const N: usize> TryFrom<SQLiteValue<'_>> for smallvec::SmallVec<[u8; N]> {
853 type Error = DrizzleError;
854
855 fn try_from(value: SQLiteValue<'_>) -> Result<Self, Self::Error> {
856 value.convert()
857 }
858}
859
860macro_rules! impl_try_from_sqlite_value_ref {
867 ($($ty:ty),* $(,)?) => {
868 $(
869 impl<'a> TryFrom<&SQLiteValue<'a>> for $ty {
870 type Error = DrizzleError;
871
872 #[inline]
873 fn try_from(value: &SQLiteValue<'a>) -> Result<Self, Self::Error> {
874 value.convert_ref()
875 }
876 }
877 )*
878 };
879}
880
881impl_try_from_sqlite_value_ref!(
882 i8,
883 i16,
884 i32,
885 i64,
886 isize,
887 u8,
888 u16,
889 u32,
890 u64,
891 usize,
892 f32,
893 f64,
894 bool,
895 String,
896 Box<String>,
897 Rc<String>,
898 Arc<String>,
899 Box<str>,
900 Rc<str>,
901 Arc<str>,
902 Box<Vec<u8>>,
903 Rc<Vec<u8>>,
904 Arc<Vec<u8>>,
905 Vec<u8>,
906 compact_str::CompactString,
907);
908
909#[cfg(feature = "uuid")]
910impl_try_from_sqlite_value_ref!(Uuid);
911
912#[cfg(feature = "chrono")]
913impl_try_from_sqlite_value_ref!(
914 chrono::NaiveDate,
915 chrono::NaiveTime,
916 chrono::NaiveDateTime,
917 chrono::DateTime<chrono::FixedOffset>,
918 chrono::DateTime<chrono::Utc>,
919 chrono::Duration,
920);
921
922#[cfg(feature = "time")]
923impl_try_from_sqlite_value_ref!(
924 time::Date,
925 time::Time,
926 time::PrimitiveDateTime,
927 time::OffsetDateTime,
928 time::Duration,
929);
930
931#[cfg(feature = "rust-decimal")]
932impl_try_from_sqlite_value_ref!(rust_decimal::Decimal);
933
934#[cfg(feature = "serde")]
935impl_try_from_sqlite_value_ref!(serde_json::Value);
936
937#[cfg(feature = "bytes")]
938impl_try_from_sqlite_value_ref!(bytes::Bytes, bytes::BytesMut);
939
940#[cfg(feature = "smallvec")]
941impl<const N: usize> TryFrom<&SQLiteValue<'_>> for smallvec::SmallVec<[u8; N]> {
942 type Error = DrizzleError;
943
944 fn try_from(value: &SQLiteValue<'_>) -> Result<Self, Self::Error> {
945 value.convert_ref()
946 }
947}
948
949impl<'a> TryFrom<&'a SQLiteValue<'a>> for &'a str {
952 type Error = DrizzleError;
953
954 fn try_from(value: &'a SQLiteValue<'a>) -> Result<Self, Self::Error> {
955 match value {
956 SQLiteValue::Text(cow) => Ok(cow.as_ref()),
957 _ => Err(DrizzleError::ConversionError(
958 format!("Cannot convert {value:?} to &str").into(),
959 )),
960 }
961 }
962}
963
964impl<'a> TryFrom<&'a SQLiteValue<'a>> for &'a [u8] {
965 type Error = DrizzleError;
966
967 fn try_from(value: &'a SQLiteValue<'a>) -> Result<Self, Self::Error> {
968 match value {
969 SQLiteValue::Blob(cow) => Ok(cow.as_ref()),
970 _ => Err(DrizzleError::ConversionError(
971 format!("Cannot convert {value:?} to &[u8]").into(),
972 )),
973 }
974 }
975}