|
@@ -153,6 +153,40 @@ impl<'a, T: Modelable> Modelable for &'a T {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+impl<T: Modelable> Modelable for Option<T> {
|
|
|
+ fn bind_to(&self, stmt: &mut sqlite::Statement, col: usize) -> sqlite::Result<()> {
|
|
|
+
|
|
|
+ match self.as_ref() {
|
|
|
+ Some(val) => {
|
|
|
+ val.bind_to(stmt, col)
|
|
|
+ }
|
|
|
+ None => {
|
|
|
+ stmt.bind(col, &sqlite::Value::Null)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fn build_from(stmt: &sqlite::Statement, col_offset: usize) -> sqlite::Result<(Self, usize)>
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
+ // note: this is needlessly expensive since we read things twice.
|
|
|
+ let val = stmt.read::<sqlite::Value>(col_offset)?;
|
|
|
+ if val.kind() == sqlite::Type::Null {
|
|
|
+ Ok((None, 1))
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ let (val, size) = T::build_from(stmt, col_offset)?;
|
|
|
+ Ok((Some(val), size))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ fn column_type() -> &'static str
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
+ T::column_type()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
impl<T: Modelable + serde::Serialize + serde::de::DeserializeOwned + 'static> Modelable for Vec<T> {
|
|
|
fn bind_to(&self, stmt: &mut sqlite::Statement, col: usize) -> sqlite::Result<()> {
|
|
|
// We serialize Vec<u8> types directly as a blob
|