annotate MySQLdb/cursors.py @ 57:9ea2b0e9302e MySQLdb

The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
author adustman
date Sat, 28 Feb 2009 04:06:44 +0000
parents 6e31278d3433
children 2d6a35051f64
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 10
diff changeset
1 """
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 10
diff changeset
2 MySQLdb Cursors
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 10
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 Cursors of various types for MySQLdb. By
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
6 default, MySQLdb uses the Cursor class.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
7
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
8 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
9
14
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 10
diff changeset
10 __revision__ = "$Revision$"[11:-2]
7773efbe9b30 Formatting and PyLint fixes. Final score: 8.21/10
adustman
parents: 10
diff changeset
11 __author__ = "$Author$"[9:-2]
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
12
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
13 import re
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
14 import sys
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
15 import weakref
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
16
31
10038670b963 Merge back r553 bug fix #2137599 for VALUES regex
kylev
parents: 18
diff changeset
17 INSERT_VALUES = re.compile(r"\svalues\s*"
10038670b963 Merge back r553 bug fix #2137599 for VALUES regex
kylev
parents: 18
diff changeset
18 r"(\(((?<!\\)'[^\)]*?\)[^\)]*(?<!\\)?'"
10038670b963 Merge back r553 bug fix #2137599 for VALUES regex
kylev
parents: 18
diff changeset
19 r"|[^\(\)]|"
10038670b963 Merge back r553 bug fix #2137599 for VALUES regex
kylev
parents: 18
diff changeset
20 r"(?:\([^\)]*\))"
10038670b963 Merge back r553 bug fix #2137599 for VALUES regex
kylev
parents: 18
diff changeset
21 r")+\))")
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
22
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
23 class BaseCursor(object):
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 """A base for Cursor classes. Useful attributes:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
26
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
27 description
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
28 A tuple of DB API 7-tuples describing the columns in
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
29 the last executed query; see PEP-249 for details.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
30
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
31 description_flags
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
32 Tuple of column flags for last query, one entry per column
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
33 in the result set. Values correspond to those in
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
34 MySQLdb.constants.FLAG. See MySQL documentation (C API)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
35 for more information. Non-standard extension.
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 arraysize
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
38 default number of rows fetchmany() will fetch
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
39
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
40 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
41
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
42 from MySQLdb.exceptions import MySQLError, Warning, Error, InterfaceError, \
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
43 DatabaseError, DataError, OperationalError, IntegrityError, \
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
44 InternalError, ProgrammingError, NotSupportedError
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
45
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
46 _defer_warnings = False
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
47 _fetch_type = None
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
48
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
49 def __init__(self, connection):
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
50 self.connection = weakref.proxy(connection)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
51 self.description = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
52 self.description_flags = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
53 self.rowcount = -1
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
54 self.arraysize = 1
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
55 self._executed = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
56 self.lastrowid = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
57 self.messages = []
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
58 self.errorhandler = connection.errorhandler
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
59 self._result = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
60 self._warnings = 0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
61 self._info = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
62 self.rownumber = None
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
63
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
64 def __del__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
65 self.close()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
66 self.errorhandler = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
67 self._result = None
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
68
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
69 def close(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
70 """Close the cursor. No further queries will be possible."""
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
71 if not self.connection:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
72 return
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
73 try:
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
74 while self.nextset():
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
75 pass
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
76 except:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
77 pass
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
78 self.connection = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
79
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
80 def _check_executed(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
81 """Ensure that .execute() has been called."""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
82 if not self._executed:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
83 self.errorhandler(self, self.ProgrammingError, "execute() first")
0
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 def _warning_check(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
86 """Check for warnings, and report via the warnings module."""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
87 from warnings import warn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
88 if self._warnings:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
89 warnings = self._get_db().show_warnings()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
90 if warnings:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
91 # This is done in two loops in case
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
92 # Warnings are set to raise exceptions.
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
93 for warning in warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
94 self.messages.append((self.Warning, warning))
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
95 for warning in warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
96 warn(warning[-1], self.Warning, 3)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
97 elif self._info:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
98 self.messages.append((self.Warning, self._info))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
99 warn(self._info, self.Warning, 3)
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 def nextset(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
102 """Advance to the next result set.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
103
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
104 Returns None if there are no more result sets.
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 if self._executed:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
107 self.fetchall()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
108 del self.messages[:]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
109
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
110 connection = self._get_db()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
111 num_rows = connection.next_result()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
112 if num_rows == -1:
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
113 return None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
114 self._do_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
115 self._post_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
116 self._warning_check()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
117 return True
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
118
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
119 def _post_get_result(self):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
120 """Stub to be overridden by MixIn."""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
121
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
122 def _get_result(self):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
123 """Stub to be overridden by MixIn."""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
124 return []
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
125
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
126 def _do_get_result(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
127 """Get the result from the last query."""
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
128 from MySQLdb.converters import lookup_converter
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
129 connection = self._get_db()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
130 self._result = self._get_result()
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
131 if self._result:
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
132 self.sql_to_python = [
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
133 lookup_converter(self, f)
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
134 for f in self._result.fields()
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
135 ]
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
136 else:
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
137 self.sql_to_python = []
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
138 self.rowcount = connection.affected_rows()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
139 self.rownumber = 0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
140 self.description = self._result and self._result.describe() or None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
141 self.description_flags = self._result and self._result.field_flags() or None
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
142 self.lastrowid = connection.insert_id()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
143 self._warnings = connection.warning_count()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
144 self._info = connection.info()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
145
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
146 def setinputsizes(self, *args):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
147 """Does nothing, required by DB API."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
148
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
149 def setoutputsizes(self, *args):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
150 """Does nothing, required by DB API."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
151
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
152 def _get_db(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
153 """Get the database connection.
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
154
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
155 Raises ProgrammingError if the connection has been closed."""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
156 if not self.connection:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
157 self.errorhandler(self, self.ProgrammingError, "cursor closed")
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
158 return self.connection._db
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
159
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
160 def execute(self, query, args=None):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
161 """Execute a query.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
162
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
163 query -- string, query to execute on server
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
164 args -- optional sequence or mapping, parameters to use with query.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
165
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
166 Note: If args is a sequence, then %s must be used as the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
167 parameter placeholder in the query. If a mapping is used,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
168 %(key)s must be used as the placeholder.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
169
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
170 Returns long integer rows affected, if any
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
171
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
172 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
173 del self.messages[:]
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
174 db = self._get_db()
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
175 charset = db.character_set_name()
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
176 if isinstance(query, unicode):
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
177 query = query.encode(charset)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
178 if args is not None:
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
179 query = query % self.connection.literal(args)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
180 try:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
181 result = self._query(query)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
182 except TypeError, msg:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
183 if msg.args[0] in ("not enough arguments for format string",
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
184 "not all arguments converted"):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
185 self.messages.append((self.ProgrammingError, msg.args[0]))
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
186 self.errorhandler(self, self.ProgrammingError, msg.args[0])
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
187 else:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
188 self.messages.append((TypeError, msg))
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
189 self.errorhandler(self, TypeError, msg)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
190 except:
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
191 exc, value, traceback = sys.exc_info()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
192 del traceback
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
193 self.messages.append((exc, value))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
194 self.errorhandler(self, exc, value)
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
195
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
196 self._executed = query
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
197 if not self._defer_warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
198 self._warning_check()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
199 return result
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
200
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
201 def executemany(self, query, args):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
202 """Execute a multi-row query.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
203
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
204 query
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
205
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
206 string, query to execute on server
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
207
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
208 args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
209
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
210 Sequence of sequences or mappings, parameters to use with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
211 query.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
212
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
213 Returns long integer rows affected, if any.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
214
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
215 This method improves performance on multiple-row INSERT and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
216 REPLACE. Otherwise it is equivalent to looping over args with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
217 execute().
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
218
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
219 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
220 del self.messages[:]
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
221 db = self._get_db()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
222 if not args:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
223 return
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
224 charset = self.connection.character_set_name()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
225 if isinstance(query, unicode):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
226 query = query.encode(charset)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
227 matched = INSERT_VALUES.match(query)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
228 if not matched:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
229 self.rowcount = sum([ self.execute(query, arg) for arg in args ])
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
230 return self.rowcount
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
231
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
232 start = matched.group('start')
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
233 end = matched.group('end')
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
234 values = matched.group('values')
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
235
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
236 try:
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
237 sql_params = [ values % self.connection.literal(arg) for arg in args ]
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
238 except TypeError, msg:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
239 if msg.args[0] in ("not enough arguments for format string",
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
240 "not all arguments converted"):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
241 self.messages.append((self.ProgrammingError, msg.args[0]))
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
242 self.errorhandler(self, self.ProgrammingError, msg.args[0])
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
243 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
244 self.messages.append((TypeError, msg))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
245 self.errorhandler(self, TypeError, msg)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
246 except:
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
247 exc, value, traceback = sys.exc_info()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
248 del traceback
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
249 self.errorhandler(self, exc, value)
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
250 self.rowcount = int(self._query(
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
251 '\n'.join([start, ',\n'.join(sql_params), end,
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
252 ])))
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
253 if not self._defer_warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
254 self._warning_check()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
255 return self.rowcount
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
256
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
257 def callproc(self, procname, args=()):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
258 """Execute stored procedure procname with args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
259
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
260 procname
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
261 string, name of procedure to execute on server
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
262
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
263 args
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
264 Sequence of parameters to use with procedure
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
265
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
266 Returns the original args.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
267
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
268 Compatibility warning: PEP-249 specifies that any modified
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
269 parameters must be returned. This is currently impossible
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
270 as they are only available by storing them in a server
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
271 variable and then retrieved by a query. Since stored
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
272 procedures return zero or more result sets, there is no
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
273 reliable way to get at OUT or INOUT parameters via callproc.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
274 The server variables are named @_procname_n, where procname
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
275 is the parameter above and n is the position of the parameter
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
276 (from zero). Once all result sets generated by the procedure
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
277 have been fetched, you can issue a SELECT @_procname_0, ...
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
278 query using .execute() to get any OUT or INOUT values.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
279
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
280 Compatibility warning: The act of calling a stored procedure
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
281 itself creates an empty result set. This appears after any
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
282 result sets generated by the procedure. This is non-standard
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
283 behavior with respect to the DB-API. Be sure to use nextset()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
284 to advance through all result sets; otherwise you may get
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
285 disconnected.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
286 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
287
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
288 db = self._get_db()
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
289 charset = self.connection.character_set_name()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
290 for index, arg in enumerate(args):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
291 query = "SET @_%s_%d=%s" % (procname, index,
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
292 self.connection.literal(arg))
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
293 if isinstance(query, unicode):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
294 query = query.encode(charset)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
295 self._query(query)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
296 self.nextset()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
297
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
298 query = "CALL %s(%s)" % (procname,
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
299 ','.join(['@_%s_%d' % (procname, i)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
300 for i in range(len(args))]))
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
301 if isinstance(query, unicode):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
302 query = query.encode(charset)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
303 self._query(query)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
304 self._executed = query
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
305 if not self._defer_warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
306 self._warning_check()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
307 return args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
308
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
309 def _do_query(self, query):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
310 """Low-levey query wrapper. Overridden by MixIns."""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
311 connection = self._get_db()
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
312 self._executed = query
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
313 connection.query(query)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
314 self._do_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
315 return self.rowcount
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
316
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
317 def _query(self, query):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
318 """Hook for _do_query."""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
319 return self._do_query(query)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
320
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
321 def _fetch_row(self, size=1):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
322 """Low-level fetch_row wrapper."""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
323 if not self._result:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
324 return ()
57
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
325 # unfortunately it is necessary to wrap these generators up as tuples
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
326 # as the rows are expected to be subscriptable.
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
327 return tuple(
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
328 (
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
329 tuple( ( f(x) for f, x in zip(self.sql_to_python, row) ) )
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
330 for row in self._result.fetch_row(size, self._fetch_type)
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
331 )
9ea2b0e9302e The pure Python SQL-to-Python conversion code. TODO: There should be a way to register plugins in the module and in the connection.
adustman
parents: 54
diff changeset
332 )
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
333
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
334 def __iter__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
335 return iter(self.fetchone, None)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
336
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
337 def fetchone(self):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
338 """Stub to be overridden by a MixIn."""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
339 return None
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
340
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
341 def fetchall(self):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
342 """Stub to be overridden by a MixIn."""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
343 return []
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
344
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
345
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
346 class CursorStoreResultMixIn(object):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
347
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
348 """This is a MixIn class which causes the entire result set to be
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
349 stored on the client side, i.e. it uses mysql_store_result(). If the
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
350 result set can be very large, consider adding a LIMIT clause to your
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
351 query, or using CursorUseResultMixIn instead."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
352
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
353 def _get_result(self):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
354 """Low-level; uses mysql_store_result()"""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
355 return self._get_db().store_result()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
356
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
357 def _query(self, query):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
358 """Low-level; executes query, gets result, and returns rowcount."""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
359 rowcount = self._do_query(query)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
360 self._post_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
361 return rowcount
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
362
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
363 def _post_get_result(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
364 """Low-level"""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
365 self._rows = self._fetch_row(0)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
366 self._result = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
367
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
368 def fetchone(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
369 """Fetches a single row from the cursor. None indicates that
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
370 no more rows are available."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
371 self._check_executed()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
372 if self.rownumber >= len(self._rows):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
373 return None
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
374 result = self._rows[self.rownumber]
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
375 self.rownumber += 1
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
376 return result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
377
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
378 def fetchmany(self, size=None):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
379 """Fetch up to size rows from the cursor. Result set may be smaller
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
380 than size. If size is not defined, cursor.arraysize is used."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
381 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
382 end = self.rownumber + (size or self.arraysize)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
383 result = self._rows[self.rownumber:end]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
384 self.rownumber = min(end, len(self._rows))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
385 return result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
386
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
387 def fetchall(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
388 """Fetchs all available rows from the cursor."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
389 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
390 if self.rownumber:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
391 result = self._rows[self.rownumber:]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
392 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
393 result = self._rows
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
394 self.rownumber = len(self._rows)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
395 return result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
396
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
397 def scroll(self, value, mode='relative'):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
398 """Scroll the cursor in the result set to a new position according
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
399 to mode.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
400
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
401 If mode is 'relative' (default), value is taken as offset to
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
402 the current position in the result set, if set to 'absolute',
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
403 value states an absolute target position."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
404 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
405 if mode == 'relative':
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
406 row = self.rownumber + value
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
407 elif mode == 'absolute':
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
408 row = value
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
409 else:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
410 self.errorhandler(self, self.ProgrammingError,
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
411 "unknown scroll mode %s" % `mode`)
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
412 if row < 0 or row >= len(self._rows):
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
413 self.errorhandler(self, IndexError, "out of range")
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
414 self.rownumber = row
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
415
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
416 def __iter__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
417 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
418 result = self.rownumber and self._rows[self.rownumber:] or self._rows
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
419 return iter(result)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
420
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
421
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
422 class CursorUseResultMixIn(object):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
423
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
424 """This is a MixIn class which causes the result set to be stored
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
425 in the server and sent row-by-row to client side, i.e. it uses
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
426 mysql_use_result(). You MUST retrieve the entire result set and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
427 close() the cursor before additional queries can be peformed on
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
428 the connection."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
429
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
430 _defer_warnings = True
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
431
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
432 def _get_result(self):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
433 """Low-level; calls mysql_use_result()"""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
434 return self._get_db().use_result()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
435
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
436 def fetchone(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
437 """Fetches a single row from the cursor."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
438 self._check_executed()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
439 rows = self._fetch_row(1)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
440 if not rows:
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
441 self._warning_check()
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
442 return None
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
443 self.rownumber = self.rownumber + 1
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
444 return rows[0]
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
445
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
446 def fetchmany(self, size=None):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
447 """Fetch up to size rows from the cursor. Result set may be smaller
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
448 than size. If size is not defined, cursor.arraysize is used."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
449 self._check_executed()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
450 rows = self._fetch_row(size or self.arraysize)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
451 self.rownumber = self.rownumber + len(rows)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
452 if not rows:
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
453 self._warning_check()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
454 return rows
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
455
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
456 def fetchall(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
457 """Fetchs all available rows from the cursor."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
458 self._check_executed()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
459 rows = self._fetch_row(0)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
460 self.rownumber = self.rownumber + len(rows)
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
461 self._warning_check()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
462 return rows
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
463
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
464 def __iter__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
465 return self
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
466
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
467 def next(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
468 row = self.fetchone()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
469 if row is None:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
470 raise StopIteration
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
471 return row
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
472
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
473
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
474 class CursorTupleRowsMixIn(object):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
475
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
476 """This is a MixIn class that causes all rows to be returned as tuples,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
477 which is the standard form required by DB API."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
478
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
479 _fetch_type = 0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
480
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
481
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
482 class CursorDictRowsMixIn(object):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
483
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
484 """This is a MixIn class that causes all rows to be returned as
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
485 dictionaries. This is a non-standard feature."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
486
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
487 _fetch_type = 1
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
488
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
489
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
490 class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
491 BaseCursor):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
492
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
493 """This is the standard Cursor class that returns rows as tuples
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
494 and stores the result set in the client."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
495
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
496
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
497 class DictCursor(CursorStoreResultMixIn, CursorDictRowsMixIn,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
498 BaseCursor):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
499
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
500 """This is a Cursor class that returns rows as dictionaries and
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
501 stores the result set in the client."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
502
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
503
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
504 class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
505 BaseCursor):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
506
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
507 """This is a Cursor class that returns rows as tuples and stores
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
508 the result set in the server."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
509
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
510
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
511 class SSDictCursor(CursorUseResultMixIn, CursorDictRowsMixIn,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
512 BaseCursor):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
513
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
514 """This is a Cursor class that returns rows as dictionaries and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
515 stores the result set in the server."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
516
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
517