Skip to content

Commit d952f2a

Browse files
committed
Refactor binary operators into single parent class
1 parent 331f507 commit d952f2a

File tree

1 file changed

+34
-78
lines changed

1 file changed

+34
-78
lines changed

django_mongodb_backend/query_conversion/expression_converters.py

Lines changed: 34 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,17 @@ def is_simple_value(cls, value):
2424
return not isinstance(value, dict) or value is None
2525

2626

27-
class _EqExpressionConverter(_BaseExpressionConverter):
28-
"""Convert $eq operation to a $match compatible query.
29-
30-
For example::
31-
"$expr": {
32-
{"$eq": ["$status", "active"]}
33-
}
34-
is converted to::
35-
{"status": "active"}
27+
class _BinaryExpressionConverter(_BaseExpressionConverter):
28+
"""
29+
Base class for optimizers that handle binary expression operations in MQL queries.
3630
"""
3731

32+
operator: str
33+
3834
@classmethod
39-
def convert(cls, eq_args):
40-
if isinstance(eq_args, list) and len(eq_args) == 2:
41-
field_expr, value = eq_args
35+
def convert(cls, args):
36+
if isinstance(args, list) and len(args) == 2:
37+
field_expr, value = args
4238

4339
# Check if first argument is a simple field reference
4440
if (
@@ -47,12 +43,28 @@ def convert(cls, eq_args):
4743
and cls.is_simple_value(value)
4844
):
4945
field_name = field_expr[1:] # Remove the $ prefix
50-
return {field_name: value}
46+
if cls.operator == "$eq":
47+
return {field_name: value}
48+
return {field_name: {cls.operator: value}}
5149

5250
return None
5351

5452

55-
class _GtExpressionConverter(_BaseExpressionConverter):
53+
class _EqExpressionConverter(_BinaryExpressionConverter):
54+
"""Convert $eq operation to a $match compatible query.
55+
56+
For example::
57+
"$expr": {
58+
{"$eq": ["$status", "active"]}
59+
}
60+
is converted to::
61+
{"status": "active"}
62+
"""
63+
64+
operator = "$eq"
65+
66+
67+
class _GtExpressionConverter(_BinaryExpressionConverter):
5668
"""Convert $gt operation to a $match compatible query.
5769
5870
For example::
@@ -63,24 +75,10 @@ class _GtExpressionConverter(_BaseExpressionConverter):
6375
{"$gt": ["price", 100]}
6476
"""
6577

66-
@classmethod
67-
def convert(cls, gt_args):
68-
if isinstance(gt_args, list) and len(gt_args) == 2:
69-
field_expr, value = gt_args
70-
71-
# Check if first argument is a simple field reference
72-
if (
73-
isinstance(field_expr, str)
74-
and field_expr.startswith("$")
75-
and cls.is_simple_value(value)
76-
):
77-
field_name = field_expr[1:] # Remove the $ prefix
78-
return {field_name: {"$gt": value}}
79-
80-
return None
78+
operator = "$gt"
8179

8280

83-
class _GteExpressionConverter(_BaseExpressionConverter):
81+
class _GteExpressionConverter(_BinaryExpressionConverter):
8482
"""Convert $gte operation to a $match compatible query.
8583
8684
For example::
@@ -91,24 +89,10 @@ class _GteExpressionConverter(_BaseExpressionConverter):
9189
{"price": {"$gte", 100}}
9290
"""
9391

94-
@classmethod
95-
def convert(cls, gte_args):
96-
if isinstance(gte_args, list) and len(gte_args) == 2:
97-
field_expr, value = gte_args
92+
operator = "$gte"
9893

99-
# Check if first argument is a simple field reference
100-
if (
101-
isinstance(field_expr, str)
102-
and field_expr.startswith("$")
103-
and cls.is_simple_value(value)
104-
):
105-
field_name = field_expr[1:] # Remove the $ prefix
106-
return {field_name: {"$gte": value}}
10794

108-
return None
109-
110-
111-
class _LtExpressionConverter(_BaseExpressionConverter):
95+
class _LtExpressionConverter(_BinaryExpressionConverter):
11296
"""Convert $lt operation to a $match compatible query.
11397
11498
For example::
@@ -119,24 +103,10 @@ class _LtExpressionConverter(_BaseExpressionConverter):
119103
{"$lt": ["price", 100]}
120104
"""
121105

122-
@classmethod
123-
def convert(cls, lt_args):
124-
if isinstance(lt_args, list) and len(lt_args) == 2:
125-
field_expr, value = lt_args
106+
operator = "$lt"
126107

127-
# Check if first argument is a simple field reference
128-
if (
129-
isinstance(field_expr, str)
130-
and field_expr.startswith("$")
131-
and cls.is_simple_value(value)
132-
):
133-
field_name = field_expr[1:] # Remove the $ prefix
134-
return {field_name: {"$lt": value}}
135-
136-
return None
137108

138-
139-
class _LteExpressionConverter(_BaseExpressionConverter):
109+
class _LteExpressionConverter(_BinaryExpressionConverter):
140110
"""Convert $lte operation to a $match compatible query.
141111
142112
For example::
@@ -147,21 +117,7 @@ class _LteExpressionConverter(_BaseExpressionConverter):
147117
{"price": {"$lte", 100}}
148118
"""
149119

150-
@classmethod
151-
def convert(cls, lte_args):
152-
if isinstance(lte_args, list) and len(lte_args) == 2:
153-
field_expr, value = lte_args
154-
155-
# Check if first argument is a simple field reference
156-
if (
157-
isinstance(field_expr, str)
158-
and field_expr.startswith("$")
159-
and cls.is_simple_value(value)
160-
):
161-
field_name = field_expr[1:] # Remove the $ prefix
162-
return {field_name: {"$lte": value}}
163-
164-
return None
120+
operator = "$lte"
165121

166122

167123
class _InExpressionConverter(_BaseExpressionConverter):
@@ -183,7 +139,7 @@ def convert(cls, in_args):
183139
# Check if first argument is a simple field reference
184140
if isinstance(field_expr, str) and field_expr.startswith("$"):
185141
field_name = field_expr[1:] # Remove the $ prefix
186-
if isinstance(values, list | tuple | set) and all(
142+
if isinstance(values, (list, tuple, set)) and all( # noqa: UP038
187143
cls.is_simple_value(v) for v in values
188144
):
189145
return {field_name: {"$in": values}}

0 commit comments

Comments
 (0)