Exemple #1
0
def get_sql_indexes_for_field(model, f, style):
    "Returns the CREATE INDEX SQL statement for a single field"
    from django.db import backend, connection
    output = []
    autoindexes_pk = getattr(connection.features, 'autoindexes_primary_keys',
                             False)
    if f.db_index and not ((f.primary_key or f.unique) and autoindexes_pk):
        unique = f.unique and 'UNIQUE ' or ''
        try:
            tablespace = f.db_tablespace or model._meta.db_tablespace
        except:  # v0.96 compatibility
            tablespace = None
        if tablespace and backend.supports_tablespaces:
            tablespace_sql = ' ' + backend.get_tablespace_sql(tablespace)
        else:
            tablespace_sql = ''
        output.append(
            style.SQL_KEYWORD('CREATE %sINDEX' % unique) + ' ' + \
            style.SQL_TABLE(connection.ops.quote_name('%s_%s' % (model._meta.db_table, f.column))) + ' ' + \
            style.SQL_KEYWORD('ON') + ' ' + \
            style.SQL_TABLE(connection.ops.quote_name(model._meta.db_table)) + ' ' + \
            "(%s)" % style.SQL_FIELD(connection.ops.quote_name(f.column)) + \
            "%s;" % tablespace_sql
        )
    return output
Exemple #2
0
 def get_sql_indexes_for_field(self, tablespace, table, f):
     "Returns the CREATE INDEX SQL statement for a single field"
     from django.db import backend
     output = []
     try:
         autopk = self.connection.features.autoindexes_primary_keys
     except AttributeError:
         autopk = False
     if f.db_index and not ((f.primary_key or f.unique) and autopk):
         unique = f.unique and 'UNIQUE ' or ''
         try:
             tablespace = f.db_tablespace or tablespace
         except: # v0.96 compatibility
             tablespace = None
         if tablespace and backend.supports_tablespaces:
             tablespace_sql = ' ' + backend.get_tablespace_sql(tablespace)
         else:
             tablespace_sql = ''
         qn = self.connection.ops.quote_name
         output.append(
             self.style.SQL_KEYWORD('CREATE %sINDEX' % unique) + ' ' + \
             self.style.SQL_TABLE(qn('%s_%s' % (table, f.column))) + ' ' + \
             self.style.SQL_KEYWORD('ON') + ' ' + \
             self.style.SQL_TABLE(qn(table)) + ' ' + \
             "(%s)" % self.style.SQL_FIELD(qn(f.column)) + \
             "%s;" % tablespace_sql
         )
     return output
Exemple #3
0
 def get_sql_indexes_for_field(self, tablespace, table, f):
     "Returns the CREATE INDEX SQL statement for a single field"
     from django.db import backend
     output = []
     try:
         autopk = self.connection.features.autoindexes_primary_keys
     except AttributeError:
         autopk = False
     if f.db_index and not ((f.primary_key or f.unique) and autopk):
         unique = f.unique and 'UNIQUE ' or ''
         try:
             tablespace = f.db_tablespace or tablespace
         except:  # v0.96 compatibility
             tablespace = None
         if tablespace and backend.supports_tablespaces:
             tablespace_sql = ' ' + backend.get_tablespace_sql(tablespace)
         else:
             tablespace_sql = ''
         qn = self.connection.ops.quote_name
         output.append(
             self.style.SQL_KEYWORD('CREATE %sINDEX' % unique) + ' ' + \
             self.style.SQL_TABLE(qn('%s_%s' % (table, f.column))) + ' ' + \
             self.style.SQL_KEYWORD('ON') + ' ' + \
             self.style.SQL_TABLE(qn(table)) + ' ' + \
             "(%s)" % self.style.SQL_FIELD(qn(f.column)) + \
             "%s;" % tablespace_sql
         )
     return output
