Skip to content

Commit b503e71

Browse files
committed
Refactor: move builtin_lookup_idx to indexes.
1 parent a5e110c commit b503e71

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

django_mongodb/indexes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
from django.core.exceptions import EmptyResultSet, FullResultSet
22
from django.db import NotSupportedError
33
from django.db.models import Index
4+
from django.db.models.expressions import Col
5+
from django.db.models.lookups import BuiltinLookup
46
from django.db.models.sql.query import Query
57
from django.db.models.sql.where import AND, XOR, WhereNode
68

9+
from .query_utils import process_rhs
10+
711

812
def _get_condition_mql(self, model, schema_editor):
913
"""Analogous to Index._get_condition_sql()."""
@@ -13,6 +17,14 @@ def _get_condition_mql(self, model, schema_editor):
1317
return where.as_mql_idx(compiler, schema_editor.connection)
1418

1519

20+
def builtin_lookup_idx(self, compiler, connection):
21+
if not isinstance(self.lhs, Col):
22+
raise NotSupportedError("Expressions as indexes are not supported.")
23+
lhs_mql = self.lhs.target.column
24+
value = process_rhs(self, compiler, connection)
25+
return connection.mongo_operators_idx[self.lookup_name](lhs_mql, value)
26+
27+
1628
def where_node_idx(self, compiler, connection):
1729
if self.connector == AND:
1830
full_needed, empty_needed = len(self.children), 1
@@ -57,5 +69,6 @@ def where_node_idx(self, compiler, connection):
5769

5870

5971
def register_indexes():
72+
BuiltinLookup.as_mql_idx = builtin_lookup_idx
6073
Index._get_condition_mql = _get_condition_mql
6174
WhereNode.as_mql_idx = where_node_idx

django_mongodb/lookups.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from django.db import NotSupportedError
2-
from django.db.models.expressions import Col
32
from django.db.models.fields.related_lookups import In, MultiColSource, RelatedIn
43
from django.db.models.lookups import (
54
BuiltinLookup,
@@ -18,14 +17,6 @@ def builtin_lookup(self, compiler, connection):
1817
return connection.mongo_operators[self.lookup_name](lhs_mql, value)
1918

2019

21-
def builtin_lookup_idx(self, compiler, connection):
22-
if not isinstance(self.lhs, Col):
23-
raise NotSupportedError("Expressions as indexes are not supported.")
24-
lhs_mql = self.lhs.target.column
25-
value = process_rhs(self, compiler, connection)
26-
return connection.mongo_operators_idx[self.lookup_name](lhs_mql, value)
27-
28-
2920
_field_resolve_expression_parameter = FieldGetDbPrepValueIterableMixin.resolve_expression_parameter
3021

3122

@@ -102,7 +93,6 @@ def uuid_text_mixin(self, compiler, connection): # noqa: ARG001
10293

10394
def register_lookups():
10495
BuiltinLookup.as_mql = builtin_lookup
105-
BuiltinLookup.as_mql_idx = builtin_lookup_idx
10696
FieldGetDbPrepValueIterableMixin.resolve_expression_parameter = (
10797
field_resolve_expression_parameter
10898
)

0 commit comments

Comments
 (0)