use crate::backend::Backend; use crate::deserialize::FromSqlRef; use crate::deserialize::{self, FromSql, Queryable}; use crate::query_builder::bind_collector::RawBytesBindCollector; use crate::serialize::{self, IsNull, Output, ToSql}; use crate::sql_types::{ self, BigInt, Binary, Bool, Double, Float, Integer, SingleValue, SmallInt, Text, }; use std::borrow::Cow; use std::error::Error; use std::fmt; use std::io::Write; #[allow(dead_code)] mod foreign_impls { use super::*; use crate::deserialize::FromSqlRow; #[derive(AsExpression, FromSqlRow)] #[diesel(foreign_derive)] #[diesel(sql_type = Bool)] struct BoolProxy(bool); #[derive(FromSqlRow)] #[cfg_attr(feature = "mysql_backend", derive(AsExpression))] #[diesel(foreign_derive)] #[cfg_attr(feature = "mysql_backend", diesel(sql_type = crate::sql_types::TinyInt))] struct I8Proxy(i8); #[derive(AsExpression, FromSqlRow)] #[diesel(foreign_derive)] #[diesel(sql_type = SmallInt)] struct I16Proxy(i16); #[derive(AsExpression, FromSqlRow)] #[diesel(foreign_derive)] #[diesel(sql_type = Integer)] struct I32Proxy(i32); #[derive(AsExpression, FromSqlRow)] #[diesel(foreign_derive)] #[diesel(sql_type = BigInt)] struct I64Proxy(i64); #[derive(FromSqlRow)] #[cfg_attr( any(feature = "mysql_backend", feature = "postgres_backend"), derive(AsExpression) )] #[diesel(foreign_derive)] #[cfg_attr( feature = "mysql_backend", diesel(sql_type = crate::sql_types::Unsigned) )] #[cfg_attr(feature = "postgres_backend", diesel(foreign_derive, sql_type = crate::sql_types::CChar))] struct U8Proxy(u8); #[derive(FromSqlRow)] #[cfg_attr(feature = "mysql_backend", derive(AsExpression))] #[diesel(foreign_derive)] #[cfg_attr(feature = "mysql_backend", diesel(sql_type = crate::sql_types::Unsigned))] struct U16Proxy(u16); #[derive(FromSqlRow)] #[cfg_attr( any(feature = "mysql_backend", feature = "postgres_backend"), derive(AsExpression) )] #[diesel(foreign_derive)] #[cfg_attr(feature = "mysql_backend", diesel(sql_type = crate::sql_types::Unsigned))] #[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::sql_types::Oid))] struct U32Proxy(u32); #[derive(FromSqlRow)] #[cfg_attr(feature = "mysql_backend", derive(AsExpression))] #[diesel(foreign_derive)] #[cfg_attr(feature = "mysql_backend", diesel(sql_type = crate::sql_types::Unsigned))] struct U64Proxy(u64); #[derive(AsExpression, FromSqlRow)] #[diesel(foreign_derive)] #[diesel(sql_type = Float)] struct F32Proxy(f32); #[derive(AsExpression, FromSqlRow)] #[diesel(foreign_derive)] #[diesel(sql_type = Double)] struct F64Proxy(f64); #[derive(AsExpression, FromSqlRow)] #[diesel(foreign_derive)] #[diesel(sql_type = Text)] #[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Date))] #[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Time))] #[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Timestamp))] #[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::sql_types::Citext))] struct StringProxy(String); #[derive(AsExpression)] #[diesel(foreign_derive, not_sized)] #[diesel(sql_type = Text)] #[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Date))] #[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Time))] #[cfg_attr(feature = "sqlite", diesel(sql_type = crate::sql_types::Timestamp))] #[cfg_attr(feature = "postgres_backend", diesel(sql_type = crate::sql_types::Citext))] struct StrProxy(str); #[derive(FromSqlRow)] #[diesel(foreign_derive)] struct VecProxy(Vec); #[derive(AsExpression)] #[diesel(foreign_derive)] #[diesel(sql_type = Binary)] struct BinaryVecProxy(Vec); #[derive(AsExpression)] #[diesel(foreign_derive, not_sized)] #[diesel(sql_type = Binary)] struct BinarySliceProxy([u8]); #[derive(AsExpression)] #[diesel(foreign_derive)] #[diesel(sql_type = Binary)] struct BinaryArrayProxy([u8; N]); } #[diagnostic::do_not_recommend] impl FromSql for String where DB: Backend, for<'a> &'a str: FromSqlRef<'a, ST, DB>, { fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result { <&str as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(|v| v.to_owned()) } } impl ToSql for str where for<'a> DB: Backend = RawBytesBindCollector>, { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { out.write_all(self.as_bytes()) .map(|_| IsNull::No) .map_err(|e| Box::new(e) as Box) } } impl ToSql for String where DB: Backend, str: ToSql, { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { (self as &str).to_sql(out) } } impl FromSql for Vec where DB: Backend, for<'a> &'a [u8]: FromSqlRef<'a, ST, DB>, { fn from_sql(mut bytes: DB::RawValue<'_>) -> deserialize::Result { <&[u8] as FromSqlRef<'_, ST, DB>>::from_sql(&mut bytes).map(|v| v.to_owned()) } } impl ToSql for Vec where DB: Backend, [u8]: ToSql, { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { (self as &[u8]).to_sql(out) } } impl ToSql for [u8; N] where DB: Backend, [u8]: ToSql, { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { self.as_slice().to_sql(out) } } impl ToSql for [u8] where for<'a> DB: Backend = RawBytesBindCollector>, { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { out.write_all(self) .map(|_| IsNull::No) .map_err(|e| Box::new(e) as Box) } } impl<'a, T: ?Sized, ST, DB> ToSql for Cow<'a, T> where T: 'a + ToOwned + ToSql, DB: Backend, Self: fmt::Debug, { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> serialize::Result { ToSql::::to_sql(&**self, out) } } impl<'a, T: ?Sized, ST, DB> FromSql for Cow<'a, T> where T: 'a + ToOwned, DB: Backend, T::Owned: FromSql, { fn from_sql(bytes: DB::RawValue<'_>) -> deserialize::Result { T::Owned::from_sql(bytes).map(Cow::Owned) } } impl<'a, T: ?Sized, ST, DB> Queryable for Cow<'a, T> where T: 'a + ToOwned, ST: SingleValue, DB: Backend, Self: FromSql, { type Row = Self; fn build(row: Self::Row) -> deserialize::Result { Ok(row) } } use crate::expression::bound::Bound; use crate::expression::{AsExpression, Expression, TypedExpressionType}; use sql_types::SqlType; impl<'a, T: ?Sized, ST> AsExpression for Cow<'a, T> where T: 'a + ToOwned, Bound>: Expression, ST: SqlType + TypedExpressionType, { type Expression = Bound; fn as_expression(self) -> Self::Expression { Bound::new(self) } } impl<'a, 'b, T: ?Sized, ST> AsExpression for &'b Cow<'a, T> where T: 'a + ToOwned, Bound: Expression, ST: SqlType + TypedExpressionType, { type Expression = Bound; fn as_expression(self) -> Self::Expression { Bound::new(&**self) } }