Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

indexes.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. from django.db.backends.utils import names_digest, split_identifier
  2. from django.db.models.expressions import Col, ExpressionList, F, Func, OrderBy
  3. from django.db.models.functions import Collate
  4. from django.db.models.query_utils import Q
  5. from django.db.models.sql import Query
  6. from django.utils.functional import partition
  7. __all__ = ["Index"]
  8. class Index:
  9. suffix = "idx"
  10. # The max length of the name of the index (restricted to 30 for
  11. # cross-database compatibility with Oracle)
  12. max_name_length = 30
  13. def __init__(
  14. self,
  15. *expressions,
  16. fields=(),
  17. name=None,
  18. db_tablespace=None,
  19. opclasses=(),
  20. condition=None,
  21. include=None,
  22. ):
  23. if opclasses and not name:
  24. raise ValueError("An index must be named to use opclasses.")
  25. if not isinstance(condition, (type(None), Q)):
  26. raise ValueError("Index.condition must be a Q instance.")
  27. if condition and not name:
  28. raise ValueError("An index must be named to use condition.")
  29. if not isinstance(fields, (list, tuple)):
  30. raise ValueError("Index.fields must be a list or tuple.")
  31. if not isinstance(opclasses, (list, tuple)):
  32. raise ValueError("Index.opclasses must be a list or tuple.")
  33. if not expressions and not fields:
  34. raise ValueError(
  35. "At least one field or expression is required to define an index."
  36. )
  37. if expressions and fields:
  38. raise ValueError(
  39. "Index.fields and expressions are mutually exclusive.",
  40. )
  41. if expressions and not name:
  42. raise ValueError("An index must be named to use expressions.")
  43. if expressions and opclasses:
  44. raise ValueError(
  45. "Index.opclasses cannot be used with expressions. Use "
  46. "django.contrib.postgres.indexes.OpClass() instead."
  47. )
  48. if opclasses and len(fields) != len(opclasses):
  49. raise ValueError(
  50. "Index.fields and Index.opclasses must have the same number of "
  51. "elements."
  52. )
  53. if fields and not all(isinstance(field, str) for field in fields):
  54. raise ValueError("Index.fields must contain only strings with field names.")
  55. if include and not name:
  56. raise ValueError("A covering index must be named.")
  57. if not isinstance(include, (type(None), list, tuple)):
  58. raise ValueError("Index.include must be a list or tuple.")
  59. self.fields = list(fields)
  60. # A list of 2-tuple with the field name and ordering ('' or 'DESC').
  61. self.fields_orders = [
  62. (field_name[1:], "DESC") if field_name.startswith("-") else (field_name, "")
  63. for field_name in self.fields
  64. ]
  65. self.name = name or ""
  66. self.db_tablespace = db_tablespace
  67. self.opclasses = opclasses
  68. self.condition = condition
  69. self.include = tuple(include) if include else ()
  70. self.expressions = tuple(
  71. F(expression) if isinstance(expression, str) else expression
  72. for expression in expressions
  73. )
  74. @property
  75. def contains_expressions(self):
  76. return bool(self.expressions)
  77. def _get_condition_sql(self, model, schema_editor):
  78. if self.condition is None:
  79. return None
  80. query = Query(model=model, alias_cols=False)
  81. where = query.build_where(self.condition)
  82. compiler = query.get_compiler(connection=schema_editor.connection)
  83. sql, params = where.as_sql(compiler, schema_editor.connection)
  84. return sql % tuple(schema_editor.quote_value(p) for p in params)
  85. def create_sql(self, model, schema_editor, using="", **kwargs):
  86. include = [
  87. model._meta.get_field(field_name).column for field_name in self.include
  88. ]
  89. condition = self._get_condition_sql(model, schema_editor)
  90. if self.expressions:
  91. index_expressions = []
  92. for expression in self.expressions:
  93. index_expression = IndexExpression(expression)
  94. index_expression.set_wrapper_classes(schema_editor.connection)
  95. index_expressions.append(index_expression)
  96. expressions = ExpressionList(*index_expressions).resolve_expression(
  97. Query(model, alias_cols=False),
  98. )
  99. fields = None
  100. col_suffixes = None
  101. else:
  102. fields = [
  103. model._meta.get_field(field_name)
  104. for field_name, _ in self.fields_orders
  105. ]
  106. if schema_editor.connection.features.supports_index_column_ordering:
  107. col_suffixes = [order[1] for order in self.fields_orders]
  108. else:
  109. col_suffixes = [""] * len(self.fields_orders)
  110. expressions = None
  111. return schema_editor._create_index_sql(
  112. model,
  113. fields=fields,
  114. name=self.name,
  115. using=using,
  116. db_tablespace=self.db_tablespace,
  117. col_suffixes=col_suffixes,
  118. opclasses=self.opclasses,
  119. condition=condition,
  120. include=include,
  121. expressions=expressions,
  122. **kwargs,
  123. )
  124. def remove_sql(self, model, schema_editor, **kwargs):
  125. return schema_editor._delete_index_sql(model, self.name, **kwargs)
  126. def deconstruct(self):
  127. path = "%s.%s" % (self.__class__.__module__, self.__class__.__name__)
  128. path = path.replace("django.db.models.indexes", "django.db.models")
  129. kwargs = {"name": self.name}
  130. if self.fields:
  131. kwargs["fields"] = self.fields
  132. if self.db_tablespace is not None:
  133. kwargs["db_tablespace"] = self.db_tablespace
  134. if self.opclasses:
  135. kwargs["opclasses"] = self.opclasses
  136. if self.condition:
  137. kwargs["condition"] = self.condition
  138. if self.include:
  139. kwargs["include"] = self.include
  140. return (path, self.expressions, kwargs)
  141. def clone(self):
  142. """Create a copy of this Index."""
  143. _, args, kwargs = self.deconstruct()
  144. return self.__class__(*args, **kwargs)
  145. def set_name_with_model(self, model):
  146. """
  147. Generate a unique name for the index.
  148. The name is divided into 3 parts - table name (12 chars), field name
  149. (8 chars) and unique hash + suffix (10 chars). Each part is made to
  150. fit its size by truncating the excess length.
  151. """
  152. _, table_name = split_identifier(model._meta.db_table)
  153. column_names = [
  154. model._meta.get_field(field_name).column
  155. for field_name, order in self.fields_orders
  156. ]
  157. column_names_with_order = [
  158. (("-%s" if order else "%s") % column_name)
  159. for column_name, (field_name, order) in zip(
  160. column_names, self.fields_orders
  161. )
  162. ]
  163. # The length of the parts of the name is based on the default max
  164. # length of 30 characters.
  165. hash_data = [table_name] + column_names_with_order + [self.suffix]
  166. self.name = "%s_%s_%s" % (
  167. table_name[:11],
  168. column_names[0][:7],
  169. "%s_%s" % (names_digest(*hash_data, length=6), self.suffix),
  170. )
  171. if len(self.name) > self.max_name_length:
  172. raise ValueError(
  173. "Index too long for multiple database support. Is self.suffix "
  174. "longer than 3 characters?"
  175. )
  176. if self.name[0] == "_" or self.name[0].isdigit():
  177. self.name = "D%s" % self.name[1:]
  178. def __repr__(self):
  179. return "<%s:%s%s%s%s%s%s%s>" % (
  180. self.__class__.__qualname__,
  181. "" if not self.fields else " fields=%s" % repr(self.fields),
  182. "" if not self.expressions else " expressions=%s" % repr(self.expressions),
  183. "" if not self.name else " name=%s" % repr(self.name),
  184. ""
  185. if self.db_tablespace is None
  186. else " db_tablespace=%s" % repr(self.db_tablespace),
  187. "" if self.condition is None else " condition=%s" % self.condition,
  188. "" if not self.include else " include=%s" % repr(self.include),
  189. "" if not self.opclasses else " opclasses=%s" % repr(self.opclasses),
  190. )
  191. def __eq__(self, other):
  192. if self.__class__ == other.__class__:
  193. return self.deconstruct() == other.deconstruct()
  194. return NotImplemented
  195. class IndexExpression(Func):
  196. """Order and wrap expressions for CREATE INDEX statements."""
  197. template = "%(expressions)s"
  198. wrapper_classes = (OrderBy, Collate)
  199. def set_wrapper_classes(self, connection=None):
  200. # Some databases (e.g. MySQL) treats COLLATE as an indexed expression.
  201. if connection and connection.features.collate_as_index_expression:
  202. self.wrapper_classes = tuple(
  203. [
  204. wrapper_cls
  205. for wrapper_cls in self.wrapper_classes
  206. if wrapper_cls is not Collate
  207. ]
  208. )
  209. @classmethod
  210. def register_wrappers(cls, *wrapper_classes):
  211. cls.wrapper_classes = wrapper_classes
  212. def resolve_expression(
  213. self,
  214. query=None,
  215. allow_joins=True,
  216. reuse=None,
  217. summarize=False,
  218. for_save=False,
  219. ):
  220. expressions = list(self.flatten())
  221. # Split expressions and wrappers.
  222. index_expressions, wrappers = partition(
  223. lambda e: isinstance(e, self.wrapper_classes),
  224. expressions,
  225. )
  226. wrapper_types = [type(wrapper) for wrapper in wrappers]
  227. if len(wrapper_types) != len(set(wrapper_types)):
  228. raise ValueError(
  229. "Multiple references to %s can't be used in an indexed "
  230. "expression."
  231. % ", ".join(
  232. [wrapper_cls.__qualname__ for wrapper_cls in self.wrapper_classes]
  233. )
  234. )
  235. if expressions[1 : len(wrappers) + 1] != wrappers:
  236. raise ValueError(
  237. "%s must be topmost expressions in an indexed expression."
  238. % ", ".join(
  239. [wrapper_cls.__qualname__ for wrapper_cls in self.wrapper_classes]
  240. )
  241. )
  242. # Wrap expressions in parentheses if they are not column references.
  243. root_expression = index_expressions[1]
  244. resolve_root_expression = root_expression.resolve_expression(
  245. query,
  246. allow_joins,
  247. reuse,
  248. summarize,
  249. for_save,
  250. )
  251. if not isinstance(resolve_root_expression, Col):
  252. root_expression = Func(root_expression, template="(%(expressions)s)")
  253. if wrappers:
  254. # Order wrappers and set their expressions.
  255. wrappers = sorted(
  256. wrappers,
  257. key=lambda w: self.wrapper_classes.index(type(w)),
  258. )
  259. wrappers = [wrapper.copy() for wrapper in wrappers]
  260. for i, wrapper in enumerate(wrappers[:-1]):
  261. wrapper.set_source_expressions([wrappers[i + 1]])
  262. # Set the root expression on the deepest wrapper.
  263. wrappers[-1].set_source_expressions([root_expression])
  264. self.set_source_expressions([wrappers[0]])
  265. else:
  266. # Use the root expression, if there are no wrappers.
  267. self.set_source_expressions([root_expression])
  268. return super().resolve_expression(
  269. query, allow_joins, reuse, summarize, for_save
  270. )
  271. def as_sqlite(self, compiler, connection, **extra_context):
  272. # Casting to numeric is unnecessary.
  273. return self.as_sql(compiler, connection, **extra_context)