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

when a correct representation cannot be obtained.
This commit is contained in:
Aymeric Augustin 2013-01-26 17:51:44 +01:00
parent d18893d5ce
commit 6605ac331a
2 changed files with 17 additions and 7 deletions

View File

@ -614,11 +614,11 @@ class BaseDatabaseOperations(object):
# Convert params to contain Unicode values. # Convert params to contain Unicode values.
to_unicode = lambda s: force_text(s, strings_only=True, errors='replace') to_unicode = lambda s: force_text(s, strings_only=True, errors='replace')
if isinstance(params, (list, tuple)): 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: 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): def last_insert_id(self, cursor, table_name, pk_name):
""" """

View File

@ -16,7 +16,7 @@ from django.db.models import fields, Sum, Avg, Variance, StdDev
from django.db.utils import ConnectionHandler, DatabaseError, load_backend from django.db.utils import ConnectionHandler, DatabaseError, load_backend
from django.test import (TestCase, skipUnlessDBFeature, skipIfDBFeature, from django.test import (TestCase, skipUnlessDBFeature, skipIfDBFeature,
TransactionTestCase) 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 import six
from django.utils.six.moves import xrange from django.utils.six.moves import xrange
from django.utils import unittest from django.utils import unittest
@ -160,24 +160,34 @@ class DateQuotingTest(TestCase):
self.assertEqual(len(classes), 1) self.assertEqual(len(classes), 1)
@override_settings(DEBUG=True)
class LastExecutedQueryTest(TestCase): class LastExecutedQueryTest(TestCase):
@override_settings(DEBUG=True)
def test_debug_sql(self): def test_debug_sql(self):
list(models.Tag.objects.filter(name="test")) list(models.Tag.objects.filter(name="test"))
sql = connection.queries[-1]['sql'].lower() sql = connection.queries[-1]['sql'].lower()
self.assertTrue(sql.startswith("select")) self.assertIn("select", sql)
self.assertIn(models.Tag._meta.db_table, sql) self.assertIn(models.Tag._meta.db_table, sql)
def test_query_encoding(self): def test_query_encoding(self):
""" """
Test that last_executed_query() returns an Unicode string 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() sql, params = tags.query.sql_with_params()
cursor = tags.query.get_compiler('default').execute_sql(None) cursor = tags.query.get_compiler('default').execute_sql(None)
last_sql = cursor.db.ops.last_executed_query(cursor, sql, params) last_sql = cursor.db.ops.last_executed_query(cursor, sql, params)
self.assertTrue(isinstance(last_sql, six.text_type)) 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): class ParameterHandlingTest(TestCase):
def test_bad_parameter_count(self): def test_bad_parameter_count(self):