Skip to main content

sql_builder/
column_type.rs

1//! The types a column can be declared with.
2
3use std::{fmt, sync::Arc};
4
5use crate::types::*;
6
7#[cfg(test)]
8#[path = "../tests/unit/column_type_tests.rs"]
9mod tests;
10
11/// The type of a column, as an entity declares it.
12#[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/// Length for var-char/binary; default to 255 in MySQL if length is not specified
58#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
59pub enum StringLen {
60    /// String size
61    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
102impl ColumnType {
103    pub fn custom<T>(ty: T) -> ColumnType
104    where
105        T: IntoIden,
106    {
107        ColumnType::Custom(ty.into_iden())
108    }
109
110    pub fn string(length: Option<u32>) -> ColumnType {
111        match length {
112            Some(s) => ColumnType::String(StringLen::N(s)),
113            None => ColumnType::String(StringLen::None),
114        }
115    }
116
117    pub fn var_binary(length: u32) -> ColumnType {
118        ColumnType::VarBinary(StringLen::N(length))
119    }
120}
121
122// All interval fields
123#[derive(Debug, Clone, Eq, PartialEq)]
124pub enum PgInterval {
125    Year,
126    Month,
127    Day,
128    Hour,
129    Minute,
130    Second,
131    YearToMonth,
132    DayToHour,
133    DayToMinute,
134    DayToSecond,
135    HourToMinute,
136    HourToSecond,
137    MinuteToSecond,
138}
139
140// All possible inputs to DATE_TRUNC (https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-TRUNC)
141#[derive(Debug, Clone, Eq, PartialEq)]
142pub enum PgDateTruncUnit {
143    Microseconds,
144    Milliseconds,
145    Second,
146    Minute,
147    Hour,
148    Day,
149    Week,
150    Month,
151    Quarter,
152    Year,
153    Decade,
154    Century,
155    Millennium,
156}
157
158impl fmt::Display for PgDateTruncUnit {
159    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
160        let text = match self {
161            PgDateTruncUnit::Microseconds => "microseconds",
162            PgDateTruncUnit::Milliseconds => "milliseconds",
163            PgDateTruncUnit::Second => "second",
164            PgDateTruncUnit::Minute => "minute",
165            PgDateTruncUnit::Hour => "hour",
166            PgDateTruncUnit::Day => "day",
167            PgDateTruncUnit::Week => "week",
168            PgDateTruncUnit::Month => "month",
169            PgDateTruncUnit::Quarter => "quarter",
170            PgDateTruncUnit::Year => "year",
171            PgDateTruncUnit::Decade => "decade",
172            PgDateTruncUnit::Century => "century",
173            PgDateTruncUnit::Millennium => "millennium",
174        };
175        f.write_str(text)
176    }
177}