Skip to content

Commit 8d2b442

Browse files
committed
INTPYTHON-973 Add support for the Right database function
1 parent 64995c8 commit 8d2b442

4 files changed

Lines changed: 23 additions & 2 deletions

File tree

django_mongodb_backend/features.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,6 @@ def django_test_expected_failures(self):
265265
"db_functions.text.test_pad.PadTests",
266266
"db_functions.text.test_repeat.RepeatTests",
267267
"db_functions.text.test_reverse.ReverseTests",
268-
"db_functions.text.test_right.RightTests",
269268
"db_functions.text.test_sha1.SHA1Tests",
270269
"db_functions.text.test_sha224.SHA224Tests",
271270
"db_functions.text.test_sha256.SHA256Tests",

django_mongodb_backend/functions.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
Lower,
3434
LTrim,
3535
Replace,
36+
Right,
3637
RTrim,
3738
StrIndex,
3839
Substr,
@@ -118,6 +119,23 @@ def left(self, compiler, connection):
118119
return self.get_substr().as_mql(compiler, connection, as_expr=True)
119120

120121

122+
def right(self, compiler, connection):
123+
expression, length = (
124+
expr.as_mql(compiler, connection, as_expr=True) for expr in self.get_source_expressions()
125+
)
126+
# Calculate substring's start value by subtracting the requested length
127+
# from the total length of the string.
128+
start = {"$max": [{"$subtract": [{"$strLenCP": expression}, length]}, 0]}
129+
return {
130+
"$cond": {
131+
"if": {"$eq": [length, None]},
132+
# $substrCP returns "" for null length; return null instead.
133+
"then": None,
134+
"else": {"$substrCP": [expression, start, length]},
135+
}
136+
}
137+
138+
121139
def length(self, compiler, connection):
122140
# Check for null first since $strLenCP only accepts strings.
123141
lhs_mql = process_lhs(self, compiler, connection, as_expr=True)
@@ -296,6 +314,7 @@ def register_functions():
296314
Now.as_mql_expr = now
297315
NullIf.as_mql_expr = null_if
298316
Replace.as_mql_expr = replace
317+
Right.as_mql_expr = right
299318
Round.as_mql_expr = round_
300319
RTrim.as_mql_expr = trim("rtrim")
301320
StrIndex.as_mql_expr = str_index

docs/releases/6.0.x.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ Django MongoDB Backend 6.0.x
1010
New features
1111
------------
1212

13+
- Added support for the following database functions:
14+
15+
- :class:`~django.db.models.functions.Right`
16+
1317
- Added support for Django's warning when performing import-time queries.
1418

1519
Bug fixes

docs/topics/known-issues.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ Database functions
8383
- :class:`~django.db.models.functions.Ord`
8484
- :class:`~django.db.models.functions.Repeat`
8585
- :class:`~django.db.models.functions.Reverse`
86-
- :class:`~django.db.models.functions.Right`
8786
- :class:`~django.db.models.functions.SHA1`,
8887
:class:`~django.db.models.functions.SHA224`,
8988
:class:`~django.db.models.functions.SHA256`,

0 commit comments

Comments
 (0)