1use super::int::integral_decimal_str;
2use crate::collation::Collation;
3use crate::decode::Decode;
4use crate::encode::{Encode, IsNull};
5use crate::error::BoxDynError;
6use crate::protocol::text::{ColumnFlags, ColumnType};
7use crate::types::Type;
8use crate::{MySql, MySqlTypeInfo, MySqlValueFormat, MySqlValueRef};
9use byteorder::{ByteOrder, LittleEndian};
10
11fn uint_type_info(ty: ColumnType) -> MySqlTypeInfo {
12 MySqlTypeInfo {
13 r#type: ty,
14 flags: ColumnFlags::BINARY | ColumnFlags::UNSIGNED,
15 collation: Collation::BINARY,
16 max_size: None,
17 }
18}
19
20fn uint_compatible(ty: &MySqlTypeInfo) -> bool {
21 matches!(
22 ty.r#type,
23 ColumnType::Tiny
24 | ColumnType::Short
25 | ColumnType::Long
26 | ColumnType::Int24
27 | ColumnType::LongLong
28 | ColumnType::Year
29 | ColumnType::Bit
30 ) && ty.flags.contains(ColumnFlags::UNSIGNED)
31}
32
33impl Type<MySql> for u8 {
34 fn type_info() -> MySqlTypeInfo {
35 uint_type_info(ColumnType::Tiny)
36 }
37
38 fn compatible(ty: &MySqlTypeInfo) -> bool {
39 uint_compatible(ty)
40 }
41}
42
43impl Type<MySql> for u16 {
44 fn type_info() -> MySqlTypeInfo {
45 uint_type_info(ColumnType::Short)
46 }
47
48 fn compatible(ty: &MySqlTypeInfo) -> bool {
49 uint_compatible(ty)
50 }
51}
52
53impl Type<MySql> for u32 {
54 fn type_info() -> MySqlTypeInfo {
55 uint_type_info(ColumnType::Long)
56 }
57
58 fn compatible(ty: &MySqlTypeInfo) -> bool {
59 uint_compatible(ty)
60 }
61}
62
63impl Type<MySql> for u64 {
64 fn type_info() -> MySqlTypeInfo {
65 uint_type_info(ColumnType::LongLong)
66 }
67
68 fn compatible(ty: &MySqlTypeInfo) -> bool {
69 uint_compatible(ty)
70 }
71}
72
73impl Encode<'_, MySql> for u8 {
74 fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> {
75 buf.extend(&self.to_le_bytes());
76
77 Ok(IsNull::No)
78 }
79}
80
81impl Encode<'_, MySql> for u16 {
82 fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> {
83 buf.extend(&self.to_le_bytes());
84
85 Ok(IsNull::No)
86 }
87}
88
89impl Encode<'_, MySql> for u32 {
90 fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> {
91 buf.extend(&self.to_le_bytes());
92
93 Ok(IsNull::No)
94 }
95}
96
97impl Encode<'_, MySql> for u64 {
98 fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> {
99 buf.extend(&self.to_le_bytes());
100
101 Ok(IsNull::No)
102 }
103}
104
105fn uint_decode(value: MySqlValueRef<'_>) -> Result<u64, BoxDynError> {
106 if value.type_info.r#type == ColumnType::Bit {
107 let buf = value.as_bytes()?;
110 let mut value: u64 = 0;
111
112 for b in buf {
113 value = (*b as u64) | (value << 8);
114 }
115
116 return Ok(value);
117 }
118
119 if matches!(
122 value.type_info.r#type,
123 ColumnType::Decimal | ColumnType::NewDecimal
124 ) {
125 return integral_decimal_str(value.as_str()?)?
126 .parse()
127 .map_err(Into::into);
128 }
129
130 Ok(match value.format() {
131 MySqlValueFormat::Text => value.as_str()?.parse()?,
132
133 MySqlValueFormat::Binary => {
134 let buf = value.as_bytes()?;
135
136 if buf.is_empty() {
138 return Err("empty buffer".into());
139 }
140
141 if buf.len() > 8 {
142 return Err(format!(
143 "expected no more than 8 bytes for unsigned integer value, got {}",
144 buf.len()
145 )
146 .into());
147 }
148
149 LittleEndian::read_uint(buf, buf.len())
150 }
151 })
152}
153
154impl Decode<'_, MySql> for u8 {
155 fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
156 uint_decode(value)?.try_into().map_err(Into::into)
157 }
158}
159
160impl Decode<'_, MySql> for u16 {
161 fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
162 uint_decode(value)?.try_into().map_err(Into::into)
163 }
164}
165
166impl Decode<'_, MySql> for u32 {
167 fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
168 uint_decode(value)?.try_into().map_err(Into::into)
169 }
170}
171
172impl Decode<'_, MySql> for u64 {
173 fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
174 uint_decode(value)
175 }
176}