Bug fixes.

This commit is contained in:
adustman
2000-04-20 00:10:42 +00:00
parent dee24e5754
commit 4dcca6779b
3 changed files with 62 additions and 43 deletions

View File

@ -1,4 +1,4 @@
%define ver 0.2.0 %define ver 0.2.1
%define rel 1 %define rel 1
Summary: Python interface to MySQL-3.22 Summary: Python interface to MySQL-3.22
Name: MySQL-python Name: MySQL-python
@ -6,8 +6,8 @@ Version: %ver
Release: %rel Release: %rel
Copyright: Python-style Copyright: Python-style
Group: Applications/Databases Group: Applications/Databases
Source: ftp://starsip.python.net/pub/crew/adustman/MySQLdb-%ver.tar.gz Source: http://dustman.net/andy/python/MySQLdb/%ver/MySQLdb-%ver.tar.gz
URL: http://starship.python.net/crew/adustman URL: http://dustman.net/andy/python/MySQLdb
Requires: python >= 1.5.2 Requires: python >= 1.5.2
BuildRoot: /tmp/mysqldb-root BuildRoot: /tmp/mysqldb-root
Packager: Andy Dustman <adustman@comstar.net> Packager: Andy Dustman <adustman@comstar.net>

View File

@ -136,7 +136,7 @@ insert_values = re.compile(r'values\s(\(.+\))', re.IGNORECASE)
def escape_dict(d, qc): def escape_dict(d, qc):
d2 = {} d2 = {}
for k,v in d.items(): for k,v in d.items():
d2[k] = qc.get(type(v), String2literal)(v) d2[k] = qc.get(type(v), String2Literal)(v)
return d2 return d2
@ -149,13 +149,11 @@ class BaseCursor:
See the MySQL docs for more information.""" See the MySQL docs for more information."""
def __init__(self, connection, warnings=1): def __init__(self, connection):
self.connection = connection self.connection = connection
self.description = None self.description = None
self.rowcount = -1 self.rowcount = -1
self.result = None
self.arraysize = 100 self.arraysize = 100
self.warnings = warnings
def close(self): def close(self):
self.connection = None self.connection = None
@ -220,18 +218,27 @@ class BaseCursor:
for a in args[1:]: q.append(qv % escape(a, qc)) for a in args[1:]: q.append(qv % escape(a, qc))
return self._query(join(q, ',\n')) return self._query(join(q, ',\n'))
def _do_query(self, q): def __do_query(self, q):
from string import split, atoi from string import split, atoi
db = self.connection.db db = self.connection.db
print q
db.query(q) db.query(q)
self.result = self._get_result() self._result = self._get_result()
self.rowcount = db.affected_rows() self.rowcount = db.affected_rows()
self.description = self.result and self.result.describe() or None self.description = self._result and self._result.describe() or None
self._insert_id = db.insert_id() self._insert_id = db.insert_id()
self._info = db.info()
self._check_for_warnings()
return self.rowcount return self.rowcount
_query = _do_query def _check_for_warnings(self): pass
_query = __do_query
def info(self):
try: return self._info
except AttributeError: raise ProgrammingError, "execute() first"
def insert_id(self): def insert_id(self):
try: return self._insert_id try: return self._insert_id
except AttributeError: raise ProgrammingError, "execute() first" except AttributeError: raise ProgrammingError, "execute() first"
@ -241,28 +248,26 @@ class BaseCursor:
class CursorWarningMixIn: class CursorWarningMixIn:
def _query(self, q): def _check_for_warnings(self):
from string import atoi, split from string import atoi, split
r = self._do_query(q) if self._info:
w = self.connection.db.info() warnings = atoi(split(self._info)[-1])
if w:
warnings = atoi(split(w)[-1])
if warnings: if warnings:
raise Warning, w raise Warning, self.info
return r
class CursorStoreResultMixIn: class CursorStoreResultMixIn:
def _get_result(self): return self.connection.db.store_result() def _get_result(self): return self.connection.db.store_result()
def _do_query(self, q): def _query(self, q):
self.connection._acquire() self.connection._acquire()
try: try:
BaseCursor._do_query(self, q) rowcount = self._BaseCursor__do_query(q)
self._rows = self.result and self._fetch_all_rows() or () self._rows = self._result and self._fetch_all_rows() or ()
self._pos = 0 self._pos = 0
del self.result del self._result
return rowcount
finally: finally:
self.connection._release() self.connection._release()
@ -300,18 +305,18 @@ class CursorStoreResultMixIn:
class CursorUseResultMixIn: class CursorUseResultMixIn:
def __init__(self, name=""): def __init__(self, connection):
BaseCursor.__init__(self, name="") BaseCursor.__init__(self, connection)
if not self.connection._acquire(0): if not self.connection._acquire(0):
raise ProgrammingError, "would deadlock" raise ProgrammingError, "would deadlock"
def close(self): def close(self):
self.connection._release() if self.connection: self.connection._release()
self.connection = None self.connection = None
def __del__(self): def __del__(self):
try: try:
del self.result del self._result
finally: finally:
self.close() self.close()
@ -340,16 +345,16 @@ class CursorUseResultMixIn:
class CursorTupleRowsMixIn: class CursorTupleRowsMixIn:
def _fetch_row(self): return self.result.fetch_row() def _fetch_row(self): return self._result.fetch_row()
def _fetch_rows(self, size): return self.result.fetch_rows(size) def _fetch_rows(self, size): return self._result.fetch_rows(size)
def _fetch_all_rows(self): return self.result.fetch_all_rows() def _fetch_all_rows(self): return self._result.fetch_all_rows()
class CursorDictRowsMixIn: class CursorDictRowsMixIn:
def _fetch_row(self): return self.result.fetch_row_as_dict() def _fetch_row(self): return self._result.fetch_row_as_dict()
def _fetch_rows(self, size): return self.result.fetch_rows_as_dict(size) def _fetch_rows(self, size): return self._result.fetch_rows_as_dict(size)
def _fetch_all_rows(self): return self.result.fetch_all_rows_as_dict() def _fetch_all_rows(self): return self._result.fetch_all_rows_as_dict()
## XXX Deprecated ## XXX Deprecated
@ -363,14 +368,25 @@ class CursorDictRowsMixIn:
return apply(self.fetchall, args, kwargs) return apply(self.fetchall, args, kwargs)
class Cursor(CursorWarningMixIn, CursorStoreResultMixIn, class CursorNW(CursorStoreResultMixIn, CursorTupleRowsMixIn,
CursorTupleRowsMixIn, BaseCursor): pass BaseCursor): pass
class DictCursor(CursorWarningMixIn, CursorStoreResultMixIn,
CursorDictRowsMixIn, BaseCursor): pass class Cursor(CursorWarningMixIn, CursorNW): pass
class SSCursor(CursorWarningMixIn, CursorUseResultMixIn,
CursorTupleRowsMixIn, BaseCursor): pass class DictCursorNW(CursorStoreResultMixIn, CursorDictRowsMixIn,
class SSDictCursor(CursorWarningMixIn, CursorUseResultMixIn, BaseCursor): pass
CursorDictRowsMixIn, BaseCursor): pass
class DictCursor(CursorWarningMixIn, DictCursorNW): pass
class SSCursorNW(CursorUseResultMixIn, CursorTupleRowsMixIn,
BaseCursor): pass
class SSCursor(CursorWarningMixIn, SSCursorNW): pass
class SSDictCursorNW(CursorUseResultMixIn, CursorDictRowsMixIn,
BaseCursor): pass
class SSDictCursor(CursorWarningMixIn, SSDictCursorNW): pass
class Connection: class Connection:

