annotate MySQLdb/cursors.py @ 0:e48810735f11 MySQLdb

Copying 1.2.1 to be the new trunk
author adustman
date Sun, 02 Apr 2006 18:20:53 +0000
parents
children b5a377255eea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
1 """MySQLdb Cursors
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
2
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
3 This module implements Cursors of various types for MySQLdb. By
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
4 default, MySQLdb uses the Cursor class.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
5
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
6 """
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 import re
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
9 insert_values = re.compile(r'\svalues\s*(\(.+\))', re.IGNORECASE)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
10 from _mysql_exceptions import Warning, Error, InterfaceError, DataError, \
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
11 DatabaseError, OperationalError, IntegrityError, InternalError, \
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
12 NotSupportedError, ProgrammingError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
13
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
14
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
15 class BaseCursor(object):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
16
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
17 """A base for Cursor classes. Useful attributes:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
18
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
19 description
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
20 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
21 the last executed query; see PEP-249 for details.
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 description_flags
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
24 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
25 in the result set. Values correspond to those in
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
26 MySQLdb.constants.FLAG. See MySQL documentation (C API)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
27 for more information. Non-standard extension.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
28
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
29 arraysize
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
30 default number of rows fetchmany() will fetch
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
31
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
32 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
33
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
34 from _mysql_exceptions import MySQLError, Warning, Error, InterfaceError, \
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
35 DatabaseError, DataError, OperationalError, IntegrityError, \
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
36 InternalError, ProgrammingError, NotSupportedError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
37
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
38 def __init__(self, connection):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
39 from weakref import proxy
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 self.connection = proxy(connection)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
42 self.description = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
43 self.description_flags = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
44 self.rowcount = -1
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
45 self.arraysize = 1
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
46 self._executed = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
47 self.lastrowid = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
48 self.messages = []
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
49 self.errorhandler = connection.errorhandler
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
50 self._result = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
51 self._warnings = 0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
52 self._info = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
53 self.rownumber = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
54
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
55 def __del__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
56 self.close()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
57 self.errorhandler = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
58 self._result = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
59
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
60 def close(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
61 """Close the cursor. No further queries will be possible."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
62 if not self.connection: return
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
63 while self.nextset(): pass
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
64 self.connection = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
65
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
66 def _check_executed(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
67 if not self._executed:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
68 self.errorhandler(self, ProgrammingError, "execute() first")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
69
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
70 def _warning_check(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
71 from warnings import warn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
72 if self._warnings:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
73 warnings = self._get_db().show_warnings()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
74 if warnings:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
75 # This is done in two loops in case
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
76 # Warnings are set to raise exceptions.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
77 for w in warnings:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
78 self.messages.append((self.Warning, w))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
79 for w in warnings:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
80 warn(w[-1], self.Warning, 3)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
81 elif self._info:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
82 self.messages.append((self.Warning, self._info))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
83 warn(self._info, self.Warning, 3)
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 nextset(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
86 """Advance to the next result set.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
87
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
88 Returns None if there are no more result sets.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
89 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
90 if self._executed:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
91 self.fetchall()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
92 del self.messages[:]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
93
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
94 db = self._get_db()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
95 nr = db.next_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
96 if nr == -1:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
97 return None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
98 self._do_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
99 self._post_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
100 self._warning_check()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
101 return 1
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
102
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
103 def _post_get_result(self): pass
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 def _do_get_result(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
106 db = self._get_db()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
107 self._result = self._get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
108 self.rowcount = db.affected_rows()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
109 self.rownumber = 0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
110 self.description = self._result and self._result.describe() or None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
111 self.description_flags = self._result and self._result.field_flags() or None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
112 self.lastrowid = db.insert_id()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
113 self._warnings = db.warning_count()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
114 self._info = db.info()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
115
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
116 def setinputsizes(self, *args):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
117 """Does nothing, required by DB API."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
118
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
119 def setoutputsizes(self, *args):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
120 """Does nothing, required by DB API."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
121
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
122 def _get_db(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
123 if not self.connection:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
124 self.errorhandler(self, ProgrammingError, "cursor closed")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
125 return self.connection
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
126
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
127 def execute(self, query, args=None):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
128
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
129 """Execute a query.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
130
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
131 query -- string, query to execute on server
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
132 args -- optional sequence or mapping, parameters to use with query.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
133
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
134 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
135 parameter placeholder in the query. If a mapping is used,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
136 %(key)s must be used as the placeholder.
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 Returns long integer rows affected, if any
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
139
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
140 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
141 from types import ListType, TupleType
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
142 from sys import exc_info
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
143 del self.messages[:]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
144 db = self._get_db()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
145 charset = db.character_set_name()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
146 query = query.encode(charset)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
147 if args is not None:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
148 query = query % db.literal(args)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
149 try:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
150 r = self._query(query)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
151 except TypeError, m:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
152 if m.args[0] in ("not enough arguments for format string",
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
153 "not all arguments converted"):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
154 self.messages.append((ProgrammingError, m.args[0]))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
155 self.errorhandler(self, ProgrammingError, m.args[0])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
156 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
157 self.messages.append((TypeError, m))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
158 self.errorhandler(self, TypeError, m)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
159 except:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
160 exc, value, tb = exc_info()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
161 del tb
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
162 self.messages.append((exc, value))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
163 self.errorhandler(self, exc, value)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
164 self._executed = query
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
165 self._warning_check()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
166 return r
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
167
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
168 def executemany(self, query, args):
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 """Execute a multi-row query.
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 query -- string, query to execute on server
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
173
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
174 args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
175
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
176 Sequence of sequences or mappings, parameters to use with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
177 query.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
178
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
179 Returns long integer rows affected, if any.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
180
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
181 This method improves performance on multiple-row INSERT and
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
182 REPLACE. Otherwise it is equivalent to looping over args with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
183 execute().
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
184
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
185 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
186 del self.messages[:]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
187 db = self._get_db()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
188 if not args: return
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
189 m = insert_values.search(query)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
190 if not m:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
191 r = 0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
192 for a in args:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
193 r = r + self.execute(query, a)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
194 return r
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
195 p = m.start(1)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
196 charset = db.character_set_name()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
197 query = query.encode(charset)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
198 qv = query[p:]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
199 qargs = db.literal(args)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
200 try:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
201 q = [ query % qargs[0] ]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
202 q.extend([ qv % a for a in qargs[1:] ])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
203 except TypeError, msg:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
204 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
205 "not all arguments converted"):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
206 self.messages.append((ProgrammingError, msg.args[0]))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
207 self.errorhandler(self, ProgrammingError, msg.args[0])
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
208 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
209 self.messages.append((TypeError, msg))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
210 self.errorhandler(self, TypeError, msg)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
211 except:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
212 from sys import exc_info
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
213 exc, value, tb = exc_info()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
214 del tb
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
215 self.errorhandler(self, exc, value)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
216 r = self._query(',\n'.join(q))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
217 self._warning_check()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
218 return r
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 def callproc(self, procname, args=()):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
221
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
222 """Execute stored procedure procname with args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
223
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
224 procname -- string, name of procedure to execute on server
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
225
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
226 args -- Sequence of parameters to use with procedure
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
227
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
228 Returns the original args.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
229
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
230 Compatibility warning: PEP-249 specifies that any modified
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
231 parameters must be returned. This is currently impossible
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
232 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
233 variable and then retrieved by a query. Since stored
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
234 procedures return zero or more result sets, there is no
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
235 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
236 The server variables are named @_procname_n, where procname
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
237 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
238 (from zero). Once all result sets generated by the procedure
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
239 have been fetched, you can issue a SELECT @_procname_0, ...
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
240 query using .execute() to get any OUT or INOUT values.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
241
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
242 Compatibility warning: The act of calling a stored procedure
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
243 itself creates an empty result set. This appears after any
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
244 result sets generated by the procedure. This is non-standard
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
245 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
246 to advance through all result sets; otherwise you may get
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
247 disconnected.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
248 """
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
249
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
250 from types import UnicodeType
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
251 db = self._get_db()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
252 charset = db.character_set_name()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
253 for index, arg in enumerate(args):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
254 q = "SET @_%s_%d=%s" % (procname, index,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
255 db.literal(arg))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
256 if type(q) is UnicodeType:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
257 q = q.encode(charset)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
258 self._query(q)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
259 self.nextset()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
260
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
261 q = "CALL %s(%s)" % (procname,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
262 ','.join(['@_%s_%d' % (procname, i)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
263 for i in range(len(args))]))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
264 if type(q) is UnicodeType:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
265 q = q.encode(charset)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
266 self._query(q)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
267 self._warning_check()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
268 return args
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
269
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
270 def _do_query(self, q):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
271 db = self._get_db()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
272 self._last_executed = q
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
273 db.query(q)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
274 self._do_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
275 return self.rowcount
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
276
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
277 def _query(self, q): return self._do_query(q)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
278
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
279 def _fetch_row(self, size=1):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
280 if not self._result:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
281 return ()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
282 return self._result.fetch_row(size, self._fetch_type)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
283
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
284 def __iter__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
285 return iter(self.fetchone, None)
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 Warning = Warning
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
288 Error = Error
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
289 InterfaceError = InterfaceError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
290 DatabaseError = DatabaseError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
291 DataError = DataError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
292 OperationalError = OperationalError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
293 IntegrityError = IntegrityError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
294 InternalError = InternalError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
295 ProgrammingError = ProgrammingError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
296 NotSupportedError = NotSupportedError
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
297
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
298
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
299 class CursorStoreResultMixIn(object):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
300
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
301 """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
302 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
303 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
304 query, or using CursorUseResultMixIn instead."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
305
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
306 def _get_result(self): return self._get_db().store_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
307
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
308 def _query(self, q):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
309 rowcount = self._do_query(q)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
310 self._post_get_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
311 return rowcount
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
312
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
313 def _post_get_result(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
314 self._rows = self._fetch_row(0)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
315 self._result = None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
316
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
317 def fetchone(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
318 """Fetches a single row from the cursor. None indicates that
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
319 no more rows are available."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
320 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
321 if self.rownumber >= len(self._rows): return None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
322 result = self._rows[self.rownumber]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
323 self.rownumber = self.rownumber+1
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
324 return result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
325
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
326 def fetchmany(self, size=None):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
327 """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
328 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
329 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
330 end = self.rownumber + (size or self.arraysize)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
331 result = self._rows[self.rownumber:end]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
332 self.rownumber = min(end, len(self._rows))
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
333 return result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
334
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
335 def fetchall(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
336 """Fetchs all available rows from the cursor."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
337 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
338 if self.rownumber:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
339 result = self._rows[self.rownumber:]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
340 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
341 result = self._rows
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
342 self.rownumber = len(self._rows)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
343 return result
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
344
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
345 def scroll(self, value, mode='relative'):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
346 """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
347 to mode.
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
348
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
349 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
350 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
351 value states an absolute target position."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
352 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
353 if mode == 'relative':
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
354 r = self.rownumber + value
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
355 elif mode == 'absolute':
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
356 r = value
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
357 else:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
358 self.errorhandler(self, ProgrammingError,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
359 "unknown scroll mode %s" % `mode`)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
360 if r < 0 or r >= len(self._rows):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
361 self.errorhandler(self, IndexError, "out of range")
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
362 self.rownumber = r
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
363
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
364 def __iter__(self):
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 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
367 return iter(result)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
368
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
369
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
370 class CursorUseResultMixIn(object):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
371
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
372 """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
373 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
374 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
375 close() the cursor before additional queries can be peformed on
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
376 the connection."""
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 _get_result(self): return self._get_db().use_result()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
379
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
380 def fetchone(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
381 """Fetches a single row from the cursor."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
382 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
383 r = self._fetch_row(1)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
384 if not r: return None
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
385 self.rownumber = self.rownumber + 1
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
386 return r[0]
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
387
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
388 def fetchmany(self, size=None):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
389 """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
390 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
391 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
392 r = self._fetch_row(size or self.arraysize)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
393 self.rownumber = self.rownumber + len(r)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
394 return r
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
395
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
396 def fetchall(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
397 """Fetchs all available rows from the cursor."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
398 self._check_executed()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
399 r = self._fetch_row(0)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
400 self.rownumber = self.rownumber + len(r)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
401 return r
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
402
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
403 def __iter__(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
404 return self
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
405
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
406 def next(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
407 row = self.fetchone()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
408 if row is None:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
409 raise StopIteration
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
410 return row
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
411
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
412
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
413 class CursorTupleRowsMixIn(object):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
414
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
415 """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
416 which is the standard form required by DB API."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
417
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
418 _fetch_type = 0
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
419
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 class CursorDictRowsMixIn(object):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
422
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
423 """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
424 dictionaries. This is a non-standard feature."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
425
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
426 _fetch_type = 1
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
427
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
428 def fetchoneDict(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
429 """Fetch a single row as a dictionary. Deprecated:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
430 Use fetchone() instead. Will be removed in 1.3."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
431 from warnings import warn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
432 warn("fetchoneDict() is non-standard and will be removed in 1.3",
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
433 DeprecationWarning, 2)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
434 return self.fetchone()
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 fetchmanyDict(self, size=None):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
437 """Fetch several rows as a list of dictionaries. Deprecated:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
438 Use fetchmany() instead. Will be removed in 1.3."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
439 from warnings import warn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
440 warn("fetchmanyDict() is non-standard and will be removed in 1.3",
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
441 DeprecationWarning, 2)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
442 return self.fetchmany(size)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
443
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
444 def fetchallDict(self):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
445 """Fetch all available rows as a list of dictionaries. Deprecated:
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
446 Use fetchall() instead. Will be removed in 1.3."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
447 from warnings import warn
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
448 warn("fetchallDict() is non-standard and will be removed in 1.3",
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
449 DeprecationWarning, 2)
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
450 return self.fetchall()
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
451
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
452
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
453 class CursorOldDictRowsMixIn(CursorDictRowsMixIn):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
454
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
455 """This is a MixIn class that returns rows as dictionaries with
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
456 the same key convention as the old Mysqldb (MySQLmodule). Don't
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
457 use this."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
458
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
459 _fetch_type = 2
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
460
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
461
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
462 class Cursor(CursorStoreResultMixIn, CursorTupleRowsMixIn,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
463 BaseCursor):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
464
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
465 """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
466 and stores the result set in the client."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
467
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
468
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
469 class DictCursor(CursorStoreResultMixIn, CursorDictRowsMixIn,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
470 BaseCursor):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
471
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
472 """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
473 stores the result set in the client."""
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
474
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 class SSCursor(CursorUseResultMixIn, CursorTupleRowsMixIn,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
477 BaseCursor):
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 """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
480 the result set in the server."""
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
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
483 class SSDictCursor(CursorUseResultMixIn, CursorDictRowsMixIn,
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
484 BaseCursor):
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
485
e48810735f11 Copying 1.2.1 to be the new trunk
adustman
parents:
diff changeset
486 """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
487 stores the result set in the server."""
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