| 1 |
use crate::backend::Backend; |
| 2 |
use crate::deserialize::FromSqlRef; |
| 3 |
use crate::deserialize::{self, FromSql, Queryable}; |
| 4 |
use crate::query_builder::bind_collector::RawBytesBindCollector; |
| 5 |
use crate::serialize::{self, IsNull, Output, ToSql}; |
| 6 |
use crate::sql_types::{ |
| 7 |
self, BigInt, Binary, Bool, Double, Float, Integer, SingleValue, SmallInt, Text, |
| 8 |
}; |
| 9 |
use std::borrow::Cow; |
| 10 |
use std::error::Error; |
| 11 |
use std::fmt; |
| 12 |
use std::io::Write; |
| 13 |
|
| 14 |
#[allow(dead_code)] |
| 15 |
mod foreign_impls { |
| 16 |
use super::*; |
| 17 |
use crate::deserialize::FromSqlRow; |
| 18 |
|
| 19 |
#[derive(AsExpression, FromSqlRow)] |
| 20 |
#[diesel(foreign_derive)] |
| 21 |
#[diesel(sql_type = Bool)] |
| 22 |
struct BoolProxy(bool); |
| 23 |
|
| 24 |
#[derive(FromSqlRow)] |
| 25 |
#[cfg_attr(feature = "mysql_backend", derive(AsExpression))] |
| 26 |
#[diesel(foreign_derive)] |
| 27 |
#[cfg_attr(feature = "mysql_backend", diesel(sql_type = crate::sql_types::TinyInt))] |
| 28 |
struct I8Proxy(i8); |
| 29 |
|
| 30 |
#[derive(AsExpression, FromSqlRow)] |
| 31 |
#[diesel(foreign_derive)] |
| 32 |
#[diesel(sql_type = SmallInt)] |
| 33 |
struct I16Proxy(i16); |
| 34 |
|
| 35 |
#[derive(AsExpression, FromSqlRow)] |
| 36 |
#[diesel(foreign_derive)] |
| 37 |
#[diesel(sql_type = Integer)] |
| 38 |
struct I32Proxy(i32); |
| 39 |
|
| 40 |
#[derive(AsExpression, FromSqlRow)] |
| 41 |
#[diesel(foreign_derive)] |
| 42 |
#[diesel(sql_type = BigInt)] |
| 43 |
struct I64Proxy(i64); |
| 44 |
|
| 45 |
#[derive(FromSqlRow)] |
| 46 |
#[cfg_attr( |
| 47 |
any(feature = "mysql_backend", feature = "postgres_backend"), |
| 48 |
derive(AsExpression) |
| 49 |
)] |
| 50 |
#[diesel(foreign_derive)] |
| 51 |
#[cfg_attr( |
| 52 |
feature = "mysql_backend", |
| 53 |
diesel(sql_type = crate::sql_types::Unsigned<crate::sql_types::TinyInt>) |
| 54 |
)] |
| 55 |
#[cfg_attr(feature = "postgres_backend", diesel(foreign_derive, sql_type = crate::sql_types::CChar))] |
| 56 |
struct U8Proxy(u8); |
| 57 |
|
| 58 |
#[derive(FromSqlRow)] |
| 59 |
#[cfg_attr(feature = "mysql_backend", derive(AsExpression))] |
| 60 |
#[diesel(foreign_derive)] |
| 61 |
#[cfg_attr(feature = "mysql_backend", diesel(sql_type = crate::sql_types::Unsigned<SmallInt>))] |
| 62 |
struct U16Proxy(u16); |
| 63 |
|
| 64 |
#[derive(FromSqlRow)] |
| 65 |
#[cfg_attr( |
| 66 |
any(feature = "mysql_backend", feature = "postgres_backend"), |
| 67 |
derive(AsExpression) |
| 68 |
)] |
| 69 |
#[diesel(foreign_derive)] |
| 70 |
#[cfg_attr(feature = "mysql_backend", diesel(sql_type = crate::sql_types::Unsigned<Integer>))] |
| 71 |
#[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::sql_types::Oid))] |
| 72 |
struct U32Proxy(u32); |
| 73 |
|
| 74 |
#[derive(FromSqlRow)] |
| 75 |
#[cfg_attr(feature = "mysql_backend", derive(AsExpression))] |
| 76 |
#[diesel(foreign_derive)] |
| 77 |
#[cfg_attr(feature = "mysql_backend", diesel(sql_type = crate::sql_types::Unsigned<BigInt>))] |
| 78 |
struct U64Proxy(u64); |
| 79 |
|
| 80 |
#[derive(AsExpression, FromSqlRow)] |
| 81 |
#[diesel(foreign_derive)] |
| 82 |
#[diesel(sql_type = Float)] |
| 83 |
struct F32Proxy(f32); |
| 84 |
|
| 85 |
#[derive(AsExpression, FromSqlRow)] |
| 86 |
#[diesel(foreign_derive)] |
| 87 |
#[diesel(sql_type = Double)] |
| 88 |
struct F64Proxy(f64); |
| 89 |
|
| 90 |
#[derive(AsExpression, FromSqlRow)] |
| 91 |
#[diesel(foreign_derive)] |
| 92 |
#[diesel(sql_type = Text)] |
| 93 |
#[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Date))] |
| 94 |
#[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Time))] |
| 95 |
#[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Timestamp))] |
| 96 |
#[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::sql_types::Citext))] |
| 97 |
struct StringProxy(String); |
| 98 |
|
| 99 |
#[derive(AsExpression)] |
| 100 |
#[diesel(foreign_derive, not_sized)] |
| 101 |
#[diesel(sql_type = Text)] |
| 102 |
#[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Date))] |
| 103 |
#[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Time))] |
| 104 |
#[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Timestamp))] |
| 105 |
#[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::sql_types::Citext))] |
| 106 |
struct StrProxy(str); |
| 107 |
|
| 108 |
#[derive(FromSqlRow)] |
| 109 |
#[diesel(foreign_derive)] |
| 110 |
struct VecProxy<T>(Vec<T>); |
| 111 |
|
| 112 |
#[derive(AsExpression)] |
| 113 |
#[diesel(foreign_derive)] |
| 114 |
#[diesel(sql_type = Binary)] |
| 115 |
struct BinaryVecProxy(Vec<u8>); |
| 116 |
|
| 117 |
#[derive(AsExpression)] |
| 118 |
#[diesel(foreign_derive, not_sized)] |
| 119 |
#[diesel(sql_type = Binary)] |
| 120 |
struct BinarySliceProxy([u8]); |
| 121 |
|
| 122 |
#[derive(AsExpression)] |
| 123 |
#[diesel(foreign_derive)] |
| 124 |
#[diesel(sql_type = Binary)] |
| 125 |
struct BinaryArrayProxy<const N: usize>([u8; N]); |
| 126 |
} |
| 127 |
|
| 128 |
#[diagnostic::do_not_recommend] |
| 129 |
impl<ST, DB> FromSql<ST, DB> for String |
| 130 |
where |
| 131 |
DB: Backend, |
| 132 |
for<'a> &'a str: FromSqlRef<'a, ST, DB>, |
| 133 |
{ |
| 134 |
fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> { |
| 135 |
<&str as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(|v| v.to_owned()) |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
impl<DB> ToSql<sql_types::Text, DB> for str |
| 140 |
where |
| 141 |
for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>, |
| 142 |
{ |
| 143 |
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { |
| 144 |
out.write_all(self.as_bytes()) |
| 145 |
.map(|_| IsNull::No) |
| 146 |
.map_err(|e| Box::new(e) as Box<dyn Error + Send + Sync>) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
impl<DB> ToSql<sql_types::Text, DB> for String |
| 151 |
where |
| 152 |
DB: Backend, |
| 153 |
str: ToSql<sql_types::Text, DB>, |
| 154 |
{ |
| 155 |
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { |
| 156 |
(self as &str).to_sql(out) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
impl<ST, DB> FromSql<ST, DB> for Vec<u8> |
| 161 |
where |
| 162 |
DB: Backend, |
| 163 |
for<'a> &'a [u8]: FromSqlRef<'a, ST, DB>, |
| 164 |
{ |
| 165 |
fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result<Self> { |
| 166 |
<&[u8] as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(|v| v.to_owned()) |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
impl<DB> ToSql<sql_types::Binary, DB> for Vec<u8> |
| 171 |
where |
| 172 |
DB: Backend, |
| 173 |
[u8]: ToSql<sql_types::Binary, DB>, |
| 174 |
{ |
| 175 |
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { |
| 176 |
(self as &[u8]).to_sql(out) |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
impl<DB, const N: usize> ToSql<sql_types::Binary, DB> for [u8; N] |
| 181 |
where |
| 182 |
DB: Backend, |
| 183 |
[u8]: ToSql<sql_types::Binary, DB>, |
| 184 |
{ |
| 185 |
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { |
| 186 |
self.as_slice().to_sql(out) |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
impl<DB> ToSql<sql_types::Binary, DB> for [u8] |
| 191 |
where |
| 192 |
for<'a> DB: Backend<BindCollector<'a> = RawBytesBindCollector<DB>>, |
| 193 |
{ |
| 194 |
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { |
| 195 |
out.write_all(self) |
| 196 |
.map(|_| IsNull::No) |
| 197 |
.map_err(|e| Box::new(e) as Box<dyn Error + Send + Sync>) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
impl<'a, T: ?Sized, ST, DB> ToSql<ST, DB> for Cow<'a, T> |
| 202 |
where |
| 203 |
T: 'a + ToOwned + ToSql<ST, DB>, |
| 204 |
DB: Backend, |
| 205 |
Self: fmt::Debug, |
| 206 |
{ |
| 207 |
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { |
| 208 |
ToSql::<ST, DB>::to_sql(&**self, out) |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
impl<'a, T: ?Sized, ST, DB> FromSql<ST, DB> for Cow<'a, T> |
| 213 |
where |
| 214 |
T: 'a + ToOwned, |
| 215 |
DB: Backend, |
| 216 |
T::Owned: FromSql<ST, DB>, |
| 217 |
{ |
| 218 |
fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result<Self> { |
| 219 |
T::Owned::from_sql(bytes).map(Cow::Owned) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
impl<'a, T: ?Sized, ST, DB> Queryable<ST, DB> for Cow<'a, T> |
| 224 |
where |
| 225 |
T: 'a + ToOwned, |
| 226 |
ST: SingleValue, |
| 227 |
DB: Backend, |
| 228 |
Self: FromSql<ST, DB>, |
| 229 |
{ |
| 230 |
type Row = Self; |
| 231 |
|
| 232 |
fn build(row: Self::Row) -> deserialize::Result<Self> { |
| 233 |
Ok(row) |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
use crate::expression::bound::Bound; |
| 238 |
use crate::expression::{AsExpression, Expression, TypedExpressionType}; |
| 239 |
use sql_types::SqlType; |
| 240 |
|
| 241 |
impl<'a, T: ?Sized, ST> AsExpression<ST> for Cow<'a, T> |
| 242 |
where |
| 243 |
T: 'a + ToOwned, |
| 244 |
Bound<ST, Cow<'a, T>>: Expression<SqlType = ST>, |
| 245 |
ST: SqlType + TypedExpressionType, |
| 246 |
{ |
| 247 |
type Expression = Bound<ST, Self>; |
| 248 |
|
| 249 |
fn as_expression(self) -> Self::Expression { |
| 250 |
Bound::new(self) |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
impl<'a, 'b, T: ?Sized, ST> AsExpression<ST> for &'b Cow<'a, T> |
| 255 |
where |
| 256 |
T: 'a + ToOwned, |
| 257 |
Bound<ST, &'b T>: Expression<SqlType = ST>, |
| 258 |
ST: SqlType + TypedExpressionType, |
| 259 |
{ |
| 260 |
type Expression = Bound<ST, &'b T>; |
| 261 |
|
| 262 |
fn as_expression(self) -> Self::Expression { |
| 263 |
Bound::new(&**self) |
| 264 |
} |
| 265 |
} |