def get_sql_indexes_for_field(model, f, style):
    "Returns the CREATE INDEX SQL statement for a single field"
    from django.db import backend, connection
    output = []
    if f.db_index and not ((f.primary_key or f.unique) and backend.autoindexes_primary_keys):
        unique = f.unique and 'UNIQUE ' or ''
        try:
            tablespace = f.db_tablespace or model._meta.db_tablespace
        except: # v0.96 compatibility
            tablespace = None
        if tablespace and backend.supports_tablespaces:
            tablespace_sql = ' ' + backend.get_tablespace_sql(tablespace)
        else:
            tablespace_sql = ''
        output.append(
            style.SQL_KEYWORD('CREATE %sINDEX' % unique) + ' ' + \
            style.SQL_TABLE(connection.ops.quote_name('%s_%s' % (model._meta.db_table, f.column))) + ' ' + \
            style.SQL_KEYWORD('ON') + ' ' + \
            style.SQL_TABLE(connection.ops.quote_name(model._meta.db_table)) + ' ' + \
            "(%s)" % style.SQL_FIELD(connection.ops.quote_name(f.column)) + \
            "%s;" % tablespace_sql
        )
    return output
def _get_many_to_many_sql_for_field(model, f, style):
    from django.db import backend, models, connection
    try:
        from django.contrib.contenttypes import generic
    except: # v0.96 compatibility
        from django.db.models.fields import generic #@UnresolvedImport
        
    ops, introspection = get_operations_and_introspection_classes(style)

    opts = model._meta
    final_output = []
    if not isinstance(f.rel, generic.GenericRel):
        try:
            tablespace = f.db_tablespace or opts.db_tablespace
        except: # v0.96 compatibility
            tablespace = None
        if tablespace and backend.supports_tablespaces and backend.autoindexes_primary_keys:
            tablespace_sql = ' ' + backend.get_tablespace_sql(tablespace, inline=True)
        else:
            tablespace_sql = ''
        table_output = [style.SQL_KEYWORD('CREATE TABLE') + ' ' + \
            style.SQL_TABLE(connection.ops.quote_name(f.m2m_db_table())) + ' (']
        table_output.append('    %s %s %s%s,' % \
            (style.SQL_FIELD(connection.ops.quote_name('id')),
            style.SQL_COLTYPE(models.AutoField(primary_key=True).db_type()),
            style.SQL_KEYWORD('NOT NULL PRIMARY KEY'),
            tablespace_sql))
        table_output.append('    %s %s %s %s (%s)%s,' % \
            (style.SQL_FIELD(connection.ops.quote_name(f.m2m_column_name())),
            style.SQL_COLTYPE(models.ForeignKey(model).db_type()),
            style.SQL_KEYWORD('NOT NULL REFERENCES'),
            style.SQL_TABLE(connection.ops.quote_name(opts.db_table)),
            style.SQL_FIELD(connection.ops.quote_name(opts.pk.column)),
#            backend.get_deferrable_sql()))
            ''))
        table_output.append('    %s %s %s %s (%s)%s,' % \
            (style.SQL_FIELD(connection.ops.quote_name(f.m2m_reverse_name())),
            style.SQL_COLTYPE(models.ForeignKey(f.rel.to).db_type()),
            style.SQL_KEYWORD('NOT NULL REFERENCES'),
            style.SQL_TABLE(connection.ops.quote_name(f.rel.to._meta.db_table)),
            style.SQL_FIELD(connection.ops.quote_name(f.rel.to._meta.pk.column)),
#            backend.get_deferrable_sql()))
            ''))
        table_output.append('    %s (%s, %s)%s' % \
            (style.SQL_KEYWORD('UNIQUE'),
            style.SQL_FIELD(connection.ops.quote_name(f.m2m_column_name())),
            style.SQL_FIELD(connection.ops.quote_name(f.m2m_reverse_name())),
            tablespace_sql))
        table_output.append(')')
        try: 
            if opts.db_tablespace and backend.supports_tablespaces:
                # f.db_tablespace is only for indices, so ignore its value here.
                table_output.append(backend.get_tablespace_sql(opts.db_tablespace))
        except: # v0.96 compatibility
            pass
        table_output.append(';')
        final_output.append('\n'.join(table_output))

        # Add any extra SQL needed to support auto-incrementing PKs
        autoinc_sql = ops.get_autoinc_sql(f.m2m_db_table())
        if autoinc_sql:
            for stmt in autoinc_sql:
                final_output.append(stmt)

    return final_output
