annotate MySQLdb/connections.py @ 75:3b03cb566032 MySQLdb

More serious restructuring and cleaning, especially in the handling of result sets. All tests pass.
author adustman
date Mon, 22 Feb 2010 03:56:44 +0000
parents 80164eb2f090
children 228a45771d14
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
1 """
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
2 MySQLdb Connections
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
3 -------------------
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
4
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
5 This module implements connections for MySQLdb. Presently there is
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
6 only one class: Connection. Others are unlikely. However, you might
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
7 want to make your own subclasses. In most cases, you will probably
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
8 override Connection.default_cursor with a non-standard Cursor class.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
9
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
10 """
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
11
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
12 __revision__ = "$Revision$"[11:-2]
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
13 __author__ = "$Author$"[9:-2]
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
14
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
15
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
16 def defaulterrorhandler(connection, cursor, errorclass, errorvalue):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
17 """
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
18 If cursor is not None, (errorclass, errorvalue) is appended to
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
19 cursor.messages; otherwise it is appended to connection.messages. Then
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
20 errorclass is raised with errorvalue as the value.
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
21
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
22 You can override this with your own error handler by assigning it to the
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
23 instance.
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
24 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
25 error = errorclass, errorvalue
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
26 if cursor:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
27 cursor.messages.append(error)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
28 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
29 connection.messages.append(error)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
30 del cursor
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
31 del connection
62
7fe4b0b37e8e Fix #2671682. (from 1.2 branch)
adustman
parents: 46
diff changeset
32 raise errorclass, errorvalue
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
33
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
36
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
37 """MySQL Database Connection Object"""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
38
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
39 errorhandler = defaulterrorhandler
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
42 DatabaseError, OperationalError, IntegrityError, InternalError, \
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
43 NotSupportedError, ProgrammingError
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
44
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
45 def __init__(self, *args, **kwargs):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
46 """
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
47 Create a connection to the database. It is strongly recommended that
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
48 you only use keyword parameters. Consult the MySQL C API documentation
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
49 for more information.
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
50
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
51 host
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
52 string, host to connect
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
53
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
54 user
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
55 string, user to connect as
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
56
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
57 passwd
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
58 string, password to use
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
59
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
60 db
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
61 string, database to use
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
62
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
63 port
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
64 integer, TCP/IP port to connect to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
65
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
66 unix_socket
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
67 string, location of unix_socket to use
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
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
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 1
diff changeset
75 connect_timeout
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
76 number of seconds to wait before the connection attempt
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
77 fails.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
78
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
79 compress
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
80 if set, compression is enabled
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
81
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
82 named_pipe
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
83 if set, a named pipe is used to connect (Windows only)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
84
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
85 init_command
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
86 command which is run once the connection is created
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
87
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
88 read_default_file
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
89 file from which default client values are read
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
90
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
91 read_default_group
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
92 configuration group to use from the default file
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
93
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
94 use_unicode
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
95 If True, text-like columns are returned as unicode objects
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
96 using the connection's character set. Otherwise, text-like
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
97 columns are returned as strings. columns are returned as
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
98 normal strings. Unicode objects will always be encoded to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
99 the connection's character set regardless of this setting.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
100
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
101 charset
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
102 If supplied, the connection character set will be changed
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
103 to this character set (MySQL-4.1 and newer). This implies
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
104 use_unicode=True.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
105
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
106 sql_mode
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
107 If supplied, the session SQL mode will be changed to this
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
108 setting (MySQL-4.1 and newer). For more details and legal
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
109 values, see the MySQL documentation.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
110
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
111 client_flag
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
112 integer, flags to use or 0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
113 (see MySQL docs or constants/CLIENTS.py)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
114
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
115 ssl
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
116 dictionary or mapping, contains SSL connection parameters;
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
117 see the MySQL documentation for more details
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
118 (mysql_ssl_set()). If this is set, and the client does not
1
e51bc565a529 Merged fixes from 1.2 branch
adustman
parents: 0
diff changeset
119 support SSL, NotSupportedError will be raised.
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
120
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
121 local_infile
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
122 integer, non-zero enables LOAD LOCAL INFILE; zero disables
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
123
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
124 There are a number of undocumented, non-standard methods. See the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
125 documentation for the MySQL C API for some hints on what they do.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
126
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
127 """
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
128 from MySQLdb.constants import CLIENT, FIELD_TYPE
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
129 from MySQLdb.converters import default_decoders, default_encoders, default_row_formatter
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
130 from MySQLdb.cursors import Cursor
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
131 import _mysql
46
4093fb968cb7 Bring back conversions for the time being, until we can get trunk actually
kylev
parents: 35
diff changeset
132
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
133 kwargs2 = kwargs.copy()
22
597efa4e0311 Trivial patch for dict.has_key() being deprecated going forward.
kylev
parents: 18
diff changeset
134
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
135 self.cursorclass = Cursor
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
136 charset = kwargs2.pop('charset', '')
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
137
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
138 self.encoders = kwargs2.pop('encoders', default_encoders)
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
139 self.decoders = kwargs2.pop('decoders', default_decoders)
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
140 self.row_formatter = kwargs2.pop('row_formatter', default_row_formatter)
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
141
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
142 client_flag = kwargs.get('client_flag', 0)
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
143 client_version = tuple(
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
144 [ int(n) for n in _mysql.get_client_info().split('.')[:2] ])
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
145 if client_version >= (4, 1):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
146 client_flag |= CLIENT.MULTI_STATEMENTS
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
147 if client_version >= (5, 0):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
148 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
149
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
150 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
151
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 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
153
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
154 self._db = _mysql.connection(*args, **kwargs2)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
155
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
156 self._server_version = tuple(
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
157 [ int(n) for n in self._db.get_server_info().split('.')[:2] ])
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
158
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
159 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
160 self._db.set_character_set(charset)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
161
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
162 if sql_mode:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
163 self.set_sql_mode(sql_mode)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
164
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
165 self._transactional = bool(self._db.server_capabilities & CLIENT.TRANSACTIONS)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
166 if self._transactional:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
167 # PEP-249 requires autocommit to be initially off
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
168 self.autocommit(False)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
169 self.messages = []
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
170 self._active_cursor = None
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):
72
c0c00294239b Check in some old changes
adustman
parents: 67
diff changeset
173 self._autocommit = do_autocommit
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
174 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
175
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 def ping(self, reconnect=False):
72
c0c00294239b Check in some old changes
adustman
parents: 67
diff changeset
177 if reconnect and not self._autocommit:
c0c00294239b Check in some old changes
adustman
parents: 67
diff changeset
178 raise ProgrammingError("autocommit must be enabled before enabling auto-reconnect; consider the consequences")
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
179 return self._db.ping(reconnect)
18
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 commit(self):
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
182 return self._db.commit()
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 rollback(self):
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
185 return self._db.rollback()
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
186
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
187 def close(self):
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
188 return self._db.close()
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
189
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
190 def escape_string(self, s):
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
191 return self._db.escape_string(s)
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
192
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
193 def string_literal(self, s):
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
194 return self._db.string_literal(s)
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
195
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
196 def cursor(self, encoders=None, decoders=None, row_formatter=None):
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
197 """
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
198 Create a cursor on which queries may be performed. The optional
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
199 cursorclass parameter is used to create the Cursor. By default,
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
200 self.cursorclass=cursors.Cursor is used.
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
201 """
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
202 if self._active_cursor:
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
203 self._active_cursor._flush()
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
204
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
205 if not encoders:
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
206 encoders = self.encoders[:]
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
207
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
208 if not decoders:
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
209 decoders = self.decoders[:]
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
210
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
211 if not row_formatter:
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
212 row_formatter = self.row_formatter
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
213
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
214 self._active_cursor = self.cursorclass(self, encoders, decoders, row_formatter)
74
80164eb2f090 This passes all test, yet is still broken and ugly in many ways.
adustman
parents: 72
diff changeset
215 return self._active_cursor
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
216
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
217 def __enter__(self):
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
218 return self.cursor()
9
0e37ee00beb7 Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents: 5
diff changeset
219
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
220 def __exit__(self, exc, value, traceback):
9
0e37ee00beb7 Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents: 5
diff changeset
221 if exc:
0e37ee00beb7 Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents: 5
diff changeset
222 self.rollback()
0e37ee00beb7 Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents: 5
diff changeset
223 else:
0e37ee00beb7 Merge changes from 1.2 branch (r470:483): Mostly build-related.
adustman
parents: 5
diff changeset
224 self.commit()
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
225
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
226 def literal(self, obj):
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
227 """
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
228 Given an object obj, returns an SQL literal as a string.
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
229
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
230 Non-standard.
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
231 """
67
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
232 for encoder in self.encoders:
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
233 f = encoder(obj)
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
234 if f:
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
235 return f(self, obj)
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
236
98d968f5af11 Reimplement MySQL->Python type conversion in C; much simpler and easier to deal with now. Hey, all my tests pass, so I guess that means I need to write some more tests.
adustman
parents: 64
diff changeset
237 raise self.NotSupportedError("could not encode as SQL", obj)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
238
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
239 def character_set_name(self):
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
240 return self._db.character_set_name()
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
241
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
242 def set_character_set(self, charset):
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
243 """Set the connection character set to charset. The character set can
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
244 only be changed in MySQL-4.1 and newer. If you try to change the
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
245 character set from the current value in an older version,
15
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
246 NotSupportedError will be raised.
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
247
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
248 Non-standard. It is better to set the character set when creating the
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
249 connection using the charset parameter."""
1
e51bc565a529 Merged fixes from 1.2 branch
adustman
parents: 0
diff changeset
250 if self.character_set_name() != charset:
e51bc565a529 Merged fixes from 1.2 branch
adustman
parents: 0
diff changeset
251 try:
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
252 self._db.set_character_set(charset)
1
e51bc565a529 Merged fixes from 1.2 branch
adustman
parents: 0
diff changeset
253 except AttributeError:
e51bc565a529 Merged fixes from 1.2 branch
adustman
parents: 0
diff changeset
254 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
255 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
256 self._db.query('SET NAMES %s' % charset)
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
257 self._db.get_result()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
258
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
259 def set_sql_mode(self, sql_mode):
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
260 """Set the connection sql_mode. See MySQL documentation for legal
15
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
261 values.
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
262
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
263 Non-standard. It is better to set this when creating the connection
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
264 using the sql_mode parameter."""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
265 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
266 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
267 self._db.query("SET SESSION sql_mode='%s'" % sql_mode)
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
268 self._db.get_result()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
269
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
270 def _warning_count(self):
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
271 """Return the number of warnings generated from the last query."""
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
272 if hasattr(self._db, "warning_count"):
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
273 return self._db.warning_count()
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
274 else:
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
275 info = self._db.info()
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
276 if info:
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
277 return int(info.split()[-1])
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
278 else:
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
279 return 0
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
280
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 15
diff changeset
281 def _show_warnings(self):
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
282 """Return detailed information about warnings as a sequence of tuples
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
283 of (Level, Code, Message). This is only supported in MySQL-4.1 and up.
15
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
284 If your server is an earlier version, an empty sequence is returned.
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
285
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
286 Non-standard. This is invoked automatically after executing a query,
a275593a1630 More doc fixes
adustman
parents: 14
diff changeset
287 so you should not usually call it yourself."""
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 9
diff changeset
288 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
289 self._db.query("SHOW WARNINGS")
75
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
290 return tuple(self._db.get_result())
3b03cb566032 More serious restructuring and cleaning, especially in the handling
adustman
parents: 74
diff changeset
291
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
292