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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use crate::decode::Decode;
use crate::encode::{Encode, IsNull};
use crate::error::BoxDynError;
use crate::io::MySqlBufMutExt;
use crate::protocol::text::ColumnType;
use crate::types::Type;
use crate::{MySql, MySqlTypeInfo, MySqlValueRef};

impl Type<MySql> for [u8] {
    fn type_info() -> MySqlTypeInfo {
        MySqlTypeInfo::binary(ColumnType::Blob)
    }

    fn compatible(ty: &MySqlTypeInfo) -> bool {
        matches!(
            ty.r#type,
            ColumnType::VarChar
                | ColumnType::Blob
                | ColumnType::TinyBlob
                | ColumnType::MediumBlob
                | ColumnType::LongBlob
                | ColumnType::String
                | ColumnType::VarString
                | ColumnType::Enum
        )
    }
}

impl Encode<'_, MySql> for &'_ [u8] {
    fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
        buf.put_bytes_lenenc(self);

        IsNull::No
    }
}

impl<'r> Decode<'r, MySql> for &'r [u8] {
    fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> {
        value.as_bytes()
    }
}

impl Type<MySql> for Box<[u8]> {
    fn type_info() -> MySqlTypeInfo {
        <&[u8] as Type<MySql>>::type_info()
    }

    fn compatible(ty: &MySqlTypeInfo) -> bool {
        <&[u8] as Type<MySql>>::compatible(ty)
    }
}

impl Encode<'_, MySql> for Box<[u8]> {
    fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
        <&[u8] as Encode<MySql>>::encode(self.as_ref(), buf)
    }
}

impl<'r> Decode<'r, MySql> for Box<[u8]> {
    fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> {
        <&[u8] as Decode<MySql>>::decode(value).map(Box::from)
    }
}

impl Type<MySql> for Vec<u8> {
    fn type_info() -> MySqlTypeInfo {
        <[u8] as Type<MySql>>::type_info()
    }

    fn compatible(ty: &MySqlTypeInfo) -> bool {
        <&[u8] as Type<MySql>>::compatible(ty)
    }
}

impl Encode<'_, MySql> for Vec<u8> {
    fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
        <&[u8] as Encode<MySql>>::encode(&**self, buf)
    }
}

impl Decode<'_, MySql> for Vec<u8> {
    fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
        <&[u8] as Decode<MySql>>::decode(value).map(ToOwned::to_owned)
    }
}