mirror of
				https://github.com/django/django.git
				synced 2025-10-30 09:06:13 +00:00 
			
		
		
		
	Refactored get_sql_sequence_reset() to DatabaseOperations.sequence_reset_sql(). Refs #5106
git-svn-id: http://code.djangoproject.com/svn/django/trunk@5964 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
		| @@ -15,7 +15,7 @@ class Command(BaseCommand): | ||||
|     def handle(self, *fixture_labels, **options): | ||||
|         from django.db.models import get_apps | ||||
|         from django.core import serializers | ||||
|         from django.db import connection, transaction, backend | ||||
|         from django.db import connection, transaction | ||||
|         from django.conf import settings | ||||
|  | ||||
|         self.style = no_style() | ||||
| @@ -105,7 +105,7 @@ class Command(BaseCommand): | ||||
|                                 (format, fixture_name, humanize(fixture_dir)) | ||||
|  | ||||
|         if count[0] > 0: | ||||
|             sequence_sql = backend.get_sql_sequence_reset(self.style, models) | ||||
|             sequence_sql = connection.ops.sequence_reset_sql(self.style, models) | ||||
|             if sequence_sql: | ||||
|                 if verbosity > 1: | ||||
|                     print "Resetting sequences" | ||||
|   | ||||
| @@ -5,5 +5,5 @@ class Command(AppCommand): | ||||
|     output_transaction = True | ||||
|  | ||||
|     def handle_app(self, app, **options): | ||||
|         from django.db import backend, models | ||||
|         return '\n'.join(backend.get_sql_sequence_reset(self.style, models.get_models(app))) | ||||
|         from django.db import connection, models | ||||
|         return '\n'.join(connection.ops.sequence_reset_sql(self.style, models.get_models(app))) | ||||
|   | ||||
| @@ -150,3 +150,13 @@ class BaseDatabaseOperations(object): | ||||
|         color_style() or no_style() in django.core.management.color. | ||||
|         """ | ||||
|         raise NotImplementedError() | ||||
|  | ||||
|     def sequence_reset_sql(self, style, model_list): | ||||
|         """ | ||||
|         Returns a list of the SQL statements required to reset sequences for | ||||
|         the given models. | ||||
|  | ||||
|         The `style` argument is a Style object as returned by either | ||||
|         color_style() or no_style() in django.core.management.color. | ||||
|         """ | ||||
|         return [] # No sequence reset required by default. | ||||
|   | ||||
| @@ -109,11 +109,6 @@ def get_start_transaction_sql(): | ||||
| def get_tablespace_sql(tablespace, inline=False): | ||||
|     return "ON %s" % quote_name(tablespace) | ||||
|  | ||||
| def get_sql_sequence_reset(style, model_list): | ||||
|     "Returns a list of the SQL statements to reset sequences for the given models." | ||||
|     # No sequence reset required | ||||
|     return [] | ||||
|  | ||||
| OPERATOR_MAPPING = { | ||||
|     'exact': '= %s', | ||||
|     'iexact': 'LIKE %s', | ||||
|   | ||||
| @@ -44,6 +44,5 @@ dictfetchone = complain | ||||
| dictfetchmany = complain | ||||
| dictfetchall = complain | ||||
| get_start_transaction_sql = complain | ||||
| get_sql_sequence_reset = complain | ||||
|  | ||||
| OPERATOR_MAPPING = {} | ||||
|   | ||||
| @@ -191,11 +191,6 @@ dictfetchall  = util.dictfetchall | ||||
| def get_start_transaction_sql(): | ||||
|     return "BEGIN;" | ||||
|  | ||||
| def get_sql_sequence_reset(style, model_list): | ||||
|     "Returns a list of the SQL statements to reset sequences for the given models." | ||||
|     # No sequence reset required | ||||
|     return [] | ||||
|  | ||||
| OPERATOR_MAPPING = { | ||||
|     'exact': '= %s', | ||||
|     'iexact': 'LIKE %s', | ||||
|   | ||||
| @@ -210,11 +210,6 @@ dictfetchall  = util.dictfetchall | ||||
| def get_start_transaction_sql(): | ||||
|     return "BEGIN;" | ||||
|  | ||||
| def get_sql_sequence_reset(style, model_list): | ||||
|     "Returns a list of the SQL statements to reset sequences for the given models." | ||||
|     # No sequence reset required | ||||
|     return [] | ||||
|  | ||||
| OPERATOR_MAPPING = { | ||||
|     'exact': '= %s', | ||||
|     'iexact': 'LIKE %s', | ||||
|   | ||||
| @@ -95,6 +95,23 @@ class DatabaseOperations(BaseDatabaseOperations): | ||||
|         else: | ||||
|             return [] | ||||
|  | ||||
|     def sequence_reset_sql(self, style, model_list): | ||||
|         from django.db import models | ||||
|         output = [] | ||||
|         query = _get_sequence_reset_sql() | ||||
|         for model in model_list: | ||||
|             for f in model._meta.fields: | ||||
|                 if isinstance(f, models.AutoField): | ||||
|                     sequence_name = get_sequence_name(model._meta.db_table) | ||||
|                     output.append(query % {'sequence':sequence_name, | ||||
|                                            'table':model._meta.db_table}) | ||||
|                     break # Only one AutoField is allowed per model, so don't bother continuing. | ||||
|             for f in model._meta.many_to_many: | ||||
|                 sequence_name = get_sequence_name(f.m2m_db_table()) | ||||
|                 output.append(query % {'sequence':sequence_name, | ||||
|                                        'table':f.m2m_db_table()}) | ||||
|         return output | ||||
|  | ||||
| class DatabaseWrapper(BaseDatabaseWrapper): | ||||
|     ops = DatabaseOperations() | ||||
|  | ||||
| @@ -244,24 +261,6 @@ def get_sequence_name(table): | ||||
|     name_length = DatabaseOperations().max_name_length() - 3 | ||||
|     return '%s_SQ' % util.truncate_name(table, name_length).upper() | ||||
|  | ||||
| def get_sql_sequence_reset(style, model_list): | ||||
|     "Returns a list of the SQL statements to reset sequences for the given models." | ||||
|     from django.db import models | ||||
|     output = [] | ||||
|     query = _get_sequence_reset_sql() | ||||
|     for model in model_list: | ||||
|         for f in model._meta.fields: | ||||
|             if isinstance(f, models.AutoField): | ||||
|                 sequence_name = get_sequence_name(model._meta.db_table) | ||||
|                 output.append(query % {'sequence':sequence_name, | ||||
|                                        'table':model._meta.db_table}) | ||||
|                 break # Only one AutoField is allowed per model, so don't bother continuing. | ||||
|         for f in model._meta.many_to_many: | ||||
|             sequence_name = get_sequence_name(f.m2m_db_table()) | ||||
|             output.append(query % {'sequence':sequence_name, | ||||
|                                    'table':f.m2m_db_table()}) | ||||
|     return output | ||||
|  | ||||
| def get_trigger_name(table): | ||||
|     name_length = DatabaseOperations().max_name_length() - 3 | ||||
|     return '%s_TR' % util.truncate_name(table, name_length).upper() | ||||
|   | ||||
| @@ -124,6 +124,35 @@ class DatabaseOperations(BaseDatabaseOperations): | ||||
|         else: | ||||
|             return [] | ||||
|  | ||||
|     def sequence_reset_sql(self, style, model_list): | ||||
|         from django.db import models | ||||
|         output = [] | ||||
|         for model in model_list: | ||||
|             # Use `coalesce` to set the sequence for each model to the max pk value if there are records, | ||||
|             # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true | ||||
|             # if there are records (as the max pk value is already in use), otherwise set it to false. | ||||
|             for f in model._meta.fields: | ||||
|                 if isinstance(f, models.AutoField): | ||||
|                     output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ | ||||
|                         (style.SQL_KEYWORD('SELECT'), | ||||
|                         style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), | ||||
|                         style.SQL_FIELD(quote_name(f.column)), | ||||
|                         style.SQL_FIELD(quote_name(f.column)), | ||||
|                         style.SQL_KEYWORD('IS NOT'), | ||||
|                         style.SQL_KEYWORD('FROM'), | ||||
|                         style.SQL_TABLE(quote_name(model._meta.db_table)))) | ||||
|                     break # Only one AutoField is allowed per model, so don't bother continuing. | ||||
|             for f in model._meta.many_to_many: | ||||
|                 output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ | ||||
|                     (style.SQL_KEYWORD('SELECT'), | ||||
|                     style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), | ||||
|                     style.SQL_FIELD(quote_name('id')), | ||||
|                     style.SQL_FIELD(quote_name('id')), | ||||
|                     style.SQL_KEYWORD('IS NOT'), | ||||
|                     style.SQL_KEYWORD('FROM'), | ||||
|                     style.SQL_TABLE(f.m2m_db_table()))) | ||||
|         return output | ||||
|  | ||||
| class DatabaseWrapper(BaseDatabaseWrapper): | ||||
|     ops = DatabaseOperations() | ||||
|  | ||||
| @@ -185,36 +214,6 @@ def dictfetchall(cursor): | ||||
| def get_start_transaction_sql(): | ||||
|     return "BEGIN;" | ||||
|  | ||||
| def get_sql_sequence_reset(style, model_list): | ||||
|     "Returns a list of the SQL statements to reset sequences for the given models." | ||||
|     from django.db import models | ||||
|     output = [] | ||||
|     for model in model_list: | ||||
|         # Use `coalesce` to set the sequence for each model to the max pk value if there are records, | ||||
|         # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true | ||||
|         # if there are records (as the max pk value is already in use), otherwise set it to false. | ||||
|         for f in model._meta.fields: | ||||
|             if isinstance(f, models.AutoField): | ||||
|                 output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ | ||||
|                     (style.SQL_KEYWORD('SELECT'), | ||||
|                     style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), | ||||
|                     style.SQL_FIELD(quote_name(f.column)), | ||||
|                     style.SQL_FIELD(quote_name(f.column)), | ||||
|                     style.SQL_KEYWORD('IS NOT'), | ||||
|                     style.SQL_KEYWORD('FROM'), | ||||
|                     style.SQL_TABLE(quote_name(model._meta.db_table)))) | ||||
|                 break # Only one AutoField is allowed per model, so don't bother continuing. | ||||
|         for f in model._meta.many_to_many: | ||||
|             output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ | ||||
|                 (style.SQL_KEYWORD('SELECT'), | ||||
|                 style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), | ||||
|                 style.SQL_FIELD(quote_name('id')), | ||||
|                 style.SQL_FIELD(quote_name('id')), | ||||
|                 style.SQL_KEYWORD('IS NOT'), | ||||
|                 style.SQL_KEYWORD('FROM'), | ||||
|                 style.SQL_TABLE(f.m2m_db_table()))) | ||||
|     return output | ||||
|  | ||||
| def typecast_string(s): | ||||
|     """ | ||||
|     Cast all returned strings to unicode strings. | ||||
|   | ||||
| @@ -86,6 +86,35 @@ class DatabaseOperations(BaseDatabaseOperations): | ||||
|         else: | ||||
|             return [] | ||||
|  | ||||
|     def sequence_reset_sql(self, style, model_list): | ||||
|         from django.db import models | ||||
|         output = [] | ||||
|         for model in model_list: | ||||
|             # Use `coalesce` to set the sequence for each model to the max pk value if there are records, | ||||
|             # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true | ||||
|             # if there are records (as the max pk value is already in use), otherwise set it to false. | ||||
|             for f in model._meta.fields: | ||||
|                 if isinstance(f, models.AutoField): | ||||
|                     output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ | ||||
|                         (style.SQL_KEYWORD('SELECT'), | ||||
|                         style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), | ||||
|                         style.SQL_FIELD(quote_name(f.column)), | ||||
|                         style.SQL_FIELD(quote_name(f.column)), | ||||
|                         style.SQL_KEYWORD('IS NOT'), | ||||
|                         style.SQL_KEYWORD('FROM'), | ||||
|                         style.SQL_TABLE(quote_name(model._meta.db_table)))) | ||||
|                     break # Only one AutoField is allowed per model, so don't bother continuing. | ||||
|             for f in model._meta.many_to_many: | ||||
|                 output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ | ||||
|                     (style.SQL_KEYWORD('SELECT'), | ||||
|                     style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), | ||||
|                     style.SQL_FIELD(quote_name('id')), | ||||
|                     style.SQL_FIELD(quote_name('id')), | ||||
|                     style.SQL_KEYWORD('IS NOT'), | ||||
|                     style.SQL_KEYWORD('FROM'), | ||||
|                     style.SQL_TABLE(f.m2m_db_table()))) | ||||
|         return output | ||||
|  | ||||
| class DatabaseWrapper(BaseDatabaseWrapper): | ||||
|     ops = DatabaseOperations() | ||||
|  | ||||
| @@ -139,36 +168,6 @@ dictfetchall = util.dictfetchall | ||||
| def get_start_transaction_sql(): | ||||
|     return "BEGIN;" | ||||
|  | ||||
| def get_sql_sequence_reset(style, model_list): | ||||
|     "Returns a list of the SQL statements to reset sequences for the given models." | ||||
|     from django.db import models | ||||
|     output = [] | ||||
|     for model in model_list: | ||||
|         # Use `coalesce` to set the sequence for each model to the max pk value if there are records, | ||||
|         # or 1 if there are none. Set the `is_called` property (the third argument to `setval`) to true | ||||
|         # if there are records (as the max pk value is already in use), otherwise set it to false. | ||||
|         for f in model._meta.fields: | ||||
|             if isinstance(f, models.AutoField): | ||||
|                 output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ | ||||
|                     (style.SQL_KEYWORD('SELECT'), | ||||
|                     style.SQL_FIELD(quote_name('%s_%s_seq' % (model._meta.db_table, f.column))), | ||||
|                     style.SQL_FIELD(quote_name(f.column)), | ||||
|                     style.SQL_FIELD(quote_name(f.column)), | ||||
|                     style.SQL_KEYWORD('IS NOT'), | ||||
|                     style.SQL_KEYWORD('FROM'), | ||||
|                     style.SQL_TABLE(quote_name(model._meta.db_table)))) | ||||
|                 break # Only one AutoField is allowed per model, so don't bother continuing. | ||||
|         for f in model._meta.many_to_many: | ||||
|             output.append("%s setval('%s', coalesce(max(%s), 1), max(%s) %s null) %s %s;" % \ | ||||
|                 (style.SQL_KEYWORD('SELECT'), | ||||
|                 style.SQL_FIELD(quote_name('%s_id_seq' % f.m2m_db_table())), | ||||
|                 style.SQL_FIELD(quote_name('id')), | ||||
|                 style.SQL_FIELD(quote_name('id')), | ||||
|                 style.SQL_KEYWORD('IS NOT'), | ||||
|                 style.SQL_KEYWORD('FROM'), | ||||
|                 style.SQL_TABLE(f.m2m_db_table()))) | ||||
|     return output | ||||
|  | ||||
| OPERATOR_MAPPING = { | ||||
|     'exact': '= %s', | ||||
|     'iexact': 'ILIKE %s', | ||||
|   | ||||
| @@ -134,11 +134,6 @@ def _sqlite_extract(lookup_type, dt): | ||||
| def get_start_transaction_sql(): | ||||
|     return "BEGIN;" | ||||
|  | ||||
| def get_sql_sequence_reset(style, model_list): | ||||
|     "Returns a list of the SQL statements to reset sequences for the given models." | ||||
|     # No sequence reset required | ||||
|     return [] | ||||
|  | ||||
| def _sqlite_date_trunc(lookup_type, dt): | ||||
|     try: | ||||
|         dt = util.typecast_timestamp(dt) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user