Skip to content

Commit 7a8e255

Browse files
committed
rename module/move code
1 parent 626442b commit 7a8e255

File tree

4 files changed

+95
-94
lines changed

4 files changed

+95
-94
lines changed

django_mongodb/features.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,9 +417,6 @@ def django_test_expected_failures(self):
417417
"model_fields.test_jsonfield.TestQuerying.test_nested_key_transform_raw_expression",
418418
"multiple_database.tests.QueryTestCase.test_raw",
419419
"prefetch_related.tests.RawQuerySetTests",
420-
"raw_query.tests.RawQueryTests.test_params",
421-
"raw_query.tests.RawQueryTests.test_params_none",
422-
"raw_query.tests.RawQueryTests.test_escaped_percent",
423420
"queries.tests.Queries1Tests.test_order_by_rawsql",
424421
"schema.test_logging.SchemaLoggerTests.test_extra_args",
425422
"schema.tests.SchemaTests.test_remove_constraints_capital_letters",

django_mongodb/manager.py renamed to django_mongodb/managers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.db.models.manager import BaseManager
22

3-
from .query import MongoQuerySet
3+
from .queryset import MongoQuerySet
44

55

66
class MongoManager(BaseManager.from_queryset(MongoQuerySet)):

django_mongodb/query.py

Lines changed: 2 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
from functools import reduce, wraps
2-
from itertools import chain
32
from operator import add as add_operator
43

5-
from django.core.exceptions import EmptyResultSet, FieldDoesNotExist, FullResultSet
6-
from django.db import DatabaseError, IntegrityError, NotSupportedError, connections
7-
from django.db.models import QuerySet
4+
from django.core.exceptions import EmptyResultSet, FullResultSet
5+
from django.db import DatabaseError, IntegrityError, NotSupportedError
86
from django.db.models.expressions import Case, Col, When
97
from django.db.models.functions import Mod
108
from django.db.models.lookups import Exact
11-
from django.db.models.query import RawModelIterable, RawQuerySet
129
from django.db.models.sql.constants import INNER
1310
from django.db.models.sql.datastructures import Join
14-
from django.db.models.sql.query import RawQuery
1511
from django.db.models.sql.where import AND, OR, XOR, ExtraWhere, NothingNode, WhereNode
1612
from pymongo.errors import BulkWriteError, DuplicateKeyError, PyMongoError
1713

@@ -306,87 +302,3 @@ def register_nodes():
306302
Join.as_mql = join
307303
NothingNode.as_mql = NothingNode.as_sql
308304
WhereNode.as_mql = where_node
309-
310-
311-
class MongoQuerySet(QuerySet):
312-
def raw_mql(self, pipeline, using=None):
313-
return MongoRawQuerySet(pipeline, model=self.model, using=using)
314-
315-
316-
class MongoRawQuery(RawQuery):
317-
def __init__(self, pipeline, using, model):
318-
self.pipeline = pipeline
319-
super().__init__(sql=None, using=using)
320-
self.model = model
321-
322-
def _execute_query(self):
323-
connection = connections[self.using]
324-
collection = connection.get_collection(self.model._meta.db_table)
325-
self.cursor = collection.aggregate(self.pipeline)
326-
327-
def __str__(self):
328-
return str(self.pipeline)
329-
330-
331-
class MongoRawQuerySet(RawQuerySet):
332-
def __init__(self, pipeline, model=None, using=None):
333-
super().__init__(pipeline, model=model, using=using)
334-
self.query = MongoRawQuery(pipeline, using=self.db, model=self.model)
335-
# Override the superclass's columns property which relies on PEP 249's
336-
# cursor.description. Instead, RawModelIterable will set the columns
337-
# based on the keys in the first result.
338-
self.columns = None
339-
340-
def iterator(self):
341-
yield from MongoRawModelIterable(self)
342-
343-
344-
class MongoRawModelIterable(RawModelIterable):
345-
def __iter__(self):
346-
"""
347-
This is mostly copied from the superclass except for the part that
348-
sets self.queryset.columns from the first document.
349-
"""
350-
db = self.queryset.db
351-
query = self.queryset.query
352-
connection = connections[db]
353-
compiler = connection.ops.compiler("SQLCompiler")(query, connection, db)
354-
query_iterator = iter(query)
355-
try:
356-
# Get the columns from the first result.
357-
try:
358-
first_item = next(query_iterator)
359-
except StopIteration:
360-
# No results.
361-
return
362-
self.queryset.columns = list(first_item.keys())
363-
# Reset the iterator to include the first item.
364-
query_iterator = self._make_result(chain([first_item], query_iterator))
365-
(
366-
model_init_names,
367-
model_init_pos,
368-
annotation_fields,
369-
) = self.queryset.resolve_model_init_order()
370-
model_cls = self.queryset.model
371-
if model_cls._meta.pk.attname not in model_init_names:
372-
raise FieldDoesNotExist("Raw query must include the primary key")
373-
fields = [self.queryset.model_fields.get(c) for c in self.queryset.columns]
374-
converters = compiler.get_converters(
375-
[f.get_col(f.model._meta.db_table) if f else None for f in fields]
376-
)
377-
if converters:
378-
query_iterator = compiler.apply_converters(query_iterator, converters)
379-
for values in query_iterator:
380-
# Associate fields to values
381-
model_init_values = [values[pos] for pos in model_init_pos]
382-
instance = model_cls.from_db(db, model_init_names, model_init_values)
383-
if annotation_fields:
384-
for column, pos in annotation_fields:
385-
setattr(instance, column, values[pos])
386-
yield instance
387-
finally:
388-
query.cursor.close()
389-
390-
def _make_result(self, query):
391-
for result in query:
392-
yield list(result.values())

