1use super::ObjectName;
14use std::fmt;
15
16#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub enum DataType {
19 Char(Option<u64>),
21 Varchar(Option<u64>),
23 Uuid,
25 Clob(u64),
27 Binary(u64),
29 Varbinary(u64),
31 Blob(u64),
33 Decimal(Option<u64>, Option<u64>),
35 Float(Option<u64>),
37 SmallInt,
39 Int,
41 BigInt,
43 Real,
45 Double,
47 Boolean,
49 Date,
51 Time,
53 Timestamp,
55 Interval,
57 Regclass,
59 Text,
61 Json,
63 Bytea,
65 Custom(ObjectName),
67 Array(Box<DataType>),
69}
70
71impl fmt::Display for DataType {
72 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
73 match self {
74 DataType::Char(size) => format_type_with_optional_length(f, "char", size),
75 DataType::Varchar(size) => {
76 format_type_with_optional_length(f, "character varying", size)
77 }
78 DataType::Uuid => write!(f, "uuid"),
79 DataType::Clob(size) => write!(f, "clob({})", size),
80 DataType::Binary(size) => write!(f, "binary({})", size),
81 DataType::Varbinary(size) => write!(f, "varbinary({})", size),
82 DataType::Blob(size) => write!(f, "blob({})", size),
83 DataType::Decimal(precision, scale) => {
84 if let Some(scale) = scale {
85 write!(f, "numeric({},{})", precision.unwrap(), scale)
86 } else {
87 format_type_with_optional_length(f, "numeric", precision)
88 }
89 }
90 DataType::Float(size) => format_type_with_optional_length(f, "float", size),
91 DataType::SmallInt => write!(f, "smallint"),
92 DataType::Int => write!(f, "int"),
93 DataType::BigInt => write!(f, "bigint"),
94 DataType::Real => write!(f, "real"),
95 DataType::Double => write!(f, "double"),
96 DataType::Boolean => write!(f, "boolean"),
97 DataType::Date => write!(f, "date"),
98 DataType::Time => write!(f, "time"),
99 DataType::Timestamp => write!(f, "timestamp"),
100 DataType::Interval => write!(f, "interval"),
101 DataType::Regclass => write!(f, "regclass"),
102 DataType::Text => write!(f, "text"),
103 DataType::Json => write!(f, "json"),
104 DataType::Bytea => write!(f, "bytea"),
105 DataType::Array(ty) => write!(f, "{}[]", ty),
106 DataType::Custom(ty) => write!(f, "{}", ty),
107 }
108 }
109}
110
111fn format_type_with_optional_length(
112 f: &mut fmt::Formatter,
113 sql_type: &'static str,
114 len: &Option<u64>,
115) -> fmt::Result {
116 write!(f, "{}", sql_type)?;
117 if let Some(len) = len {
118 write!(f, "({})", len)?;
119 }
120 Ok(())
121}