sqlx_mysql/types/
bool.rs

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