django_mongodb/queryset.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
from itertools import chain
2+
3+
from django.core.exceptions import FieldDoesNotExist
4+
from django.db import connections
5+
from django.db.models import QuerySet
6+
from django.db.models.query import RawModelIterable as BaseRawModelIterable
7+
from django.db.models.query import RawQuerySet as BaseRawQuerySet
8+
from django.db.models.sql.query import RawQuery as BaseRawQuery
9+
10+
11+
class MongoQuerySet(QuerySet):
12+
def raw_mql(self, pipeline, using=None):
13+
return RawQuerySet(pipeline, model=self.model, using=using)
14+
15+
16+
class RawQuery(BaseRawQuery):
17+
def __init__(self, pipeline, using, model):
18+
self.pipeline = pipeline
19+
super().__init__(sql=None, using=using)
20+
self.model = model
21+
22+
def _execute_query(self):
23+
connection = connections[self.using]
24+
collection = connection.get_collection(self.model._meta.db_table)
25+
self.cursor = collection.aggregate(self.pipeline)
26+
27+
def __str__(self):
28+
return str(self.pipeline)
29+
30+
31+
class RawQuerySet(BaseRawQuerySet):
32+
def __init__(self, pipeline, model=None, using=None):
33+
super().__init__(pipeline, model=model, using=using)
34+
self.query = RawQuery(pipeline, using=self.db, model=self.model)
35+
# Override the superclass's columns property which relies on PEP 249's
36+
# cursor.description. Instead, RawModelIterable will set the columns
37+
# based on the keys in the first result.
38+
self.columns = None
39+
40+
def iterator(self):
41+
yield from RawModelIterable(self)
42+
43+
44+
class RawModelIterable(BaseRawModelIterable):
45+
def __iter__(self):
46+
"""
47+
This is mostly copied from the superclass except for the part that sets
48+
self.queryset.columns from the first document.
49+
"""
50+
db = self.queryset.db
51+
query = self.queryset.query
52+
connection = connections[db]
53+
compiler = connection.ops.compiler("SQLCompiler")(query, connection, db)
54+
query_iterator = iter(query)
55+
try:
56+
# Get the columns from the first result.
57+
try:
58+
first_item = next(query_iterator)
59+
except StopIteration:
60+
# No results.
61+
return
62+
self.queryset.columns = list(first_item.keys())
63+
# Reset the iterator to include the first item.
64+
query_iterator = self._make_result(chain([first_item], query_iterator))
65+
(
66+
model_init_names,
67+
model_init_pos,
68+
annotation_fields,
69+
) = self.queryset.resolve_model_init_order()
70+
model_cls = self.queryset.model
71+
if model_cls._meta.pk.attname not in model_init_names:
72+
raise FieldDoesNotExist("Raw query must include the primary key")
73+
fields = [self.queryset.model_fields.get(c) for c in self.queryset.columns]
74+
converters = compiler.get_converters(
75+
[f.get_col(f.model._meta.db_table) if f else None for f in fields]
76+
)
77+
if converters:
78+
query_iterator = compiler.apply_converters(query_iterator, converters)
79+
for values in query_iterator:
80+
# Associate fields to values
81+
model_init_values = [values[pos] for pos in model_init_pos]
82+
instance = model_cls.from_db(db, model_init_names, model_init_values)
83+
if annotation_fields:
84+
for column, pos in annotation_fields:
85+
setattr(instance, column, values[pos])
86+
yield instance
87+
finally:
88+
query.cursor.close()
89+
90+
def _make_result(self, query):
91+
for result in query:
92+
yield list(result.values())

0 commit comments

Comments
 (0)