sqlx_gen/typemap/
sqlite.rs1use super::RustType;
2
3pub fn map_type(declared_type: &str) -> RustType {
4 let upper = declared_type.to_uppercase();
5
6 if upper.contains("INT") {
7 return RustType::simple("i64");
8 }
9 if upper.contains("CHAR") || upper.contains("TEXT") || upper.contains("CLOB") {
10 return RustType::simple("String");
11 }
12 if upper.contains("BLOB") || upper.is_empty() {
13 return RustType::simple("Vec<u8>");
14 }
15 if upper.contains("REAL") || upper.contains("FLOAT") || upper.contains("DOUBLE") {
16 return RustType::simple("f64");
17 }
18 if upper.contains("BOOL") {
19 return RustType::simple("bool");
20 }
21 if upper.contains("TIMESTAMP") || upper.contains("DATETIME") {
22 return RustType::with_import("NaiveDateTime", "use chrono::NaiveDateTime;");
23 }
24 if upper.contains("DATE") {
25 return RustType::with_import("NaiveDate", "use chrono::NaiveDate;");
26 }
27 if upper.contains("TIME") {
28 return RustType::with_import("NaiveTime", "use chrono::NaiveTime;");
29 }
30 if upper.contains("NUMERIC") || upper.contains("DECIMAL") {
31 return RustType::simple("f64");
32 }
33
34 RustType::simple("String")
36}
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn test_integer() {
44 assert_eq!(map_type("INTEGER").path, "i64");
45 }
46
47 #[test]
48 fn test_int() {
49 assert_eq!(map_type("INT").path, "i64");
50 }
51
52 #[test]
53 fn test_bigint() {
54 assert_eq!(map_type("BIGINT").path, "i64");
55 }
56
57 #[test]
58 fn test_smallint() {
59 assert_eq!(map_type("SMALLINT").path, "i64");
60 }
61
62 #[test]
63 fn test_tinyint() {
64 assert_eq!(map_type("TINYINT").path, "i64");
65 }
66
67 #[test]
68 fn test_mediumint() {
69 assert_eq!(map_type("MEDIUMINT").path, "i64");
70 }
71
72 #[test]
73 fn test_text() {
74 assert_eq!(map_type("TEXT").path, "String");
75 }
76
77 #[test]
78 fn test_varchar() {
79 assert_eq!(map_type("VARCHAR(255)").path, "String");
80 }
81
82 #[test]
83 fn test_character() {
84 assert_eq!(map_type("CHARACTER(20)").path, "String");
85 }
86
87 #[test]
88 fn test_clob() {
89 assert_eq!(map_type("CLOB").path, "String");
90 }
91
92 #[test]
93 fn test_blob() {
94 assert_eq!(map_type("BLOB").path, "Vec<u8>");
95 }
96
97 #[test]
98 fn test_empty_type() {
99 assert_eq!(map_type("").path, "Vec<u8>");
100 }
101
102 #[test]
103 fn test_real() {
104 assert_eq!(map_type("REAL").path, "f64");
105 }
106
107 #[test]
108 fn test_float() {
109 assert_eq!(map_type("FLOAT").path, "f64");
110 }
111
112 #[test]
113 fn test_double() {
114 assert_eq!(map_type("DOUBLE").path, "f64");
115 }
116
117 #[test]
118 fn test_double_precision() {
119 assert_eq!(map_type("DOUBLE PRECISION").path, "f64");
120 }
121
122 #[test]
123 fn test_boolean() {
124 assert_eq!(map_type("BOOLEAN").path, "bool");
125 }
126
127 #[test]
128 fn test_timestamp() {
129 let rt = map_type("TIMESTAMP");
130 assert_eq!(rt.path, "NaiveDateTime");
131 assert!(rt.needs_import.as_ref().unwrap().contains("chrono"));
132 }
133
134 #[test]
135 fn test_datetime() {
136 let rt = map_type("DATETIME");
137 assert_eq!(rt.path, "NaiveDateTime");
138 assert!(rt.needs_import.is_some());
139 }
140
141 #[test]
142 fn test_date() {
143 let rt = map_type("DATE");
144 assert_eq!(rt.path, "NaiveDate");
145 assert!(rt.needs_import.as_ref().unwrap().contains("chrono"));
146 }
147
148 #[test]
149 fn test_time() {
150 let rt = map_type("TIME");
151 assert_eq!(rt.path, "NaiveTime");
152 assert!(rt.needs_import.as_ref().unwrap().contains("chrono"));
153 }
154
155 #[test]
156 fn test_numeric() {
157 assert_eq!(map_type("NUMERIC").path, "f64");
158 }
159
160 #[test]
161 fn test_decimal() {
162 assert_eq!(map_type("DECIMAL").path, "f64");
163 }
164
165 #[test]
166 fn test_case_insensitive() {
167 assert_eq!(map_type("integer").path, "i64");
168 }
169
170 #[test]
171 fn test_fallback_unknown_type() {
172 assert_eq!(map_type("JSON").path, "String");
173 }
174}