View File

@ -115,7 +115,7 @@ the patch. If that fails, read the FAQ.
and may reflect features of the next release. and may reflect features of the next release.
<sect1>FAQs <sect1>FAQs
<p>A FAQ is available at <p>A FAQ is available at
<htmlurl url="http://starship.python.net/crew/adustman/MySQLdb-FAQ.html">. <htmlurl url="http://dustman.net/andy/python/MySQLdb-FAQ.html">.
<sect>_mysql module<label id="_mysql"> <sect>_mysql module<label id="_mysql">
<P> <P>
If you want to write applications which are portable across databases, If you want to write applications which are portable across databases,
@ -371,7 +371,7 @@ creating a new subclass, assuming the new database is reasonably
standard. standard.
For an example of this, see the author's For an example of this, see the author's
<htmlurl url="http://starship.python.net/crew/adustman" <htmlurl url="http://dustman.net/andy/python"
name="SQLDict module">, which allows standard queries to be name="SQLDict module">, which allows standard queries to be
defined and accessed using an object which looks like a defined and accessed using an object which looks like a
dictionary, and reads/writes user-defined objects. dictionary, and reads/writes user-defined objects.
@ -381,7 +381,8 @@ you can easily derive your own subclasses. There are several Cursor
classes in MySQLdb: classes in MySQLdb:
<p> <p>
<descrip> <descrip>
<tag>CursorBase</tag> The base class for Cursor objects. <tag>BaseCursor</tag> The base class for Cursor objects.
This does not raise Warnings.
<tag>CursorWarningMixIn</tag> Causes the Warning exception to be raised <tag>CursorWarningMixIn</tag> Causes the Warning exception to be raised
on queries which produce warnings. on queries which produce warnings.
@ -428,6 +429,8 @@ Use only if you are dealing with potentially large result sets.
<tag/SSDictCursor/ Like <tt/SSCursor/ except it returns rows as <tag/SSDictCursor/ Like <tt/SSCursor/ except it returns rows as
dictionaries. dictionaries.
<tag/XXXCursorNW/> Cursors with the "NW" suffix do not raise Warnings.
</descrip> </descrip>
<p>For an example of how to use these classes, <p>For an example of how to use these classes,
read the code. If you need something more exotic than this, read the code. If you need something more exotic than this,