@@ -182,6 +182,27 @@ def execute(self, query, args=None):
182
182
"""
183
183
while self .nextset ():
184
184
pass
185
+
186
+ mogrified_query = self ._mogrify (query , args )
187
+
188
+ assert isinstance (mogrified_query , (bytes , bytearray ))
189
+ res = self ._query (mogrified_query )
190
+ return res
191
+
192
+ def _mogrify (self , query , args = None ):
193
+ """Generates the query to be sent to the server with argument
194
+ interpolation and proper encoding. This is for internal use, see
195
+ mogrify() for the external API that returns a string.
196
+
197
+ query -- string, query to execute on server
198
+ args -- optional sequence or mapping, parameters to use with query.
199
+
200
+ Note: If args is a sequence, then %s must be used as the
201
+ parameter placeholder in the query. If a mapping is used,
202
+ %(key)s must be used as the placeholder.
203
+
204
+ Returns bytes or bytearray representing the query
205
+ """
185
206
db = self ._get_db ()
186
207
187
208
if isinstance (query , str ):
@@ -202,9 +223,22 @@ def execute(self, query, args=None):
202
223
except TypeError as m :
203
224
raise ProgrammingError (str (m ))
204
225
205
- assert isinstance (query , (bytes , bytearray ))
206
- res = self ._query (query )
207
- return res
226
+ return query
227
+
228
+ def mogrify (self , query , args = None ):
229
+ """Get the query exactly as it would be sent to the database running the
230
+ execute() method.
231
+
232
+ query -- string, query to execute on server
233
+ args -- optional sequence or mapping, parameters to use with query.
234
+
235
+ Note: If args is a sequence, then %s must be used as the
236
+ parameter placeholder in the query. If a mapping is used,
237
+ %(key)s must be used as the placeholder.
238
+
239
+ Returns string representing query that would be executed by the server
240
+ """
241
+ return self ._mogrify (query , args ).decode ()
208
242
209
243
def executemany (self , query , args ):
210
244
# type: (str, list) -> int
0 commit comments