annotate MySQLdb/cursors.py @ 7:b1e508854b27 MySQLdb

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