Skip to content

Commit 5b9fa93

Browse files
committed
More clean ups.
1 parent b8baa4e commit 5b9fa93

File tree

4 files changed

+18
-36
lines changed

4 files changed

+18
-36
lines changed

django_mongodb_backend/fields/json.py

Lines changed: 0 additions & 3 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").

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: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

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)