|
@@ -57,13 +57,17 @@ impl<'l> QueryInterface<'l> {
|
|
}
|
|
}
|
|
|
|
|
|
/// Helper function to process an expected one result
|
|
/// Helper function to process an expected one result
|
|
- fn expect_one_result<T>(&self, stmt: &mut sqlite::Statement, with_result: &mut dyn FnMut(&mut sqlite::Statement) -> Option<T>) -> Option<T> {
|
|
|
|
|
|
+ fn expect_one_result<T>(
|
|
|
|
+ &self,
|
|
|
|
+ stmt: &mut sqlite::Statement,
|
|
|
|
+ with_result: &mut dyn FnMut(&mut sqlite::Statement) -> Option<T>,
|
|
|
|
+ ) -> Option<T> {
|
|
let state = stmt.next();
|
|
let state = stmt.next();
|
|
assert!(state.is_ok());
|
|
assert!(state.is_ok());
|
|
assert_eq!(state.ok(), Some(sqlite::State::Row));
|
|
assert_eq!(state.ok(), Some(sqlite::State::Row));
|
|
|
|
|
|
let res = with_result(stmt);
|
|
let res = with_result(stmt);
|
|
-
|
|
|
|
|
|
+
|
|
let state = stmt.next();
|
|
let state = stmt.next();
|
|
assert!(state.is_ok());
|
|
assert!(state.is_ok());
|
|
assert_eq!(state.ok(), Some(sqlite::State::Done));
|
|
assert_eq!(state.ok(), Some(sqlite::State::Done));
|
|
@@ -72,7 +76,11 @@ impl<'l> QueryInterface<'l> {
|
|
}
|
|
}
|
|
|
|
|
|
/// Search for an entity by a property
|
|
/// Search for an entity by a property
|
|
- pub fn get_one_by<T: Entity<Column = C>, C: EntityColumns<Entity = T>, V: crate::model::Modelable>(
|
|
|
|
|
|
+ pub fn get_one_by<
|
|
|
|
+ T: Entity<Column = C>,
|
|
|
|
+ C: EntityColumns<Entity = T>,
|
|
|
|
+ V: crate::model::Modelable,
|
|
|
|
+ >(
|
|
&self,
|
|
&self,
|
|
c: C,
|
|
c: C,
|
|
val: V,
|
|
val: V,
|
|
@@ -80,7 +88,8 @@ impl<'l> QueryInterface<'l> {
|
|
let table_name = <T as Entity>::table_name();
|
|
let table_name = <T as Entity>::table_name();
|
|
let column_name = <T as Entity>::name(c);
|
|
let column_name = <T as Entity>::name(c);
|
|
|
|
|
|
- let mut prepared = self.db
|
|
|
|
|
|
+ let mut prepared = self
|
|
|
|
+ .db
|
|
.conn
|
|
.conn
|
|
.prepare(&format!(
|
|
.prepare(&format!(
|
|
"SELECT * FROM \"{}\" WHERE \"{}\" = ?",
|
|
"SELECT * FROM \"{}\" WHERE \"{}\" = ?",
|
|
@@ -95,14 +104,15 @@ impl<'l> QueryInterface<'l> {
|
|
return self.expect_one_result(&mut prepared, &mut |stmt| {
|
|
return self.expect_one_result(&mut prepared, &mut |stmt| {
|
|
let id: i64 = stmt.read(0).ok()?;
|
|
let id: i64 = stmt.read(0).ok()?;
|
|
let mut rd = crate::model::load::RowDeserializer::from_row(&stmt);
|
|
let mut rd = crate::model::load::RowDeserializer::from_row(&stmt);
|
|
- return Some(WithID::wrap(T::deserialize(&mut rd).ok()?, id))
|
|
|
|
- })
|
|
|
|
|
|
+ return Some(WithID::wrap(T::deserialize(&mut rd).ok()?, id));
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
/// Search for an entity by ID
|
|
/// Search for an entity by ID
|
|
- pub fn get_one_by_id<I: EntityID<Entity=T>, T: Entity>(&self, id: I) -> Option<WithID<T>> {
|
|
|
|
|
|
+ pub fn get_one_by_id<I: EntityID<Entity = T>, T: Entity>(&self, id: I) -> Option<WithID<T>> {
|
|
let table_name = <T as Entity>::table_name();
|
|
let table_name = <T as Entity>::table_name();
|
|
- let mut prepared = self.db
|
|
|
|
|
|
+ let mut prepared = self
|
|
|
|
+ .db
|
|
.conn
|
|
.conn
|
|
.prepare(&format!("SELECT * FROM \"{}\" WHERE id = ?", table_name))
|
|
.prepare(&format!("SELECT * FROM \"{}\" WHERE id = ?", table_name))
|
|
.ok()?;
|
|
.ok()?;
|
|
@@ -112,12 +122,16 @@ impl<'l> QueryInterface<'l> {
|
|
return self.expect_one_result(&mut prepared, &mut |stmt| {
|
|
return self.expect_one_result(&mut prepared, &mut |stmt| {
|
|
let id: i64 = stmt.read(0).ok()?;
|
|
let id: i64 = stmt.read(0).ok()?;
|
|
let mut rd = crate::model::load::RowDeserializer::from_row(&stmt);
|
|
let mut rd = crate::model::load::RowDeserializer::from_row(&stmt);
|
|
- return Some(WithID::wrap(T::deserialize(&mut rd).ok()?, id))
|
|
|
|
- })
|
|
|
|
|
|
+ return Some(WithID::wrap(T::deserialize(&mut rd).ok()?, id));
|
|
|
|
+ });
|
|
}
|
|
}
|
|
|
|
|
|
/// Search for all entities matching a property
|
|
/// Search for all entities matching a property
|
|
- pub fn get_all_by<T: Entity<Column = C>, C: EntityColumns<Entity = T>, V: crate::model::Modelable>(
|
|
|
|
|
|
+ pub fn get_all_by<
|
|
|
|
+ T: Entity<Column = C>,
|
|
|
|
+ C: EntityColumns<Entity = T>,
|
|
|
|
+ V: crate::model::Modelable,
|
|
|
|
+ >(
|
|
&self,
|
|
&self,
|
|
c: C,
|
|
c: C,
|
|
val: V,
|
|
val: V,
|
|
@@ -125,7 +139,8 @@ impl<'l> QueryInterface<'l> {
|
|
let table_name = <T as Entity>::table_name();
|
|
let table_name = <T as Entity>::table_name();
|
|
let column_name = <T as Entity>::name(c);
|
|
let column_name = <T as Entity>::name(c);
|
|
|
|
|
|
- let mut prepared = self.db
|
|
|
|
|
|
+ let mut prepared = self
|
|
|
|
+ .db
|
|
.conn
|
|
.conn
|
|
.prepare(&format!(
|
|
.prepare(&format!(
|
|
"SELECT * FROM \"{}\" WHERE \"{}\" = ?",
|
|
"SELECT * FROM \"{}\" WHERE \"{}\" = ?",
|
|
@@ -152,23 +167,24 @@ impl<'l> QueryInterface<'l> {
|
|
|
|
|
|
/// Add an entity to its table
|
|
/// Add an entity to its table
|
|
pub fn add<T: Entity + serde::Serialize>(&self, m: &T) -> Option<<T as Entity>::ID> {
|
|
pub fn add<T: Entity + serde::Serialize>(&self, m: &T) -> Option<<T as Entity>::ID> {
|
|
-
|
|
|
|
let placeholders = (0..(<T as Entity>::column_count() - 1))
|
|
let placeholders = (0..(<T as Entity>::column_count() - 1))
|
|
.map(|n| "?".to_string())
|
|
.map(|n| "?".to_string())
|
|
.collect::<Vec<_>>()
|
|
.collect::<Vec<_>>()
|
|
.join(",");
|
|
.join(",");
|
|
|
|
|
|
- let mut prepared = self.db.conn.prepare(&format!(
|
|
|
|
- "INSERT INTO \"{}\" VALUES (NULL, {}) RETURNING \"id\"",
|
|
|
|
- <T as Entity>::table_name(),
|
|
|
|
- placeholders
|
|
|
|
- )).ok()?;
|
|
|
|
|
|
+ let mut prepared = self
|
|
|
|
+ .db
|
|
|
|
+ .conn
|
|
|
|
+ .prepare(&format!(
|
|
|
|
+ "INSERT INTO \"{}\" VALUES (NULL, {}) RETURNING \"id\"",
|
|
|
|
+ <T as Entity>::table_name(),
|
|
|
|
+ placeholders
|
|
|
|
+ ))
|
|
|
|
+ .ok()?;
|
|
|
|
|
|
crate::model::store::serialize_into(&mut prepared, m).ok()?;
|
|
crate::model::store::serialize_into(&mut prepared, m).ok()?;
|
|
|
|
|
|
- let rowid = self.expect_one_result(&mut prepared, &mut |stmt| {
|
|
|
|
- stmt.read::<i64>(0).ok()
|
|
|
|
- })?;
|
|
|
|
|
|
+ let rowid = self.expect_one_result(&mut prepared, &mut |stmt| stmt.read::<i64>(0).ok())?;
|
|
|
|
|
|
Some(<T as Entity>::ID::from_raw_id(rowid))
|
|
Some(<T as Entity>::ID::from_raw_id(rowid))
|
|
|
|
|