Skip to content

Commit 3d858ca

Browse files
committed
Added table creation for new types
1 parent 1108d34 commit 3d858ca

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

src/create_column.rs

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<'until_build, 'post_build> CreateColumn<'post_build>
113113
"\"{}\" {} ",
114114
d.name,
115115
match d.data_type {
116-
DbType::VarBinary => "BLOB",
116+
DbType::Binary | DbType::Uuid => "BLOB",
117117
DbType::VarChar
118118
| DbType::Date
119119
| DbType::DateTime
@@ -126,6 +126,9 @@ impl<'until_build, 'post_build> CreateColumn<'post_build>
126126
| DbType::Int64
127127
| DbType::Boolean => "INTEGER",
128128
DbType::Float | DbType::Double => "REAL",
129+
DbType::BitVec | DbType::MacAddress | DbType::IpNetwork => unreachable!(
130+
"BitVec, MacAddress and IpNetwork are not available for sqlite"
131+
),
129132
}
130133
)
131134
.unwrap();
@@ -203,7 +206,12 @@ impl<'until_build, 'post_build> CreateColumn<'post_build>
203206

204207
if let Some(a) = a_opt {
205208
if let Annotation::MaxLength(max_length) = a.annotation {
206-
write!(s, "VARCHAR({max_length}) ").unwrap();
209+
// utf8mb4 is 4 bytes wide, so that's the maximum for varchar
210+
if *max_length < 2i32.pow(14) - 1 {
211+
write!(s, "VARCHAR({max_length}) ").unwrap();
212+
} else {
213+
write!(s, "LONGTEXT ").unwrap();
214+
}
207215
} else {
208216
return Err(Error::SQLBuildError(String::from(
209217
"VARCHAR must have a max_length annotation",
@@ -215,26 +223,7 @@ impl<'until_build, 'post_build> CreateColumn<'post_build>
215223
)));
216224
}
217225
}
218-
DbType::VarBinary => {
219-
let a_opt = d
220-
.annotations
221-
.iter()
222-
.find(|x| x.annotation.eq_shallow(&Annotation::MaxLength(0)));
223-
224-
if let Some(a) = a_opt {
225-
if let Annotation::MaxLength(max_length) = a.annotation {
226-
write!(s, "VARBINARY({max_length}) ").unwrap();
227-
} else {
228-
return Err(Error::SQLBuildError(
229-
"VARBINARY must have a max_length annotation".to_string(),
230-
));
231-
}
232-
} else {
233-
return Err(Error::SQLBuildError(
234-
"VARBINARY must have a max_length annotation".to_string(),
235-
));
236-
}
237-
}
226+
DbType::Binary | DbType::Uuid => write!(s, "LONGBLOB ").unwrap(),
238227
DbType::Int8 => write!(s, "TINYINT(255) ").unwrap(),
239228
DbType::Int16 => write!(s, "SMALLINT(255) ").unwrap(),
240229
DbType::Int32 => write!(s, "INT(255) ").unwrap(),
@@ -275,6 +264,9 @@ impl<'until_build, 'post_build> CreateColumn<'post_build>
275264
));
276265
}
277266
}
267+
DbType::BitVec | DbType::IpNetwork | DbType::MacAddress => {
268+
unreachable!("BitVec, MacAddress and IpNetwork are not available for mysql")
269+
}
278270
};
279271

280272
for (idx, x) in d.annotations.iter().enumerate() {
@@ -403,7 +395,11 @@ impl<'until_build, 'post_build> CreateColumn<'post_build>
403395
));
404396
}
405397
}
406-
DbType::VarBinary => write!(s, "bytea ").unwrap(),
398+
DbType::Uuid => write!(s, "uuid ").unwrap(),
399+
DbType::MacAddress => write!(s, "macaddr ").unwrap(),
400+
DbType::IpNetwork => write!(s, "inet ").unwrap(),
401+
DbType::BitVec => write!(s, "varbit ").unwrap(),
402+
DbType::Binary => write!(s, "bytea ").unwrap(),
407403
DbType::Int8 => write!(s, "smallint ").unwrap(),
408404
DbType::Int16 => {
409405
if d.annotations

0 commit comments

Comments
 (0)