Django

Code

Changeset 5964

Show
Ignore:
Timestamp:
08/19/07 19:21:10 (1 year ago)
Author:
adrian
Message:

Refactored get_sql_sequence_reset() to DatabaseOperations?.sequence_reset_sql(). Refs #5106

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/management/commands/loaddata.py

    r5913 r5964  
    1616        from django.db.models import get_apps 
    1717        from django.core import serializers 
    18         from django.db import connection, transaction, backend 
     18        from django.db import connection, transaction 
    1919        from django.conf import settings 
    2020 
     
    106106 
    107107        if count[0] > 0: 
    108             sequence_sql = backend.get_sql_sequence_reset(self.style, models) 
     108            sequence_sql = connection.ops.sequence_reset_sql(self.style, models) 
    109109            if sequence_sql: 
    110110                if verbosity > 1: 
  • django/trunk/django/core/management/commands/sqlsequencereset.py

    r5898 r5964  
    66 
    77    def handle_app(self, app, **options): 
    8         from django.db import backend, models 
    9         return '\n'.join(backend.get_sql_sequence_reset(self.style, models.get_models(app))) 
     8        from django.db import connection, models 
     9        return '\n'.join(connection.ops.sequence_reset_sql(self.style, models.get_models(app))) 
  • django/trunk/django/db/backends/ado_mssql/base.py

    r5963 r5964  
    110110    return "ON %s" % quote_name(tablespace) 
    111111 
    112 def get_sql_sequence_reset(style, model_list): 
    113     "Returns a list of the SQL statements to reset sequences for the given models." 
    114     # No sequence reset required 
    115     return [] 
    116  
    117112OPERATOR_MAPPING = { 
    118113    'exact': '= %s', 
  • django/trunk/django/db/backends/dummy/base.py

    r5963 r5964  
    4545dictfetchall = complain 
    4646get_start_transaction_sql = complain 
    47 get_sql_sequence_reset = complain 
    4847 
    4948OPERATOR_MAPPING = {} 
  • django/trunk/django/db/backends/__init__.py

    r5963 r5964  
    151151        """ 
    152152        raise NotImplementedError() 
     153 
     154    def sequence_reset_sql(self, style, model_list): 
     155        """ 
     156        Returns a list of the SQL statements required to reset sequences for 
     157        the given models. 
     158 
     159        The `style` argument is a Style object as returned by either 
     160        color_style() or no_style() in django.core.management.color. 
     161        """ 
     162        return [] # No sequence reset required by default. 
  • django/trunk/django/db/backends/mysql/base.py

    r5963 r5964  
    192192    return "BEGIN;" 
    193193 
    194 def get_sql_sequence_reset(style, model_list): 
    195     "Returns a list of the SQL statements to reset sequences for the given models." 
    196     # No sequence reset required 
    197     return [] 
    198  
    199194OPERATOR_MAPPING = { 
    200195    'exact': '= %s', 
  • django/trunk/django/db/backends/mysql_old/base.py

    r5963 r5964  
    211211    return "BEGIN;" 
    212212 
    213 def get_sql_sequence_reset(style, model_list): 
    214     "Returns a list of the SQL statements to reset sequences for the given models." 
    215     # No sequence reset required 
    216     return [] 
    217  
    218213OPERATOR_MAPPING = { 
    219214    'exact': '= %s', 
  • django/trunk/django/db/backends/oracle/base.py

    r5963 r5964  
    9696            return [] 
    9797 
     98    def sequence_reset_sql(self, style, model_list): 
     99        from django.db import models 
     100        output = [] 
     101        query = _get_sequence_reset_sql() 
     102        for model in model_list: 
     103            for f in model._meta.fields: 
     104                if isinstance(f, models.AutoField): 
     105                    sequence_name = get_sequence_name(model._meta.db_table) 
     106                    output.append(query % {'sequence':sequence_name, 
     107                                           'table':model._meta.db_table}) 
     108                    break # Only one AutoField is allowed per model, so don't bother continuing. 
     109            for f in model._meta.many_to_many: 
     110                sequence_name = get_sequence_name(f.m2m_db_table()) 
     111                output.append(query % {'sequence':sequence_name, 
     112                                       'table':f.m2m_db_table()}) 
     113        return output 
     114 
    98115class DatabaseWrapper(BaseDatabaseWrapper): 
    99116    ops = DatabaseOperations() 
     
    244261    name_length = DatabaseOperations().max_name_length() - 3 
    245262    return '%s_SQ' % util.truncate_name(table, name_length).upper() 
    246  
    247 def get_sql_sequence_reset(style, model_list): 
    248     "Returns a list of the SQL statements to reset sequences for the given models." 
    249     from django.db import models 
    250     output = [] 
    251     query = _get_sequence_reset_sql() 
    252     for model in model_list: 
    253         for f in model._meta.fields: 
    254             if isinstance(f, models.AutoField): 
    255                 sequence_name = get_sequence_name(model._meta.db_table) 
    256                 output.append(query % {'sequence':sequence_name, 
    257                                        'table':model._meta.db_table}) 
    258                 break # Only one AutoField is allowed per model, so don't bother continuing. 
    259         for f in model._meta.many_to_many: 
    260             sequence_name = get_sequence_name(f.m2m_db_table()) 
    261             output.append(query % {'sequence':sequence_name, 
    262                                    'table':f.m2m_db_table()}) 
    263     return output 
    264263 
    265264def get_trigger_name(table): 
  • django/trunk/django/db/backends/postgresql/base.py

    r5963 r5964  
    125125            return [] 
    126126 
     127    def sequence_reset_sql(self, style, model_list): 
     128        from django.db import models 
     129        output = [] 
     130        for model in model_list: 
     131            # Use `coalesce` to set the sequence for each model to the max pk value if there are records, 
     132            # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true 
     133            # if there are records (as the max pk value is already in use), otherwise set it to false. 
     134            for f in model._meta.fields: 
     135                if isinstance(f, models.AutoField): 
     136                    output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 
     137                        (style.SQL_KEYWORD('SELECT'), 
     138                        style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), 
     139                        style.SQL_FIELD(quote_name(f.column)), 
     140                        style.SQL_FIELD(quote_name(f.column)), 
     141                        style.SQL_KEYWORD('IS NOT'), 
     142                        style.SQL_KEYWORD('FROM'), 
     143                        style.SQL_TABLE(quote_name(model._meta.db_table)))) 
     144                    break # Only one AutoField is allowed per model, so don't bother continuing. 
     145            for f in model._meta.many_to_many: 
     146                output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 
     147                    (style.SQL_KEYWORD('SELECT'), 
     148                    style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), 
     149                    style.SQL_FIELD(quote_name('id')), 
     150                    style.SQL_FIELD(quote_name('id')), 
     151                    style.SQL_KEYWORD('IS NOT'), 
     152                    style.SQL_KEYWORD('FROM'), 
     153                    style.SQL_TABLE(f.m2m_db_table()))) 
     154        return output 
     155 
    127156class DatabaseWrapper(BaseDatabaseWrapper): 
    128157    ops = DatabaseOperations() 
     
    186215    return "BEGIN;" 
    187216 
    188 def get_sql_sequence_reset(style, model_list): 
    189     "Returns a list of the SQL statements to reset sequences for the given models." 
    190     from django.db import models 
    191     output = [] 
    192     for model in model_list: 
    193         # Use `coalesce` to set the sequence for each model to the max pk value if there are records, 
    194         # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true 
    195         # if there are records (as the max pk value is already in use), otherwise set it to false. 
    196         for f in model._meta.fields: 
    197             if isinstance(f, models.AutoField): 
    198                 output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 
    199                     (style.SQL_KEYWORD('SELECT'), 
    200                     style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), 
    201                     style.SQL_FIELD(quote_name(f.column)), 
    202                     style.SQL_FIELD(quote_name(f.column)), 
    203                     style.SQL_KEYWORD('IS NOT'), 
    204                     style.SQL_KEYWORD('FROM'), 
    205                     style.SQL_TABLE(quote_name(model._meta.db_table)))) 
    206                 break # Only one AutoField is allowed per model, so don't bother continuing. 
    207         for f in model._meta.many_to_many: 
    208             output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 
    209                 (style.SQL_KEYWORD('SELECT'), 
    210                 style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), 
    211                 style.SQL_FIELD(quote_name('id')), 
    212                 style.SQL_FIELD(quote_name('id')), 
    213                 style.SQL_KEYWORD('IS NOT'), 
    214                 style.SQL_KEYWORD('FROM'), 
    215                 style.SQL_TABLE(f.m2m_db_table()))) 
    216     return output 
    217  
    218217def typecast_string(s): 
    219218    """ 
  • django/trunk/django/db/backends/postgresql_psycopg2/base.py

    r5963 r5964  
    8787            return [] 
    8888 
     89    def sequence_reset_sql(self, style, model_list): 
     90        from django.db import models 
     91        output = [] 
     92        for model in model_list: 
     93            # Use `coalesce` to set the sequence for each model to the max pk value if there are records, 
     94            # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true 
     95            # if there are records (as the max pk value is already in use), otherwise set it to false. 
     96            for f in model._meta.fields: 
     97                if isinstance(f, models.AutoField): 
     98                    output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 
     99                        (style.SQL_KEYWORD('SELECT'), 
     100                        style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), 
     101                        style.SQL_FIELD(quote_name(f.column)), 
     102                        style.SQL_FIELD(quote_name(f.column)), 
     103                        style.SQL_KEYWORD('IS NOT'), 
     104                        style.SQL_KEYWORD('FROM'), 
     105                        style.SQL_TABLE(quote_name(model._meta.db_table)))) 
     106                    break # Only one AutoField is allowed per model, so don't bother continuing. 
     107            for f in model._meta.many_to_many: 
     108                output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 
     109                    (style.SQL_KEYWORD('SELECT'), 
     110                    style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), 
     111                    style.SQL_FIELD(quote_name('id')), 
     112                    style.SQL_FIELD(quote_name('id')), 
     113                    style.SQL_KEYWORD('IS NOT'), 
     114                    style.SQL_KEYWORD('FROM'), 
     115                    style.SQL_TABLE(f.m2m_db_table()))) 
     116        return output 
     117 
    89118class DatabaseWrapper(BaseDatabaseWrapper): 
    90119    ops = DatabaseOperations() 
     
    140169    return "BEGIN;" 
    141170 
    142 def get_sql_sequence_reset(style, model_list): 
    143     "Returns a list of the SQL statements to reset sequences for the given models." 
    144     from django.db import models 
    145     output = [] 
    146     for model in model_list: 
    147         # Use `coalesce` to set the sequence for each model to the max pk value if there are records, 
    148         # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true 
    149         # if there are records (as the max pk value is already in use), otherwise set it to false. 
    150         for f in model._meta.fields: 
    151             if isinstance(f, models.AutoField): 
    152                 output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 
    153                     (style.SQL_KEYWORD('SELECT'), 
    154                     style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), 
    155                     style.SQL_FIELD(quote_name(f.column)), 
    156                     style.SQL_FIELD(quote_name(f.column)), 
    157                     style.SQL_KEYWORD('IS NOT'), 
    158                     style.SQL_KEYWORD('FROM'), 
    159                     style.SQL_TABLE(quote_name(model._meta.db_table)))) 
    160                 break # Only one AutoField is allowed per model, so don't bother continuing. 
    161         for f in model._meta.many_to_many: 
    162             output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ 
    163                 (style.SQL_KEYWORD('SELECT'), 
    164                 style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), 
    165                 style.SQL_FIELD(quote_name('id')), 
    166                 style.SQL_FIELD(quote_name('id')), 
    167                 style.SQL_KEYWORD('IS NOT'), 
    168                 style.SQL_KEYWORD('FROM'), 
    169                 style.SQL_TABLE(f.m2m_db_table()))) 
    170     return output 
    171  
    172171OPERATOR_MAPPING = { 
    173172    'exact': '= %s', 
  • django/trunk/django/db/backends/sqlite3/base.py

    r5963 r5964  
    135135    return "BEGIN;" 
    136136 
    137 def get_sql_sequence_reset(style, model_list): 
    138     "Returns a list of the SQL statements to reset sequences for the given models." 
    139     # No sequence reset required 
    140     return [] 
    141  
    142137def _sqlite_date_trunc(lookup_type, dt): 
    143138    try: