Skip to content

Commit

Permalink
Fixed #17158 -- Used a non-ambiguous representation of SQL queries
Browse files Browse the repository at this point in the history
when a correct representation cannot be obtained.
  • Loading branch information
aaugustin committed Jan 26, 2013
1 parent d18893d commit 6605ac3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 3 additions & 3 deletions django/db/backends/__init__.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -614,11 +614,11 @@ def last_executed_query(self, cursor, sql, params):
# 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):
""" """
Expand Down
18 changes: 14 additions & 4 deletions tests/regressiontests/backends/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
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
Expand Down Expand Up @@ -160,24 +160,34 @@ def test_django_extract(self):
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):
Expand Down

0 comments on commit 6605ac3

Please sign in to comment.