subqueries.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. """
  2. Query subclasses which provide extra functionality beyond simple data retrieval.
  3. """
  4. from django.core.exceptions import FieldError
  5. from django.db.models.fields import DateField, FieldDoesNotExist
  6. from django.db.models.sql.constants import *
  7. from django.db.models.sql.datastructures import Date
  8. from django.db.models.sql.query import Query
  9. from django.db.models.sql.where import AND, Constraint
  10. from django.utils.datastructures import SortedDict
  11. from django.utils.functional import Promise
  12. from django.utils.encoding import force_text
  13. from django.utils import six
  14. __all__ = ['DeleteQuery', 'UpdateQuery', 'InsertQuery', 'DateQuery',
  15. 'AggregateQuery']
  16. class DeleteQuery(Query):
  17. """
  18. Delete queries are done through this class, since they are more constrained
  19. than general queries.
  20. """
  21. compiler = 'SQLDeleteCompiler'
  22. def do_query(self, table, where, using):
  23. self.tables = [table]
  24. self.where = where
  25. self.get_compiler(using).execute_sql(None)
  26. def delete_batch(self, pk_list, using, field=None):
  27. """
  28. Set up and execute delete queries for all the objects in pk_list.
  29. More than one physical query may be executed if there are a
  30. lot of values in pk_list.
  31. """
  32. if not field:
  33. field = self.model._meta.pk
  34. for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
  35. where = self.where_class()
  36. where.add((Constraint(None, field.column, field), 'in',
  37. pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]), AND)
  38. self.do_query(self.model._meta.db_table, where, using=using)
  39. class UpdateQuery(Query):
  40. """
  41. Represents an "update" SQL query.
  42. """
  43. compiler = 'SQLUpdateCompiler'
  44. def __init__(self, *args, **kwargs):
  45. super(UpdateQuery, self).__init__(*args, **kwargs)
  46. self._setup_query()
  47. def _setup_query(self):
  48. """
  49. Runs on initialization and after cloning. Any attributes that would
  50. normally be set in __init__ should go in here, instead, so that they
  51. are also set up after a clone() call.
  52. """
  53. self.values = []
  54. self.related_ids = None
  55. if not hasattr(self, 'related_updates'):
  56. self.related_updates = {}
  57. def clone(self, klass=None, **kwargs):
  58. return super(UpdateQuery, self).clone(klass,
  59. related_updates=self.related_updates.copy(), **kwargs)
  60. def update_batch(self, pk_list, values, using):
  61. pk_field = self.model._meta.pk
  62. self.add_update_values(values)
  63. for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE):
  64. self.where = self.where_class()
  65. self.where.add((Constraint(None, pk_field.column, pk_field), 'in',
  66. pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE]),
  67. AND)
  68. self.get_compiler(using).execute_sql(None)
  69. def add_update_values(self, values):
  70. """
  71. Convert a dictionary of field name to value mappings into an update
  72. query. This is the entry point for the public update() method on
  73. querysets.
  74. """
  75. values_seq = []
  76. for name, val in six.iteritems(values):
  77. field, model, direct, m2m = self.model._meta.get_field_by_name(name)
  78. if not direct or m2m:
  79. raise FieldError('Cannot update model field %r (only non-relations and foreign keys permitted).' % field)
  80. if model:
  81. self.add_related_update(model, field, val)
  82. continue
  83. values_seq.append((field, model, val))
  84. return self.add_update_fields(values_seq)
  85. def add_update_fields(self, values_seq):
  86. """
  87. Turn a sequence of (field, model, value) triples into an update query.
  88. Used by add_update_values() as well as the "fast" update path when
  89. saving models.
  90. """
  91. # Check that no Promise object passes to the query. Refs #10498.
  92. values_seq = [(value[0], value[1], force_text(value[2]))
  93. if isinstance(value[2], Promise) else value
  94. for value in values_seq]
  95. self.values.extend(values_seq)
  96. def add_related_update(self, model, field, value):
  97. """
  98. Adds (name, value) to an update query for an ancestor model.
  99. Updates are coalesced so that we only run one update query per ancestor.
  100. """
  101. try:
  102. self.related_updates[model].append((field, None, value))
  103. except KeyError:
  104. self.related_updates[model] = [(field, None, value)]
  105. def get_related_updates(self):
  106. """
  107. Returns a list of query objects: one for each update required to an
  108. ancestor model. Each query will have the same filtering conditions as
  109. the current query but will only update a single table.
  110. """
  111. if not self.related_updates:
  112. return []
  113. result = []
  114. for model, values in six.iteritems(self.related_updates):
  115. query = UpdateQuery(model)
  116. query.values = values
  117. if self.related_ids is not None:
  118. query.add_filter(('pk__in', self.related_ids))
  119. result.append(query)
  120. return result
  121. class InsertQuery(Query):
  122. compiler = 'SQLInsertCompiler'
  123. def __init__(self, *args, **kwargs):
  124. super(InsertQuery, self).__init__(*args, **kwargs)
  125. self.fields = []
  126. self.objs = []
  127. def clone(self, klass=None, **kwargs):
  128. extras = {
  129. 'fields': self.fields[:],
  130. 'objs': self.objs[:],
  131. 'raw': self.raw,
  132. }
  133. extras.update(kwargs)
  134. return super(InsertQuery, self).clone(klass, **extras)
  135. def insert_values(self, fields, objs, raw=False):
  136. """
  137. Set up the insert query from the 'insert_values' dictionary. The
  138. dictionary gives the model field names and their target values.
  139. If 'raw_values' is True, the values in the 'insert_values' dictionary
  140. are inserted directly into the query, rather than passed as SQL
  141. parameters. This provides a way to insert NULL and DEFAULT keywords
  142. into the query, for example.
  143. """
  144. self.fields = fields
  145. # Check that no Promise object reaches the DB. Refs #10498.
  146. for field in fields:
  147. for obj in objs:
  148. value = getattr(obj, field.attname)
  149. if isinstance(value, Promise):
  150. setattr(obj, field.attname, force_text(value))
  151. self.objs = objs
  152. self.raw = raw
  153. class DateQuery(Query):
  154. """
  155. A DateQuery is a normal query, except that it specifically selects a single
  156. date field. This requires some special handling when converting the results
  157. back to Python objects, so we put it in a separate class.
  158. """
  159. compiler = 'SQLDateCompiler'
  160. def add_date_select(self, field_name, lookup_type, order='ASC'):
  161. """
  162. Converts the query into a date extraction query.
  163. """
  164. try:
  165. result = self.setup_joins(
  166. field_name.split(LOOKUP_SEP),
  167. self.get_meta(),
  168. self.get_initial_alias(),
  169. False
  170. )
  171. except FieldError:
  172. raise FieldDoesNotExist("%s has no field named '%s'" % (
  173. self.model._meta.object_name, field_name
  174. ))
  175. field = result[0]
  176. assert isinstance(field, DateField), "%r isn't a DateField." \
  177. % field.name
  178. alias = result[3][-1]
  179. select = Date((alias, field.column), lookup_type)
  180. self.select = [select]
  181. self.select_fields = [None]
  182. self.select_related = False # See #7097.
  183. self.aggregates = SortedDict() # See 18056.
  184. self.set_extra_mask([])
  185. self.distinct = True
  186. self.order_by = order == 'ASC' and [1] or [-1]
  187. if field.null:
  188. self.add_filter(("%s__isnull" % field_name, False))
  189. class AggregateQuery(Query):
  190. """
  191. An AggregateQuery takes another query as a parameter to the FROM
  192. clause and only selects the elements in the provided list.
  193. """
  194. compiler = 'SQLAggregateCompiler'
  195. def add_subquery(self, query, using):
  196. self.subquery, self.sub_params = query.get_compiler(using).as_sql(with_col_aliases=True)