|
@@ -99,8 +99,8 @@ impl<'l> QueryInterface<'l> {
|
|
|
fn expect_one_result<T>(
|
|
|
&self,
|
|
|
stmt: &mut sqlite::Statement,
|
|
|
- with_result: &mut dyn FnMut(&mut sqlite::Statement) -> Result<T, crate::Error>,
|
|
|
- ) -> Result<Option<T>, crate::Error> {
|
|
|
+ with_result: &mut dyn FnMut(&mut sqlite::Statement) -> Result<T, Error>,
|
|
|
+ ) -> Result<Option<T>, Error> {
|
|
|
let state = stmt.next()?;
|
|
|
if state != sqlite::State::Row {
|
|
|
return Ok(None);
|
|
@@ -111,10 +111,10 @@ impl<'l> QueryInterface<'l> {
|
|
|
|
|
|
/// Helper function to process an expected zero results
|
|
|
/// Note that this errors out if there is any result
|
|
|
- fn expect_no_result(&self, stmt: &mut sqlite::Statement) -> Result<(), crate::Error> {
|
|
|
+ fn expect_no_result(&self, stmt: &mut sqlite::Statement) -> Result<(), Error> {
|
|
|
let state = stmt.next()?;
|
|
|
if state != sqlite::State::Done {
|
|
|
- return Err(crate::Error::ExecFailure);
|
|
|
+ return Err(Error::ExecFailure);
|
|
|
}
|
|
|
|
|
|
Ok(())
|
|
@@ -131,30 +131,24 @@ impl<'l> QueryInterface<'l> {
|
|
|
hash: u64,
|
|
|
create: Create,
|
|
|
mut with: With,
|
|
|
- ) -> Return
|
|
|
+ ) -> Result<Return, Error>
|
|
|
where {
|
|
|
let mut cache = self.cache.borrow_mut();
|
|
|
let query = cache.entry(hash).or_insert_with(create);
|
|
|
let res = with(query);
|
|
|
- query.reset().expect("Couldn't reset query");
|
|
|
- res
|
|
|
+ query.reset()?;
|
|
|
+ Ok(res)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
impl<'l> QueryInterface<'l> {
|
|
|
/// Add an entity to its table in the database. Alias for `add`.
|
|
|
- pub fn insert<T: Entity + serde::Serialize>(
|
|
|
- &self,
|
|
|
- m: &T,
|
|
|
- ) -> Result<<T as Entity>::ID, crate::Error> {
|
|
|
+ pub fn insert<T: Entity + serde::Serialize>(&self, m: &T) -> Result<<T as Entity>::ID, Error> {
|
|
|
self.add(m)
|
|
|
}
|
|
|
|
|
|
/// Add an entity to its table in the database.
|
|
|
- pub fn add<T: Entity + serde::Serialize>(
|
|
|
- &self,
|
|
|
- m: &T,
|
|
|
- ) -> Result<<T as Entity>::ID, crate::Error> {
|
|
|
+ pub fn add<T: Entity + serde::Serialize>(&self, m: &T) -> Result<<T as Entity>::ID, Error> {
|
|
|
let mut hasher = std::collections::hash_map::DefaultHasher::new();
|
|
|
"add".hash(&mut hasher);
|
|
|
std::any::TypeId::of::<T>().hash(&mut hasher);
|
|
@@ -179,12 +173,12 @@ impl<'l> QueryInterface<'l> {
|
|
|
crate::model::store::serialize_into(stmt, m)?;
|
|
|
|
|
|
let rowid = self.expect_one_result(stmt, &mut |stmt| {
|
|
|
- stmt.read::<i64>(0).map_err(crate::Error::DBError)
|
|
|
+ stmt.read::<i64>(0).map_err(Error::DBError)
|
|
|
})?;
|
|
|
|
|
|
Ok(<T as Entity>::ID::from_raw_id(rowid.unwrap()))
|
|
|
},
|
|
|
- )
|
|
|
+ )?
|
|
|
}
|
|
|
}
|
|
|
|