Pārlūkot izejas kodu

Add comparison support to Filterable.

Kestrel 2 gadi atpakaļ
vecāks
revīzija
4ba3f63b3a
3 mainītis faili ar 44 papildinājumiem un 3 dzēšanām
  1. 1 1
      Cargo.lock
  2. 1 1
      microrm/src/lib.rs
  3. 42 1
      microrm/src/query/build.rs

+ 1 - 1
Cargo.lock

@@ -358,7 +358,7 @@ dependencies = [
 
 [[package]]
 name = "microrm"
-version = "0.2.4"
+version = "0.3.0"
 dependencies = [
  "base64",
  "const-str",

+ 1 - 1
microrm/src/lib.rs

@@ -13,7 +13,7 @@ use meta::Metaschema;
 pub use microrm_macros::{make_index, Entity, Modelable};
 
 pub use error::Error;
-pub use query::{QueryInterface, WithID};
+pub use query::{QueryInterface, WithID, build::CompareOp};
 pub use schema::Schema;
 
 pub mod prelude {

+ 42 - 1
microrm/src/query/build.rs

@@ -148,6 +148,27 @@ where
     }
 }
 
+#[derive(Debug)]
+pub enum CompareOp {
+    LessThan,
+    AtMost,
+    Equals,
+    AtLeast,
+    MoreThan,
+}
+
+impl CompareOp {
+    fn ch(&self) -> &'static str {
+        match self {
+            Self::LessThan => "<",
+            Self::AtMost => "<=",
+            Self::Equals => "=",
+            Self::AtLeast => ">=",
+            Self::MoreThan => ">",
+        }
+    }
+}
+
 /// Any query that can have a WHERE clause attached to it
 pub trait Filterable<'r, 'q>: Resolvable<'r, 'q, Self::Table>
 where
@@ -166,6 +187,25 @@ where
         Filter {
             wrap: self,
             col,
+            op: CompareOp::Equals,
+            given,
+            _ghost: PhantomData,
+        }
+    }
+
+    fn by_op<C: EntityColumn<Entity = Self::Table>, G: Modelable + ?Sized>(
+        self,
+        col: C,
+        op: CompareOp,
+        given: &'r G
+    ) -> Filter<'r, 'q, Self, C, G>
+    where
+        Self: Sized,
+    {
+        Filter {
+            wrap: self,
+            col,
+            op,
             given,
             _ghost: PhantomData,
         }
@@ -270,6 +310,7 @@ where
 {
     wrap: F,
     col: C,
+    op: CompareOp,
     given: &'r G,
     _ghost: PhantomData<&'q ()>,
 }
@@ -291,7 +332,7 @@ where
     fn derive(&self) -> DerivedQuery {
         self.wrap
             .derive()
-            .add(QueryPart::Where, format!("{} = ?", self.col.name()))
+            .add(QueryPart::Where, format!("{} {} ?", self.col.name(), self.op.ch()))
     }
 
     fn contribute<H: Hasher>(&self, hasher: &mut H) {