Skip to content

add support for ExpressionWrapper #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion django_mongodb/expressions.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
from django.db.models.expressions import Col, Value
from django.db.models.expressions import Col, ExpressionWrapper, Value


def col(self, compiler, connection): # noqa: ARG001
return f"${self.target.column}"


def expression_wrapper(self, compiler, connection):
return self.expression.as_mql(compiler, connection)


def value(self, compiler, connection): # noqa: ARG001
return {"$literal": self.value}


def register_expressions():
Col.as_mql = col
ExpressionWrapper.as_mql = expression_wrapper
Value.as_mql = value
11 changes: 4 additions & 7 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,18 @@ class DatabaseFeatures(BaseDatabaseFeatures):
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_extract_outerref",
"db_functions.datetime.test_extract_trunc.DateFunctionTests.test_trunc_subquery_with_parameters",
"lookup.tests.LookupQueryingTests.test_filter_subquery_lhs",
# ExpressionWrapper not supported.
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_expression_annotation_with_aggregation",
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_f_expression_annotation_with_aggregation",
"annotations.tests.NonAggregateAnnotationTestCase.test_empty_expression_annotation",
"annotations.tests.NonAggregateAnnotationTestCase.test_full_expression_annotation",
# Invalid $project :: caused by :: Unknown expression $count,
"annotations.tests.NonAggregateAnnotationTestCase.test_full_expression_annotation_with_aggregation",
"annotations.tests.NonAggregateAnnotationTestCase.test_grouping_by_q_expression_annotation",
"annotations.tests.NonAggregateAnnotationTestCase.test_mixed_type_annotation_numbers",
"annotations.tests.NonAggregateAnnotationTestCase.test_q_expression_annotation_with_aggregation",
"lookup.tests.LookupQueryingTests.test_filter_wrapped_lookup_lhs",
# CombinedExpression not implemented.
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_annotation_commutative",
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_expression_annotation_with_aggregation",
"annotations.tests.NonAggregateAnnotationTestCase.test_combined_f_expression_annotation_with_aggregation",
"annotations.tests.NonAggregateAnnotationTestCase.test_decimal_annotation",
"annotations.tests.NonAggregateAnnotationTestCase.test_defer_annotation",
"annotations.tests.NonAggregateAnnotationTestCase.test_filter_decimal_annotation",
"annotations.tests.NonAggregateAnnotationTestCase.test_mixed_type_annotation_numbers",
"annotations.tests.NonAggregateAnnotationTestCase.test_values_annotation",
# Func not implemented.
"annotations.tests.NonAggregateAnnotationTestCase.test_custom_functions",
Expand Down
8 changes: 7 additions & 1 deletion django_mongodb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.core.exceptions import EmptyResultSet, FullResultSet
from django.db import DatabaseError, IntegrityError
from django.db.models import Value
from django.db.models.sql.where import AND, XOR, WhereNode
from pymongo import ASCENDING, DESCENDING
from pymongo.errors import DuplicateKeyError, PyMongoError
Expand Down Expand Up @@ -91,7 +92,12 @@ def get_cursor(self):
column = expr.target.column
except AttributeError:
# Generate the MQL for an annotation.
fields[name] = expr.as_mql(self.compiler, self.connection)
try:
fields[name] = expr.as_mql(self.compiler, self.connection)
except EmptyResultSet:
fields[name] = Value(False).as_mql(self.compiler, self.connection)
except FullResultSet:
fields[name] = Value(True).as_mql(self.compiler, self.connection)
else:
# If name != column, then this is an annotatation referencing
# another column.
Expand Down