77 "strings"
88)
99
10- var OperatorMap = map [string ]string {
10+ var BasicOperatorMap = map [string ]string {
1111 "$gt" : ">" ,
1212 "$gte" : ">=" ,
1313}
@@ -36,15 +36,15 @@ func (c *Converter) Convert(query []byte) (string, []any, error) {
3636 return "" , nil , err
3737 }
3838
39- conditions , values , err := c .convertFilter (mongoFilter )
39+ conditions , values , err := c .convertFilter (mongoFilter , 0 )
4040 if err != nil {
4141 return "" , nil , err
4242 }
4343
4444 return conditions , values , nil
4545}
4646
47- func (c * Converter ) convertFilter (filter map [string ]any ) (string , []any , error ) {
47+ func (c * Converter ) convertFilter (filter map [string ]any , paramIndex int ) (string , []any , error ) {
4848 var conditions []string
4949 var values []any
5050
@@ -56,31 +56,75 @@ func (c *Converter) convertFilter(filter map[string]any) (string, []any, error)
5656
5757 for _ , key := range keys {
5858 value := filter [key ]
59- switch v := value .( type ) {
60- case map [ string ] any :
61- inner := [] string {}
62- operators := [] string {}
63- for operator := range v {
64- operators = append ( operators , operator )
59+
60+ switch key {
61+ case "$or" , "$and" :
62+ orConditions , ok := anyToSliceMapAny ( value )
63+ if ! ok {
64+ return "" , nil , fmt . Errorf ( "invalid value for $or operator (must be array of objects): %v" , value )
6565 }
66- sort . Strings ( operators )
67- for _ , operator := range operators {
68- value := v [ operator ]
69- op , ok := OperatorMap [ operator ]
70- if ! ok {
71- return "" , nil , fmt . Errorf ( "unknown operator: %s" , operator )
66+
67+ inner := [] string {}
68+ for _ , orCondition := range orConditions {
69+ innerConditions , innerValues , err := c . convertFilter ( orCondition , paramIndex )
70+ if err != nil {
71+ return "" , nil , err
7272 }
73- inner = append (inner , fmt .Sprintf ("(%s %s $%d)" , c .columnName (key ), op , len (values )+ 1 ))
74- values = append (values , value )
73+ paramIndex += len (innerValues )
74+ inner = append (inner , innerConditions )
75+ values = append (values , innerValues ... )
76+ }
77+ op := "AND"
78+ if key == "$or" {
79+ op = "OR"
7580 }
76- innerResult := strings .Join (inner , " AND " )
7781 if len (inner ) > 1 {
78- innerResult = "(" + innerResult + ")"
82+ conditions = append (conditions , "(" + strings .Join (inner , " " + op + " " )+ ")" )
83+ } else {
84+ conditions = append (conditions , strings .Join (inner , " " + op + " " ))
7985 }
80- conditions = append (conditions , innerResult )
8186 default :
82- conditions = append (conditions , fmt .Sprintf ("(%s = $%d)" , c .columnName (key ), len (values )+ 1 ))
83- values = append (values , value )
87+ switch v := value .(type ) {
88+ case map [string ]any :
89+ inner := []string {}
90+ operators := []string {}
91+ for operator := range v {
92+ operators = append (operators , operator )
93+ }
94+ sort .Strings (operators )
95+ for _ , operator := range operators {
96+ switch operator {
97+ case "$or" :
98+ return "" , nil , fmt .Errorf ("$or as scalar operator not supported" )
99+ case "$and" :
100+ return "" , nil , fmt .Errorf ("$and as scalar operator not supported" )
101+ case "$in" :
102+ inner = append (inner , fmt .Sprintf ("(%s = ANY(?))" , c .columnName (key )))
103+ if ! isScalarSlice (v [operator ]) {
104+ return "" , nil , fmt .Errorf ("invalid value for $in operator (must array of primatives): %v" , v [operator ])
105+ }
106+ values = append (values , v [operator ])
107+ default :
108+ value := v [operator ]
109+ op , ok := BasicOperatorMap [operator ]
110+ if ! ok {
111+ return "" , nil , fmt .Errorf ("unknown operator: %s" , operator )
112+ }
113+ paramIndex ++
114+ inner = append (inner , fmt .Sprintf ("(%s %s $%d)" , c .columnName (key ), op , paramIndex ))
115+ values = append (values , value )
116+ }
117+ }
118+ innerResult := strings .Join (inner , " AND " )
119+ if len (inner ) > 1 {
120+ innerResult = "(" + innerResult + ")"
121+ }
122+ conditions = append (conditions , innerResult )
123+ default :
124+ paramIndex ++
125+ conditions = append (conditions , fmt .Sprintf ("(%s = $%d)" , c .columnName (key ), paramIndex ))
126+ values = append (values , value )
127+ }
84128 }
85129 }
86130
0 commit comments