当前位置: 首页>>代码示例>>Python>>正文


Python BaseDatabaseCreation._digest方法代码示例

本文整理汇总了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
开发者ID:BardRobort,项目名称:django,代码行数:33,代码来源:schema.py

示例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")
开发者ID:AMHZR,项目名称:appartments,代码行数:70,代码来源:generic.py


注:本文中的django.db.backends.creation.BaseDatabaseCreation._digest方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。