|
@@ -1,10 +1,10 @@
|
|
|
-use std::marker::PhantomData;
|
|
|
-use std::hash::{Hash,Hasher};
|
|
|
-use crate::{Entity,QueryInterface,Error};
|
|
|
-use super::build::{QueryComponent,StaticVersion,DerivedQuery,QueryPart};
|
|
|
-use super::{Filterable,Resolvable};
|
|
|
+use super::build::{DerivedQuery, QueryComponent, QueryPart, StaticVersion};
|
|
|
+use super::{Filterable, Resolvable};
|
|
|
use crate::entity::EntityColumn;
|
|
|
use crate::model::Modelable;
|
|
|
+use crate::{Entity, Error, QueryInterface};
|
|
|
+use std::hash::{Hash, Hasher};
|
|
|
+use std::marker::PhantomData;
|
|
|
|
|
|
pub struct Update<'r, 'q, T: Entity> {
|
|
|
qi: &'r QueryInterface<'q>,
|
|
@@ -20,10 +20,7 @@ impl<'r, 'q, T: Entity> Update<'r, 'q, T> {
|
|
|
}
|
|
|
|
|
|
pub fn to(self, to: &'r T) -> Entire<'r, 'q, T> {
|
|
|
- Entire {
|
|
|
- wrap: self,
|
|
|
- to,
|
|
|
- }
|
|
|
+ Entire { wrap: self, to }
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -40,10 +37,7 @@ impl<'r, 'q, T: Entity> StaticVersion for Update<'r, 'q, T> {
|
|
|
|
|
|
impl<'r, 'q, T: Entity> QueryComponent for Update<'r, 'q, T> {
|
|
|
fn derive(&self) -> DerivedQuery {
|
|
|
- DerivedQuery::new().add(
|
|
|
- QueryPart::Root,
|
|
|
- format!("UPDATE {}", T::table_name()),
|
|
|
- )
|
|
|
+ DerivedQuery::new().add(QueryPart::Root, format!("UPDATE {}", T::table_name()))
|
|
|
}
|
|
|
|
|
|
fn contribute<H: Hasher>(&self, hasher: &mut H) {
|
|
@@ -69,7 +63,7 @@ impl<'r, 'q, T: Entity> Resolvable<'r, 'q, T> for Update<'r, 'q, T> {
|
|
|
|
|
|
pub struct Entire<'r, 'q, T: Entity> {
|
|
|
wrap: Update<'r, 'q, T>,
|
|
|
- to: &'r T
|
|
|
+ to: &'r T,
|
|
|
}
|
|
|
|
|
|
impl<'r, 'q, T: Entity> StaticVersion for Entire<'r, 'q, T> {
|
|
@@ -96,7 +90,7 @@ impl<'r, 'q, T: Entity> QueryComponent for Entire<'r, 'q, T> {
|
|
|
fn bind(&self, stmt: &mut sqlite::Statement<'_>) -> Result<usize, Error> {
|
|
|
let mut ind = self.wrap.bind(stmt)?;
|
|
|
|
|
|
- self.to.visit_values::<Error,_>(&mut |val| {
|
|
|
+ self.to.visit_values::<Error, _>(&mut |val| {
|
|
|
val.bind_to(stmt, ind)?;
|
|
|
ind += 1;
|
|
|
Ok(())
|
|
@@ -128,13 +122,13 @@ where
|
|
|
given: &'r G,
|
|
|
) -> Set<'r, 'q, Self, C, G>
|
|
|
where
|
|
|
- Self: Sized
|
|
|
+ Self: Sized,
|
|
|
{
|
|
|
Set {
|
|
|
wrap: self,
|
|
|
col,
|
|
|
given,
|
|
|
- _ghost: PhantomData
|
|
|
+ _ghost: PhantomData,
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -196,8 +190,8 @@ where
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl<'r, 'q, S: Settable<'r, 'q>, C: EntityColumn, G: Modelable + ?Sized>
|
|
|
- Settable<'r, 'q> for Set<'r, 'q, S, C, G>
|
|
|
+impl<'r, 'q, S: Settable<'r, 'q>, C: EntityColumn, G: Modelable + ?Sized> Settable<'r, 'q>
|
|
|
+ for Set<'r, 'q, S, C, G>
|
|
|
where
|
|
|
<S as StaticVersion>::Is: Settable<'static, 'static>,
|
|
|
'q: 'r,
|
|
@@ -205,8 +199,8 @@ where
|
|
|
type Table = C::Entity;
|
|
|
}
|
|
|
|
|
|
-impl<'r, 'q, S: Settable<'r, 'q>, C: EntityColumn, G: Modelable + ?Sized>
|
|
|
- Filterable<'r, 'q> for Set<'r, 'q, S, C, G>
|
|
|
+impl<'r, 'q, S: Settable<'r, 'q>, C: EntityColumn, G: Modelable + ?Sized> Filterable<'r, 'q>
|
|
|
+ for Set<'r, 'q, S, C, G>
|
|
|
where
|
|
|
<S as StaticVersion>::Is: Settable<'static, 'static>,
|
|
|
'q: 'r,
|
|
@@ -217,23 +211,55 @@ where
|
|
|
#[cfg(test)]
|
|
|
mod test {
|
|
|
use crate::prelude::*;
|
|
|
- use crate::test_support::KVStore;
|
|
|
use crate::query::Resolvable;
|
|
|
+ use crate::test_support::KVStore;
|
|
|
|
|
|
#[test]
|
|
|
fn simple_update() {
|
|
|
let db = crate::DB::new_in_memory(crate::Schema::new().entity::<KVStore>()).unwrap();
|
|
|
let qi = db.query_interface();
|
|
|
|
|
|
- qi.add(&KVStore { key: "key".into(), value: "value".into() }).unwrap();
|
|
|
- qi.add(&KVStore { key: "key2".into(), value: "value2".into() }).unwrap();
|
|
|
- qi.add(&KVStore { key: "key2".into(), value: "value2b".into() }).unwrap();
|
|
|
-
|
|
|
- assert_eq!(qi.get().by(KVStore::Key, "key").one().unwrap().unwrap().value, "value");
|
|
|
+ qi.add(&KVStore {
|
|
|
+ key: "key".into(),
|
|
|
+ value: "value".into(),
|
|
|
+ })
|
|
|
+ .unwrap();
|
|
|
+ qi.add(&KVStore {
|
|
|
+ key: "key2".into(),
|
|
|
+ value: "value2".into(),
|
|
|
+ })
|
|
|
+ .unwrap();
|
|
|
+ qi.add(&KVStore {
|
|
|
+ key: "key2".into(),
|
|
|
+ value: "value2b".into(),
|
|
|
+ })
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ assert_eq!(
|
|
|
+ qi.get()
|
|
|
+ .by(KVStore::Key, "key")
|
|
|
+ .one()
|
|
|
+ .unwrap()
|
|
|
+ .unwrap()
|
|
|
+ .value,
|
|
|
+ "value"
|
|
|
+ );
|
|
|
assert_eq!(qi.get().by(KVStore::Key, "key2").all().unwrap().len(), 2);
|
|
|
|
|
|
- qi.update().update(KVStore::Value, "newvalue").by(KVStore::Key, "key").exec().unwrap();
|
|
|
- assert_eq!(qi.get().by(KVStore::Key, "key").one().unwrap().unwrap().value, "newvalue");
|
|
|
+ qi.update()
|
|
|
+ .update(KVStore::Value, "newvalue")
|
|
|
+ .by(KVStore::Key, "key")
|
|
|
+ .exec()
|
|
|
+ .unwrap();
|
|
|
+ assert_eq!(
|
|
|
+ qi.get()
|
|
|
+ .by(KVStore::Key, "key")
|
|
|
+ .one()
|
|
|
+ .unwrap()
|
|
|
+ .unwrap()
|
|
|
+ .value,
|
|
|
+ "newvalue"
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
@@ -241,14 +267,26 @@ mod test {
|
|
|
let db = crate::DB::new_in_memory(crate::Schema::new().entity::<KVStore>()).unwrap();
|
|
|
let qi = db.query_interface();
|
|
|
|
|
|
- let id = qi.add(&KVStore { key: "a".into(), value: "b".into() }).unwrap();
|
|
|
+ let id = qi
|
|
|
+ .add(&KVStore {
|
|
|
+ key: "a".into(),
|
|
|
+ value: "b".into(),
|
|
|
+ })
|
|
|
+ .unwrap();
|
|
|
|
|
|
let check = qi.get().by(KVStore::ID, &id).all().unwrap();
|
|
|
assert_eq!(check.len(), 1);
|
|
|
assert_eq!(check[0].key, "a");
|
|
|
assert_eq!(check[0].value, "b");
|
|
|
-
|
|
|
- qi.update().to(&KVStore { key: "c".into(), value: "d".into() }).by(KVStore::ID, &id).exec().unwrap();
|
|
|
+
|
|
|
+ qi.update()
|
|
|
+ .to(&KVStore {
|
|
|
+ key: "c".into(),
|
|
|
+ value: "d".into(),
|
|
|
+ })
|
|
|
+ .by(KVStore::ID, &id)
|
|
|
+ .exec()
|
|
|
+ .unwrap();
|
|
|
|
|
|
let check = qi.get().by(KVStore::ID, &id).all().unwrap();
|
|
|
assert_eq!(check.len(), 1);
|