6
6
override Connection.default_cursor with a non-standard Cursor class.
7
7
8
8
"""
9
- import cursors
9
+ import MySQLdb . cursors
10
10
from _mysql_exceptions import Warning , Error , InterfaceError , DataError , \
11
11
DatabaseError , OperationalError , IntegrityError , InternalError , \
12
12
NotSupportedError , ProgrammingError
@@ -33,7 +33,7 @@ def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
33
33
connection .messages .append (error )
34
34
del cursor
35
35
del connection
36
- raise errorclass , errorvalue
36
+ raise errorclass ( errorvalue )
37
37
38
38
re_numeric_part = re .compile (r"^(\d+)" )
39
39
@@ -57,7 +57,7 @@ class Connection(_mysql.connection):
57
57
58
58
"""MySQL Database Connection Object"""
59
59
60
- default_cursor = cursors .Cursor
60
+ default_cursor = MySQLdb . cursors .Cursor
61
61
62
62
def __init__ (self , * args , ** kwargs ):
63
63
"""
@@ -109,17 +109,9 @@ def __init__(self, *args, **kwargs):
109
109
cursorclass
110
110
class object, used to create cursors (keyword only)
111
111
112
- use_unicode
113
- If True, text-like columns are returned as unicode objects
114
- using the connection's character set. Otherwise, text-like
115
- columns are returned as strings. columns are returned as
116
- normal strings. Unicode objects will always be encoded to
117
- the connection's character set regardless of this setting.
118
-
119
112
charset
120
113
If supplied, the connection character set will be changed
121
- to this character set (MySQL-4.1 and newer). This implies
122
- use_unicode=True.
114
+ to this character set (MySQL-4.1 and newer).
123
115
124
116
sql_mode
125
117
If supplied, the session SQL mode will be changed to this
@@ -143,15 +135,15 @@ class object, used to create cursors (keyword only)
143
135
documentation for the MySQL C API for some hints on what they do.
144
136
145
137
"""
146
- from constants import CLIENT , FIELD_TYPE
147
- from converters import conversions
138
+ from . constants import CLIENT , FIELD_TYPE
139
+ from . converters import conversions
148
140
from weakref import proxy , WeakValueDictionary
149
141
150
142
import types
151
143
152
144
kwargs2 = kwargs .copy ()
153
145
154
- if kwargs . has_key ( 'conv' ) :
146
+ if 'conv' in kwargs :
155
147
conv = kwargs ['conv' ]
156
148
else :
157
149
conv = conversions
@@ -165,14 +157,8 @@ class object, used to create cursors (keyword only)
165
157
kwargs2 ['conv' ] = conv2
166
158
167
159
self .cursorclass = kwargs2 .pop ('cursorclass' , self .default_cursor )
168
- charset = kwargs2 .pop ('charset' , '' )
160
+ charset = kwargs2 .pop ('charset' , 'utf8 ' )
169
161
170
- if charset :
171
- use_unicode = True
172
- else :
173
- use_unicode = False
174
-
175
- use_unicode = kwargs2 .pop ('use_unicode' , use_unicode )
176
162
sql_mode = kwargs2 .pop ('sql_mode' , '' )
177
163
178
164
client_flag = kwargs .get ('client_flag' , 0 )
@@ -197,34 +183,23 @@ def string_literal(obj, dummy=None):
197
183
return db .string_literal (obj )
198
184
return string_literal
199
185
200
- def _get_unicode_literal ():
201
- def unicode_literal (u , dummy = None ):
202
- return db .literal (u .encode (unicode_literal .charset ))
203
- return unicode_literal
204
-
205
- def _get_string_decoder ():
206
- def string_decoder (s ):
207
- return s .decode (string_decoder .charset )
208
- return string_decoder
209
-
186
+ def _get_bytes_literal ():
187
+ def bytes_literal (u , dummy = None ):
188
+ return db .literal (u .decode (bytes_literal .charset ))
189
+ return bytes_literal
190
+
210
191
string_literal = _get_string_literal ()
211
- self .unicode_literal = unicode_literal = _get_unicode_literal ()
212
- self .string_decoder = string_decoder = _get_string_decoder ()
192
+ self .bytes_literal = bytes_literal = _get_bytes_literal ()
213
193
if not charset :
214
194
charset = self .character_set_name ()
215
195
self .set_character_set (charset )
216
196
217
197
if sql_mode :
218
198
self .set_sql_mode (sql_mode )
219
199
220
- if use_unicode :
221
- self .converter [FIELD_TYPE .STRING ].append ((None , string_decoder ))
222
- self .converter [FIELD_TYPE .VAR_STRING ].append ((None , string_decoder ))
223
- self .converter [FIELD_TYPE .VARCHAR ].append ((None , string_decoder ))
224
- self .converter [FIELD_TYPE .BLOB ].append ((None , string_decoder ))
225
-
226
- self .encoders [types .StringType ] = string_literal
227
- self .encoders [types .UnicodeType ] = unicode_literal
200
+ self .encoders [str ] = string_literal
201
+ self .encoders [bytes ] = bytes_literal
202
+
228
203
self ._transactional = self .server_capabilities & CLIENT .TRANSACTIONS
229
204
if self ._transactional :
230
205
# PEP-249 requires autocommit to be initially off
@@ -295,17 +270,16 @@ def set_character_set(self, charset):
295
270
except AttributeError :
296
271
if self ._server_version < (4 , 1 ):
297
272
raise NotSupportedError ("server is too old to set charset" )
298
- self .query ('SET NAMES %s' % charset )
273
+ self .query ('SET NAMES {!s}' . format ( charset ) )
299
274
self .store_result ()
300
- self .string_decoder .charset = charset
301
- self .unicode_literal .charset = charset
275
+ self .bytes_literal .charset = charset
302
276
303
277
def set_sql_mode (self , sql_mode ):
304
278
"""Set the connection sql_mode. See MySQL documentation for
305
279
legal values."""
306
280
if self ._server_version < (4 , 1 ):
307
281
raise NotSupportedError ("server is too old to set sql_mode" )
308
- self .query ("SET SESSION sql_mode='%s'" % sql_mode )
282
+ self .query ("SET SESSION sql_mode='{!s}'" . format ( sql_mode ) )
309
283
self .store_result ()
310
284
311
285
def show_warnings (self ):
0 commit comments