Skip to content

Commit a8cc909

Browse files
committed
Refactor: move WhereNode.as_mql_idx to index.py
1 parent 383fbd9 commit a8cc909

File tree

2 files changed

+47
-44
lines changed

2 files changed

+47
-44
lines changed

django_mongodb/indexes.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
from django.core.exceptions import EmptyResultSet, FullResultSet
2+
from django.db import NotSupportedError
13
from django.db.models import Index
24
from django.db.models.sql.query import Query
5+
from django.db.models.sql.where import AND, XOR, WhereNode
36

47

58
def _get_condition_mql(self, model, schema_editor):
@@ -10,5 +13,49 @@ def _get_condition_mql(self, model, schema_editor):
1013
return where.as_mql_idx(compiler, schema_editor.connection)
1114

1215

16+
def where_node_idx(self, compiler, connection):
17+
if self.connector == AND:
18+
full_needed, empty_needed = len(self.children), 1
19+
else:
20+
full_needed, empty_needed = 1, len(self.children)
21+
if self.connector == AND:
22+
operator = "$and"
23+
elif self.connector == XOR:
24+
raise NotSupportedError("Xor in indexes is not supported")
25+
else:
26+
operator = "$or"
27+
if self.negated:
28+
raise NotSupportedError("Negated field in indexes is not supported")
29+
children_mql = []
30+
for child in self.children:
31+
try:
32+
mql = child.as_mql_idx(compiler, connection)
33+
except EmptyResultSet:
34+
empty_needed -= 1
35+
except FullResultSet:
36+
full_needed -= 1
37+
else:
38+
if mql:
39+
children_mql.append(mql)
40+
else:
41+
full_needed -= 1
42+
43+
if empty_needed == 0:
44+
raise EmptyResultSet
45+
if full_needed == 0:
46+
return {}
47+
48+
if len(children_mql) == 1:
49+
mql = children_mql[0]
50+
elif len(children_mql) > 1:
51+
mql = {operator: children_mql}
52+
else:
53+
mql = {}
54+
if not mql:
55+
raise FullResultSet
56+
return mql
57+
58+
1359
def register_indexes():
1460
Index._get_condition_mql = _get_condition_mql
61+
WhereNode.as_mql_idx = where_node_idx

django_mongodb/query.py

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -297,52 +297,8 @@ def where_node(self, compiler, connection):
297297
return mql
298298

299299

300-
def where_node_idx(self, compiler, connection):
301-
if self.connector == AND:
302-
full_needed, empty_needed = len(self.children), 1
303-
else:
304-
full_needed, empty_needed = 1, len(self.children)
305-
if self.connector == AND:
306-
operator = "$and"
307-
elif self.connector == XOR:
308-
raise NotSupportedError("Xor in indexes is not supported")
309-
else:
310-
operator = "$or"
311-
if self.negated:
312-
raise NotSupportedError("Negated field in indexes is not supported")
313-
children_mql = []
314-
for child in self.children:
315-
try:
316-
mql = child.as_mql_idx(compiler, connection)
317-
except EmptyResultSet:
318-
empty_needed -= 1
319-
except FullResultSet:
320-
full_needed -= 1
321-
else:
322-
if mql:
323-
children_mql.append(mql)
324-
else:
325-
full_needed -= 1
326-
327-
if empty_needed == 0:
328-
raise EmptyResultSet
329-
if full_needed == 0:
330-
return {}
331-
332-
if len(children_mql) == 1:
333-
mql = children_mql[0]
334-
elif len(children_mql) > 1:
335-
mql = {operator: children_mql}
336-
else:
337-
mql = {}
338-
if not mql:
339-
raise FullResultSet
340-
return mql
341-
342-
343300
def register_nodes():
344301
ExtraWhere.as_mql = extra_where
345302
Join.as_mql = join
346303
NothingNode.as_mql = NothingNode.as_sql
347304
WhereNode.as_mql = where_node
348-
WhereNode.as_mql_idx = where_node_idx

0 commit comments

Comments
 (0)