Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #17158 -- Used a non-ambiguous representation of SQL queries

when a correct representation cannot be obtained.
  • Loading branch information...
commit 6605ac331a9e03fa41c301d122c5727c0d98b970 1 parent d18893d
@aaugustin aaugustin authored
View
6 django/db/backends/__init__.py
@@ -614,11 +614,11 @@ def last_executed_query(self, cursor, sql, params):
# Convert params to contain Unicode values.
to_unicode = lambda s: force_text(s, strings_only=True, errors='replace')
if isinstance(params, (list, tuple)):
- u_params = tuple([to_unicode(val) for val in params])
+ u_params = tuple(to_unicode(val) for val in params)
else:
- u_params = dict([(to_unicode(k), to_unicode(v)) for k, v in params.items()])
+ u_params = dict((to_unicode(k), to_unicode(v)) for k, v in params.items())
- return force_text(sql) % u_params
+ return six.text_type("QUERY = %r - PARAMS = %r") % (sql, u_params)
def last_insert_id(self, cursor, table_name, pk_name):
"""
View
18 tests/regressiontests/backends/tests.py
@@ -16,7 +16,7 @@
from django.db.utils import ConnectionHandler, DatabaseError, load_backend
from django.test import (TestCase, skipUnlessDBFeature, skipIfDBFeature,
TransactionTestCase)
-from django.test.utils import override_settings
+from django.test.utils import override_settings, str_prefix
from django.utils import six
from django.utils.six.moves import xrange
from django.utils import unittest
@@ -160,24 +160,34 @@ def test_django_extract(self):
self.assertEqual(len(classes), 1)
+@override_settings(DEBUG=True)
class LastExecutedQueryTest(TestCase):
- @override_settings(DEBUG=True)
+
def test_debug_sql(self):
list(models.Tag.objects.filter(name="test"))
sql = connection.queries[-1]['sql'].lower()
- self.assertTrue(sql.startswith("select"))
+ self.assertIn("select", sql)
self.assertIn(models.Tag._meta.db_table, sql)
def test_query_encoding(self):
"""
Test that last_executed_query() returns an Unicode string
"""
- tags = models.Tag.objects.extra(select={'föö':1})
+ tags = models.Tag.objects.extra(select={'föö': 1})
sql, params = tags.query.sql_with_params()
cursor = tags.query.get_compiler('default').execute_sql(None)
last_sql = cursor.db.ops.last_executed_query(cursor, sql, params)
self.assertTrue(isinstance(last_sql, six.text_type))
+ @unittest.skipUnless(connection.vendor == 'sqlite',
+ "This test is specific to SQLite.")
+ def test_no_interpolation_on_sqlite(self):
+ # Regression for #17158
+ # This shouldn't raise an exception
+ query = "SELECT strftime('%Y', 'now');"
+ connection.cursor().execute(query)
+ self.assertEqual(connection.queries[-1]['sql'],
+ str_prefix("QUERY = %(_)s\"SELECT strftime('%%Y', 'now');\" - PARAMS = ()"))
class ParameterHandlingTest(TestCase):
def test_bad_parameter_count(self):
Please sign in to comment.
Something went wrong with that request. Please try again.