|
@@ -160,7 +160,10 @@ impl<'l> QueryInterface<'l> {
|
|
|
query.reset().expect("Couldn't reset query");
|
|
|
with(query)
|
|
|
}
|
|
|
+}
|
|
|
|
|
|
+// Legacy interface
|
|
|
+impl<'l> QueryInterface<'l> {
|
|
|
/// Search for an entity by a property
|
|
|
pub fn get_one_by<C: EntityColumn, V: Modelable>(
|
|
|
&self,
|
|
@@ -420,3 +423,138 @@ impl<'l> QueryInterface<'l> {
|
|
|
)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+/*
|
|
|
+
|
|
|
+qi.get().by(Table::Column, value).by(Table::OtherColumn, value).one()
|
|
|
+qi.get().id(some_id).one()
|
|
|
+qi.get().by(Table::Column, value).all()
|
|
|
+
|
|
|
+ * */
|
|
|
+
|
|
|
+pub struct BuildResult(String, bool);
|
|
|
+
|
|
|
+pub trait Buildable {
|
|
|
+ fn build() -> BuildResult;
|
|
|
+}
|
|
|
+
|
|
|
+pub trait Gettable<'sq, 'l, T: Entity> {
|
|
|
+ type Builder: Buildable;
|
|
|
+ fn qi(&self) -> &'sq QueryInterface<'l> { todo!() }
|
|
|
+}
|
|
|
+
|
|
|
+pub struct GetQuery<'sq, 'l, T: Entity> {
|
|
|
+ qi: &'sq QueryInterface<'l>,
|
|
|
+ _ghost: std::marker::PhantomData<(T, &'sq str)>,
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+impl<'sq, 'l, T: Entity> Gettable<'sq, 'l, T> for GetQuery<'sq, 'l, T> {
|
|
|
+ type Builder = GetBuild<T>;
|
|
|
+}
|
|
|
+
|
|
|
+pub struct GetBuild<T: Entity> { _ghost: std::marker::PhantomData<T> }
|
|
|
+
|
|
|
+impl<T: Entity> Buildable for GetBuild<T> {
|
|
|
+ fn build() -> BuildResult {
|
|
|
+ todo!()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+pub struct ByClause<'sq, 'l, T: Entity, Col: EntityColumn<Entity = T>, Wrap: Gettable<'sq, 'l, T>> {
|
|
|
+ wrap: Wrap,
|
|
|
+ col: &'sq Col,
|
|
|
+ data: &'sq dyn Modelable,
|
|
|
+ _ghost: std::marker::PhantomData<(&'sq str, &'l str, T, Col, Wrap)>,
|
|
|
+}
|
|
|
+
|
|
|
+pub struct ByBuild<Col: EntityColumn, Wrap: Buildable> {
|
|
|
+ _ghost: std::marker::PhantomData<(Col, Wrap)>
|
|
|
+}
|
|
|
+
|
|
|
+impl<Col: EntityColumn, Wrap: Buildable> Buildable for ByBuild<Col, Wrap> {
|
|
|
+ fn build() -> BuildResult {
|
|
|
+ Wrap::build();
|
|
|
+ todo!()
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<'sq, 'l, T: Entity> GetQuery<'sq, 'l, T> {
|
|
|
+ pub fn by<NCol: EntityColumn<Entity = T>>(self, col: &'sq NCol, data: &'sq dyn Modelable) -> ByClause<'sq, 'l, T, NCol, Self> {
|
|
|
+ ByClause { wrap: self, col, data, _ghost: std::marker::PhantomData }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<'sq, 'l, T: Entity, Col: EntityColumn<Entity = T>, Wrap: Gettable<'sq, 'l, T>> Gettable<'sq, 'l, T> for ByClause<'sq, 'l, T, Col, Wrap> {
|
|
|
+ type Builder = ByBuild<Col, Wrap::Builder>;
|
|
|
+
|
|
|
+ /*fn build_query(&self) -> (String, bool) {
|
|
|
+ let (parent, is_where) = self.wrap.build_query();
|
|
|
+ (format!("{} {} {} = ?", parent, if is_where { "AND" } else { "WHERE" }, "<>"), true)
|
|
|
+ }*/
|
|
|
+}
|
|
|
+
|
|
|
+impl<'sq, 'l, T: Entity, Col: EntityColumn<Entity = T>, Wrap: Gettable<'sq, 'l, T>> ByClause<'sq, 'l, T, Col, Wrap> {
|
|
|
+ fn build_query(&self) {
|
|
|
+ // <Self as Gettable<T>>::build_query(self);
|
|
|
+ // <Self as Gettable<T>>::Builder;
|
|
|
+ <<Self as Gettable<T>>::Builder>::build();
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn one(self) -> Option<T> {
|
|
|
+ // let ty = std::any::TypeId::of::<ByClause<'static, 'static, T, Col, Wrap>>();
|
|
|
+ todo!()
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn all(self) -> Vec<T> {
|
|
|
+ todo!()
|
|
|
+ }
|
|
|
+
|
|
|
+ pub fn by<NCol: EntityColumn<Entity = T>>(self, col: &'sq NCol, data: &'sq dyn Modelable) -> ByClause<'sq, 'l, T, NCol, Self> {
|
|
|
+ ByClause { wrap: self, col, data, _ghost: std::marker::PhantomData }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl<'l> QueryInterface<'l> {
|
|
|
+ // pub fn get<'q, T: Entity>(&'q self) -> GetQuery<T> where 'q: 'l {
|
|
|
+ pub fn get<'sq, T: Entity>(&'sq self) -> GetQuery<T> where 'sq: 'l {
|
|
|
+ GetQuery::<'sq, 'l, _> { qi: self, _ghost: std::marker::PhantomData }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[cfg(test)]
|
|
|
+mod get_test {
|
|
|
+ use microrm_macros::Entity;
|
|
|
+ use serde::{Serialize, Deserialize};
|
|
|
+
|
|
|
+ use crate::QueryInterface;
|
|
|
+
|
|
|
+
|
|
|
+ #[derive(Entity, Serialize, Deserialize)]
|
|
|
+ #[microrm_internal]
|
|
|
+ pub struct KVStore {
|
|
|
+ key: String,
|
|
|
+ value: String
|
|
|
+ }
|
|
|
+
|
|
|
+ fn simple_test<'sq, 'l>(qi: &'sq QueryInterface<'l>) {
|
|
|
+ let g = qi.get::<'sq, KVStore>();
|
|
|
+ // qi.get::<'l,'_,KVStore>();
|
|
|
+
|
|
|
+ //let t = g.by(&KVStore::Key, &"test");
|
|
|
+ //drop(t);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ #[test]
|
|
|
+ fn test_build_query() {
|
|
|
+ let db = crate::DB::new_in_memory(crate::Schema::new()).unwrap();
|
|
|
+ let qi = db.query_interface();
|
|
|
+
|
|
|
+ simple_test(&qi);
|
|
|
+
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|