diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
index 6b2ecae..deb4ce6 100644
a
|
b
|
class DatabaseOperations(BaseDatabaseOperations):
|
269 | 269 | # With MySQLdb, cursor objects have an (undocumented) "_last_executed" |
270 | 270 | # attribute where the exact query sent to the database is saved. |
271 | 271 | # See MySQLdb/cursors.py in the source distribution. |
272 | | return cursor._last_executed.decode('utf-8') |
| 272 | def to_unicode(chunk): |
| 273 | try: |
| 274 | return chunk.decode('utf-8') |
| 275 | except UnicodeDecodeError: |
| 276 | return chunk.decode('unicode-escape') |
| 277 | try: |
| 278 | return cursor._last_executed.decode('utf-8') |
| 279 | except UnicodeDecodeError: |
| 280 | return ' '.join(to_unicode(chunk) for chunk in cursor._last_executed.split(b' ')) |
273 | 281 | |
274 | 282 | def no_limit_value(self): |
275 | 283 | # 2**64 - 1, as recommended by the MySQL documentation |
diff --git a/tests/backends/models.py b/tests/backends/models.py
index 5876cbe..036be93 100644
a
|
b
|
class Post(models.Model):
|
62 | 62 | class Reporter(models.Model): |
63 | 63 | first_name = models.CharField(max_length=30) |
64 | 64 | last_name = models.CharField(max_length=30) |
| 65 | raw_data = models.BinaryField() |
65 | 66 | |
66 | 67 | def __str__(self): |
67 | 68 | return "%s %s" % (self.first_name, self.last_name) |
diff --git a/tests/backends/tests.py b/tests/backends/tests.py
index 7c68863..59dace2 100644
a
|
b
|
class DateQuotingTest(TestCase):
|
157 | 157 | class LastExecutedQueryTest(TestCase): |
158 | 158 | |
159 | 159 | def test_debug_sql(self): |
160 | | list(models.Tag.objects.filter(name="test")) |
| 160 | list(models.Reporter.objects.filter(first_name="test")) |
161 | 161 | sql = connection.queries[-1]['sql'].lower() |
162 | 162 | self.assertIn("select", sql) |
163 | | self.assertIn(models.Tag._meta.db_table, sql) |
| 163 | self.assertIn(models.Reporter._meta.db_table, sql) |
164 | 164 | |
165 | 165 | def test_query_encoding(self): |
166 | 166 | """ |
167 | 167 | Test that last_executed_query() returns an Unicode string |
168 | 168 | """ |
169 | | tags = models.Tag.objects.extra(select={'föö': 1}) |
170 | | sql, params = tags.query.sql_with_params() |
171 | | cursor = tags.query.get_compiler('default').execute_sql(None) |
| 169 | persons = models.Reporter.objects.filter(raw_data=b'\x00\x46 \xFE').extra(select={'föö': 1}) |
| 170 | sql, params = persons.query.sql_with_params() |
| 171 | cursor = persons.query.get_compiler('default').execute_sql(None) |
172 | 172 | last_sql = cursor.db.ops.last_executed_query(cursor, sql, params) |
173 | 173 | self.assertTrue(isinstance(last_sql, six.text_type)) |
174 | 174 | |