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


Python utils.truncate_name方法代码示例

本文整理汇总了Python中django.db.backends.utils.truncate_name方法的典型用法代码示例。如果您正苦于以下问题:Python utils.truncate_name方法的具体用法?Python utils.truncate_name怎么用?Python utils.truncate_name使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.db.backends.utils的用法示例。


在下文中一共展示了utils.truncate_name方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: sql_indexes_for_fields

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def sql_indexes_for_fields(self, model, fields, style):
        if len(fields) == 1 and fields[0].db_tablespace:
            tablespace_sql = self.connection.ops.tablespace_sql(fields[0].db_tablespace)
        elif model._meta.db_tablespace:
            tablespace_sql = self.connection.ops.tablespace_sql(model._meta.db_tablespace)
        else:
            tablespace_sql = ""
        if tablespace_sql:
            tablespace_sql = " " + tablespace_sql

        field_names = []
        qn = self.connection.ops.quote_name
        for f in fields:
            field_names.append(style.SQL_FIELD(qn(f.column)))

        index_name = "%s_%s" % (model._meta.db_table, self._digest([f.name for f in fields]))

        return [
            style.SQL_KEYWORD("CREATE INDEX") + " " +
            style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + " " +
            style.SQL_KEYWORD("ON") + " " +
            style.SQL_TABLE(qn(model._meta.db_table)) + " " +
            "(%s)" % style.SQL_FIELD(", ".join(field_names)) +
            "%s;" % tablespace_sql,
        ] 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:27,代码来源:creation.py

示例2: sql_remove_table_constraints

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def sql_remove_table_constraints(self, model, references_to_delete, style):
        if not model._meta.managed or model._meta.proxy or model._meta.swapped:
            return []
        output = []
        qn = self.connection.ops.quote_name
        for rel_class, f in references_to_delete[model]:
            table = rel_class._meta.db_table
            col = f.column
            r_table = model._meta.db_table
            r_col = model._meta.get_field(f.rel.field_name).column
            r_name = '%s_refs_%s_%s' % (
                col, r_col, self._digest(table, r_table))
            output.append('%s %s %s %s;' % (
                style.SQL_KEYWORD('ALTER TABLE'),
                style.SQL_TABLE(qn(table)),
                style.SQL_KEYWORD(self.connection.ops.drop_foreignkey_sql()),
                style.SQL_FIELD(qn(truncate_name(
                    r_name, self.connection.ops.max_name_length())))
            ))
        del references_to_delete[model]
        return output 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:23,代码来源:creation.py

示例3: sql_destroy_indexes_for_fields

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def sql_destroy_indexes_for_fields(self, model, fields, style):
        if len(fields) == 1 and fields[0].db_tablespace:
            tablespace_sql = self.connection.ops.tablespace_sql(fields[0].db_tablespace)
        elif model._meta.db_tablespace:
            tablespace_sql = self.connection.ops.tablespace_sql(model._meta.db_tablespace)
        else:
            tablespace_sql = ""
        if tablespace_sql:
            tablespace_sql = " " + tablespace_sql

        field_names = []
        qn = self.connection.ops.quote_name
        for f in fields:
            field_names.append(style.SQL_FIELD(qn(f.column)))

        index_name = "%s_%s" % (model._meta.db_table, self._digest([f.name for f in fields]))

        return [
            style.SQL_KEYWORD("DROP INDEX") + " " +
            style.SQL_TABLE(qn(truncate_name(index_name, self.connection.ops.max_name_length()))) + " " +
            ";",
        ] 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:24,代码来源:creation.py

示例4: __add_psudokey_column

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def __add_psudokey_column( self, style, cursor, table_name, pk_name, column_list ):
        qn = self.connection.ops.quote_name
        max_name_length = self.connection.ops.max_name_length()
        
        sql = style.SQL_KEYWORD( 'ALTER TABLE ' ) + \
            style.SQL_TABLE( qn( table_name ) ) + \
            style.SQL_KEYWORD( ' ADD COLUMN ' ) + \
            style.SQL_FIELD( qn( truncate_name( "%s%s" % ( self.psudo_column_prefix, "_".join( column_list ) ), max_name_length ) ) ) + \
            style.SQL_KEYWORD( ' GENERATED ALWAYS AS( CASE WHEN ' ) + \
            style.SQL_FIELD( "%s %s" % ( " IS NULL OR ".join( column_list ), 'IS NULL THEN ' ) ) + \
            style.SQL_FIELD( qn( pk_name ) ) + \
            style.SQL_KEYWORD( ' END ) ;' )
        cursor.execute( 'SET INTEGRITY FOR ' + style.SQL_TABLE( qn( table_name ) ) + ' OFF CASCADE DEFERRED;' )
        cursor.execute( sql )
        cursor.execute( 'SET INTEGRITY FOR ' + style.SQL_TABLE( table_name ) + ' IMMEDIATE CHECKED;' )
        cursor.close()
        
    #private method to create dictionary of login credentials for test database 
