Skip to content

Commit b0068d4

Browse files
committed
More clean ups.
1 parent b8baa4e commit b0068d4

File tree

4 files changed

+28
-46
lines changed

4 files changed

+28
-46
lines changed

django_mongodb_backend/fields/json.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,7 @@ def data_contains(self, compiler, connection, as_path=False): # noqa: ARG001
5454
def _has_key_predicate(path, root_column=None, negated=False, as_path=False):
5555
"""Return MQL to check for the existence of `path`."""
5656
if as_path:
57-
# if not negated:
5857
return {path: {"$exists": not negated}}
59-
# return {"$and": [{path: {"$exists": True}}, {path: {"$ne": None}}]}
60-
# return {"$or": [{path: {"$exists": False}}, {path: None}]}
6158
result = {
6259
"$and": [
6360
# The path must exist (i.e. not be "missing").
@@ -81,9 +78,9 @@ def has_key_check_simple_expression(self):
8178
def has_key_lookup(self, compiler, connection, as_path=False):
8279
"""Return MQL to check for the existence of a key."""
8380
rhs = self.rhs
81+
lhs = process_lhs(self, compiler, connection, as_path=as_path)
8482
if not isinstance(rhs, (list, tuple)):
8583
rhs = [rhs]
86-
lhs = process_lhs(self, compiler, connection, as_path=as_path)
8784
paths = []
8885
# Transform any "raw" keys into KeyTransforms to allow consistent handling
8986
# in the code that follows.
@@ -198,7 +195,7 @@ def key_transform_is_null_expr(self, compiler, connection):
198195
previous = previous.lhs
199196
root_column = previous.as_mql(compiler, connection)
200197
lhs_mql = process_lhs(self, compiler, connection, as_path=False)
201-
rhs_mql = process_rhs(self, compiler, connection)
198+
rhs_mql = process_rhs(self, compiler, connection, as_path=False)
202199
return _has_key_predicate(lhs_mql, root_column, negated=rhs_mql)
203200

204201

django_mongodb_backend/functions.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767

6868
def cast(self, compiler, connection):
6969
output_type = connection.data_types[self.output_field.get_internal_type()]
70-
lhs_mql = process_lhs(self, compiler, connection, as_path=False)[0]
70+
lhs_mql = process_lhs(self, compiler, connection)[0]
7171
if max_length := self.output_field.max_length:
7272
lhs_mql = {"$substrCP": [lhs_mql, 0, max_length]}
7373
# Skip the conversion for "object" as it doesn't need to be transformed for
@@ -81,22 +81,22 @@ def cast(self, compiler, connection):
8181

8282

8383
def concat(self, compiler, connection):
84-
return self.get_source_expressions()[0].as_mql(compiler, connection, as_path=False)
84+
return self.get_source_expressions()[0].as_mql(compiler, connection)
8585

8686

8787
def concat_pair(self, compiler, connection):
8888
# null on either side results in null for expression, wrap with coalesce.
8989
coalesced = self.coalesce()
90-
return super(ConcatPair, coalesced).as_mql_expr(compiler, connection)
90+
return super(ConcatPair, coalesced).as_mql(compiler, connection)
9191

9292

9393
def cot(self, compiler, connection):
94-
lhs_mql = process_lhs(self, compiler, connection, as_path=False)
94+
lhs_mql = process_lhs(self, compiler, connection)
9595
return {"$divide": [1, {"$tan": lhs_mql}]}
9696

9797

98-
def extract(self, compiler, connection, as_path=False):
99-
lhs_mql = process_lhs(self, compiler, connection, as_path=as_path)
98+
def extract(self, compiler, connection):
99+
lhs_mql = process_lhs(self, compiler, connection)
100100
operator = EXTRACT_OPERATORS.get(self.lookup_name)
101101
if operator is None:
102102
raise NotSupportedError(f"{self.__class__.__name__} is not supported.")
@@ -105,22 +105,8 @@ def extract(self, compiler, connection, as_path=False):
105105
return {f"${operator}": lhs_mql}
106106

107107

108-
def func(self, compiler, connection, as_path=False):
109-
lhs_mql = process_lhs(self, compiler, connection, as_path=False)
110-
if self.function is None:
111-
raise NotSupportedError(f"{self} may need an as_mql() method.")
112-
operator = MONGO_OPERATORS.get(self.__class__, self.function.lower())
113-
if as_path:
114-
return {"$expr": {f"${operator}": lhs_mql}}
115-
return {f"${operator}": lhs_mql}
116-
117-
118-
def func_path(self, compiler, connection): # noqa: ARG001
119-
raise NotSupportedError(f"{self} may need an as_mql_path() method.")
120-
121-
122-
def func_expr(self, compiler, connection):
123-
lhs_mql = process_lhs(self, compiler, connection, as_path=False)
108+
def func(self, compiler, connection):
109+
lhs_mql = process_lhs(self, compiler, connection)
124110
if self.function is None:
125111
raise NotSupportedError(f"{self} may need an as_mql() method.")
126112
operator = MONGO_OPERATORS.get(self.__class__, self.function.lower())
@@ -291,8 +277,8 @@ def register_functions():
291277
ConcatPair.as_mql_expr = concat_pair
292278
Cot.as_mql_expr = cot
293279
Extract.as_mql_expr = extract
294-
Func.as_mql_path = func_path
295-
Func.as_mql_expr = func_expr
280+
Func.as_mql_expr = func
281+
Func.can_use_path = False
296282
JSONArray.as_mql_expr = process_lhs
297283
Left.as_mql_expr = left
298284
Length.as_mql_expr = length
@@ -312,4 +298,3 @@ def register_functions():
312298
TruncDate.as_mql_expr = trunc_date
313299
TruncTime.as_mql_expr = trunc_time
314300
Upper.as_mql_expr = preserve_null("toUpper")
315-
Func.can_use_path = False

django_mongodb_backend/lookups.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@
1212
from .query_utils import is_constant_value, process_lhs, process_rhs
1313

1414

15-
def builtin_lookup_path(self, compiler, connection):
16-
lhs_mql = process_lhs(self, compiler, connection, as_path=True)
17-
value = process_rhs(self, compiler, connection, as_path=True)
18-
return connection.mongo_match_operators[self.lookup_name](lhs_mql, value)
19-
20-
2115
def builtin_lookup_expr(self, compiler, connection):
2216
value = process_rhs(self, compiler, connection, as_path=False)
2317
lhs_mql = process_lhs(self, compiler, connection, as_path=False)
2418
return connection.mongo_expr_operators[self.lookup_name](lhs_mql, value)
2519

2620

21+
def builtin_lookup_path(self, compiler, connection):
22+
lhs_mql = process_lhs(self, compiler, connection, as_path=True)
23+
value = process_rhs(self, compiler, connection, as_path=True)
24+
return connection.mongo_match_operators[self.lookup_name](lhs_mql, value)
25+
26+
2727
_field_resolve_expression_parameter = FieldGetDbPrepValueIterableMixin.resolve_expression_parameter
2828

2929

@@ -85,18 +85,18 @@ def get_subquery_wrapping_pipeline(self, compiler, connection, field_name, expr)
8585
]
8686

8787

88-
def is_null_path(self, compiler, connection):
88+
def is_null_expr(self, compiler, connection):
8989
if not isinstance(self.rhs, bool):
9090
raise ValueError("The QuerySet value for an isnull lookup must be True or False.")
91-
lhs_mql = process_lhs(self, compiler, connection, as_path=True)
92-
return connection.mongo_match_operators["isnull"](lhs_mql, self.rhs)
91+
lhs_mql = process_lhs(self, compiler, connection, as_path=False)
92+
return connection.mongo_expr_operators["isnull"](lhs_mql, self.rhs)
9393

9494

95-
def is_null_expr(self, compiler, connection):
95+
def is_null_path(self, compiler, connection):
9696
if not isinstance(self.rhs, bool):
9797
raise ValueError("The QuerySet value for an isnull lookup must be True or False.")
98-
lhs_mql = process_lhs(self, compiler, connection, as_path=False)
99-
return connection.mongo_expr_operators["isnull"](lhs_mql, self.rhs)
98+
lhs_mql = process_lhs(self, compiler, connection, as_path=True)
99+
return connection.mongo_match_operators["isnull"](lhs_mql, self.rhs)
100100

101101

102102
# from https://www.pcre.org/current/doc/html/pcre2pattern.html#SEC4
@@ -146,9 +146,8 @@ def can_use_path(self):
146146

147147

148148
def register_lookups():
149-
Lookup.can_use_path = can_use_path
150-
BuiltinLookup.as_mql_path = builtin_lookup_path
151149
BuiltinLookup.as_mql_expr = builtin_lookup_expr
150+
BuiltinLookup.as_mql_path = builtin_lookup_path
152151
FieldGetDbPrepValueIterableMixin.resolve_expression_parameter = (
153152
field_resolve_expression_parameter
154153
)
@@ -157,6 +156,7 @@ def register_lookups():
157156
In.get_subquery_wrapping_pipeline = get_subquery_wrapping_pipeline
158157
IsNull.as_mql_path = is_null_path
159158
IsNull.as_mql_expr = is_null_expr
159+
Lookup.can_use_path = can_use_path
160160
PatternLookup.prep_lookup_value_mongo = pattern_lookup_prep_lookup_value
161161
# Patching the main method, it is not supported yet.
162162
UUIDTextMixin.as_mql = uuid_text_mixin

django_mongodb_backend/query.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,12 @@ def where_node(self, compiler, connection, as_path=False):
336336
return mql
337337

338338

339-
def nothing_node(self, compiler, connection, as_path=None): # noqa: ARG001
339+
def nothing_node(self, compiler, connection):
340340
return self.as_sql(compiler, connection)
341341

342342

343343
def register_nodes():
344344
ExtraWhere.as_mql = extra_where
345345
Join.as_mql = join
346-
NothingNode.as_mql = nothing_node
346+
NothingNode.as_mql_expr = nothing_node
347347
WhereNode.as_mql = where_node

0 commit comments

Comments
 (0)