7
7
"strings"
8
8
)
9
9
10
- var OperatorMap = map [string ]string {
10
+ var BasicOperatorMap = map [string ]string {
11
11
"$gt" : ">" ,
12
12
"$gte" : ">=" ,
13
13
}
@@ -36,15 +36,15 @@ func (c *Converter) Convert(query []byte) (string, []any, error) {
36
36
return "" , nil , err
37
37
}
38
38
39
- conditions , values , err := c .convertFilter (mongoFilter )
39
+ conditions , values , err := c .convertFilter (mongoFilter , 0 )
40
40
if err != nil {
41
41
return "" , nil , err
42
42
}
43
43
44
44
return conditions , values , nil
45
45
}
46
46
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 ) {
48
48
var conditions []string
49
49
var values []any
50
50
@@ -56,31 +56,75 @@ func (c *Converter) convertFilter(filter map[string]any) (string, []any, error)
56
56
57
57
for _ , key := range keys {
58
58
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 )
65
65
}
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
72
72
}
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"
75
80
}
76
- innerResult := strings .Join (inner , " AND " )
77
81
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 + " " ))
79
85
}
80
- conditions = append (conditions , innerResult )
81
86
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
+ }
84
128
}
85
129
}
86
130
0 commit comments