diesel_async/pg/
serialize.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use diesel::pg::PgTypeMetadata;
use tokio_postgres::types::{private::BytesMut, IsNull, Type, WrongType};

#[derive(Debug)]
pub(super) struct ToSqlHelper(pub(super) PgTypeMetadata, pub(super) Option<Vec<u8>>);

impl tokio_postgres::types::ToSql for ToSqlHelper {
    fn to_sql(
        &self,
        _ty: &Type,
        out: &mut BytesMut,
    ) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>>
    where
        Self: Sized,
    {
        if let Some(ref bytes) = self.1 {
            out.extend_from_slice(bytes);
            Ok(IsNull::No)
        } else {
            Ok(IsNull::Yes)
        }
    }

    fn accepts(_ty: &Type) -> bool
    where
        Self: Sized,
    {
        // this should be called anymore
        true
    }

    fn to_sql_checked(
        &self,
        ty: &Type,
        out: &mut BytesMut,
    ) -> Result<IsNull, Box<dyn std::error::Error + Sync + Send>> {
        if Type::from_oid(self.0.oid()?)
            .map(|d| ty != &d)
            .unwrap_or(false)
        {
            return Err(Box::new(WrongType::new::<Self>(ty.clone())));
        }
        self.to_sql(ty, out)
    }
}