Browse Source

Add support for Option<T>, bump version, and link to repo.

Kestrel 2 years ago
parent
commit
703702d0d5
2 changed files with 36 additions and 1 deletions
  1. 2 1
      microrm/Cargo.toml
  2. 34 0
      microrm/src/model/modelable.rs

+ 2 - 1
microrm/Cargo.toml

@@ -1,9 +1,10 @@
 [package]
 name = "microrm"
-version = "0.3.4"
+version = "0.3.5"
 edition = "2021"
 license = "BSD-4-Clause"
 authors = ["Kestrel <kestrel@flying-kestrel.ca>"]
+repository = "https://git.flying-kestrel.ca/kestrel/microrm"
 description = "Lightweight ORM using sqlite as a backend."
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

+ 34 - 0
microrm/src/model/modelable.rs

@@ -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