| 1 | use crate::decode::Decode; |
| 2 | use crate::encode::{Encode, IsNull}; |
| 3 | use crate::error::BoxDynError; |
| 4 | use crate::types::Type; |
| 5 | use crate::{ |
| 6 | protocol::text::{ColumnFlags, ColumnType}, |
| 7 | MySql, MySqlTypeInfo, MySqlValueRef, |
| 8 | }; |
| 9 | |
| 10 | impl Type<MySql> for bool { |
| 11 | fn type_info() -> MySqlTypeInfo { |
| 12 | // MySQL has no actual `BOOLEAN` type, the type is an alias of `TINYINT(1)` |
| 13 | MySqlTypeInfo { |
| 14 | flags: ColumnFlags::BINARY | ColumnFlags::UNSIGNED, |
| 15 | max_size: Some(1), |
| 16 | r#type: ColumnType::Tiny, |
| 17 | } |
| 18 | } |
| 19 | |
| 20 | fn 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::Bit |
| 29 | ) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl Encode<'_, MySql> for bool { |
| 34 | fn encode_by_ref(&self, buf: &mut Vec<u8>) -> Result<IsNull, BoxDynError> { |
| 35 | <i8 as Encode<MySql>>::encode(*self as i8, buf) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl Decode<'_, MySql> for bool { |
| 40 | fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> { |
| 41 | Ok(<i8 as Decode<MySql>>::decode(value)? != 0) |
| 42 | } |
| 43 | } |