Skip to main content

sea_schema/sqlite/def/
types.rs

1use sea_query::{ColumnType, StringLen};
2use std::num::ParseIntError;
3
4pub fn parse_type(data_type: String) -> Result<ColumnType, ParseIntError> {
5    let data_type = data_type.as_str();
6    let mut type_name = data_type;
7    let mut parts: Vec<u32> = Vec::new();
8    if let Some((prefix, suffix)) = data_type.split_once('(') {
9        if let Some(suffix) = suffix.strip_suffix(')') {
10            type_name = prefix;
11            for part in suffix.split(',') {
12                if let Ok(part) = part.trim().parse() {
13                    parts.push(part);
14                } else {
15                    break;
16                }
17            }
18        }
19    }
20    Ok(match type_name.to_lowercase().as_str() {
21        "char" => ColumnType::Char(parts.into_iter().next()),
22        "varchar" => ColumnType::String(match parts.into_iter().next() {
23            Some(length) => StringLen::N(length),
24            None => StringLen::None,
25        }),
26        "text" => ColumnType::Text,
27        "tinyint" => ColumnType::TinyInteger,
28        "smallint" => ColumnType::SmallInteger,
29        "integer" => ColumnType::BigInteger,
30        "bigint" => ColumnType::BigInteger,
31        "float" => ColumnType::Float,
32        "double" => ColumnType::Double,
33        "real" => {
34            if parts.len() == 2 {
35                ColumnType::Decimal(Some((parts[0], parts[1])))
36            } else {
37                ColumnType::Double
38            }
39        }
40        "decimal" | "real_decimal" => ColumnType::Decimal(if parts.len() == 2 {
41            Some((parts[0], parts[1]))
42        } else {
43            None
44        }),
45        "datetime_text" => ColumnType::DateTime,
46        "timestamp" | "timestamp_text" => ColumnType::Timestamp,
47        "timestamp_with_timezone_text" => ColumnType::TimestampWithTimeZone,
48        "time_text" => ColumnType::Time,
49        "date_text" => ColumnType::Date,
50        "blob" => {
51            if parts.len() == 1 {
52                ColumnType::Binary(parts[0])
53            } else {
54                ColumnType::Blob
55            }
56        }
57        "varbinary_blob" if parts.len() == 1 => {
58            ColumnType::VarBinary(match parts.into_iter().next() {
59                Some(length) => StringLen::N(length),
60                None => StringLen::None,
61            })
62        }
63        "boolean" => ColumnType::Boolean,
64        "money" | "real_money" => ColumnType::Money(if parts.len() == 2 {
65            Some((parts[0], parts[1]))
66        } else {
67            None
68        }),
69        "json_text" => ColumnType::Json,
70        "jsonb_text" => ColumnType::JsonBinary,
71        "uuid_text" => ColumnType::Uuid,
72        type_name => {
73            if type_name.contains("int") {
74                ColumnType::BigInteger
75            } else if type_name.contains("char")
76                || type_name.contains("clob")
77                || type_name.contains("text")
78            {
79                ColumnType::Text
80            } else if type_name.contains("real")
81                || type_name.contains("floa")
82                || type_name.contains("doub")
83            {
84                ColumnType::Double
85            } else {
86                ColumnType::custom(data_type.to_owned())
87            }
88        }
89    })
90}
91
92/// The `hidden` field returned by `PRAGMA table_xinfo`.
93#[derive(Debug, PartialEq, Clone)]
94pub enum ColumnVisibility {
95    /// `0` - ordinary column
96    Visible,
97    /// `1` - hidden column in a virtual table
98    HiddenVirtual,
99    /// `2` - generated VIRTUAL column
100    GeneratedVirtual,
101    /// `3` - generated STORED column
102    GeneratedStored,
103}
104
105impl ColumnVisibility {
106    pub fn from_hidden(value: i8) -> Self {
107        match value {
108            1 => Self::HiddenVirtual,
109            2 => Self::GeneratedVirtual,
110            3 => Self::GeneratedStored,
111            _ => Self::Visible,
112        }
113    }
114}
115
116/// The default types for an SQLite `dflt_value`
117#[derive(Debug, PartialEq, Clone)]
118pub enum DefaultType {
119    Integer(i64),
120    Float(f32),
121    String(String),
122    Null,
123    Unspecified,
124    CurrentTimestamp,
125}