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
46
use sqlx_core::{
    encode::{Encode, IsNull},
    error::BoxDynError,
    types::Type,
};

use crate::{arguments::ExaBuffer, type_info::ExaDataType, ExaTypeInfo, Exasol};

impl<T> Encode<'_, Exasol> for Option<T>
where
    for<'q> T: Encode<'q, Exasol> + Type<Exasol>,
{
    #[inline]
    fn produces(&self) -> Option<ExaTypeInfo> {
        if let Some(v) = self {
            v.produces()
        } else {
            Some(ExaDataType::Null.into())
        }
    }

    #[inline]
    fn encode(self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
        if let Some(v) = self {
            v.encode(buf)
        } else {
            buf.append(())?;
            Ok(IsNull::Yes)
        }
    }

    #[inline]
    fn encode_by_ref(&self, buf: &mut ExaBuffer) -> Result<IsNull, BoxDynError> {
        if let Some(v) = self {
            v.encode_by_ref(buf)
        } else {
            buf.append(())?;
            Ok(IsNull::Yes)
        }
    }

    #[inline]
    fn size_hint(&self) -> usize {
        self.as_ref().map(Encode::size_hint).unwrap_or_default()
    }
}