Skip to content

Commit 5de095b

Browse files
committed
Rename DatabaseWrapper operator helpers
Prepare the way for operators that generate $match sytnax.
1 parent 204e722 commit 5de095b

File tree

2 files changed

+28
-28
lines changed

2 files changed

+28
-28
lines changed

django_mongodb_backend/base.py

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from .features import DatabaseFeatures
2121
from .introspection import DatabaseIntrospection
2222
from .operations import DatabaseOperations
23-
from .query_utils import regex_match
2423
from .schema import DatabaseSchemaEditor
2524
from .utils import OperationDebugWrapper
2625
from .validation import DatabaseValidation
@@ -97,44 +96,51 @@ class DatabaseWrapper(BaseDatabaseWrapper):
9796
}
9897
_connection_pools = {}
9998

100-
def _isnull_operator(a, b):
101-
is_null = {
99+
def _isnull_expr(field, is_null):
100+
mql = {
102101
"$or": [
103102
# The path does not exist (i.e. is "missing")
104-
{"$eq": [{"$type": a}, "missing"]},
103+
{"$eq": [{"$type": field}, "missing"]},
105104
# or the value is None.
106-
{"$eq": [a, None]},
105+
{"$eq": [field, None]},
107106
]
108107
}
109-
return is_null if b else {"$not": is_null}
108+
return mql if is_null else {"$not": mql}
109+
110+
def _regex_expr(field, regex_vals, insensitive=False):
111+
regex = {"$concat": regex_vals} if isinstance(regex_vals, tuple) else regex_vals
112+
options = "i" if insensitive else ""
113+
return {"$regexMatch": {"input": field, "regex": regex, "options": options}}
110114

111115
mongo_operators = {
112116
"exact": lambda a, b: {"$eq": [a, b]},
113117
"gt": lambda a, b: {"$gt": [a, b]},
114118
"gte": lambda a, b: {"$gte": [a, b]},
115119
# MongoDB considers null less than zero. Exclude null values to match
116120
# SQL behavior.
117-
"lt": lambda a, b: {"$and": [{"$lt": [a, b]}, DatabaseWrapper._isnull_operator(a, False)]},
118-
"lte": lambda a, b: {
119-
"$and": [{"$lte": [a, b]}, DatabaseWrapper._isnull_operator(a, False)]
120-
},
121+
"lt": lambda a, b: {"$and": [{"$lt": [a, b]}, DatabaseWrapper._isnull_expr(a, False)]},
122+
"lte": lambda a, b: {"$and": [{"$lte": [a, b]}, DatabaseWrapper._isnull_expr(a, False)]},
121123
"in": lambda a, b: {"$in": [a, b]},
122-
"isnull": _isnull_operator,
124+
"isnull": _isnull_expr,
123125
"range": lambda a, b: {
124126
"$and": [
125-
{"$or": [DatabaseWrapper._isnull_operator(b[0], True), {"$gte": [a, b[0]]}]},
126-
{"$or": [DatabaseWrapper._isnull_operator(b[1], True), {"$lte": [a, b[1]]}]},
127+
{"$or": [DatabaseWrapper._isnull_expr(b[0], True), {"$gte": [a, b[0]]}]},
128+
{"$or": [DatabaseWrapper._isnull_expr(b[1], True), {"$lte": [a, b[1]]}]},
127129
]
128130
},
129-
"iexact": lambda a, b: regex_match(a, ("^", b, {"$literal": "$"}), insensitive=True),
130-
"startswith": lambda a, b: regex_match(a, ("^", b)),
131-
"istartswith": lambda a, b: regex_match(a, ("^", b), insensitive=True),
132-
"endswith": lambda a, b: regex_match(a, (b, {"$literal": "$"})),
133-
"iendswith": lambda a, b: regex_match(a, (b, {"$literal": "$"}), insensitive=True),
134-
"contains": lambda a, b: regex_match(a, b),
135-
"icontains": lambda a, b: regex_match(a, b, insensitive=True),
136-
"regex": lambda a, b: regex_match(a, b),
137-
"iregex": lambda a, b: regex_match(a, b, insensitive=True),
131+
"iexact": lambda a, b: DatabaseWrapper._regex_expr(
132+
a, ("^", b, {"$literal": "$"}), insensitive=True
133+
),
134+
"startswith": lambda a, b: DatabaseWrapper._regex_expr(a, ("^", b)),
135+
"istartswith": lambda a, b: DatabaseWrapper._regex_expr(a, ("^", b), insensitive=True),
136+
"endswith": lambda a, b: DatabaseWrapper._regex_expr(a, (b, {"$literal": "$"})),
137+
"iendswith": lambda a, b: DatabaseWrapper._regex_expr(
138+
a, (b, {"$literal": "$"}), insensitive=True
139+
),
140+
"contains": lambda a, b: DatabaseWrapper._regex_expr(a, b),
141+
"icontains": lambda a, b: DatabaseWrapper._regex_expr(a, b, insensitive=True),
142+
"regex": lambda a, b: DatabaseWrapper._regex_expr(a, b),
143+
"iregex": lambda a, b: DatabaseWrapper._regex_expr(a, b, insensitive=True),
138144
}
139145

140146
display_name = "MongoDB"

django_mongodb_backend/query_utils.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,3 @@ def process_rhs(node, compiler, connection):
4545
if hasattr(node, "prep_lookup_value_mongo"):
4646
value = node.prep_lookup_value_mongo(value)
4747
return value
48-
49-
50-
def regex_match(field, regex_vals, insensitive=False):
51-
regex = {"$concat": regex_vals} if isinstance(regex_vals, tuple) else regex_vals
52-
options = "i" if insensitive else ""
53-
return {"$regexMatch": {"input": field, "regex": regex, "options": options}}

0 commit comments

Comments
 (0)