@@ -186,7 +186,7 @@ class object, used to create cursors (keyword only)
186
186
187
187
use_unicode = kwargs2 .pop ('use_unicode' , use_unicode )
188
188
sql_mode = kwargs2 .pop ('sql_mode' , '' )
189
- binary_prefix = kwargs2 .pop ('binary_prefix' , False )
189
+ self . _binary_prefix = kwargs2 .pop ('binary_prefix' , False )
190
190
191
191
client_flag = kwargs .get ('client_flag' , 0 )
192
192
client_version = tuple ([ numeric_part (n ) for n in _mysql .get_client_info ().split ('.' )[:2 ] ])
@@ -208,38 +208,28 @@ class object, used to create cursors (keyword only)
208
208
209
209
self ._server_version = tuple ([ numeric_part (n ) for n in self .get_server_info ().split ('.' )[:2 ] ])
210
210
211
+ self .encoding = 'ascii' # overriden in set_character_set()
211
212
db = proxy (self )
212
- def _get_string_literal ():
213
- # Note: string_literal() is called for bytes object on Python 3 (via bytes_literal)
214
- def string_literal (obj , dummy = None ):
215
- return db .string_literal (obj )
216
- return string_literal
217
-
218
- def _get_unicode_literal ():
219
- if PY2 :
220
- # unicode_literal is called for only unicode object.
221
- def unicode_literal (u , dummy = None ):
222
- return db .string_literal (u .encode (unicode_literal .charset ))
223
- else :
224
- # unicode_literal() is called for arbitrary object.
225
- def unicode_literal (u , dummy = None ):
226
- return db .string_literal (str (u ).encode (unicode_literal .charset ))
227
- return unicode_literal
228
-
229
- def _get_bytes_literal ():
230
- def bytes_literal (obj , dummy = None ):
231
- return b'_binary' + db .string_literal (obj )
232
- return bytes_literal
233
-
234
- def _get_string_decoder ():
235
- def string_decoder (s ):
236
- return s .decode (string_decoder .charset )
237
- return string_decoder
238
-
239
- string_literal = _get_string_literal ()
240
- self .unicode_literal = unicode_literal = _get_unicode_literal ()
241
- bytes_literal = _get_bytes_literal ()
242
- self .string_decoder = string_decoder = _get_string_decoder ()
213
+
214
+ # Note: string_literal() is called for bytes object on Python 3 (via bytes_literal)
215
+ def string_literal (obj , dummy = None ):
216
+ return db .string_literal (obj )
217
+
218
+ if PY2 :
219
+ # unicode_literal is called for only unicode object.
220
+ def unicode_literal (u , dummy = None ):
221
+ return db .string_literal (u .encode (db .encoding ))
222
+ else :
223
+ # unicode_literal() is called for arbitrary object.
224
+ def unicode_literal (u , dummy = None ):
225
+ return db .string_literal (str (u ).encode (db .encoding ))
226
+
227
+ def bytes_literal (obj , dummy = None ):
228
+ return b'_binary' + db .string_literal (obj )
229
+
230
+ def string_decoder (s ):
231
+ return s .decode (db .encoding )
232
+
243
233
if not charset :
244
234
charset = self .character_set_name ()
245
235
self .set_character_set (charset )
@@ -253,12 +243,7 @@ def string_decoder(s):
253
243
self .converter [FIELD_TYPE .VARCHAR ].append ((None , string_decoder ))
254
244
self .converter [FIELD_TYPE .BLOB ].append ((None , string_decoder ))
255
245
256
- if binary_prefix :
257
- self .encoders [bytes ] = string_literal if PY2 else bytes_literal
258
- self .encoders [bytearray ] = bytes_literal
259
- else :
260
- self .encoders [bytes ] = string_literal
261
-
246
+ self .encoders [bytes ] = string_literal
262
247
self .encoders [unicode ] = unicode_literal
263
248
self ._transactional = self .server_capabilities & CLIENT .TRANSACTIONS
264
249
if self ._transactional :
@@ -305,6 +290,16 @@ def __exit__(self, exc, value, tb):
305
290
else :
306
291
self .commit ()
307
292
293
+ def _bytes_literal (self , bs ):
294
+ assert isinstance (bs , (bytes , bytearray ))
295
+ x = self .string_literal (bs ) # x is escaped and quoted bytes
296
+ if self ._binary_prefix :
297
+ return b'_binary' + x
298
+ return x
299
+
300
+ def _tuple_literal (self , t , d ):
301
+ return "(%s)" % (',' .join (map (self .literal , t )))
302
+
308
303
def literal (self , o ):
309
304
"""If o is a single object, returns an SQL literal as a string.
310
305
If o is a non-string sequence, the items of the sequence are
@@ -313,7 +308,14 @@ def literal(self, o):
313
308
Non-standard. For internal use; do not use this in your
314
309
applications.
315
310
"""
316
- s = self .escape (o , self .encoders )
311
+ if isinstance (o , bytearray ):
312
+ s = self ._bytes_literal (o )
313
+ elif not PY2 and isinstance (o , bytes ):
314
+ s = self ._bytes_literal (o )
315
+ elif isinstance (o , (tuple , list )):
316
+ s = self ._tuple_literal (o )
317
+ else :
318
+ s = self .escape (o , self .encoders )
317
319
# Python 3(~3.4) doesn't support % operation for bytes object.
318
320
# We should decode it before using %.
319
321
# Decoding with ascii and surrogateescape allows convert arbitrary
@@ -360,8 +362,6 @@ def set_character_set(self, charset):
360
362
raise NotSupportedError ("server is too old to set charset" )
361
363
self .query ('SET NAMES %s' % charset )
362
364
self .store_result ()
363
- self .string_decoder .charset = py_charset
364
- self .unicode_literal .charset = py_charset
365
365
self .encoding = py_charset
366
366
367
367
def set_sql_mode (self , sql_mode ):
0 commit comments