Ticket #1763: db_fix.patch
File db_fix.patch, 21.6 KB (added by , 18 years ago) |
---|
-
django/db/models/base.py
7 7 from django.db.models.related import RelatedObject 8 8 from django.db.models.query import orderlist2sql, delete_objects 9 9 from django.db.models.options import Options, AdminOptions 10 from django.db import connection, backend, transaction 10 from django.db import backend, transaction 11 import django.db 11 12 from django.db.models import signals 12 13 from django.db.models.loading import register_models 13 14 from django.dispatch import dispatcher … … 149 150 dispatcher.send(signal=signals.pre_save, sender=self.__class__, instance=self) 150 151 151 152 non_pks = [f for f in self._meta.fields if not f.primary_key] 152 cursor = connection.cursor()153 cursor = django.db.connection.cursor() 153 154 154 155 # First, try an UPDATE. If that doesn't update anything, do an INSERT. 155 156 pk_val = self._get_pk_val() … … 361 362 rel = rel_field.rel.to 362 363 m2m_table = rel_field.m2m_db_table() 363 364 this_id = self._get_pk_val() 364 cursor = connection.cursor()365 cursor = django.db.connection.cursor() 365 366 cursor.execute("DELETE FROM %s WHERE %s = %%s" % \ 366 367 (backend.quote_name(m2m_table), 367 368 backend.quote_name(rel_field.m2m_column_name())), [this_id]) … … 379 380 # ORDERING METHODS ######################### 380 381 381 382 def method_set_order(ordered_obj, self, id_list): 382 cursor = connection.cursor()383 cursor = django.db.connection.cursor() 383 384 # Example: "UPDATE poll_choices SET _order = %s WHERE poll_id = %s AND id = %s" 384 385 sql = "UPDATE %s SET %s = %%s WHERE %s = %%s AND %s = %%s" % \ 385 386 (backend.quote_name(ordered_obj.db_table), backend.quote_name('_order'), … … 390 391 transaction.commit_unless_managed() 391 392 392 393 def method_get_order(ordered_obj, self): 393 cursor = connection.cursor()394 cursor = django.db.connection.cursor() 394 395 # Example: "SELECT id FROM poll_choices WHERE poll_id = %s ORDER BY _order" 395 396 sql = "SELECT %s FROM %s WHERE %s = %%s ORDER BY %s" % \ 396 397 (backend.quote_name(ordered_obj._meta.pk.column), -
django/db/models/manager.py
1 1 from django.utils.functional import curry 2 from django.db import backend , connection2 from django.db import backend 3 3 from django.db.models.query import QuerySet 4 4 from django.dispatch import dispatcher 5 5 from django.db.models import signals -
django/db/models/fields/related.py
1 from django.db import backend, connection,transaction1 from django.db import backend, transaction 2 2 from django.db.models import signals 3 3 from django.db.models.fields import AutoField, Field, IntegerField, get_ul_class 4 4 from django.db.models.related import RelatedObject … … 7 7 from django.core import validators 8 8 from django import forms 9 9 from django.dispatch import dispatcher 10 import django.db 10 11 11 12 # For Python 2.3 12 13 if not hasattr(__builtins__, 'set'): … … 282 283 # source_col_name: the PK colname in join_table for the source object 283 284 # target_col_name: the PK colname in join_table for the target object 284 285 # *objs - objects to add 285 from django.db import connection286 286 287 287 # Add the newly created or already existing objects to the join table. 288 288 # First find out which items are already added, to avoid adding them twice 289 289 new_ids = set([obj._get_pk_val() for obj in objs]) 290 cursor = connection.cursor()290 cursor = django.db.connection.cursor() 291 291 cursor.execute("SELECT %s FROM %s WHERE %s = %%s AND %s IN (%s)" % \ 292 292 (target_col_name, self.join_table, source_col_name, 293 293 target_col_name, ",".join(['%s'] * len(new_ids))), … … 308 308 # source_col_name: the PK colname in join_table for the source object 309 309 # target_col_name: the PK colname in join_table for the target object 310 310 # *objs - objects to remove 311 from django.db import connection312 311 313 312 for obj in objs: 314 313 if not isinstance(obj, self.model): 315 314 raise ValueError, "objects to remove() must be %s instances" % self.model._meta.object_name 316 315 # Remove the specified objects from the join table 317 cursor = connection.cursor()316 cursor = django.db.connection.cursor() 318 317 for obj in objs: 319 318 cursor.execute("DELETE FROM %s WHERE %s = %%s AND %s = %%s" % \ 320 319 (self.join_table, source_col_name, target_col_name), … … 323 322 324 323 def _clear_items(self, source_col_name): 325 324 # source_col_name: the PK colname in join_table for the source object 326 from django.db import connection 327 cursor = connection.cursor() 325 cursor = django.db.connection.cursor() 328 326 cursor.execute("DELETE FROM %s WHERE %s = %%s" % \ 329 327 (self.join_table, source_col_name), 330 328 [self._pk_val]) -
django/db/models/__init__.py
1 1 from django.conf import settings 2 2 from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured 3 3 from django.core import validators 4 from django.db import backend , connection4 from django.db import backend 5 5 from django.db.models.loading import get_apps, get_app, get_models, get_model, register_models 6 6 from django.db.models.query import Q 7 7 from django.db.models.manager import Manager -
django/db/models/query.py
1 from django.db import backend, connection, transaction 1 from django.db import backend, transaction 2 import django.db 2 3 from django.db.models.fields import DateField, FieldDoesNotExist 3 4 from django.db.models import signals 4 5 from django.dispatch import dispatcher … … 158 159 # undefined, so we convert it to a list of tuples. 159 160 extra_select = self._select.items() 160 161 161 cursor = connection.cursor()162 cursor = django.db.connection.cursor() 162 163 select, sql, params = self._get_sql_clause() 163 164 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) 164 165 fill_cache = self._select_related … … 184 185 counter._limit = None 185 186 counter._select_related = False 186 187 select, sql, params = counter._get_sql_clause() 187 cursor = connection.cursor()188 cursor = django.db.connection.cursor() 188 189 if self._distinct: 189 190 id_col = "%s.%s" % (backend.quote_name(self.model._meta.db_table), 190 191 backend.quote_name(self.model._meta.pk.column)) … … 488 489 columns = [f.column for f in self.model._meta.fields] 489 490 field_names = [f.attname for f in self.model._meta.fields] 490 491 491 cursor = connection.cursor()492 cursor = django.db.connection.cursor() 492 493 select, sql, params = self._get_sql_clause() 493 494 select = ['%s.%s' % (backend.quote_name(self.model._meta.db_table), backend.quote_name(c)) for c in columns] 494 495 cursor.execute("SELECT " + (self._distinct and "DISTINCT " or "") + ",".join(select) + sql, params) … … 515 516 sql = 'SELECT %s %s GROUP BY 1 ORDER BY 1 %s' % \ 516 517 (backend.get_date_trunc_sql(self._kind, '%s.%s' % (backend.quote_name(self.model._meta.db_table), 517 518 backend.quote_name(self._field.column))), sql, self._order) 518 cursor = connection.cursor()519 cursor = django.db.connection.cursor() 519 520 cursor.execute(sql, params) 520 521 # We have to manually run typecast_timestamp(str()) on the results, because 521 522 # MySQL doesn't automatically cast the result of date functions as datetime … … 863 864 ordered_classes = seen_objs.keys() 864 865 ordered_classes.reverse() 865 866 866 cursor = connection.cursor()867 cursor = django.db.connection.cursor() 867 868 868 869 for cls in ordered_classes: 869 870 seen_objs[cls] = seen_objs[cls].items() -
django/db/transaction.py
16 16 import thread 17 17 except ImportError: 18 18 import dummy_thread as thread 19 from django.db import connection 19 import django.db 20 20 from django.conf import settings 21 21 22 22 class TransactionManagementError(Exception): … … 123 123 if top: 124 124 top[-1] = flag 125 125 if not flag and is_dirty(): 126 connection._commit()126 django.db.connection._commit() 127 127 set_clean() 128 128 else: 129 129 raise TransactionManagementError("This code isn't under transaction management") … … 133 133 Commits changes if the system is not in managed transaction mode. 134 134 """ 135 135 if not is_managed(): 136 connection._commit()136 django.db.connection._commit() 137 137 else: 138 138 set_dirty() 139 139 … … 142 142 Rolls back changes if the system is not in managed transaction mode. 143 143 """ 144 144 if not is_managed(): 145 connection._rollback()145 django.db.connection._rollback() 146 146 else: 147 147 set_dirty() 148 148 … … 150 150 """ 151 151 Does the commit itself and resets the dirty flag. 152 152 """ 153 connection._commit()153 django.db.connection._commit() 154 154 set_clean() 155 155 156 156 def rollback(): 157 157 """ 158 158 This function does the rollback itself and resets the dirty flag. 159 159 """ 160 connection._rollback()160 django.db.connection._rollback() 161 161 set_clean() 162 162 163 163 ############## -
django/core/context_processors.py
25 25 context_extras = {} 26 26 if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: 27 27 context_extras['debug'] = True 28 from django.db import connection29 context_extras['sql_queries'] = connection.queries28 import django.db 29 context_extras['sql_queries'] = django.db.connection.queries 30 30 return context_extras 31 31 32 32 def i18n(request): -
django/core/cache/backends/db.py
1 1 "Database cache backend." 2 2 3 3 from django.core.cache.backends.base import BaseCache 4 from django.db import connection,transaction4 from django.db import transaction 5 5 import base64, time 6 6 from datetime import datetime 7 7 try: … … 25 25 self._cull_frequency = 3 26 26 27 27 def get(self, key, default=None): 28 cursor = connection.cursor()28 cursor = django.db.connection.cursor() 29 29 cursor.execute("SELECT cache_key, value, expires FROM %s WHERE cache_key = %%s" % self._table, [key]) 30 30 row = cursor.fetchone() 31 31 if row is None: … … 40 40 def set(self, key, value, timeout=None): 41 41 if timeout is None: 42 42 timeout = self.default_timeout 43 cursor = connection.cursor()43 cursor = django.db.connection.cursor() 44 44 cursor.execute("SELECT COUNT(*) FROM %s" % self._table) 45 45 num = cursor.fetchone()[0] 46 46 now = datetime.now().replace(microsecond=0) … … 61 61 transaction.commit_unless_managed() 62 62 63 63 def delete(self, key): 64 cursor = connection.cursor()64 cursor = django.db.connection.cursor() 65 65 cursor.execute("DELETE FROM %s WHERE cache_key = %%s" % self._table, [key]) 66 66 transaction.commit_unless_managed() 67 67 68 68 def has_key(self, key): 69 cursor = connection.cursor()69 cursor = django.db.connection.cursor() 70 70 cursor.execute("SELECT cache_key FROM %s WHERE cache_key = %%s" % self._table, [key]) 71 71 return cursor.fetchone() is not None 72 72 -
django/core/management.py
63 63 64 64 def _get_table_list(): 65 65 "Gets a list of all db tables that are physically installed." 66 from django.db import connection,get_introspection_module67 cursor = connection.cursor()66 from django.db import get_introspection_module 67 cursor = django.db.connection.cursor() 68 68 return get_introspection_module().get_table_list(cursor) 69 69 70 70 # If the foreign key points to an AutoField, a PositiveIntegerField or a … … 244 244 245 245 def get_sql_delete(app): 246 246 "Returns a list of the DROP TABLE SQL statements for the given app." 247 from django.db import backend, connection,models, get_introspection_module247 from django.db import backend, models, get_introspection_module 248 248 introspection = get_introspection_module() 249 249 250 250 # This should work even if a connecton isn't available 251 251 try: 252 cursor = connection.cursor()252 cursor = django.db.connection.cursor() 253 253 except: 254 254 cursor = None 255 255 … … 308 308 # directly into a database client, to avoid locking issues. 309 309 if cursor: 310 310 cursor.close() 311 connection.close()311 django.db.connection.close() 312 312 313 313 return output[::-1] # Reverse it, to deal with table dependencies. 314 314 get_sql_delete.help_doc = "Prints the DROP TABLE SQL statements for the given app name(s)." … … 419 419 420 420 def syncdb(): 421 421 "Creates the database tables for all apps in INSTALLED_APPS whose tables haven't already been created." 422 from django.db import connection,transaction, models, get_creation_module422 from django.db import transaction, models, get_creation_module 423 423 from django.db.models import signals 424 424 from django.conf import settings 425 425 from django.dispatch import dispatcher … … 439 439 440 440 data_types = get_creation_module().DATA_TYPES 441 441 442 cursor = connection.cursor()442 cursor = django.db.connection.cursor() 443 443 444 444 # Get a list of all existing database tables, 445 445 # so we know what needs to be added. … … 554 554 555 555 def install(app): 556 556 "Executes the equivalent of 'get_sql_all' in the current database." 557 from django.db import connection,transaction557 from django.db import transaction 558 558 559 559 app_name = app.__name__.split('.')[-2] 560 560 … … 566 566 sql_list = get_sql_all(app) 567 567 568 568 try: 569 cursor = connection.cursor()569 cursor = django.db.connection.cursor() 570 570 for sql in sql_list: 571 571 cursor.execute(sql) 572 572 except Exception, e: … … 584 584 585 585 def reset(app): 586 586 "Executes the equivalent of 'get_sql_reset' in the current database." 587 from django.db import connection,transaction587 from django.db import transaction 588 588 from cStringIO import StringIO 589 589 app_name = app.__name__.split('.')[-2] 590 590 … … 602 602 Type 'yes' to continue, or 'no' to cancel: """) 603 603 if confirm == 'yes': 604 604 try: 605 cursor = connection.cursor()605 cursor = django.db.connection.cursor() 606 606 for sql in sql_list: 607 607 cursor.execute(sql) 608 608 except Exception, e: … … 684 684 685 685 def inspectdb(): 686 686 "Generator that introspects the tables in the given database name and returns a Django model, one line at a time." 687 from django.db import connection,get_introspection_module687 from django.db import get_introspection_module 688 688 from django.conf import settings 689 689 import keyword 690 690 … … 694 694 object_name = table_name.title().replace('_', '') 695 695 return object_name.endswith('s') and object_name[:-1] or object_name 696 696 697 cursor = connection.cursor()697 cursor = django.db.connection.cursor() 698 698 yield "# This is an auto-generated Django model module." 699 699 yield "# You'll have to do the following manually to clean this up:" 700 700 yield "# * Rearrange models' order" … … 1014 1014 1015 1015 def createcachetable(tablename): 1016 1016 "Creates the table needed to use the SQL cache backend" 1017 from django.db import backend, connection,transaction, get_creation_module, models1017 from django.db import backend, transaction, get_creation_module, models 1018 1018 data_types = get_creation_module().DATA_TYPES 1019 1019 fields = ( 1020 1020 # "key" is a reserved word in MySQL, so use "cache_key" instead. … … 1041 1041 for i, line in enumerate(table_output): 1042 1042 full_statement.append(' %s%s' % (line, i < len(table_output)-1 and ',' or '')) 1043 1043 full_statement.append(');') 1044 curs = connection.cursor()1044 curs = django.db.connection.cursor() 1045 1045 curs.execute("\n".join(full_statement)) 1046 1046 for statement in index_output: 1047 1047 curs.execute(statement) -
django/contrib/auth/models.py
1 1 from django.core import validators 2 from django.db import backend, connection, models 2 from django.db import backend, models 3 import django.db 3 4 from django.contrib.contenttypes.models import ContentType 4 5 from django.utils.translation import gettext_lazy as _ 5 6 import datetime … … 130 131 "Returns a list of permission strings that this user has through his/her groups." 131 132 if not hasattr(self, '_group_perm_cache'): 132 133 import sets 133 cursor = connection.cursor()134 cursor = django.db.connection.cursor() 134 135 # The SQL below works out to the following, after DB quoting: 135 136 # cursor.execute(""" 136 137 # SELECT ct."app_label", p."codename" -
django/bin/daily_cleanup.py
1 1 "Daily cleanup file" 2 2 3 from django.db import backend, connection, transaction 3 from django.db import backend, transaction 4 import django.db 4 5 5 6 DOCUMENTATION_DIRECTORY = '/home/html/documentation/' 6 7 7 8 def clean_up(): 8 9 # Clean up old database records 9 cursor = connection.cursor()10 cursor = django.db.connection.cursor() 10 11 cursor.execute("DELETE FROM %s WHERE %s < NOW()" % \ 11 12 (backend.quote_name('core_sessions'), backend.quote_name('expire_date'))) 12 13 cursor.execute("DELETE FROM %s WHERE %s < NOW() - INTERVAL '1 week'" % \ -
tests/modeltests/custom_methods/models.py
25 25 Verbose version of get_articles_from_same_day_1, which does a custom 26 26 database query for the sake of demonstration. 27 27 """ 28 from django.db import connection29 cursor = connection.cursor()28 import django.db 29 cursor = django.db.connection.cursor() 30 30 cursor.execute(""" 31 31 SELECT id, headline, pub_date 32 32 FROM custom_methods_article -
tests/runtests.py
94 94 # Manually set DEBUG = False. 95 95 settings.DEBUG = False 96 96 97 from django.db import connection97 import django.db 98 98 from django.core import management 99 99 import django.db.models 100 100 … … 128 128 # Create the test database and connect to it. We need to autocommit 129 129 # if the database supports it because PostgreSQL doesn't allow 130 130 # CREATE/DROP DATABASE statements within transactions. 131 cursor = connection.cursor()132 self._set_autocommit( connection)131 cursor = django.db.connection.cursor() 132 self._set_autocommit(django.db.connection) 133 133 self.output(1, "Creating test database") 134 134 try: 135 135 cursor.execute("CREATE DATABASE %s" % TEST_DATABASE_NAME) … … 142 142 else: 143 143 print "Tests cancelled." 144 144 return 145 connection.close()145 django.db.connection.close() 146 146 old_database_name = settings.DATABASE_NAME 147 147 settings.DATABASE_NAME = TEST_DATABASE_NAME 148 148 149 149 # Initialize the test database. 150 cursor = connection.cursor()150 cursor = django.db.connection.cursor() 151 151 152 152 # Run the tests for each test model. 153 153 self.output(1, "Running app tests") … … 228 228 # to do so, because it's not allowed to delete a database while being 229 229 # connected to it. 230 230 if settings.DATABASE_ENGINE != "sqlite3": 231 connection.close()231 django.db.connection.close() 232 232 settings.DATABASE_NAME = old_database_name 233 cursor = connection.cursor()233 cursor = django.db.connection.cursor() 234 234 self.output(1, "Deleting test database") 235 self._set_autocommit( connection)235 self._set_autocommit(django.db.connection) 236 236 time.sleep(1) # To avoid "database is being accessed by other users" errors. 237 237 cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME) 238 238