|
@@ -17,7 +17,10 @@ macro_rules! integral {
|
|
|
stmt.read::<i64>(col_offset).map(|x| (x as Self, 1))
|
|
|
}
|
|
|
|
|
|
- fn column_type() -> &'static str where Self: Sized {
|
|
|
+ fn column_type() -> &'static str
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
"integer"
|
|
|
}
|
|
|
}
|
|
@@ -44,7 +47,10 @@ impl Modelable for f64 {
|
|
|
{
|
|
|
stmt.read(col_offset).map(|x| (x, 1))
|
|
|
}
|
|
|
- fn column_type() -> &'static str where Self: Sized {
|
|
|
+ fn column_type() -> &'static str
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
"numeric"
|
|
|
}
|
|
|
}
|
|
@@ -60,7 +66,10 @@ impl Modelable for bool {
|
|
|
{
|
|
|
unreachable!("sqlite only gives Strings back, not &strs!");
|
|
|
}
|
|
|
- fn column_type() -> &'static str where Self: Sized {
|
|
|
+ fn column_type() -> &'static str
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
"integer"
|
|
|
}
|
|
|
}
|
|
@@ -76,7 +85,10 @@ impl<'a> Modelable for &'a str {
|
|
|
unreachable!("sqlite only gives Strings back, not &strs!");
|
|
|
}
|
|
|
|
|
|
- fn column_type() -> &'static str where Self: Sized {
|
|
|
+ fn column_type() -> &'static str
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
"text"
|
|
|
}
|
|
|
}
|
|
@@ -91,7 +103,10 @@ impl Modelable for std::string::String {
|
|
|
{
|
|
|
stmt.read(col_offset).map(|x| (x, 1))
|
|
|
}
|
|
|
- fn column_type() -> &'static str where Self: Sized {
|
|
|
+ fn column_type() -> &'static str
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
"text"
|
|
|
}
|
|
|
}
|
|
@@ -106,22 +121,13 @@ impl<'a> Modelable for &'a [u8] {
|
|
|
{
|
|
|
unreachable!("sqlite only gives Vec<u8> back, not &[u8]!");
|
|
|
}
|
|
|
- fn column_type() -> &'static str where Self: Sized {
|
|
|
- "blob"
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-/*impl Modelable for Vec<u8> {
|
|
|
- fn bind_to(&self, stmt: &mut sqlite::Statement, col: usize) -> sqlite::Result<()> {
|
|
|
- self.bind(stmt, col)
|
|
|
- }
|
|
|
- fn build_from(stmt: &sqlite::Statement, col_offset: usize) -> sqlite::Result<(Self, usize)>
|
|
|
+ fn column_type() -> &'static str
|
|
|
where
|
|
|
Self: Sized,
|
|
|
{
|
|
|
- stmt.read(col_offset).map(|x| (x, 1))
|
|
|
+ "blob"
|
|
|
}
|
|
|
-}*/
|
|
|
+}
|
|
|
|
|
|
impl<'a, T: Modelable> Modelable for &'a T {
|
|
|
fn bind_to(&self, stmt: &mut sqlite::Statement, col: usize) -> sqlite::Result<()> {
|
|
@@ -133,7 +139,10 @@ impl<'a, T: Modelable> Modelable for &'a T {
|
|
|
{
|
|
|
unreachable!();
|
|
|
}
|
|
|
- fn column_type() -> &'static str where Self: Sized {
|
|
|
+ fn column_type() -> &'static str
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
unreachable!();
|
|
|
}
|
|
|
}
|
|
@@ -141,10 +150,13 @@ impl<'a, T: Modelable> Modelable for &'a T {
|
|
|
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
|
|
|
- if std::mem::size_of::<T>() == 1 && std::any::TypeId::of::<T>() == std::any::TypeId::of::<u8>() {
|
|
|
+ if std::mem::size_of::<T>() == 1
|
|
|
+ && std::any::TypeId::of::<T>() == std::any::TypeId::of::<u8>()
|
|
|
+ {
|
|
|
// this bit is unsafe, but is perfectly reasonable...
|
|
|
- let byte_slice = unsafe { std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len()) };
|
|
|
- return byte_slice.bind_to(stmt, col)
|
|
|
+ let byte_slice =
|
|
|
+ unsafe { std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len()) };
|
|
|
+ return byte_slice.bind_to(stmt, col);
|
|
|
}
|
|
|
serde_json::to_string(self).unwrap().bind_to(stmt, col)
|
|
|
}
|
|
@@ -153,14 +165,15 @@ impl<T: Modelable + serde::Serialize + serde::de::DeserializeOwned + 'static> Mo
|
|
|
Self: Sized,
|
|
|
{
|
|
|
// Deserialize one-byte types directly from the blob
|
|
|
- if std::mem::size_of::<T>() == 1 && std::any::TypeId::of::<T>() == std::any::TypeId::of::<u8>() {
|
|
|
+ if std::mem::size_of::<T>() == 1
|
|
|
+ && std::any::TypeId::of::<T>() == std::any::TypeId::of::<u8>()
|
|
|
+ {
|
|
|
let blob: Vec<u8> = stmt.read(col_offset)?;
|
|
|
|
|
|
// we know the return value is a u8 because the typeid matches, so while normally this
|
|
|
// is hilariously unsafe, right now it's perfectly okay.
|
|
|
Ok((unsafe { std::mem::transmute::<Vec<u8>, Vec<T>>(blob) }, 1))
|
|
|
- }
|
|
|
- else {
|
|
|
+ } else {
|
|
|
let s = String::build_from(stmt, col_offset)?;
|
|
|
Ok((
|
|
|
serde_json::from_str::<Vec<T>>(s.0.as_str()).map_err(|e| sqlite::Error {
|
|
@@ -171,7 +184,10 @@ impl<T: Modelable + serde::Serialize + serde::de::DeserializeOwned + 'static> Mo
|
|
|
))
|
|
|
}
|
|
|
}
|
|
|
- fn column_type() -> &'static str where Self: Sized {
|
|
|
+ fn column_type() -> &'static str
|
|
|
+ where
|
|
|
+ Self: Sized,
|
|
|
+ {
|
|
|
"blob"
|
|
|
}
|
|
|
}
|