1#[cfg(not(feature = "std"))]
14use alloc::boxed::Box;
15use core::fmt;
16
17#[cfg(feature = "serde")]
18use serde::{Deserialize, Serialize};
19
20use crate::ast::ObjectName;
21
22#[derive(Debug, Clone, PartialEq, Eq, Hash)]
24#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
25pub enum DataType {
26 Char(Option<u64>),
28 Varchar(Option<u64>),
30 Uuid,
32 Clob(u64),
34 Binary(u64),
36 Varbinary(u64),
38 Blob(u64),
40 Decimal(Option<u64>, Option<u64>),
42 Float(Option<u64>),
44 TinyInt(Option<u64>),
46 SmallInt(Option<u64>),
48 Int(Option<u64>),
50 BigInt(Option<u64>),
52 Real,
54 Double,
56 Boolean,
58 Date,
60 Time,
62 Timestamp,
64 Interval,
66 Regclass,
68 Text,
70 String,
72 Bytea,
74 Custom(ObjectName),
76 Array(Box<DataType>),
78}
79
80impl fmt::Display for DataType {
81 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
82 match self {
83 DataType::Char(size) => format_type_with_optional_length(f, "CHAR", size),
84 DataType::Varchar(size) => {
85 format_type_with_optional_length(f, "CHARACTER VARYING", size)
86 }
87 DataType::Uuid => write!(f, "UUID"),
88 DataType::Clob(size) => write!(f, "CLOB({})", size),
89 DataType::Binary(size) => write!(f, "BINARY({})", size),
90 DataType::Varbinary(size) => write!(f, "VARBINARY({})", size),
91 DataType::Blob(size) => write!(f, "BLOB({})", size),
92 DataType::Decimal(precision, scale) => {
93 if let Some(scale) = scale {
94 write!(f, "NUMERIC({},{})", precision.unwrap(), scale)
95 } else {
96 format_type_with_optional_length(f, "NUMERIC", precision)
97 }
98 }
99 DataType::Float(size) => format_type_with_optional_length(f, "FLOAT", size),
100 DataType::TinyInt(zerofill) => format_type_with_optional_length(f, "TINYINT", zerofill),
101 DataType::SmallInt(zerofill) => {
102 format_type_with_optional_length(f, "SMALLINT", zerofill)
103 }
104 DataType::Int(zerofill) => format_type_with_optional_length(f, "INT", zerofill),
105 DataType::BigInt(zerofill) => format_type_with_optional_length(f, "BIGINT", zerofill),
106 DataType::Real => write!(f, "REAL"),
107 DataType::Double => write!(f, "DOUBLE"),
108 DataType::Boolean => write!(f, "BOOLEAN"),
109 DataType::Date => write!(f, "DATE"),
110 DataType::Time => write!(f, "TIME"),
111 DataType::Timestamp => write!(f, "TIMESTAMP"),
112 DataType::Interval => write!(f, "INTERVAL"),
113 DataType::Regclass => write!(f, "REGCLASS"),
114 DataType::Text => write!(f, "TEXT"),
115 DataType::String => write!(f, "STRING"),
116 DataType::Bytea => write!(f, "BYTEA"),
117 DataType::Array(ty) => write!(f, "{}[]", ty),
118 DataType::Custom(ty) => write!(f, "{}", ty),
119 }
120 }
121}
122
123fn format_type_with_optional_length(
124 f: &mut fmt::Formatter,
125 sql_type: &'static str,
126 len: &Option<u64>,
127) -> fmt::Result {
128 write!(f, "{}", sql_type)?;
129 if let Some(len) = len {
130 write!(f, "({})", len)?;
131 }
132 Ok(())
133}