use std::io::prelude::*; #[cfg(feature = "postgres_backend")] use crate::deserialize::FromSqlRef; use crate::deserialize::{self, FromSql, Queryable}; use crate::pg::{Pg, PgValue}; use crate::serialize::{self, IsNull, Output, ToSql}; use crate::sql_types; #[cfg(feature = "postgres_backend")] impl FromSql for bool { fn from_sql(bytes: PgValue<'_>) -> deserialize::Result { let first_byte = bytes .as_bytes() .first() .ok_or("Received an empty response from the server")?; Ok(*first_byte != 0) } } #[cfg(feature = "postgres_backend")] impl ToSql for bool { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result { out.write_all(&[*self as u8]) .map(|_| IsNull::No) .map_err(Into::into) } } #[cfg(feature = "postgres_backend")] impl FromSql for u8 { fn from_sql(bytes: PgValue<'_>) -> deserialize::Result { let first_byte = bytes .as_bytes() .first() .ok_or("Received an empty response from the server")?; Ok(*first_byte) } } #[cfg(feature = "postgres_backend")] impl ToSql for u8 { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result { out.write_all(&[*self]) .map(|_| IsNull::No) .map_err(Into::into) } } /// The returned pointer is *only* valid for the lifetime to the argument of /// `from_sql`. This impl is intended for uses where you want to write a new /// impl in terms of `String`, but don't want to allocate. We have to return a /// raw pointer instead of a reference with a lifetime due to the structure of /// `FromSql` #[cfg(feature = "postgres_backend")] impl FromSql for *const str { fn from_sql(value: PgValue<'_>) -> deserialize::Result { use std::str; let string = str::from_utf8(value.as_bytes())?; Ok(string as *const _) } } #[cfg(feature = "postgres_backend")] impl<'a> FromSqlRef<'a, sql_types::Text, Pg> for &'a str { fn from_sql(value: &'a mut PgValue<'_>) -> deserialize::Result { Ok(core::str::from_utf8(value.as_bytes())?) } } #[cfg(feature = "postgres_backend")] impl Queryable for *const str { type Row = Self; fn build(row: Self::Row) -> deserialize::Result { Ok(row) } } #[cfg(feature = "postgres_backend")] impl ToSql for String { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result { out.write_all(self.as_bytes())?; Ok(IsNull::No) } } #[cfg(feature = "postgres_backend")] impl ToSql for str { fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Pg>) -> serialize::Result { out.write_all(self.as_bytes())?; Ok(IsNull::No) } } #[cfg(feature = "postgres_backend")] impl FromSql for String { fn from_sql(value: PgValue<'_>) -> deserialize::Result { let string = String::from_utf8(value.as_bytes().to_vec())?; Ok(string) } } /// The returned pointer is *only* valid for the lifetime to the argument of /// `from_sql`. This impl is intended for uses where you want to write a new /// impl in terms of `Vec`, but don't want to allocate. We have to return a /// raw pointer instead of a reference with a lifetime due to the structure of /// `FromSql` #[cfg(feature = "postgres_backend")] impl FromSql for *const [u8] { fn from_sql(value: PgValue<'_>) -> deserialize::Result { Ok(value.as_bytes() as *const _) } } impl<'a> FromSqlRef<'a, sql_types::Binary, Pg> for &'a [u8] { fn from_sql(bytes: &'a mut PgValue<'_>) -> deserialize::Result { Ok(bytes.as_bytes()) } } #[cfg(feature = "postgres_backend")] impl Queryable for *const [u8] { type Row = Self; fn build(row: Self::Row) -> deserialize::Result { Ok(row) } } #[cfg(test)] mod tests { use super::*; #[diesel_test_helper::test] fn cchar_to_sql() { use crate::query_builder::bind_collector::ByteWrapper; let mut buffer = Vec::new(); let mut bytes = Output::test(ByteWrapper(&mut buffer)); ToSql::::to_sql(&b'A', &mut bytes).unwrap(); ToSql::::to_sql(&b'\xc4', &mut bytes).unwrap(); assert_eq!(buffer, vec![65u8, 196u8]); } #[diesel_test_helper::test] fn cchar_from_sql() { let result = >::from_nullable_sql(None); assert_eq!( result.unwrap_err().to_string(), "Unexpected null for non-null column" ); } #[diesel_test_helper::test] fn bool_to_sql() { use crate::query_builder::bind_collector::ByteWrapper; let mut buffer = Vec::new(); let mut bytes = Output::test(ByteWrapper(&mut buffer)); ToSql::::to_sql(&true, &mut bytes).unwrap(); ToSql::::to_sql(&false, &mut bytes).unwrap(); assert_eq!(buffer, vec![1u8, 0u8]); } #[diesel_test_helper::test] fn no_bool_from_sql() { let result = >::from_nullable_sql(None); assert_eq!( result.unwrap_err().to_string(), "Unexpected null for non-null column" ); } }