sql_ast/ast/
data_type.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13use super::ObjectName;
14use std::fmt;
15
16/// SQL data types
17#[derive(Debug, Clone, PartialEq, Eq, Hash)]
18pub enum DataType {
19    /// Fixed-length character type e.g. CHAR(10)
20    Char(Option<u64>),
21    /// Variable-length character type e.g. VARCHAR(10)
22    Varchar(Option<u64>),
23    /// Uuid type
24    Uuid,
25    /// Large character object e.g. CLOB(1000)
26    Clob(u64),
27    /// Fixed-length binary type e.g. BINARY(10)
28    Binary(u64),
29    /// Variable-length binary type e.g. VARBINARY(10)
30    Varbinary(u64),
31    /// Large binary object e.g. BLOB(1000)
32    Blob(u64),
33    /// Decimal type with optional precision and scale e.g. DECIMAL(10,2)
34    Decimal(Option<u64>, Option<u64>),
35    /// Floating point with optional precision e.g. FLOAT(8)
36    Float(Option<u64>),
37    /// Small integer
38    SmallInt,
39    /// Integer
40    Int,
41    /// Big integer
42    BigInt,
43    /// Floating point e.g. REAL
44    Real,
45    /// Double e.g. DOUBLE PRECISION
46    Double,
47    /// Boolean
48    Boolean,
49    /// Date
50    Date,
51    /// Time
52    Time,
53    /// Timestamp
54    Timestamp,
55    /// Interval
56    Interval,
57    /// Regclass used in postgresql serial
58    Regclass,
59    /// Text
60    Text,
61    /// Json type
62    Json,
63    /// Bytea
64    Bytea,
65    /// Custom type such as enums
66    Custom(ObjectName),
67    /// Arrays
68    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}