Skip to content

Commit a2edb76

Browse files
postal2600robhudson
postal2600
authored andcommitted
Added support for executemany. Fixes issue #120
Signed-off-by: Rob Hudson <rob@cogit8.org>
1 parent 2f76c21 commit a2edb76

File tree

2 files changed

+45
-0
lines changed

2 files changed

+45
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ News for django-debug-toolbar
44
develop branch
55
--------------
66

7+
* Added support for `executemany`. Thanks to postal2600.
8+
79
* Added support for LogBook. Thanks to Vincent Driessen.
810

911
* Added clean_params method to DatabaseStatTracker to scrub non-unicode

debug_toolbar/panels/sql.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,49 @@ def execute(self, sql, params=()):
125125
pass
126126
del cur_frame
127127

128+
# We keep `sql` to maintain backwards compatibility
129+
self.db.queries.append({
130+
'sql': self.db.ops.last_executed_query(self.cursor, sql, params),
131+
'duration': duration,
132+
'raw_sql': sql,
133+
'params': _params,
134+
'hash': sha_constructor(settings.SECRET_KEY + sql + _params).hexdigest(),
135+
'stacktrace': stacktrace,
136+
'start_time': start,
137+
'stop_time': stop,
138+
'is_slow': (duration > SQL_WARNING_THRESHOLD),
139+
'is_select': sql.lower().strip().startswith('select'),
140+
'template_info': template_info,
141+
})
142+
143+
def executemany(self, sql, params=()):
144+
start = datetime.now()
145+
try:
146+
return self.cursor.executemany(sql, params)
147+
finally:
148+
stop = datetime.now()
149+
duration = ms_from_timedelta(stop - start)
150+
stacktrace = tidy_stacktrace(traceback.extract_stack())
151+
_params = ''
152+
try:
153+
_params = simplejson.dumps([force_unicode(x, strings_only=True) for x in params])
154+
except TypeError:
155+
pass # object not JSON serializable
156+
157+
template_info = None
158+
cur_frame = sys._getframe().f_back
159+
try:
160+
while cur_frame is not None:
161+
if cur_frame.f_code.co_name == 'render':
162+
node = cur_frame.f_locals['self']
163+
if isinstance(node, Node):
164+
template_info = get_template_info(node.source)
165+
break
166+
cur_frame = cur_frame.f_back
167+
except:
168+
pass
169+
del cur_frame
170+
128171
# We keep `sql` to maintain backwards compatibility
129172
self.db.queries.append({
130173
'sql': self.db.ops.last_executed_query(self.cursor, sql, params),

0 commit comments

Comments
 (0)