Ticket #18592: 18592.diff

File 18592.diff, 2.4 KB (added by Claude Paroz, 12 years ago)

Graceful failure for MySQL _last_executed

  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index 91dc4d2..c34481f 100644
    a b class DatabaseOperations(BaseDatabaseOperations):  
    234234        # With MySQLdb, cursor objects have an (undocumented) "_last_executed"
    235235        # attribute where the exact query sent to the database is saved.
    236236        # See MySQLdb/cursors.py in the source distribution.
    237         return cursor._last_executed
     237        return getattr(cursor, '_last_executed', None)
    238238
    239239    def no_limit_value(self):
    240240        # 2**64 - 1, as recommended by the MySQL documentation
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index 193d01b..988797a 100644
    a b class DateQuotingTest(TestCase):  
    104104
    105105class LastExecutedQueryTest(TestCase):
    106106
    107     def setUp(self):
    108         # connection.queries will not be filled in without this
    109         settings.DEBUG = True
    110 
    111     def tearDown(self):
    112         settings.DEBUG = False
     107    def test_last_executed_query(self):
     108        """
     109        last_executed_query should not raise an exception even if no previous
     110        query has been run.
     111        """
     112        cursor = connection.cursor()
     113        try:
     114            connection.ops.last_executed_query(cursor, '', ())
     115        except Exception:
     116            self.fail("'last_executed_query' should not raise an exception.")
    113117
    114118    # There are no tests for the sqlite backend because it does not
    115119    # implement paramater escaping. See #14091.
    116120
    117121    @unittest.skipUnless(connection.vendor in ('oracle', 'postgresql'),
    118122                         "These backends use the standard parameter escaping rules")
     123    @override_settings(DEBUG=True) # so as connection.queries will be filled
    119124    def test_parameter_escaping(self):
    120125        # check that both numbers and string are properly quoted
    121126        list(models.Tag.objects.filter(name="special:\\\"':", object_id=12))
    class LastExecutedQueryTest(TestCase):  
    125130
    126131    @unittest.skipUnless(connection.vendor == 'mysql',
    127132                         "MySQL uses backslashes to escape parameters.")
     133    @override_settings(DEBUG=True)
    128134    def test_parameter_escaping(self):
    129135        list(models.Tag.objects.filter(name="special:\\\"':", object_id=12))
    130136        sql = connection.queries[-1]['sql']
Back to Top