开发者ID:ibmdb,项目名称:python-ibmdb-django,代码行数:20,代码来源:creation.py

示例5: test_migrate_syncdb_deferred_sql_executed_with_schemaeditor

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def test_migrate_syncdb_deferred_sql_executed_with_schemaeditor(self):
        """
        For an app without migrations, editor.execute() is used for executing
        the syncdb deferred SQL.
        """
        stdout = io.StringIO()
        with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute:
            call_command('migrate', run_syncdb=True, verbosity=1, stdout=stdout, no_color=True)
            create_table_count = len([call for call in execute.mock_calls if 'CREATE TABLE' in str(call)])
            self.assertEqual(create_table_count, 2)
            # There's at least one deferred SQL for creating the foreign key
            # index.
            self.assertGreater(len(execute.mock_calls), 2)
        stdout = stdout.getvalue()
        self.assertIn('Synchronize unmigrated apps: unmigrated_app_syncdb', stdout)
        self.assertIn('Creating tables...', stdout)
        table_name = truncate_name('unmigrated_app_syncdb_classroom', connection.ops.max_name_length())
        self.assertIn('Creating table %s' % table_name, stdout) 
开发者ID:nesdis,项目名称:djongo,代码行数:20,代码来源:test_commands.py

示例6: sql_destroy_indexes_for_fields

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def sql_destroy_indexes_for_fields(self, model, fields, style):
        # Django 1.6
        if len(fields) == 1 and fields[0].db_tablespace:
            tablespace_sql = self.connection.ops.tablespace_sql(
                fields[0].db_tablespace)
        elif model._meta.db_tablespace:
            tablespace_sql = self.connection.ops.tablespace_sql(
                model._meta.db_tablespace)
        else:
            tablespace_sql = ""
        if tablespace_sql:
            tablespace_sql = " " + tablespace_sql

        field_names = []
        qn = self.connection.ops.quote_name
        for f in fields:
            field_names.append(style.SQL_FIELD(qn(f.column)))

        index_name = "{0}_{1}".format(model._meta.db_table,
                                      self._digest([f.name for f in fields]))

        return [
            style.SQL_KEYWORD("DROP INDEX") + " " +
            style.SQL_TABLE(qn(truncate_name(index_name,
                                             self.connection.ops.max_name_length()))) + " " +
            style.SQL_KEYWORD("ON") + " " +
            style.SQL_TABLE(qn(model._meta.db_table)) + ";",
        ] 
开发者ID:LuciferJack,项目名称:python-mysql-pool,代码行数:30,代码来源:creation.py

示例7: sql_for_pending_references

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def sql_for_pending_references(self, model, style, pending_references):
        """
        Returns any ALTER TABLE statements to add constraints after the fact.
        """
        opts = model._meta
        if not opts.managed or opts.swapped:
            return []
        qn = self.connection.ops.quote_name
        final_output = []
        if model in pending_references:
            for rel_class, f in pending_references[model]:
                rel_opts = rel_class._meta
                r_table = rel_opts.db_table
                r_col = f.column
                table = opts.db_table
                col = opts.get_field(f.rel.field_name).column
                # For MySQL, r_name must be unique in the first 64 characters.
                # So we are careful with character usage here.
                r_name = '%s_refs_%s_%s' % (
                    r_col, col, self._digest(r_table, table))
                final_output.append(style.SQL_KEYWORD('ALTER TABLE') +
                    ' %s ADD CONSTRAINT %s FOREIGN KEY (%s) REFERENCES %s (%s)%s;' %
                    (qn(r_table), qn(truncate_name(
                        r_name, self.connection.ops.max_name_length())),
                    qn(r_col), qn(table), qn(col),
                    self.connection.ops.deferrable_sql()))
            del pending_references[model]
        return final_output 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:30,代码来源:creation.py

