Skip to main content

derive_sql/traits/
flavor.rs

1use super::*;
2
3mod sql_type; use sql_type::{SqlType, RawType};
4
5/// Enum to advise on the SQL flavor supported by the connection.
6pub enum Flavor {
7  /// SQLite type connection
8  SQLite,
9  /// MySQL type connection
10  MySQL,
11  /// PostgreSQL type connection
12  PostgreSQL,
13  // Not any of the other...
14  //Other,
15}
16
17impl std::fmt::Display for Flavor {
18  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
19    match self {
20      Flavor::SQLite => write!(f, "SQLite"),
21      Flavor::MySQL => write!(f, "MySQL"),
22      Flavor::PostgreSQL => write!(f, "PostgreSQL"),
23    }
24  }
25}
26
27impl Flavor {
28  /// Retrieve row identifier. Supported for SQLite and PostgreSQL. 
29  /// Returns an error for MySQL.
30  pub fn row_id(&self) -> Result<String> {
31    match self {
32      Flavor::SQLite => Ok("rowid"),
33      Flavor::MySQL => Err(Error::MySQLRowIdNotSupported),
34      Flavor::PostgreSQL => Ok("CTID"),
35    }.map(|s| s.to_string())
36  }
37
38  /// Handle column name
39  pub fn column(&self, column: &str) -> Result<String> {
40    match self {
41      Flavor::SQLite
42      | Flavor::MySQL => Ok(format!("`{column}`")),
43      Flavor::PostgreSQL => Ok(format!("{column}")),
44    }
45  }
46
47  /// Handle table name
48  pub fn table(&self, table: &str) -> Result<String> {
49    match self {
50      Flavor::SQLite
51      | Flavor::MySQL => Ok(format!("`{table}`")),
52      Flavor::PostgreSQL => Ok(format!("{table}")),
53    }
54  }
55
56  /// Handle values
57  pub fn value(&self, i: usize) -> Result<String> {
58    match self {
59      Flavor::SQLite
60      | Flavor::MySQL => Ok(format!("?")),
61      Flavor::PostgreSQL => Ok(format!("${index}", index = i+1)),
62    }
63  }
64
65  /// Conver to SQL type
66  pub fn sql_type(&self, ty: &str) -> Result<SqlType> {
67    /* match nullable {
68      true  => Ok(SqlType::Nullable(self.raw_type(ty)?)),
69      false => Ok(SqlType::NonNullable(self.raw_type(ty)?)),
70    }*/
71    Ok(SqlType::Nullable(self.raw_type(ty)?))
72  }
73
74  /// Convert to SQL raw type
75  fn raw_type(&self, ty: &str) -> Result<RawType> {
76    match (self, ty) {
77        (Flavor::SQLite,     "i8") 
78      | (Flavor::MySQL,      "i8") 
79      | (Flavor::SQLite,     "u8") 
80      | (Flavor::MySQL,      "u8") 
81      | (Flavor::SQLite,     "i16") 
82      | (Flavor::MySQL,      "i16") 
83      | (Flavor::SQLite,     "u16") 
84      | (Flavor::MySQL,      "u16") 
85      | (Flavor::SQLite,     "i32") 
86      | (Flavor::MySQL,      "i32") 
87      | (Flavor::SQLite,     "u32") 
88      | (Flavor::MySQL,      "u32") 
89      | (Flavor::SQLite,     "i64") 
90      | (Flavor::MySQL,      "i64") 
91      | (Flavor::SQLite,     "u64") 
92      | (Flavor::MySQL,      "u64") 
93      | (Flavor::SQLite,     "usize") 
94      | (Flavor::MySQL,      "usize") 
95      => Ok(RawType::Int),
96
97        (Flavor::PostgreSQL, "i8") 
98      | (Flavor::PostgreSQL, "u8") 
99      | (Flavor::PostgreSQL, "i16") 
100      | (Flavor::PostgreSQL, "u16") 
101      => Ok(RawType::SmallInt),
102
103        (Flavor::PostgreSQL, "i32") 
104      | (Flavor::PostgreSQL, "u32") 
105      => Ok(RawType::Int),
106
107        (Flavor::PostgreSQL, "i64") 
108      | (Flavor::PostgreSQL, "u64") 
109      | (Flavor::PostgreSQL, "usize") 
110      => Ok(RawType::BigInt),
111
112        (Flavor::SQLite,     "f32") 
113      | (Flavor::MySQL,      "f32") 
114      | (Flavor::SQLite,     "f64") 
115      | (Flavor::MySQL,      "f64") 
116      => Ok(RawType::Double),
117
118        (Flavor::PostgreSQL, "f32") 
119      => Ok(RawType::Real),
120
121        (Flavor::PostgreSQL, "f64") 
122      => Ok(RawType::Double),
123
124        (Flavor::SQLite,     "bool") 
125      | (Flavor::MySQL,      "bool") 
126      | (Flavor::PostgreSQL, "bool") 
127      => Ok(RawType::Boolean),
128
129        (Flavor::SQLite,     "String") 
130      | (Flavor::MySQL,      "String") 
131      | (Flavor::PostgreSQL, "String") 
132      => Ok(RawType::Text),
133
134        (Flavor::SQLite,     "DateTime") 
135      | (Flavor::MySQL,      "DateTime") 
136      | (Flavor::PostgreSQL, "DateTime") 
137      | (Flavor::SQLite,     "NaiveDateTime") 
138      | (Flavor::MySQL,      "NaiveDateTime") 
139      | (Flavor::PostgreSQL, "NaiveDateTime") 
140      => Ok(RawType::DateTime),
141
142        (Flavor::SQLite,     "NaiveDate") 
143      | (Flavor::MySQL,      "NaiveDate") 
144      | (Flavor::PostgreSQL, "NaiveDate") 
145      => Ok(RawType::Date),
146      
147      _ => Err(Error::SqlTypeNotSupported(self.to_string(), ty.to_string())),
148
149    }
150  }
151}