1use crate::arguments::XuguArgumentValue;
2use crate::protocol::text::ColumnType;
3use crate::{Xugu, XuguTypeInfo, XuguValueRef};
4use sqlx_core::decode::Decode;
5use sqlx_core::encode::{Encode, IsNull};
6use sqlx_core::error::BoxDynError;
7use sqlx_core::types::Type;
8use std::borrow::Cow;
9
10impl Type<Xugu> for [u8] {
11 fn type_info() -> XuguTypeInfo {
12 XuguTypeInfo::binary(ColumnType::BLOB)
13 }
14
15 fn compatible(ty: &XuguTypeInfo) -> bool {
16 matches!(
17 ty.r#type,
18 ColumnType::BLOB
19 | ColumnType::BLOB_I
20 | ColumnType::BLOB_M
21 | ColumnType::BLOB_OM
22 | ColumnType::BLOB_S
23 | ColumnType::CHAR
24 | ColumnType::NCHAR
25 | ColumnType::CLOB
26 | ColumnType::BINARY
27 | ColumnType::GUID
28 )
29 }
30}
31
32impl<'q> Encode<'q, Xugu> for &'q [u8] {
33 fn encode_by_ref(&self, args: &mut Vec<XuguArgumentValue<'q>>) -> Result<IsNull, BoxDynError> {
34 args.push(XuguArgumentValue::Bin(Cow::Borrowed(self)));
35
36 Ok(IsNull::No)
37 }
38}
39
40impl<'q> Decode<'q, Xugu> for &'q [u8] {
41 fn decode(value: XuguValueRef<'q>) -> Result<Self, BoxDynError> {
42 value.as_bytes()
43 }
44}
45
46impl Type<Xugu> for Box<[u8]> {
47 fn type_info() -> XuguTypeInfo {
48 <&[u8] as Type<Xugu>>::type_info()
49 }
50
51 fn compatible(ty: &XuguTypeInfo) -> bool {
52 <&[u8] as Type<Xugu>>::compatible(ty)
53 }
54}
55
56impl Encode<'_, Xugu> for Box<[u8]> {
57 fn encode(self, args: &mut Vec<XuguArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
58 args.push(XuguArgumentValue::Bin(Cow::Owned(self.into_vec())));
59
60 Ok(IsNull::No)
61 }
62
63 fn encode_by_ref(&self, args: &mut Vec<XuguArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
64 args.push(XuguArgumentValue::Bin(Cow::Owned(self.clone().into_vec())));
65
66 Ok(IsNull::No)
67 }
68}
69
70impl<'r> Decode<'r, Xugu> for Box<[u8]> {
71 fn decode(value: XuguValueRef<'r>) -> Result<Self, BoxDynError> {
72 value.as_bytes().map(Box::from)
73 }
74}
75
76impl Type<Xugu> for Vec<u8> {
77 fn type_info() -> XuguTypeInfo {
78 <[u8] as Type<Xugu>>::type_info()
79 }
80
81 fn compatible(ty: &XuguTypeInfo) -> bool {
82 <&[u8] as Type<Xugu>>::compatible(ty)
83 }
84}
85
86impl Encode<'_, Xugu> for Vec<u8> {
87 fn encode(self, args: &mut Vec<XuguArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
88 args.push(XuguArgumentValue::Bin(Cow::Owned(self)));
89
90 Ok(IsNull::No)
91 }
92
93 fn encode_by_ref(&self, args: &mut Vec<XuguArgumentValue<'_>>) -> Result<IsNull, BoxDynError> {
94 args.push(XuguArgumentValue::Bin(Cow::Owned(self.clone())));
95
96 Ok(IsNull::No)
97 }
98}
99
100impl Decode<'_, Xugu> for Vec<u8> {
101 fn decode(value: XuguValueRef<'_>) -> Result<Self, BoxDynError> {
102 value.as_bytes().map(ToOwned::to_owned)
103 }
104}