summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorAymeric Augustin <aymeric.augustin@m4x.org>2015-09-13 09:30:35 +0200
committerAymeric Augustin <aymeric.augustin@m4x.org>2015-09-17 23:01:33 +0200
commit4f6a7663bcddffb114f2647f9928cbf1fdd8e4b5 (patch)
tree648c2ae602182f4ae41095ddd40a8515cf5a1ced /django
parentfc8a6a9b002aef90ff68f3d95e560db1ea728c76 (diff)
Refs #14091 -- Fixed connection.queries on SQLite.
Diffstat (limited to 'django')
-rw-r--r--django/db/backends/sqlite3/operations.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/django/db/backends/sqlite3/operations.py b/django/db/backends/sqlite3/operations.py
index 895252ef9c..7f99eaa271 100644
--- a/django/db/backends/sqlite3/operations.py
+++ b/django/db/backends/sqlite3/operations.py
@@ -103,6 +103,39 @@ class DatabaseOperations(BaseDatabaseOperations):
def pk_default_value(self):
return "NULL"
+ def _quote_params_for_last_executed_query(self, params):
+ """
+ Only for last_executed_query! Don't use this to execute SQL queries!
+ """
+ sql = 'SELECT ' + ', '.join(['QUOTE(?)'] * len(params))
+ # Bypass Django's wrappers and use the underlying sqlite3 connection
+ # to avoid logging this query - it would trigger infinite recursion.
+ cursor = self.connection.connection.cursor()
+ # Native sqlite3 cursors cannot be used as context managers.
+ try:
+ return cursor.execute(sql, params).fetchone()
+ finally:
+ cursor.close()
+
+ def last_executed_query(self, cursor, sql, params):
+ # Python substitutes parameters in Modules/_sqlite/cursor.c with:
+ # pysqlite_statement_bind_parameters(self->statement, parameters, allow_8bit_chars);
+ # Unfortunately there is no way to reach self->statement from Python,
+ # so we quote and substitute parameters manually.
+ if params:
+ if isinstance(params, (list, tuple)):
+ params = self._quote_params_for_last_executed_query(params)
+ else:
+ keys = params.keys()
+ values = tuple(params.values())
+ values = self._quote_params_for_last_executed_query(values)
+ params = dict(zip(keys, values))
+ return sql % params
+ # For consistency with SQLiteCursorWrapper.execute(), just return sql
+ # when there are no parameters. See #13648 and #17158.
+ else:
+ return sql
+
def quote_name(self, name):
if name.startswith('"') and name.endswith('"'):
return name # Quoting once is enough.