| 1 | use crate::decode::Decode; |
| 2 | use crate::encode::{Encode, IsNull}; |
| 3 | use crate::error::BoxDynError; |
| 4 | use crate::io::MySqlBufMutExt; |
| 5 | use crate::protocol::text::ColumnType; |
| 6 | use crate::types::Type; |
| 7 | use crate::{MySql, MySqlTypeInfo, MySqlValueRef}; |
| 8 | |
| 9 | impl Type<MySql> for [u8] { |
| 10 | fn type_info() -> MySqlTypeInfo { |
| 11 | MySqlTypeInfo::binary(ColumnType::Blob) |
| 12 | } |
| 13 | |
| 14 | fn compatible(ty: &MySqlTypeInfo) -> bool { |
| 15 | matches!( |
| 16 | ty.r#type, |
| 17 | ColumnType::VarChar |
| 18 | | ColumnType::Blob |
| 19 | | ColumnType::TinyBlob |
| 20 | | ColumnType::MediumBlob |
| 21 | | ColumnType::LongBlob |
| 22 | | ColumnType::String |
| 23 | | ColumnType::VarString |
| 24 | | ColumnType::Enum |
| 25 | ) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl Encode<'_, MySql> for &'_ [u8] { |
| 30 | fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> { |
| 31 | buf.put_bytes_lenenc(self); |
| 32 | |
| 33 | Ok(IsNull::No) |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | impl<'r> Decode<'r, MySql> for &'r [u8] { |
| 38 | fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> { |
| 39 | value.as_bytes() |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl Type<MySql> for Box<[u8]> { |
| 44 | fn type_info() -> MySqlTypeInfo { |
| 45 | <&[u8] as Type<MySql>>::type_info() |
| 46 | } |
| 47 | |
| 48 | fn compatible(ty: &MySqlTypeInfo) -> bool { |
| 49 | <&[u8] as Type<MySql>>::compatible(ty) |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | impl Encode<'_, MySql> for Box<[u8]> { |
| 54 | fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> { |
| 55 | <&[u8] as Encode<MySql>>::encode(self.as_ref(), buf) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | impl<'r> Decode<'r, MySql> for Box<[u8]> { |
| 60 | fn decode(value: MySqlValueRef<'r>) -> Result<Self, BoxDynError> { |
| 61 | <&[u8] as Decode<MySql>>::decode(value).map(Box::from) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | impl Type<MySql> for Vec<u8> { |
| 66 | fn type_info() -> MySqlTypeInfo { |
| 67 | <[u8] as Type<MySql>>::type_info() |
| 68 | } |
| 69 | |
| 70 | fn compatible(ty: &MySqlTypeInfo) -> bool { |
| 71 | <&[u8] as Type<MySql>>::compatible(ty) |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | impl Encode<'_, MySql> for Vec<u8> { |
| 76 | fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> { |
| 77 | <&[u8] as Encode<MySql>>::encode(&**self, buf) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | impl Decode<'_, MySql> for Vec<u8> { |
| 82 | fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> { |
| 83 | <&[u8] as Decode<MySql>>::decode(value).map(ToOwned::to_owned) |
| 84 | } |
| 85 | } |