|
1 | 1 | from functools import reduce, wraps |
2 | | -from itertools import chain |
3 | 2 | from operator import add as add_operator |
4 | 3 |
|
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 |
8 | 6 | from django.db.models.expressions import Case, Col, When |
9 | 7 | from django.db.models.functions import Mod |
10 | 8 | from django.db.models.lookups import Exact |
11 | | -from django.db.models.query import RawModelIterable, RawQuerySet |
12 | 9 | from django.db.models.sql.constants import INNER |
13 | 10 | from django.db.models.sql.datastructures import Join |
14 | | -from django.db.models.sql.query import RawQuery |
15 | 11 | from django.db.models.sql.where import AND, OR, XOR, ExtraWhere, NothingNode, WhereNode |
16 | 12 | from pymongo.errors import BulkWriteError, DuplicateKeyError, PyMongoError |
17 | 13 |
|
@@ -306,87 +302,3 @@ def register_nodes(): |
306 | 302 | Join.as_mql = join |
307 | 303 | NothingNode.as_mql = NothingNode.as_sql |
308 | 304 | 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()) |
0 commit comments