1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use crate::protocol::text::{ColumnDefinition, ColumnType};
use rbdc::ext::ustr::UStr;

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MySqlColumn {
    pub ordinal: usize,
    pub name: UStr,
    pub type_info: MySqlTypeInfo,
    // #[serde(skip)]
    // pub flags: Option<ColumnFlags>,
}

/// Type information for a MySql type.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct MySqlTypeInfo {
    pub r#type: ColumnType,
    // pub flags: ColumnFlags,
    pub char_set: u16,
    // [max_size] for integer types, this is (M) in BIT(M) or TINYINT(M)
    // #[serde(default)]
    // pub max_size: Option<u32>,
}
impl MySqlTypeInfo {
    fn is_null(&self) -> bool {
        matches!(self.r#type, ColumnType::Null)
    }

    pub const fn binary(ty: ColumnType) -> Self {
        Self {
            r#type: ty,
            // flags: ColumnFlags::BINARY,
            char_set: 63,
        }
    }

    #[doc(hidden)]
    pub const fn null() -> Self {
        Self {
            r#type: ColumnType::Null,
            // flags: ColumnFlags::BINARY,
            char_set: 63,
        }
    }

    #[doc(hidden)]
    pub const fn __enum() -> Self {
        Self {
            r#type: ColumnType::Enum,
            // flags: ColumnFlags::BINARY,
            char_set: 63,
        }
    }

    #[doc(hidden)]
    pub fn __type_feature_gate(&self) -> Option<&'static str> {
        match self.r#type {
            ColumnType::Date | ColumnType::Time | ColumnType::Timestamp | ColumnType::Datetime => {
                Some("time")
            }
            ColumnType::Json => Some("json"),
            ColumnType::NewDecimal => Some("bigdecimal"),
            _ => None,
        }
    }

    pub(crate) fn from_column(column: &ColumnDefinition) -> Self {
        Self {
            r#type: column.r#type,
            // flags: column.flags,
            char_set: column.char_set,
        }
    }

    pub(crate) fn from_type(ty: ColumnType) -> Self {
        Self {
            r#type: ty,
            // flags: column.flags,
            char_set: 63,
        }
    }
}