@@ -137,6 +137,10 @@ class object, used to create cursors (keyword only)
137
137
If True, autocommit is enabled.
138
138
If None, autocommit isn't set and server default is used.
139
139
140
+ :param bool binary_prefix:
141
+ If set, the '_binary' prefix will be used for raw byte query
142
+ arguments (e.g. Binary). This is disabled by default.
143
+
140
144
There are a number of undocumented, non-standard methods. See the
141
145
documentation for the MySQL C API for some hints on what they do.
142
146
"""
@@ -174,6 +178,7 @@ class object, used to create cursors (keyword only)
174
178
175
179
use_unicode = kwargs2 .pop ('use_unicode' , use_unicode )
176
180
sql_mode = kwargs2 .pop ('sql_mode' , '' )
181
+ binary_prefix = kwargs2 .pop ('binary_prefix' , False )
177
182
178
183
client_flag = kwargs .get ('client_flag' , 0 )
179
184
client_version = tuple ([ numeric_part (n ) for n in _mysql .get_client_info ().split ('.' )[:2 ] ])
@@ -197,7 +202,7 @@ class object, used to create cursors (keyword only)
197
202
198
203
db = proxy (self )
199
204
def _get_string_literal ():
200
- # Note: string_literal() is called for bytes object on Python 3.
205
+ # Note: string_literal() is called for bytes object on Python 3 (via bytes_literal)
201
206
def string_literal (obj , dummy = None ):
202
207
return db .string_literal (obj )
203
208
return string_literal
@@ -206,20 +211,26 @@ def _get_unicode_literal():
206
211
if PY2 :
207
212
# unicode_literal is called for only unicode object.
208
213
def unicode_literal (u , dummy = None ):
209
- return db .literal (u .encode (unicode_literal .charset ))
214
+ return db .string_literal (u .encode (unicode_literal .charset ))
210
215
else :
211
216
# unicode_literal() is called for arbitrary object.
212
217
def unicode_literal (u , dummy = None ):
213
- return db .literal (str (u ).encode (unicode_literal .charset ))
218
+ return db .string_literal (str (u ).encode (unicode_literal .charset ))
214
219
return unicode_literal
215
220
221
+ def _get_bytes_literal ():
222
+ def bytes_literal (obj , dummy = None ):
223
+ return b'_binary' + db .string_literal (obj )
224
+ return bytes_literal
225
+
216
226
def _get_string_decoder ():
217
227
def string_decoder (s ):
218
228
return s .decode (string_decoder .charset )
219
229
return string_decoder
220
230
221
231
string_literal = _get_string_literal ()
222
232
self .unicode_literal = unicode_literal = _get_unicode_literal ()
233
+ bytes_literal = _get_bytes_literal ()
223
234
self .string_decoder = string_decoder = _get_string_decoder ()
224
235
if not charset :
225
236
charset = self .character_set_name ()
@@ -234,7 +245,12 @@ def string_decoder(s):
234
245
self .converter [FIELD_TYPE .VARCHAR ].append ((None , string_decoder ))
235
246
self .converter [FIELD_TYPE .BLOB ].append ((None , string_decoder ))
236
247
237
- self .encoders [bytes ] = string_literal
248
+ if binary_prefix :
249
+ self .encoders [bytes ] = string_literal if PY2 else bytes_literal
250
+ self .encoders [bytearray ] = bytes_literal
251
+ else :
252
+ self .encoders [bytes ] = string_literal
253
+
238
254
self .encoders [unicode ] = unicode_literal
239
255
self ._transactional = self .server_capabilities & CLIENT .TRANSACTIONS
240
256
if self ._transactional :
0 commit comments