Exemple #6
0
def _get_many_to_many_sql_for_field(model, f, style):
    from django.db import backend, models, connection
    try:
        from django.contrib.contenttypes import generic
    except:  # v0.96 compatibility
        from django.db.models.fields import generic  #@UnresolvedImport

    ops, introspection = get_operations_and_introspection_classes(style)

    opts = model._meta
    final_output = []
    if not isinstance(f.rel, generic.GenericRel):
        try:
            tablespace = f.db_tablespace or opts.db_tablespace
        except:  # v0.96 compatibility
            tablespace = None
        autoindexes_pk = getattr(connection.features,
                                 'autoindexes_primary_keys', False)
        if tablespace and backend.supports_tablespaces and autoindexes_pk:
            tablespace_sql = ' ' + backend.get_tablespace_sql(tablespace,
                                                              inline=True)
        else:
            tablespace_sql = ''
        table_output = [style.SQL_KEYWORD('CREATE TABLE') + ' ' + \
            style.SQL_TABLE(connection.ops.quote_name(f.m2m_db_table())) + ' (']
        table_output.append('    %s %s %s%s,' % \
            (style.SQL_FIELD(connection.ops.quote_name('id')),
            style.SQL_COLTYPE(models.AutoField(primary_key=True).db_type()),
            style.SQL_KEYWORD('NOT NULL PRIMARY KEY'),
            tablespace_sql))
        table_output.append('    %s %s %s %s (%s)%s,' % \
            (style.SQL_FIELD(connection.ops.quote_name(f.m2m_column_name())),
            style.SQL_COLTYPE(models.ForeignKey(model).db_type()),
            style.SQL_KEYWORD('NOT NULL REFERENCES'),
            style.SQL_TABLE(connection.ops.quote_name(opts.db_table)),
            style.SQL_FIELD(connection.ops.quote_name(opts.pk.column)),
               #            backend.get_deferrable_sql()))
            ''))
        table_output.append('    %s %s %s %s (%s)%s,' % \
            (style.SQL_FIELD(connection.ops.quote_name(f.m2m_reverse_name())),
            style.SQL_COLTYPE(models.ForeignKey(f.rel.to).db_type()),
            style.SQL_KEYWORD('NOT NULL REFERENCES'),
            style.SQL_TABLE(connection.ops.quote_name(f.rel.to._meta.db_table)),
            style.SQL_FIELD(connection.ops.quote_name(f.rel.to._meta.pk.column)),
               #            backend.get_deferrable_sql()))
            ''))
        table_output.append('    %s (%s, %s)%s' % \
            (style.SQL_KEYWORD('UNIQUE'),
            style.SQL_FIELD(connection.ops.quote_name(f.m2m_column_name())),
            style.SQL_FIELD(connection.ops.quote_name(f.m2m_reverse_name())),
            tablespace_sql))
        table_output.append(')')
        try:
            if opts.db_tablespace and backend.supports_tablespaces:
                # f.db_tablespace is only for indices, so ignore its value here.
                table_output.append(
                    backend.get_tablespace_sql(opts.db_tablespace))
        except:  # v0.96 compatibility
            pass
        table_output.append(';')
        final_output.append('\n'.join(table_output))

        # Add any extra SQL needed to support auto-incrementing PKs
        autoinc_sql = ops.get_autoinc_sql(f.m2m_db_table())
        if autoinc_sql:
            for stmt in autoinc_sql:
                final_output.append(stmt)

    return final_output