1use std::{fmt, sync::Arc};
4
5use crate::types::*;
6
7#[cfg(test)]
8#[path = "../tests/unit/column_type_tests.rs"]
9mod tests;
10
11#[derive(Debug, Clone)]
13pub enum ColumnType {
14 Char(Option<u32>),
15 String(StringLen),
16 Text,
17 Blob,
18 TinyInteger,
19 SmallInteger,
20 Integer,
21 BigInteger,
22 TinyUnsigned,
23 SmallUnsigned,
24 Unsigned,
25 BigUnsigned,
26 Float,
27 Double,
28 Decimal(Option<(u32, u32)>),
29 DateTime,
30 Timestamp,
31 TimestampWithTimeZone,
32 Time,
33 Date,
34 Year,
35 Interval(Option<PgInterval>, Option<u32>),
36 Binary(u32),
37 VarBinary(StringLen),
38 Bit(Option<u32>),
39 VarBit(u32),
40 Boolean,
41 Money(Option<(u32, u32)>),
42 Json,
43 JsonBinary,
44 Uuid,
45 Custom(DynIden),
46 Enum {
47 name: DynIden,
48 variants: Vec<DynIden>,
49 },
50 Array(Arc<ColumnType>),
51 Vector(Option<u32>),
52 Cidr,
53 Inet,
54 MacAddr,
55}
56
57#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
59pub enum StringLen {
60 N(u32),
62 Max,
63 #[default]
64 None,
65}
66
67impl PartialEq for ColumnType {
68 fn eq(&self, other: &Self) -> bool {
69 match (self, other) {
70 (Self::Char(l0), Self::Char(r0))
71 | (Self::Bit(l0), Self::Bit(r0))
72 | (Self::Vector(l0), Self::Vector(r0)) => l0 == r0,
73 (Self::String(l0), Self::String(r0)) | (Self::VarBinary(l0), Self::VarBinary(r0)) => {
74 l0 == r0
75 }
76 (Self::Decimal(l0), Self::Decimal(r0)) | (Self::Money(l0), Self::Money(r0)) => l0 == r0,
77 (Self::Interval(l0, l1), Self::Interval(r0, r1)) => l0 == r0 && l1 == r1,
78 (Self::Binary(l0), Self::Binary(r0)) | (Self::VarBit(l0), Self::VarBit(r0)) => l0 == r0,
79 (Self::Custom(l0), Self::Custom(r0)) => l0.to_string() == r0.to_string(),
80 (
81 Self::Enum {
82 name: l_name,
83 variants: l_variants,
84 },
85 Self::Enum {
86 name: r_name,
87 variants: r_variants,
88 },
89 ) => {
90 l_name.to_string() == r_name.to_string()
91 && l_variants
92 .iter()
93 .map(|v| v.to_string())
94 .eq(r_variants.iter().map(|v| v.to_string()))
95 }
96 (Self::Array(l0), Self::Array(r0)) => l0 == r0,
97 _ => core::mem::discriminant(self) == core::mem::discriminant(other),
98 }
99 }
100}
101
102#[derive(Debug, Clone, Copy, PartialEq, Eq)]
106pub enum ColumnKind {
107 Integer,
109 BigInteger,
111 Float,
113 Uuid,
115 Text,
117 Json,
119 Other,
121}
122
123impl ColumnType {
124 pub const fn kind(&self) -> ColumnKind {
126 match self {
127 Self::TinyInteger
128 | Self::SmallInteger
129 | Self::Integer
130 | Self::TinyUnsigned
131 | Self::SmallUnsigned
132 | Self::Unsigned => ColumnKind::Integer,
133 Self::BigInteger | Self::BigUnsigned => ColumnKind::BigInteger,
134 Self::Float | Self::Double => ColumnKind::Float,
135 Self::Uuid => ColumnKind::Uuid,
136 Self::Char(_) | Self::String(_) | Self::Text => ColumnKind::Text,
137 Self::Json | Self::JsonBinary => ColumnKind::Json,
138 _ => ColumnKind::Other,
139 }
140 }
141
142 pub fn custom<T>(ty: T) -> ColumnType
143 where
144 T: IntoIden,
145 {
146 ColumnType::Custom(ty.into_iden())
147 }
148
149 pub fn string(length: Option<u32>) -> ColumnType {
150 match length {
151 Some(s) => ColumnType::String(StringLen::N(s)),
152 None => ColumnType::String(StringLen::None),
153 }
154 }
155
156 pub fn var_binary(length: u32) -> ColumnType {
157 ColumnType::VarBinary(StringLen::N(length))
158 }
159}
160
161#[derive(Debug, Clone, Eq, PartialEq)]
163pub enum PgInterval {
164 Year,
165 Month,
166 Day,
167 Hour,
168 Minute,
169 Second,
170 YearToMonth,
171 DayToHour,
172 DayToMinute,
173 DayToSecond,
174 HourToMinute,
175 HourToSecond,
176 MinuteToSecond,
177}
178
179#[derive(Debug, Clone, Eq, PartialEq)]
181pub enum PgDateTruncUnit {
182 Microseconds,
183 Milliseconds,
184 Second,
185 Minute,
186 Hour,
187 Day,
188 Week,
189 Month,
190 Quarter,
191 Year,
192 Decade,
193 Century,
194 Millennium,
195}
196
197impl fmt::Display for PgDateTruncUnit {
198 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199 let text = match self {
200 PgDateTruncUnit::Microseconds => "microseconds",
201 PgDateTruncUnit::Milliseconds => "milliseconds",
202 PgDateTruncUnit::Second => "second",
203 PgDateTruncUnit::Minute => "minute",
204 PgDateTruncUnit::Hour => "hour",
205 PgDateTruncUnit::Day => "day",
206 PgDateTruncUnit::Week => "week",
207 PgDateTruncUnit::Month => "month",
208 PgDateTruncUnit::Quarter => "quarter",
209 PgDateTruncUnit::Year => "year",
210 PgDateTruncUnit::Decade => "decade",
211 PgDateTruncUnit::Century => "century",
212 PgDateTruncUnit::Millennium => "millennium",
213 };
214 f.write_str(text)
215 }
216}