1use std::fmt::{self, Display, Formatter};
2
3pub(crate) use sqlx_core::type_info::*;
4
5use crate::protocol::text::{ColumnDefinition, ColumnFlags, ColumnType};
6
7#[derive(Debug, Clone)]
9#[cfg_attr(feature = "offline", derive(serde::Serialize, serde::Deserialize))]
10pub struct MySqlTypeInfo {
11 pub(crate) r#type: ColumnType,
12 pub(crate) flags: ColumnFlags,
13
14 #[cfg_attr(feature = "offline", serde(default))]
16 pub(crate) max_size: Option<u32>,
17}
18
19impl MySqlTypeInfo {
20 pub(crate) const fn binary(ty: ColumnType) -> Self {
21 Self {
22 r#type: ty,
23 flags: ColumnFlags::BINARY,
24 max_size: None,
25 }
26 }
27
28 #[doc(hidden)]
29 pub const fn __enum() -> Self {
30 Self {
39 r#type: ColumnType::String,
40 flags: ColumnFlags::ENUM,
41 max_size: None,
42 }
43 }
44
45 #[doc(hidden)]
46 pub fn __type_feature_gate(&self) -> Option<&'static str> {
47 match self.r#type {
48 ColumnType::Date | ColumnType::Time | ColumnType::Timestamp | ColumnType::Datetime => {
49 Some("time")
50 }
51
52 ColumnType::Json => Some("json"),
53 ColumnType::NewDecimal => Some("bigdecimal"),
54
55 _ => None,
56 }
57 }
58
59 pub(crate) fn from_column(column: &ColumnDefinition) -> Self {
60 Self {
61 r#type: column.r#type,
62 flags: column.flags,
63 max_size: Some(column.max_size),
64 }
65 }
66}
67
68impl Display for MySqlTypeInfo {
69 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
70 f.pad(self.name())
71 }
72}
73
74impl TypeInfo for MySqlTypeInfo {
75 fn is_null(&self) -> bool {
76 matches!(self.r#type, ColumnType::Null)
77 }
78
79 fn name(&self) -> &str {
80 self.r#type.name(self.flags, self.max_size)
81 }
82}
83
84impl PartialEq<MySqlTypeInfo> for MySqlTypeInfo {
85 fn eq(&self, other: &MySqlTypeInfo) -> bool {
86 if self.r#type != other.r#type {
87 return false;
88 }
89
90 match self.r#type {
91 ColumnType::Tiny
92 | ColumnType::Short
93 | ColumnType::Long
94 | ColumnType::Int24
95 | ColumnType::LongLong => {
96 return self.flags.contains(ColumnFlags::UNSIGNED)
97 == other.flags.contains(ColumnFlags::UNSIGNED);
98 }
99
100 ColumnType::VarChar
102 | ColumnType::Blob
103 | ColumnType::TinyBlob
104 | ColumnType::MediumBlob
105 | ColumnType::LongBlob
106 | ColumnType::String
107 | ColumnType::VarString
108 | ColumnType::Enum => {
109 return self.flags == other.flags;
110 }
111 _ => {}
112 }
113
114 true
115 }
116}
117
118impl Eq for MySqlTypeInfo {}