cdbc_mysql/types/
bool.rs

1use cdbc::decode::Decode;
2use cdbc::encode::{Encode, IsNull};
3use cdbc::error::BoxDynError;
4use crate::{
5    protocol::text::{ColumnFlags, ColumnType},
6    MySql, MySqlTypeInfo, MySqlValueRef,
7};
8use cdbc::types::Type;
9
10impl 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            char_set: 63,
16            max_size: Some(1),
17            r#type: ColumnType::Tiny,
18        }
19    }
20
21    fn compatible(ty: &MySqlTypeInfo) -> bool {
22        matches!(
23            ty.r#type,
24            ColumnType::Tiny
25                | ColumnType::Short
26                | ColumnType::Long
27                | ColumnType::Int24
28                | ColumnType::LongLong
29                | ColumnType::Bit
30        )
31    }
32}
33
34impl Encode<'_, MySql> for bool {
35    fn encode_by_ref(&self, buf: &mut Vec<u8>) -> IsNull {
36        <i8 as Encode<MySql>>::encode(*self as i8, buf)
37    }
38}
39
40impl Decode<'_, MySql> for bool {
41    fn decode(value: MySqlValueRef<'_>) -> Result<Self, BoxDynError> {
42        Ok(<i8 as Decode<MySql>>::decode(value)? != 0)
43    }
44}