示例8: _create_index_name

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [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, self._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:lanbing510,项目名称:GTDWeb,代码行数:33,代码来源:schema.py

示例9: quote_name

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def quote_name(self, name):
        # SQL92 requires delimited (quoted) names to be case-sensitive.  When
        # not quoted, Oracle has case-insensitive behavior for identifiers, but
        # always defaults to uppercase.
        # We simplify things by making Oracle identifiers always uppercase.
        if not name.startswith('"') and not name.endswith('"'):
            name = '"%s"' % truncate_name(name.upper(), self.max_name_length())
        # Oracle puts the query text into a (query % args) construct, so % signs
        # in names need to be escaped. The '%%' will be collapsed back to '%' at
        # that stage so we aren't really making the name longer here.
        name = name.replace('%', '%%')
        return name.upper() 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:14,代码来源:operations.py

示例10: _get_sequence_name

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def _get_sequence_name(self, table):
        name_length = self.max_name_length() - 3
        return '%s_SQ' % truncate_name(table, name_length).upper() 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:5,代码来源:operations.py

示例11: _get_trigger_name

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def _get_trigger_name(self, table):
        name_length = self.max_name_length() - 3
        return '%s_TR' % truncate_name(table, name_length).upper() 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:5,代码来源:operations.py

示例12: deferred_class_factory

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def deferred_class_factory(model, attrs):
    """
    Returns a class object that is a copy of "model" with the specified "attrs"
    being replaced with DeferredAttribute objects. The "pk_value" ties the
    deferred attributes to a particular instance of the model.
    """
    if not attrs:
        return model
    # Never create deferred models based on deferred model
    if model._deferred:
        # Deferred models are proxies for the non-deferred model. We never
        # create chains of defers => proxy_for_model is the non-deferred
        # model.
        model = model._meta.proxy_for_model
    # The app registry wants a unique name for each model, otherwise the new
    # class won't be created (we get an exception). Therefore, we generate
    # the name using the passed in attrs. It's OK to reuse an existing class
    # object if the attrs are identical.
    name = "%s_Deferred_%s" % (model.__name__, '_'.join(sorted(list(attrs))))
    name = utils.truncate_name(name, 80, 32)

    try:
        return apps.get_model(model._meta.app_label, name)

    except LookupError:

        class Meta:
            proxy = True
            app_label = model._meta.app_label

        overrides = {attr: DeferredAttribute(attr, model) for attr in attrs}
        overrides["Meta"] = Meta
        overrides["__module__"] = model.__module__
        overrides["_deferred"] = True
        return type(str(name), (model,), overrides)


# The above function is also used to unpickle model instances with deferred
# fields. 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:41,代码来源:query_utils.py

示例13: sql_indexes_for_field

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def sql_indexes_for_field(self, model, f, style):
        "Return any spatial index creation SQL for the field."
        from django.contrib.gis.db.models.fields import GeometryField

        output = super(OracleCreation, self).sql_indexes_for_field(model, f, style)

        if isinstance(f, GeometryField):
            gqn = self.connection.ops.geo_quote_name
            qn = self.connection.ops.quote_name
            db_table = model._meta.db_table

            output.append(style.SQL_KEYWORD('INSERT INTO ') +
                          style.SQL_TABLE('USER_SDO_GEOM_METADATA') +
                          ' (%s, %s, %s, %s)\n  ' % tuple(map(qn, ['TABLE_NAME', 'COLUMN_NAME', 'DIMINFO', 'SRID'])) +
                          style.SQL_KEYWORD(' VALUES ') + '(\n    ' +
                          style.SQL_TABLE(gqn(db_table)) + ',\n    ' +
                          style.SQL_FIELD(gqn(f.column)) + ',\n    ' +
                          style.SQL_KEYWORD("MDSYS.SDO_DIM_ARRAY") + '(\n      ' +
                          style.SQL_KEYWORD("MDSYS.SDO_DIM_ELEMENT") +
                          ("('LONG', %s, %s, %s),\n      " % (f._extent[0], f._extent[2], f._tolerance)) +
                          style.SQL_KEYWORD("MDSYS.SDO_DIM_ELEMENT") +
                          ("('LAT', %s, %s, %s)\n    ),\n" % (f._extent[1], f._extent[3], f._tolerance)) +
                          '    %s\n  );' % f.srid)

            if f.spatial_index:
                # Getting the index name, Oracle doesn't allow object
                # names > 30 characters.
                idx_name = truncate_name('%s_%s_id' % (db_table, f.column), 30)

                output.append(style.SQL_KEYWORD('CREATE INDEX ') +
                              style.SQL_TABLE(qn(idx_name)) +
                              style.SQL_KEYWORD(' ON ') +
                              style.SQL_TABLE(qn(db_table)) + '(' +
                              style.SQL_FIELD(qn(f.column)) + ') ' +
                              style.SQL_KEYWORD('INDEXTYPE IS ') +
                              style.SQL_TABLE('MDSYS.SPATIAL_INDEX') + ';')
        return output 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:39,代码来源:creation.py

示例14: _create_spatial_index_name

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def _create_spatial_index_name(self, model, field):
        # Oracle doesn't allow object names > 30 characters. Use this scheme
        # instead of self._create_index_name() for backwards compatibility.
        return truncate_name('%s_%s_id' % (model._meta.db_table, field.column), 30) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:6,代码来源:schema.py

示例15: _get_no_autofield_sequence_name

# 需要导入模块: from django.db.backends import utils [as 别名]
# 或者: from django.db.backends.utils import truncate_name [as 别名]
def _get_no_autofield_sequence_name(self, table):
        """
        Manually created sequence name to keep backward compatibility for
        AutoFields that aren't Oracle identity columns.
        """
        name_length = self.max_name_length() - 3
        return '%s_SQ' % truncate_name(strip_quotes(table), name_length).upper() 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:9,代码来源:operations.py


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