annotate MySQLdb/cursors.py @ 70:29b4cfd9af07 MySQLdb

show_warnings was renamed show_warnings (bug 2796727)
author kylev
date Fri, 24 Jul 2009 00:35:20 +0000
parents 98d968f5af11
children c0c00294239b
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
65
7a60c4574baf figleaf revealed that the INSERT_VALUES regex never matched. Added a test for this, and fixed the regex (forgot to add group anchors)
adustman
parents: 64
diff changeset
17 INSERT_VALUES = re.compile(r"(?P<start>.+values\s*)"
7a60c4574baf figleaf revealed that the INSERT_VALUES regex never matched. Added a test for this, and fixed the regex (forgot to add group anchors)
adustman
parents: 64
diff changeset
18 r"(?P<values>\(((?<!\\)'[^\)]*?\)[^\)]*(?<!\\)?'|[^\(\)]|(?:\([^\)]*\)))+\))"
7a60c4574baf figleaf revealed that the INSERT_VALUES regex never matched. Added a test for this, and fixed the regex (forgot to add group anchors)
adustman
parents: 64
diff changeset
19 r"(?P<end>.*)", re.I)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
20
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: 57
diff changeset
21
2d6a35051f64 Cursor MixIns: DEAD. More of the new type conversion scheme exposed. Two tests failing because encoding hasn't been finished yet.
adustman
parents: 57
diff changeset
22 class Cursor(object):
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
23
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
24 """A base for Cursor classes. Useful attributes:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
25
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
26 description
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
27 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
28 the last executed query; see PEP-249 for details.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
29
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
30 description_flags
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
31 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
32 in the result set. Values correspond to those in
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
33 MySQLdb.constants.FLAG. See MySQL documentation (C API)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
34 for more information. Non-standard extension.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
35
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
36 arraysize
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
37 default number of rows fetchmany() will fetch
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
38
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
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
41 from MySQLdb.exceptions import MySQLError, Warning, Error, InterfaceError, \
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
42 DatabaseError, DataError, OperationalError, IntegrityError, \
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
43 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
44
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
45 _defer_warnings = False
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
46 _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
47
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: 66
diff changeset
48 def __init__(self, connection, 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: 57
diff changeset
49 from MySQLdb.converters import default_decoders
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
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: 66
diff changeset
63 self._encoders = encoders
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
64
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
65 def __del__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
66 self.close()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
67 self.errorhandler = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
68 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
69
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
70 def close(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
71 """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
72 if not self.connection:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
73 return
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
74 try:
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
75 while self.nextset():
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
76 pass
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
77 except:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
78 pass
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
79 self.connection = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
80
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
81 def _check_executed(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
82 """Ensure that .execute() has been called."""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
83 if not self._executed:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
84 self.errorhandler(self, self.ProgrammingError, "execute() first")
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
85
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
86 def _warning_check(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
87 """Check for warnings, and report via the warnings module."""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
88 from warnings import warn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
89 if self._warnings:
70
29b4cfd9af07 show_warnings was renamed show_warnings (bug 2796727)
kylev
parents: 67
diff changeset
90 warnings = self._get_db()._show_warnings()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
91 if warnings:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
92 # This is done in two loops in case
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
93 # 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
94 for warning in warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
95 self.messages.append((self.Warning, warning))
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
96 for warning in warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
97 warn(warning[-1], self.Warning, 3)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
98 elif self._info:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
99 self.messages.append((self.Warning, self._info))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
100 warn(self._info, self.Warning, 3)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
101
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
102 def nextset(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
103 """Advance to the next result set.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
104
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
105 Returns None if there are no more result sets.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
106 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
107 if self._executed:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
108 self.fetchall()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
109 del self.messages[:]
70
29b4cfd9af07 show_warnings was renamed show_warnings (bug 2796727)
kylev
parents: 67
diff changeset
110
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
111 connection = self._get_db()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
112 num_rows = connection.next_result()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
113 if num_rows == -1:
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
114 return None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
115 self._do_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
116 self._post_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
117 self._warning_check()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
118 return True
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
119
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
120 def _do_get_result(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
121 """Get the result from the last query."""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
122 connection = self._get_db()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
123 self._result = self._get_result()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
124 self.rowcount = connection.affected_rows()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
125 self.rownumber = 0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
126 self.description = self._result and self._result.describe() or None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
127 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
128 self.lastrowid = connection.insert_id()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
129 self._warnings = connection.warning_count()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
130 self._info = connection.info()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
131
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
132 def setinputsizes(self, *args):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
133 """Does nothing, required by DB API."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
134
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
135 def setoutputsizes(self, *args):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
136 """Does nothing, required by DB API."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
137
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
138 def _get_db(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
139 """Get the database connection.
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
140
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
141 Raises ProgrammingError if the connection has been closed."""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
142 if not self.connection:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
143 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
144 return self.connection._db
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 execute(self, query, args=None):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
147 """Execute a query.
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 query -- string, query to execute on server
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
150 args -- optional sequence or mapping, parameters to use with query.
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 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
153 parameter placeholder in the query. If a mapping is used,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
154 %(key)s must be used as the placeholder.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
155
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
156 Returns long integer rows affected, if any
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
157
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
158 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
159 del self.messages[:]
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
160 db = self._get_db()
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
161 charset = db.character_set_name()
4
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
162 if isinstance(query, unicode):
b5a377255eea Merge changes from MySQLdb-1.2 branch (448-455)
adustman
parents: 0
diff changeset
163 query = query.encode(charset)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
164 try:
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: 66
diff changeset
165 if args is not None:
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: 66
diff changeset
166 query = query % tuple(map(self.connection.literal, args))
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
167 result = self._query(query)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
168 except TypeError, msg:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
169 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
170 "not all arguments converted"):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
171 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
172 self.errorhandler(self, self.ProgrammingError, msg.args[0])
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
173 else:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
174 self.messages.append((TypeError, msg))
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
175 self.errorhandler(self, TypeError, msg)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
176 except:
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
177 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
178 del traceback
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
179 self.messages.append((exc, value))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
180 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
181
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
182 self._executed = query
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
183 if not self._defer_warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
184 self._warning_check()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
185 return result
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
186
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
187 def executemany(self, query, args):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
188 """Execute a multi-row query.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
189
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
190 query
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
191
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
192 string, query to execute on server
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
193
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
194 args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
195
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
196 Sequence of sequences or mappings, parameters to use with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
197 query.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
198
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
199 Returns long integer rows affected, if any.
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 This method improves performance on multiple-row INSERT and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
202 REPLACE. Otherwise it is equivalent to looping over args with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
203 execute().
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
204
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
205 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
206 del self.messages[:]
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
207 db = self._get_db()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
208 if not args:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
209 return
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
210 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
211 if isinstance(query, unicode):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
212 query = query.encode(charset)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
213 matched = INSERT_VALUES.match(query)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
214 if not matched:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
215 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
216 return self.rowcount
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
217
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
218 start = matched.group('start')
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
219 end = matched.group('end')
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
220 values = matched.group('values')
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
221
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
222 try:
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: 66
diff changeset
223 sql_params = ( values % tuple(map(self.connection.literal, row)) for row in args )
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: 66
diff changeset
224 multirow_query = '\n'.join([start, ',\n'.join(sql_params), end])
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: 66
diff changeset
225 self._executed = multirow_query
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: 66
diff changeset
226 self.rowcount = int(self._query(multirow_query))
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: 66
diff changeset
227
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
228 except TypeError, msg:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
229 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
230 "not all arguments converted"):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
231 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
232 self.errorhandler(self, self.ProgrammingError, msg.args[0])
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
233 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
234 self.messages.append((TypeError, msg))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
235 self.errorhandler(self, TypeError, msg)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
236 except:
54
6e31278d3433 There's no good reason to delay imports when the module is (1) useless without
kylev
parents: 31
diff changeset
237 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
238 del traceback
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
239 self.errorhandler(self, exc, value)
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: 66
diff changeset
240
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
241 if not self._defer_warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
242 self._warning_check()
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
243 return self.rowcount
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
244
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
245 def callproc(self, procname, args=()):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
246 """Execute stored procedure procname with args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
247
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
248 procname
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
249 string, name of procedure to execute on server
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
250
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
251 args
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
252 Sequence of parameters to use with procedure
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
253
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
254 Returns the original args.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
255
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
256 Compatibility warning: PEP-249 specifies that any modified
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
257 parameters must be returned. This is currently impossible
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
258 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
259 variable and then retrieved by a query. Since stored
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
260 procedures return zero or more result sets, there is no
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
261 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
262 The server variables are named @_procname_n, where procname
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
263 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
264 (from zero). Once all result sets generated by the procedure
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
265 have been fetched, you can issue a SELECT @_procname_0, ...
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
266 query using .execute() to get any OUT or INOUT values.
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: The act of calling a stored procedure
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
269 itself creates an empty result set. This appears after any
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
270 result sets generated by the procedure. This is non-standard
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
271 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
272 to advance through all result sets; otherwise you may get
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
273 disconnected.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
274 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
275
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
276 db = self._get_db()
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
277 charset = self.connection.character_set_name()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
278 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
279 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
280 self.connection.literal(arg))
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
281 if isinstance(query, unicode):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
282 query = query.encode(charset)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
283 self._query(query)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
284 self.nextset()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
285
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
286 query = "CALL %s(%s)" % (procname,
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
287 ','.join(['@_%s_%d' % (procname, i)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
288 for i in range(len(args))]))
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
289 if isinstance(query, unicode):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
290 query = query.encode(charset)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
291 self._query(query)
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
292 self._executed = query
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
293 if not self._defer_warnings:
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
294 self._warning_check()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
295 return args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
296
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
297 def _do_query(self, query):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
298 """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
299 connection = self._get_db()
18
d55bfb1a4701 Tons of changes from major refactoring/cleanup. This is all really broken
adustman
parents: 14
diff changeset
300 self._executed = query
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
301 connection.query(query)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
302 self._do_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
303 return self.rowcount
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
304
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
305 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
306 """Low-level fetch_row wrapper."""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
307 if not self._result:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
308 return ()
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: 66
diff changeset
309 return self._result.fetch_row(size, self._fetch_type)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
310
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
311 def __iter__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
312 return iter(self.fetchone, None)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
313
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
314 def _get_result(self):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
315 """Low-level; uses mysql_store_result()"""
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
316 return self._get_db().store_result()
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
317
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
318 def _query(self, query):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
319 """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
320 rowcount = self._do_query(query)
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
321 self._post_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
322 return rowcount
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
323
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
324 def _post_get_result(self):
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
325 """Low-level"""
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
326 self._rows = self._fetch_row(0)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
327 self._result = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
328
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
329 def fetchone(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
330 """Fetches a single row from the cursor. None indicates that
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
331 no more rows are available."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
332 self._check_executed()
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
333 if self.rownumber >= len(self._rows):
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
334 return None
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
335 result = self._rows[self.rownumber]
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
336 self.rownumber += 1
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
337 return result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
338
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
339 def fetchmany(self, size=None):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
340 """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
341 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
342 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
343 end = self.rownumber + (size or self.arraysize)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
344 result = self._rows[self.rownumber:end]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
345 self.rownumber = min(end, len(self._rows))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
346 return result
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 def fetchall(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
349 """Fetchs all available rows from the cursor."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
350 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
351 if self.rownumber:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
352 result = self._rows[self.rownumber:]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
353 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
354 result = self._rows
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
355 self.rownumber = len(self._rows)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
356 return result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
357
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
358 def scroll(self, value, mode='relative'):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
359 """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
360 to mode.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
361
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
362 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
363 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
364 value states an absolute target position."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
365 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
366 if mode == 'relative':
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
367 row = self.rownumber + value
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
368 elif mode == 'absolute':
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
369 row = value
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
370 else:
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
371 self.errorhandler(self, self.ProgrammingError,
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
372 "unknown scroll mode %s" % `mode`)
10
3f4c6af70e52 Me and PyLint had a knife fight, but PyLint had a gun.
adustman
parents: 8
diff changeset
373 if row < 0 or row >= len(self._rows):
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
374 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
375 self.rownumber = row
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
376
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
377 def __iter__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
378 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
379 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
380 return iter(result)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
381
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
382 _fetch_type = 0