Changeset 5964
- Timestamp:
- 08/19/07 19:21:10 (1 year ago)
- Files:
-
- django/trunk/django/core/management/commands/loaddata.py (modified) (2 diffs)
- django/trunk/django/core/management/commands/sqlsequencereset.py (modified) (1 diff)
- django/trunk/django/db/backends/ado_mssql/base.py (modified) (1 diff)
- django/trunk/django/db/backends/dummy/base.py (modified) (1 diff)
- django/trunk/django/db/backends/__init__.py (modified) (1 diff)
- django/trunk/django/db/backends/mysql/base.py (modified) (1 diff)
- django/trunk/django/db/backends/mysql_old/base.py (modified) (1 diff)
- django/trunk/django/db/backends/oracle/base.py (modified) (2 diffs)
- django/trunk/django/db/backends/postgresql/base.py (modified) (2 diffs)
- django/trunk/django/db/backends/postgresql_psycopg2/base.py (modified) (2 diffs)
- django/trunk/django/db/backends/sqlite3/base.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/core/management/commands/loaddata.py
r5913 r5964 16 16 from django.db.models import get_apps 17 17 from django.core import serializers 18 from django.db import connection, transaction , backend18 from django.db import connection, transaction 19 19 from django.conf import settings 20 20 … … 106 106 107 107 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) 109 109 if sequence_sql: 110 110 if verbosity > 1: django/trunk/django/core/management/commands/sqlsequencereset.py
r5898 r5964 6 6 7 7 def handle_app(self, app, **options): 8 from django.db import backend, models9 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 110 110 return "ON %s" % quote_name(tablespace) 111 111 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 required115 return []116 117 112 OPERATOR_MAPPING = { 118 113 'exact': '= %s', django/trunk/django/db/backends/dummy/base.py
r5963 r5964 45 45 dictfetchall = complain 46 46 get_start_transaction_sql = complain 47 get_sql_sequence_reset = complain48 47 49 48 OPERATOR_MAPPING = {} django/trunk/django/db/backends/__init__.py
r5963 r5964 151 151 """ 152 152 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 192 192 return "BEGIN;" 193 193 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 required197 return []198 199 194 OPERATOR_MAPPING = { 200 195 'exact': '= %s', django/trunk/django/db/backends/mysql_old/base.py
r5963 r5964 211 211 return "BEGIN;" 212 212 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 required216 return []217 218 213 OPERATOR_MAPPING = { 219 214 'exact': '= %s', django/trunk/django/db/backends/oracle/base.py
r5963 r5964 96 96 return [] 97 97 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 98 115 class DatabaseWrapper(BaseDatabaseWrapper): 99 116 ops = DatabaseOperations() … … 244 261 name_length = DatabaseOperations().max_name_length() - 3 245 262 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 models250 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 output264 263 265 264 def get_trigger_name(table): django/trunk/django/db/backends/postgresql/base.py
r5963 r5964 125 125 return [] 126 126 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 127 156 class DatabaseWrapper(BaseDatabaseWrapper): 128 157 ops = DatabaseOperations() … … 186 215 return "BEGIN;" 187 216 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 models191 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 true195 # 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 output217 218 217 def typecast_string(s): 219 218 """ django/trunk/django/db/backends/postgresql_psycopg2/base.py
r5963 r5964 87 87 return [] 88 88 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 89 118 class DatabaseWrapper(BaseDatabaseWrapper): 90 119 ops = DatabaseOperations() … … 140 169 return "BEGIN;" 141 170 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 models145 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 true149 # 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 output171 172 171 OPERATOR_MAPPING = { 173 172 'exact': '= %s', django/trunk/django/db/backends/sqlite3/base.py
r5963 r5964 135 135 return "BEGIN;" 136 136 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 required140 return []141 142 137 def _sqlite_date_trunc(lookup_type, dt): 143 138 try:
