测试gitnore
This commit is contained in:
@@ -5,11 +5,11 @@ from django.db.models.query_utils import Q
|
||||
from django.db.models.sql import Query
|
||||
from django.utils.functional import partition
|
||||
|
||||
__all__ = ["Index"]
|
||||
__all__ = ['Index']
|
||||
|
||||
|
||||
class Index:
|
||||
suffix = "idx"
|
||||
suffix = 'idx'
|
||||
# The max length of the name of the index (restricted to 30 for
|
||||
# cross-database compatibility with Oracle)
|
||||
max_name_length = 30
|
||||
@@ -25,48 +25,46 @@ class Index:
|
||||
include=None,
|
||||
):
|
||||
if opclasses and not name:
|
||||
raise ValueError("An index must be named to use opclasses.")
|
||||
raise ValueError('An index must be named to use opclasses.')
|
||||
if not isinstance(condition, (type(None), Q)):
|
||||
raise ValueError("Index.condition must be a Q instance.")
|
||||
raise ValueError('Index.condition must be a Q instance.')
|
||||
if condition and not name:
|
||||
raise ValueError("An index must be named to use condition.")
|
||||
raise ValueError('An index must be named to use condition.')
|
||||
if not isinstance(fields, (list, tuple)):
|
||||
raise ValueError("Index.fields must be a list or tuple.")
|
||||
raise ValueError('Index.fields must be a list or tuple.')
|
||||
if not isinstance(opclasses, (list, tuple)):
|
||||
raise ValueError("Index.opclasses must be a list or tuple.")
|
||||
raise ValueError('Index.opclasses must be a list or tuple.')
|
||||
if not expressions and not fields:
|
||||
raise ValueError(
|
||||
"At least one field or expression is required to define an index."
|
||||
'At least one field or expression is required to define an '
|
||||
'index.'
|
||||
)
|
||||
if expressions and fields:
|
||||
raise ValueError(
|
||||
"Index.fields and expressions are mutually exclusive.",
|
||||
'Index.fields and expressions are mutually exclusive.',
|
||||
)
|
||||
if expressions and not name:
|
||||
raise ValueError("An index must be named to use expressions.")
|
||||
raise ValueError('An index must be named to use expressions.')
|
||||
if expressions and opclasses:
|
||||
raise ValueError(
|
||||
"Index.opclasses cannot be used with expressions. Use "
|
||||
"django.contrib.postgres.indexes.OpClass() instead."
|
||||
'Index.opclasses cannot be used with expressions. Use '
|
||||
'django.contrib.postgres.indexes.OpClass() instead.'
|
||||
)
|
||||
if opclasses and len(fields) != len(opclasses):
|
||||
raise ValueError(
|
||||
"Index.fields and Index.opclasses must have the same number of "
|
||||
"elements."
|
||||
)
|
||||
raise ValueError('Index.fields and Index.opclasses must have the same number of elements.')
|
||||
if fields and not all(isinstance(field, str) for field in fields):
|
||||
raise ValueError("Index.fields must contain only strings with field names.")
|
||||
raise ValueError('Index.fields must contain only strings with field names.')
|
||||
if include and not name:
|
||||
raise ValueError("A covering index must be named.")
|
||||
raise ValueError('A covering index must be named.')
|
||||
if not isinstance(include, (type(None), list, tuple)):
|
||||
raise ValueError("Index.include must be a list or tuple.")
|
||||
raise ValueError('Index.include must be a list or tuple.')
|
||||
self.fields = list(fields)
|
||||
# A list of 2-tuple with the field name and ordering ('' or 'DESC').
|
||||
self.fields_orders = [
|
||||
(field_name[1:], "DESC") if field_name.startswith("-") else (field_name, "")
|
||||
(field_name[1:], 'DESC') if field_name.startswith('-') else (field_name, '')
|
||||
for field_name in self.fields
|
||||
]
|
||||
self.name = name or ""
|
||||
self.name = name or ''
|
||||
self.db_tablespace = db_tablespace
|
||||
self.opclasses = opclasses
|
||||
self.condition = condition
|
||||
@@ -89,10 +87,8 @@ class Index:
|
||||
sql, params = where.as_sql(compiler, schema_editor.connection)
|
||||
return sql % tuple(schema_editor.quote_value(p) for p in params)
|
||||
|
||||
def create_sql(self, model, schema_editor, using="", **kwargs):
|
||||
include = [
|
||||
model._meta.get_field(field_name).column for field_name in self.include
|
||||
]
|
||||
def create_sql(self, model, schema_editor, using='', **kwargs):
|
||||
include = [model._meta.get_field(field_name).column for field_name in self.include]
|
||||
condition = self._get_condition_sql(model, schema_editor)
|
||||
if self.expressions:
|
||||
index_expressions = []
|
||||
@@ -113,36 +109,29 @@ class Index:
|
||||
col_suffixes = [order[1] for order in self.fields_orders]
|
||||
expressions = None
|
||||
return schema_editor._create_index_sql(
|
||||
model,
|
||||
fields=fields,
|
||||
name=self.name,
|
||||
using=using,
|
||||
db_tablespace=self.db_tablespace,
|
||||
col_suffixes=col_suffixes,
|
||||
opclasses=self.opclasses,
|
||||
condition=condition,
|
||||
include=include,
|
||||
expressions=expressions,
|
||||
**kwargs,
|
||||
model, fields=fields, name=self.name, using=using,
|
||||
db_tablespace=self.db_tablespace, col_suffixes=col_suffixes,
|
||||
opclasses=self.opclasses, condition=condition, include=include,
|
||||
expressions=expressions, **kwargs,
|
||||
)
|
||||
|
||||
def remove_sql(self, model, schema_editor, **kwargs):
|
||||
return schema_editor._delete_index_sql(model, self.name, **kwargs)
|
||||
|
||||
def deconstruct(self):
|
||||
path = "%s.%s" % (self.__class__.__module__, self.__class__.__name__)
|
||||
path = path.replace("django.db.models.indexes", "django.db.models")
|
||||
kwargs = {"name": self.name}
|
||||
path = '%s.%s' % (self.__class__.__module__, self.__class__.__name__)
|
||||
path = path.replace('django.db.models.indexes', 'django.db.models')
|
||||
kwargs = {'name': self.name}
|
||||
if self.fields:
|
||||
kwargs["fields"] = self.fields
|
||||
kwargs['fields'] = self.fields
|
||||
if self.db_tablespace is not None:
|
||||
kwargs["db_tablespace"] = self.db_tablespace
|
||||
kwargs['db_tablespace'] = self.db_tablespace
|
||||
if self.opclasses:
|
||||
kwargs["opclasses"] = self.opclasses
|
||||
kwargs['opclasses'] = self.opclasses
|
||||
if self.condition:
|
||||
kwargs["condition"] = self.condition
|
||||
kwargs['condition'] = self.condition
|
||||
if self.include:
|
||||
kwargs["include"] = self.include
|
||||
kwargs['include'] = self.include
|
||||
return (path, self.expressions, kwargs)
|
||||
|
||||
def clone(self):
|
||||
@@ -159,44 +148,36 @@ class Index:
|
||||
fit its size by truncating the excess length.
|
||||
"""
|
||||
_, table_name = split_identifier(model._meta.db_table)
|
||||
column_names = [
|
||||
model._meta.get_field(field_name).column
|
||||
for field_name, order in self.fields_orders
|
||||
]
|
||||
column_names = [model._meta.get_field(field_name).column for field_name, order in self.fields_orders]
|
||||
column_names_with_order = [
|
||||
(("-%s" if order else "%s") % column_name)
|
||||
for column_name, (field_name, order) in zip(
|
||||
column_names, self.fields_orders
|
||||
)
|
||||
(('-%s' if order else '%s') % column_name)
|
||||
for column_name, (field_name, order) in zip(column_names, self.fields_orders)
|
||||
]
|
||||
# The length of the parts of the name is based on the default max
|
||||
# length of 30 characters.
|
||||
hash_data = [table_name] + column_names_with_order + [self.suffix]
|
||||
self.name = "%s_%s_%s" % (
|
||||
self.name = '%s_%s_%s' % (
|
||||
table_name[:11],
|
||||
column_names[0][:7],
|
||||
"%s_%s" % (names_digest(*hash_data, length=6), self.suffix),
|
||||
'%s_%s' % (names_digest(*hash_data, length=6), self.suffix),
|
||||
)
|
||||
if len(self.name) > self.max_name_length:
|
||||
raise ValueError(
|
||||
"Index too long for multiple database support. Is self.suffix "
|
||||
"longer than 3 characters?"
|
||||
)
|
||||
if self.name[0] == "_" or self.name[0].isdigit():
|
||||
self.name = "D%s" % self.name[1:]
|
||||
assert len(self.name) <= self.max_name_length, (
|
||||
'Index too long for multiple database support. Is self.suffix '
|
||||
'longer than 3 characters?'
|
||||
)
|
||||
if self.name[0] == '_' or self.name[0].isdigit():
|
||||
self.name = 'D%s' % self.name[1:]
|
||||
|
||||
def __repr__(self):
|
||||
return "<%s:%s%s%s%s%s%s%s>" % (
|
||||
self.__class__.__qualname__,
|
||||
"" if not self.fields else " fields=%s" % repr(self.fields),
|
||||
"" if not self.expressions else " expressions=%s" % repr(self.expressions),
|
||||
"" if not self.name else " name=%s" % repr(self.name),
|
||||
""
|
||||
if self.db_tablespace is None
|
||||
else " db_tablespace=%s" % repr(self.db_tablespace),
|
||||
"" if self.condition is None else " condition=%s" % self.condition,
|
||||
"" if not self.include else " include=%s" % repr(self.include),
|
||||
"" if not self.opclasses else " opclasses=%s" % repr(self.opclasses),
|
||||
return '<%s:%s%s%s%s%s>' % (
|
||||
self.__class__.__name__,
|
||||
'' if not self.fields else " fields='%s'" % ', '.join(self.fields),
|
||||
'' if not self.expressions else " expressions='%s'" % ', '.join([
|
||||
str(expression) for expression in self.expressions
|
||||
]),
|
||||
'' if self.condition is None else ' condition=%s' % self.condition,
|
||||
'' if not self.include else " include='%s'" % ', '.join(self.include),
|
||||
'' if not self.opclasses else " opclasses='%s'" % ', '.join(self.opclasses),
|
||||
)
|
||||
|
||||
def __eq__(self, other):
|
||||
@@ -207,20 +188,17 @@ class Index:
|
||||
|
||||
class IndexExpression(Func):
|
||||
"""Order and wrap expressions for CREATE INDEX statements."""
|
||||
|
||||
template = "%(expressions)s"
|
||||
template = '%(expressions)s'
|
||||
wrapper_classes = (OrderBy, Collate)
|
||||
|
||||
def set_wrapper_classes(self, connection=None):
|
||||
# Some databases (e.g. MySQL) treats COLLATE as an indexed expression.
|
||||
if connection and connection.features.collate_as_index_expression:
|
||||
self.wrapper_classes = tuple(
|
||||
[
|
||||
wrapper_cls
|
||||
for wrapper_cls in self.wrapper_classes
|
||||
if wrapper_cls is not Collate
|
||||
]
|
||||
)
|
||||
self.wrapper_classes = tuple([
|
||||
wrapper_cls
|
||||
for wrapper_cls in self.wrapper_classes
|
||||
if wrapper_cls is not Collate
|
||||
])
|
||||
|
||||
@classmethod
|
||||
def register_wrappers(cls, *wrapper_classes):
|
||||
@@ -244,17 +222,16 @@ class IndexExpression(Func):
|
||||
if len(wrapper_types) != len(set(wrapper_types)):
|
||||
raise ValueError(
|
||||
"Multiple references to %s can't be used in an indexed "
|
||||
"expression."
|
||||
% ", ".join(
|
||||
[wrapper_cls.__qualname__ for wrapper_cls in self.wrapper_classes]
|
||||
)
|
||||
"expression." % ', '.join([
|
||||
wrapper_cls.__qualname__ for wrapper_cls in self.wrapper_classes
|
||||
])
|
||||
)
|
||||
if expressions[1 : len(wrappers) + 1] != wrappers:
|
||||
if expressions[1:len(wrappers) + 1] != wrappers:
|
||||
raise ValueError(
|
||||
"%s must be topmost expressions in an indexed expression."
|
||||
% ", ".join(
|
||||
[wrapper_cls.__qualname__ for wrapper_cls in self.wrapper_classes]
|
||||
)
|
||||
'%s must be topmost expressions in an indexed expression.'
|
||||
% ', '.join([
|
||||
wrapper_cls.__qualname__ for wrapper_cls in self.wrapper_classes
|
||||
])
|
||||
)
|
||||
# Wrap expressions in parentheses if they are not column references.
|
||||
root_expression = index_expressions[1]
|
||||
@@ -266,7 +243,7 @@ class IndexExpression(Func):
|
||||
for_save,
|
||||
)
|
||||
if not isinstance(resolve_root_expression, Col):
|
||||
root_expression = Func(root_expression, template="(%(expressions)s)")
|
||||
root_expression = Func(root_expression, template='(%(expressions)s)')
|
||||
|
||||
if wrappers:
|
||||
# Order wrappers and set their expressions.
|
||||
@@ -283,9 +260,7 @@ class IndexExpression(Func):
|
||||
else:
|
||||
# Use the root expression, if there are no wrappers.
|
||||
self.set_source_expressions([root_expression])
|
||||
return super().resolve_expression(
|
||||
query, allow_joins, reuse, summarize, for_save
|
||||
)
|
||||
return super().resolve_expression(query, allow_joins, reuse, summarize, for_save)
|
||||
|
||||
def as_sqlite(self, compiler, connection, **extra_context):
|
||||
# Casting to numeric is unnecessary.
|
||||
|
||||
Reference in New Issue
Block a user