1
+ from django .core .exceptions import EmptyResultSet , FullResultSet
2
+ from django .db import NotSupportedError
1
3
from django .db .models import Index
2
4
from django .db .models .sql .query import Query
5
+ from django .db .models .sql .where import AND , XOR , WhereNode
3
6
4
7
5
8
def _get_condition_mql (self , model , schema_editor ):
@@ -10,5 +13,49 @@ def _get_condition_mql(self, model, schema_editor):
10
13
return where .as_mql_idx (compiler , schema_editor .connection )
11
14
12
15
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
+
13
59
def register_indexes ():
14
60
Index ._get_condition_mql = _get_condition_mql
61
+ WhereNode .as_mql_idx = where_node_idx
0 commit comments