diesel_async/pg/
serialize.rs

1use diesel::pg::PgTypeMetadata;
2use tokio_postgres::types::{private::BytesMut, IsNull, Type, WrongType};
3
4#[derive(Debug)]
5pub(super) struct ToSqlHelper(pub(super) PgTypeMetadata, pub(super) Option<Vec<u8>>);
6
7impl tokio_postgres::types::ToSql for ToSqlHelper {
8    fn to_sql(
9        &self,
10        _ty: &Type,
11        out: &mut BytesMut,
12    ) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>>
13    where
14        Self: Sized,
15    {
16        if let Some(ref bytes) = self.1 {
17            out.extend_from_slice(bytes);
18            Ok(IsNull::No)
19        } else {
20            Ok(IsNull::Yes)
21        }
22    }
23
24    fn accepts(_ty: &Type) -> bool
25    where
26        Self: Sized,
27    {
28        // this should be called anymore
29        true
30    }
31
32    fn to_sql_checked(
33        &self,
34        ty: &Type,
35        out: &mut BytesMut,
36    ) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>> {
37        if Type::from_oid(self.0.oid()?)
38            .map(|d| ty != &d)
39            .unwrap_or(false)
40        {
41            return Err(Box::new(WrongType::new::<Self>(ty.clone())));
42        }
43        self.to_sql(ty, out)
44    }
45}