Mercurial > p > mysql-python > mysqldb-2
comparison MySQLdb/cursors.py @ 4:b5a377255eea MySQLdb
Merge changes from MySQLdb-1.2 branch (448-455)
author | adustman |
---|---|
date | Tue, 24 Oct 2006 19:52:31 +0000 |
parents | e48810735f11 |
children | b70cce9bd065 |
comparison
equal
deleted
inserted
replaced
3:df195ac92df6 | 4:b5a377255eea |
---|---|
32 """ | 32 """ |
33 | 33 |
34 from _mysql_exceptions import MySQLError, Warning, Error, InterfaceError, \ | 34 from _mysql_exceptions import MySQLError, Warning, Error, InterfaceError, \ |
35 DatabaseError, DataError, OperationalError, IntegrityError, \ | 35 DatabaseError, DataError, OperationalError, IntegrityError, \ |
36 InternalError, ProgrammingError, NotSupportedError | 36 InternalError, ProgrammingError, NotSupportedError |
37 | 37 |
38 _defer_warnings = False | |
39 | |
38 def __init__(self, connection): | 40 def __init__(self, connection): |
39 from weakref import proxy | 41 from weakref import proxy |
40 | 42 |
41 self.connection = proxy(connection) | 43 self.connection = proxy(connection) |
42 self.description = None | 44 self.description = None |
141 from types import ListType, TupleType | 143 from types import ListType, TupleType |
142 from sys import exc_info | 144 from sys import exc_info |
143 del self.messages[:] | 145 del self.messages[:] |
144 db = self._get_db() | 146 db = self._get_db() |
145 charset = db.character_set_name() | 147 charset = db.character_set_name() |
146 query = query.encode(charset) | 148 if isinstance(query, unicode): |
149 query = query.encode(charset) | |
147 if args is not None: | 150 if args is not None: |
148 query = query % db.literal(args) | 151 query = query % db.literal(args) |
149 try: | 152 try: |
150 r = self._query(query) | 153 r = self._query(query) |
151 except TypeError, m: | 154 except TypeError, m: |
160 exc, value, tb = exc_info() | 163 exc, value, tb = exc_info() |
161 del tb | 164 del tb |
162 self.messages.append((exc, value)) | 165 self.messages.append((exc, value)) |
163 self.errorhandler(self, exc, value) | 166 self.errorhandler(self, exc, value) |
164 self._executed = query | 167 self._executed = query |
165 self._warning_check() | 168 if not self._defer_warnings: self._warning_check() |
166 return r | 169 return r |
167 | 170 |
168 def executemany(self, query, args): | 171 def executemany(self, query, args): |
169 | 172 |
170 """Execute a multi-row query. | 173 """Execute a multi-row query. |
212 from sys import exc_info | 215 from sys import exc_info |
213 exc, value, tb = exc_info() | 216 exc, value, tb = exc_info() |
214 del tb | 217 del tb |
215 self.errorhandler(self, exc, value) | 218 self.errorhandler(self, exc, value) |
216 r = self._query(',\n'.join(q)) | 219 r = self._query(',\n'.join(q)) |
217 self._warning_check() | 220 if not self._defer_warnings: self._warning_check() |
218 return r | 221 return r |
219 | 222 |
220 def callproc(self, procname, args=()): | 223 def callproc(self, procname, args=()): |
221 | 224 |
222 """Execute stored procedure procname with args | 225 """Execute stored procedure procname with args |
251 db = self._get_db() | 254 db = self._get_db() |
252 charset = db.character_set_name() | 255 charset = db.character_set_name() |
253 for index, arg in enumerate(args): | 256 for index, arg in enumerate(args): |
254 q = "SET @_%s_%d=%s" % (procname, index, | 257 q = "SET @_%s_%d=%s" % (procname, index, |
255 db.literal(arg)) | 258 db.literal(arg)) |
256 if type(q) is UnicodeType: | 259 if isinstance(q, unicode): |
257 q = q.encode(charset) | 260 q = q.encode(charset) |
258 self._query(q) | 261 self._query(q) |
259 self.nextset() | 262 self.nextset() |
260 | 263 |
261 q = "CALL %s(%s)" % (procname, | 264 q = "CALL %s(%s)" % (procname, |
262 ','.join(['@_%s_%d' % (procname, i) | 265 ','.join(['@_%s_%d' % (procname, i) |
263 for i in range(len(args))])) | 266 for i in range(len(args))])) |
264 if type(q) is UnicodeType: | 267 if type(q) is UnicodeType: |
265 q = q.encode(charset) | 268 q = q.encode(charset) |
266 self._query(q) | 269 self._query(q) |
267 self._warning_check() | 270 self._executed = q |
271 if not self._defer_warnings: self._warning_check() | |
268 return args | 272 return args |
269 | 273 |
270 def _do_query(self, q): | 274 def _do_query(self, q): |
271 db = self._get_db() | 275 db = self._get_db() |
272 self._last_executed = q | 276 self._last_executed = q |
373 in the server and sent row-by-row to client side, i.e. it uses | 377 in the server and sent row-by-row to client side, i.e. it uses |
374 mysql_use_result(). You MUST retrieve the entire result set and | 378 mysql_use_result(). You MUST retrieve the entire result set and |
375 close() the cursor before additional queries can be peformed on | 379 close() the cursor before additional queries can be peformed on |
376 the connection.""" | 380 the connection.""" |
377 | 381 |
382 _defer_warnings = True | |
383 | |
378 def _get_result(self): return self._get_db().use_result() | 384 def _get_result(self): return self._get_db().use_result() |
379 | 385 |
380 def fetchone(self): | 386 def fetchone(self): |
381 """Fetches a single row from the cursor.""" | 387 """Fetches a single row from the cursor.""" |
382 self._check_executed() | 388 self._check_executed() |
383 r = self._fetch_row(1) | 389 r = self._fetch_row(1) |
384 if not r: return None | 390 if not r: |
391 self._warning_check() | |
392 return None | |
385 self.rownumber = self.rownumber + 1 | 393 self.rownumber = self.rownumber + 1 |
386 return r[0] | 394 return r[0] |
387 | 395 |
388 def fetchmany(self, size=None): | 396 def fetchmany(self, size=None): |
389 """Fetch up to size rows from the cursor. Result set may be smaller | 397 """Fetch up to size rows from the cursor. Result set may be smaller |
390 than size. If size is not defined, cursor.arraysize is used.""" | 398 than size. If size is not defined, cursor.arraysize is used.""" |
391 self._check_executed() | 399 self._check_executed() |
392 r = self._fetch_row(size or self.arraysize) | 400 r = self._fetch_row(size or self.arraysize) |
393 self.rownumber = self.rownumber + len(r) | 401 self.rownumber = self.rownumber + len(r) |
402 if not r: | |
403 self._warning_check() | |
394 return r | 404 return r |
395 | 405 |
396 def fetchall(self): | 406 def fetchall(self): |
397 """Fetchs all available rows from the cursor.""" | 407 """Fetchs all available rows from the cursor.""" |
398 self._check_executed() | 408 self._check_executed() |
399 r = self._fetch_row(0) | 409 r = self._fetch_row(0) |
400 self.rownumber = self.rownumber + len(r) | 410 self.rownumber = self.rownumber + len(r) |
411 self._warning_check() | |
401 return r | 412 return r |
402 | 413 |
403 def __iter__(self): | 414 def __iter__(self): |
404 return self | 415 return self |
405 | 416 |