Mercurial > p > mysql-python > mysqldb-2
annotate MySQLdb/connections.py @ 64:2d6a35051f64 MySQLdb
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
author | adustman |
---|---|
date | Sat, 28 Mar 2009 13:37:58 +0000 |
parents | 7fe4b0b37e8e |
children | 98d968f5af11 |
rev | line source |
---|---|
0 | 1 """ |
14 | 2 MySQLdb Connections |
3 ------------------- | |
0 | 4 |
5 This module implements connections for MySQLdb. Presently there is | |
6 only one class: Connection. Others are unlikely. However, you might | |
7 want to make your own subclasses. In most cases, you will probably | |
8 override Connection.default_cursor with a non-standard Cursor class. | |
9 | |
10 """ | |
14 | 11 |
12 __revision__ = "$Revision$"[11:-2] | |
13 __author__ = "$Author$"[9:-2] | |
14 | |
0 | 15 |
16 def defaulterrorhandler(connection, cursor, errorclass, errorvalue): | |
17 """ | |
14 | 18 If cursor is not None, (errorclass, errorvalue) is appended to |
19 cursor.messages; otherwise it is appended to connection.messages. Then | |
20 errorclass is raised with errorvalue as the value. | |
0 | 21 |
14 | 22 You can override this with your own error handler by assigning it to the |
23 instance. | |
0 | 24 """ |
25 error = errorclass, errorvalue | |
26 if cursor: | |
27 cursor.messages.append(error) | |
28 else: | |
29 connection.messages.append(error) | |
30 del cursor | |
31 del connection | |
62 | 32 raise errorclass, errorvalue |
0 | 33 |
34 | |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
35 class Connection(object): |
0 | 36 |
37 """MySQL Database Connection Object""" | |
38 | |
14 | 39 errorhandler = defaulterrorhandler |
0 | 40 |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
41 from MySQLdb.exceptions import Warning, Error, InterfaceError, DataError, \ |
14 | 42 DatabaseError, OperationalError, IntegrityError, InternalError, \ |
43 NotSupportedError, ProgrammingError | |
44 | |
0 | 45 def __init__(self, *args, **kwargs): |
46 """ | |
14 | 47 Create a connection to the database. It is strongly recommended that |
48 you only use keyword parameters. Consult the MySQL C API documentation | |
49 for more information. | |
0 | 50 |
51 host | |
52 string, host to connect | |
53 | |
54 user | |
55 string, user to connect as | |
56 | |
57 passwd | |
58 string, password to use | |
59 | |
60 db | |
61 string, database to use | |
62 | |
63 port | |
64 integer, TCP/IP port to connect to | |
65 | |
66 unix_socket | |
67 string, location of unix_socket to use | |
68 | |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
69 decoders |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
70 list, SQL decoder stack |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
71 |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
72 encoders |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
73 list, SQL encoder stack |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
74 |
4 | 75 connect_timeout |
0 | 76 number of seconds to wait before the connection attempt |
77 fails. | |
78 | |
79 compress | |
80 if set, compression is enabled | |
81 | |
82 named_pipe | |
83 if set, a named pipe is used to connect (Windows only) | |
84 | |
85 init_command | |
86 command which is run once the connection is created | |
87 | |
88 read_default_file | |
89 file from which default client values are read | |
90 | |
91 read_default_group | |
92 configuration group to use from the default file | |
93 | |
94 use_unicode | |
95 If True, text-like columns are returned as unicode objects | |
96 using the connection's character set. Otherwise, text-like | |
97 columns are returned as strings. columns are returned as | |
98 normal strings. Unicode objects will always be encoded to | |
99 the connection's character set regardless of this setting. | |
100 | |
101 charset | |
102 If supplied, the connection character set will be changed | |
103 to this character set (MySQL-4.1 and newer). This implies | |
104 use_unicode=True. | |
105 | |
106 sql_mode | |
107 If supplied, the session SQL mode will be changed to this | |
108 setting (MySQL-4.1 and newer). For more details and legal | |
109 values, see the MySQL documentation. | |
110 | |
111 client_flag | |
112 integer, flags to use or 0 | |
113 (see MySQL docs or constants/CLIENTS.py) | |
114 | |
115 ssl | |
116 dictionary or mapping, contains SSL connection parameters; | |
117 see the MySQL documentation for more details | |
118 (mysql_ssl_set()). If this is set, and the client does not | |
1 | 119 support SSL, NotSupportedError will be raised. |
0 | 120 |
121 local_infile | |
122 integer, non-zero enables LOAD LOCAL INFILE; zero disables | |
123 | |
124 There are a number of undocumented, non-standard methods. See the | |
125 documentation for the MySQL C API for some hints on what they do. | |
126 | |
127 """ | |
14 | 128 from MySQLdb.constants import CLIENT, FIELD_TYPE |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
129 from MySQLdb.converters import default_decoders, default_encoders |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
130 from MySQLdb.converters import simple_type_encoders as conversions |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
131 from MySQLdb.cursors import Cursor |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
132 import _mysql |
46
4093fb968cb7
Bring back conversions for the time being, until we can get trunk actually
kylev
parents:
35
diff
changeset
|
133 |
0 | 134 kwargs2 = kwargs.copy() |
22
597efa4e0311
Trivial patch for dict.has_key() being deprecated going forward.
kylev
parents:
18
diff
changeset
|
135 |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
136 self.cursorclass = Cursor |
0 | 137 charset = kwargs2.pop('charset', '') |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
138 if 'decoders' not in kwargs2: |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
139 kwargs2['decoders'] = default_decoders; |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
140 self.decoders = kwargs2.pop('decoders', default_decoders) # XXX kwargs2['decoders'] |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
141 self.encoders = conversions # XXX kwargs2.pop('encoders', default_encoders) |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
142 |
0 | 143 client_flag = kwargs.get('client_flag', 0) |
14 | 144 client_version = tuple( |
145 [ int(n) for n in _mysql.get_client_info().split('.')[:2] ]) | |
0 | 146 if client_version >= (4, 1): |
147 client_flag |= CLIENT.MULTI_STATEMENTS | |
148 if client_version >= (5, 0): | |
149 client_flag |= CLIENT.MULTI_RESULTS | |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
150 |
0 | 151 kwargs2['client_flag'] = client_flag |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
152 |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
153 sql_mode = kwargs2.pop('sql_mode', None) |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
154 |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
155 self._db = _mysql.connection(*args, **kwargs2) |
0 | 156 |
14 | 157 self._server_version = tuple( |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
158 [ int(n) for n in self._db.get_server_info().split('.')[:2] ]) |
0 | 159 |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
160 if charset: |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
161 self._db.set_character_set(charset) |
0 | 162 |
163 if sql_mode: | |
164 self.set_sql_mode(sql_mode) | |
165 | |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
166 self._transactional = bool(self._db.server_capabilities & CLIENT.TRANSACTIONS) |
0 | 167 if self._transactional: |
168 # PEP-249 requires autocommit to be initially off | |
169 self.autocommit(False) | |
170 self.messages = [] | |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
171 |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
172 def autocommit(self, do_autocommit): |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
173 return self._db.autocommit(do_autocommit) |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
174 |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
175 def ping(self, reconnect=False): |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
176 return self._db.ping(reconnect) |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
177 |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
178 def commit(self): |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
179 return self._db.commit() |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
180 |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
181 def rollback(self): |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
182 return self._db.rollback() |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
183 |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
184 def close(self): |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
185 return self._db.close() |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
186 |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
187 def cursor(self, decoders=None, encoders=None): |
0 | 188 """ |
14 | 189 Create a cursor on which queries may be performed. The optional |
190 cursorclass parameter is used to create the Cursor. By default, | |
191 self.cursorclass=cursors.Cursor is used. | |
0 | 192 """ |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
193 if not decoders: |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
194 decoders = self.decoders[:] |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
195 if not encoders: |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
196 encoders = self.encoders.copy() #[:] |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
197 |
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
198 return self.cursorclass(self, decoders, encoders) |
0 | 199 |
14 | 200 def __enter__(self): |
201 return self.cursor() | |
9
0e37ee00beb7
Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents:
5
diff
changeset
|
202 |
14 | 203 def __exit__(self, exc, value, traceback): |
9
0e37ee00beb7
Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents:
5
diff
changeset
|
204 if exc: |
0e37ee00beb7
Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents:
5
diff
changeset
|
205 self.rollback() |
0e37ee00beb7
Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents:
5
diff
changeset
|
206 else: |
0e37ee00beb7
Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents:
5
diff
changeset
|
207 self.commit() |
0e37ee00beb7
Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents:
5
diff
changeset
|
208 |
14 | 209 def literal(self, obj): |
0 | 210 """ |
14 | 211 If obj is a single object, returns an SQL literal as a string. If |
212 obj is a non-string sequence, the items of the sequence are converted | |
213 and returned as a sequence. | |
0 | 214 |
14 | 215 Non-standard. For internal use; do not use this in your applications. |
216 """ | |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
217 return self._db.escape(obj, self.encoders) |
0 | 218 |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
219 def _warning_count(self): |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
220 """Return the number of warnings generated from the last query.""" |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
221 if hasattr(self._db, "warning_count"): |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
222 return self._db.warning_count() |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
223 else: |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
224 info = self._db.info() |
0 | 225 if info: |
14 | 226 return int(info.split()[-1]) |
0 | 227 else: |
228 return 0 | |
64
2d6a35051f64
Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents:
62
diff
changeset
|
229 |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
230 def character_set_name(self): |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
231 return self._db.character_set_name() |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
232 |
0 | 233 def set_character_set(self, charset): |
14 | 234 """Set the connection character set to charset. The character set can |
235 only be changed in MySQL-4.1 and newer. If you try to change the | |
236 character set from the current value in an older version, | |
15 | 237 NotSupportedError will be raised. |
238 | |
239 Non-standard. It is better to set the character set when creating the | |
240 connection using the charset parameter.""" | |
1 | 241 if self.character_set_name() != charset: |
242 try: | |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
243 self._db.set_character_set(charset) |
1 | 244 except AttributeError: |
245 if self._server_version < (4, 1): | |
35
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
22
diff
changeset
|
246 raise self.NotSupportedError("server is too old to set charset") |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
247 self._db.query('SET NAMES %s' % charset) |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
248 self._db.store_result() |
0 | 249 self.string_decoder.charset = charset |
250 self.unicode_literal.charset = charset | |
251 | |
252 def set_sql_mode(self, sql_mode): | |
14 | 253 """Set the connection sql_mode. See MySQL documentation for legal |
15 | 254 values. |
255 | |
256 Non-standard. It is better to set this when creating the connection | |
257 using the sql_mode parameter.""" | |
0 | 258 if self._server_version < (4, 1): |
35
e7bd07afbcb9
Conflict-filled merge from 1.2br for 558:559 set and exception fixes
kylev
parents:
22
diff
changeset
|
259 raise self.NotSupportedError("server is too old to set sql_mode") |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
260 self._db.query("SET SESSION sql_mode='%s'" % sql_mode) |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
261 self._db.store_result() |
0 | 262 |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
263 def _show_warnings(self): |
14 | 264 """Return detailed information about warnings as a sequence of tuples |
265 of (Level, Code, Message). This is only supported in MySQL-4.1 and up. | |
15 | 266 If your server is an earlier version, an empty sequence is returned. |
267 | |
268 Non-standard. This is invoked automatically after executing a query, | |
269 so you should not usually call it yourself.""" | |
14 | 270 if self._server_version < (4, 1): return () |
18
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
271 self._db.query("SHOW WARNINGS") |
d55bfb1a4701
Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents:
15
diff
changeset
|
272 result = self._db.store_result() |
14 | 273 warnings = result.fetch_row(0) |
0 | 274 return warnings |
275 |