Debian Codegraph @

src/features/sqlx.rs

raw
1 use sqlx::error::BoxDynError;
2 #[cfg(any(
3 feature = "sqlx-mysql",
4 feature = "sqlx-postgres",
5 feature = "sqlx-sqlite"
6 ))]
7 use sqlx::{encode::IsNull, Encode};
8 use sqlx::{Database, Decode, Type};
9
10 use crate::{CompactString, ToCompactString};
11
12 #[cfg_attr(docsrs, doc(cfg(feature = "sqlx")))]
13 impl<'r, DB> Decode<'r, DB> for CompactString
14 where
15 DB: Database,
16 for<'x> &'x str: Decode<'x, DB> + Type<DB>,
17 {
18 fn decode(value: <DB as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
19 let value = <&str as Decode<DB>>::decode(value)?;
20 Ok(value.try_to_compact_string()?)
21 }
22 }
23
24 #[cfg(feature = "sqlx-mysql")]
25 #[cfg_attr(docsrs, doc(cfg(feature = "sqlx-mysql")))]
26 impl Type<sqlx::MySql> for CompactString
27 where
28 for<'x> &'x str: Type<sqlx::MySql>,
29 {
30 #[inline]
31 fn type_info() -> <sqlx::MySql as Database>::TypeInfo {
32 <std::string::String as Type<sqlx::MySql>>::type_info()
33 }
34
35 #[inline]
36 fn compatible(ty: &<sqlx::MySql as Database>::TypeInfo) -> bool {
37 <std::string::String as Type<sqlx::MySql>>::compatible(ty)
38 }
39 }
40
41 #[cfg(feature = "sqlx-mysql")]
42 #[cfg_attr(docsrs, doc(cfg(feature = "sqlx-mysql")))]
43 impl<'q> Encode<'q, sqlx::MySql> for CompactString {
44 fn encode_by_ref(
45 &self,
46 buf: &mut <sqlx::MySql as Database>::ArgumentBuffer,
47 ) -> Result<IsNull, BoxDynError> {
48 Encode::<'_, sqlx::MySql>::encode_by_ref(&self.as_str(), buf)
49 }
50
51 #[inline]
52 fn produces(&self) -> Option<<sqlx::MySql as Database>::TypeInfo> {
53 <&str as Encode<'_, sqlx::MySql>>::produces(&self.as_str())
54 }
55
56 #[inline]
57 fn size_hint(&self) -> usize {
58 <&str as Encode<'_, sqlx::MySql>>::size_hint(&self.as_str())
59 }
60 }
61
62 #[cfg(feature = "sqlx-postgres")]
63 #[cfg_attr(docsrs, doc(cfg(feature = "sqlx-postgres")))]
64 impl Type<sqlx::Postgres> for CompactString
65 where
66 for<'x> &'x str: Type<sqlx::Postgres>,
67 {
68 #[inline]
69 fn type_info() -> <sqlx::Postgres as Database>::TypeInfo {
70 <std::string::String as Type<sqlx::Postgres>>::type_info()
71 }
72
73 #[inline]
74 fn compatible(ty: &<sqlx::Postgres as Database>::TypeInfo) -> bool {
75 <std::string::String as Type<sqlx::Postgres>>::compatible(ty)
76 }
77 }
78
79 #[cfg(feature = "sqlx-postgres")]
80 #[cfg_attr(docsrs, doc(cfg(feature = "sqlx-postgres")))]
81 impl<'q> Encode<'q, sqlx::Postgres> for CompactString {
82 fn encode_by_ref(
83 &self,
84 buf: &mut <sqlx::Postgres as Database>::ArgumentBuffer,
85 ) -> Result<IsNull, BoxDynError> {
86 Encode::<'_, sqlx::Postgres>::encode_by_ref(&self.as_str(), buf)
87 }
88
89 #[inline]
90 fn produces(&self) -> Option<<sqlx::Postgres as Database>::TypeInfo> {
91 <&str as Encode<'_, sqlx::Postgres>>::produces(&self.as_str())
92 }
93
94 #[inline]
95 fn size_hint(&self) -> usize {
96 <&str as Encode<'_, sqlx::Postgres>>::size_hint(&self.as_str())
97 }
98 }
99
100 #[cfg(feature = "sqlx-postgres")]
101 #[cfg_attr(docsrs, doc(cfg(feature = "sqlx-postgres")))]
102 impl sqlx::postgres::PgHasArrayType for CompactString {
103 #[inline]
104 fn array_type_info() -> sqlx::postgres::PgTypeInfo {
105 <std::string::String as sqlx::postgres::PgHasArrayType>::array_type_info()
106 }
107
108 #[inline]
109 fn array_compatible(ty: &sqlx::postgres::PgTypeInfo) -> bool {
110 <std::string::String as sqlx::postgres::PgHasArrayType>::array_compatible(ty)
111 }
112 }
113
114 #[cfg(feature = "sqlx-sqlite")]
115 #[cfg_attr(docsrs, doc(cfg(feature = "sqlx-sqlite")))]
116 impl Type<sqlx::Sqlite> for CompactString
117 where
118 for<'x> &'x str: Type<sqlx::Sqlite>,
119 {
120 #[inline]
121 fn type_info() -> <sqlx::Sqlite as Database>::TypeInfo {
122 <std::string::String as Type<sqlx::Sqlite>>::type_info()
123 }
124
125 #[inline]
126 fn compatible(ty: &<sqlx::Sqlite as Database>::TypeInfo) -> bool {
127 <std::string::String as Type<sqlx::Sqlite>>::compatible(ty)
128 }
129 }
130
131 #[cfg(feature = "sqlx-sqlite")]
132 #[cfg_attr(docsrs, doc(cfg(feature = "sqlx-sqlite")))]
133 impl<'q> Encode<'q, sqlx::Sqlite> for CompactString {
134 fn encode(
135 self,
136 buf: &mut <sqlx::Sqlite as Database>::ArgumentBuffer,
137 ) -> Result<IsNull, BoxDynError> {
138 Encode::<'_, sqlx::Sqlite>::encode(self.into_string(), buf)
139 }
140
141 fn encode_by_ref(
142 &self,
143 buf: &mut <sqlx::Sqlite as Database>::ArgumentBuffer,
144 ) -> Result<IsNull, BoxDynError> {
145 Encode::<'_, sqlx::Sqlite>::encode(alloc::string::String::from(self.as_str()), buf)
146 }
147
148 #[inline]
149 fn produces(&self) -> Option<<sqlx::Sqlite as Database>::TypeInfo> {
150 <&str as Encode<'_, sqlx::Sqlite>>::produces(&self.as_str())
151 }
152
153 #[inline]
154 fn size_hint(&self) -> usize {
155 <&str as Encode<'_, sqlx::Sqlite>>::size_hint(&self.as_str())
156 }
157 }