File tree Expand file tree Collapse file tree 4 files changed +68
-1
lines changed Expand file tree Collapse file tree 4 files changed +68
-1
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,22 @@ class Expr:
118118 ['LC82030282019133LGN00']
119119 """
120120
121+ def __add__ (self , other : "Expr" ) -> "Expr" :
122+ """Combines two cql2 expressions using the AND operator.
123+
124+ Args:
125+ other (Expr): The other expression
126+
127+ Returns:
128+ Expr: The combined expression
129+
130+ Examples:
131+ >>> from cql2 import Expr
132+ >>> expr1 = Expr("landsat:scene_id = 'LC82030282019133LGN00'")
133+ >>> expr2 = Expr("landsat:cloud_cover = 10")
134+ >>> expr = expr1 + expr2
135+ """
136+
121137class SqlQuery :
122138 """A SQL query"""
123139
Original file line number Diff line number Diff line change @@ -85,6 +85,14 @@ impl Expr {
8585 fn to_sql ( & self ) -> Result < SqlQuery > {
8686 self . 0 . to_sql ( ) . map ( SqlQuery :: from) . map_err ( Error :: from)
8787 }
88+
89+ fn __add__ ( & self , rhs : & Expr ) -> Result < Expr > {
90+ Ok ( Expr ( self . 0 . clone ( ) + rhs. 0 . clone ( ) ) )
91+ }
92+
93+ fn __eq__ ( & self , rhs : & Expr ) -> bool {
94+ self . 0 == rhs. 0
95+ }
8896}
8997
9098impl From < :: cql2:: SqlQuery > for SqlQuery {
Original file line number Diff line number Diff line change @@ -57,3 +57,11 @@ def test_validate() -> None:
5757 )
5858 with pytest .raises (ValidationError ):
5959 expr .validate ()
60+
61+
62+ def test_add () -> None :
63+ assert Expr ("True" ) + Expr ("false" ) == Expr ("true AND false" )
64+
65+
66+ def test_eq () -> None :
67+ assert Expr ("True" ) == Expr ("true" )
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ use pg_escape::{quote_identifier, quote_literal};
77use serde:: { Deserialize , Serialize } ;
88use serde_json:: Value ;
99use std:: collections:: HashSet ;
10- use std:: ops:: Deref ;
10+ use std:: ops:: { Add , Deref } ;
1111use std:: str:: FromStr ;
1212use unaccent:: unaccent;
1313use wkt:: TryFromWkt ;
@@ -614,6 +614,41 @@ impl FromStr for Expr {
614614 }
615615 }
616616}
617+
618+ impl Add for Expr {
619+ type Output = Expr ;
620+
621+ ///
622+ /// Combines two expressions with the `+` operator.
623+ ///
624+ /// # Examples
625+ ///
626+ /// ```
627+ /// use cql2::Expr;
628+ /// use std::ops::Add;
629+ ///
630+ /// let expr1 = Expr::Bool(true);
631+ /// let expr2 = Expr::Bool(false);
632+ /// let expected_expr: Expr = "true and false".parse().unwrap();
633+ /// assert_eq!(expr1 + expr2, expected_expr);
634+ /// ```
635+ ///
636+ /// ```
637+ /// use cql2::Expr;
638+ /// use std::ops::Add;
639+ ///
640+ /// let expr1 = Expr::Bool(true);
641+ /// let expr2 = Expr::Bool(false);
642+ /// let expected_expr: Expr = "true and false".parse().unwrap();
643+ /// assert_eq!(expr1.add(expr2), expected_expr);
644+ /// ```
645+ fn add ( self , other : Expr ) -> Expr {
646+ Expr :: Operation {
647+ op : "and" . to_string ( ) ,
648+ args : vec ! [ Box :: new( self ) , Box :: new( other) ] ,
649+ }
650+ }
651+ }
617652#[ cfg( test) ]
618653mod tests {
619654 use super :: Expr ;
You can’t perform that action at this time.
0 commit comments