本文整理匯總了Python中django.db.backends.creation.BaseDatabaseCreation._digest方法的典型用法代碼示例。如果您正苦於以下問題:Python BaseDatabaseCreation._digest方法的具體用法?Python BaseDatabaseCreation._digest怎麽用?Python BaseDatabaseCreation._digest使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.db.backends.creation.BaseDatabaseCreation
的用法示例。
在下文中一共展示了BaseDatabaseCreation._digest方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _create_index_name
# 需要導入模塊: from django.db.backends.creation import BaseDatabaseCreation [as 別名]
# 或者: from django.db.backends.creation.BaseDatabaseCreation import _digest [as 別名]
def _create_index_name(self, model, column_names, suffix=""):
"""
Generates a unique name for an index/unique constraint.
"""
# If there is just one column in the index, use a default algorithm from Django
if len(column_names) == 1 and not suffix:
return truncate_name(
'%s_%s' % (model._meta.db_table, BaseDatabaseCreation._digest(column_names[0])),
self.connection.ops.max_name_length()
)
# Else generate the name for the index using a different algorithm
table_name = model._meta.db_table.replace('"', '').replace('.', '_')
index_unique_name = '_%x' % abs(hash((table_name, ','.join(column_names))))
max_length = self.connection.ops.max_name_length() or 200
# If the index name is too long, truncate it
index_name = ('%s_%s%s%s' % (
table_name, column_names[0], index_unique_name, suffix,
)).replace('"', '').replace('.', '_')
if len(index_name) > max_length:
part = ('_%s%s%s' % (column_names[0], index_unique_name, suffix))
index_name = '%s%s' % (table_name[:(max_length - len(part))], part)
# It shouldn't start with an underscore (Oracle hates this)
if index_name[0] == "_":
index_name = index_name[1:]
# If it's STILL too long, just hash it down
if len(index_name) > max_length:
index_name = hashlib.md5(force_bytes(index_name)).hexdigest()[:max_length]
# It can't start with a number on Oracle, so prepend D if we need to
if index_name[0].isdigit():
index_name = "D%s" % index_name[:-1]
return index_name
示例2: DatabaseOperations
# 需要導入模塊: from django.db.backends.creation import BaseDatabaseCreation [as 別名]
# 或者: from django.db.backends.creation.BaseDatabaseCreation import _digest [as 別名]
#.........這裏部分代碼省略.........
# This also creates normal indexes in 1.1.
if hasattr(self._get_connection().creation, "sql_indexes_for_field"):
# Make a fake model to pass in, with only db_table
model = self.mock_model("FakeModelForGISCreation", table_name)
for stmt in self._get_connection().creation.sql_indexes_for_field(model, field, no_style()):
self.add_deferred_sql(stmt)
if sql:
return sql % sqlparams
else:
return None
def _field_sanity(self, field):
"""
Placeholder for DBMS-specific field alterations (some combos aren't valid,
e.g. DEFAULT and TEXT on MySQL)
"""
return field
def _default_value_workaround(self, value):
"""
DBMS-specific value alterations (this really works around
missing functionality in Django backends)
"""
if isinstance(value, bool) and not self.has_booleans:
return int(value)
else:
return value
def foreign_key_sql(self, from_table_name, from_column_name, to_table_name, to_column_name):
"""
Generates a full SQL statement to add a foreign key constraint
"""
constraint_name = '%s_refs_%s_%s' % (from_column_name, to_column_name, self._digest(from_table_name, to_table_name))
return 'ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' % (
self.quote_name(from_table_name),
self.quote_name(self.shorten_name(constraint_name)),
self.quote_name(from_column_name),
self.quote_name(to_table_name),
self.quote_name(to_column_name),
self._get_connection().ops.deferrable_sql() # Django knows this
)
@invalidate_table_constraints
def delete_foreign_key(self, table_name, column):
"""
Drop a foreign key constraint
"""
if self.dry_run:
if self.debug:
print(' - no dry run output for delete_foreign_key() due to dynamic DDL, sorry')
return # We can't look at the DB to get the constraints
constraints = self._find_foreign_constraints(table_name, column)
if not constraints:
raise ValueError("Cannot find a FOREIGN KEY constraint on table %s, column %s" % (table_name, column))
for constraint_name in constraints:
self.execute(self.delete_foreign_key_sql % {
"table": self.quote_name(table_name),
"constraint": self.quote_name(constraint_name),
})
drop_foreign_key = alias('delete_foreign_key')
def _find_foreign_constraints(self, table_name, column_name=None):
constraints = self._constraints_affecting_columns(
table_name, [column_name], "FOREIGN KEY")