summaryrefslogtreecommitdiff
path: root/django/db/backends/utils.py
diff options
context:
space:
mode:
authorShai Berger <shai@platonix.com>2017-09-21 19:13:09 +0300
committerTim Graham <timograham@gmail.com>2017-09-21 12:13:09 -0400
commitd612026c37f85769322a44c0284ca2d2be6b6e5b (patch)
tree3b8e13157e72dba25878837d055706f135181ba1 /django/db/backends/utils.py
parent347551c2a1bc78b3a5886be6adde0c8151001acc (diff)
Refs #28595 -- Added a hook to add execute wrappers for database queries.
Thanks Adam Johnson, Carl Meyer, Anssi Kääriäinen, Mariusz Felisiak, Michael Manfre, and Tim Graham for discussion and review.
Diffstat (limited to 'django/db/backends/utils.py')
-rw-r--r--django/db/backends/utils.py15
1 files changed, 14 insertions, 1 deletions
diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py
index 9634807a87..816164d36a 100644
--- a/django/db/backends/utils.py
+++ b/django/db/backends/utils.py
@@ -1,5 +1,6 @@
import datetime
import decimal
+import functools
import hashlib
import logging
import re
@@ -65,6 +66,18 @@ class CursorWrapper:
return self.cursor.callproc(procname, params, kparams)
def execute(self, sql, params=None):
+ return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
+
+ def executemany(self, sql, param_list):
+ return self._execute_with_wrappers(sql, param_list, many=True, executor=self._executemany)
+
+ def _execute_with_wrappers(self, sql, params, many, executor):
+ context = {'connection': self.db, 'cursor': self}
+ for wrapper in reversed(self.db.execute_wrappers):
+ executor = functools.partial(wrapper, executor)
+ return executor(sql, params, many, context)
+
+ def _execute(self, sql, params, *ignored_wrapper_args):
self.db.validate_no_broken_transaction()
with self.db.wrap_database_errors:
if params is None:
@@ -72,7 +85,7 @@ class CursorWrapper:
else:
return self.cursor.execute(sql, params)
- def executemany(self, sql, param_list):
+ def _executemany(self, sql, param_list, *ignored_wrapper_args):
self.db.validate_no_broken_transaction()
with self.db.wrap_database_errors:
return self.cursor.executemany(sql, param_list)