Add support for warning_count() and constants needed for

issuing multiple statements.
This commit is contained in:
adustman
2004-12-31 01:30:17 +00:00
parent 59cdc0aaea
commit 6943657d87
3 changed files with 68 additions and 34 deletions

View File

@ -21,3 +21,9 @@ INTERACTIVE = 1024
SSL = 2048 SSL = 2048
IGNORE_SIGPIPE = 4096 IGNORE_SIGPIPE = 4096
TRANSACTIONS = 8192 # mysql_com.h was WRONG prior to 3.23.35 TRANSACTIONS = 8192 # mysql_com.h was WRONG prior to 3.23.35
RESERVED = 16384
SECURE_CONNECTION = 32768
MULTI_STATEMENTS = 65536
MULTI_RESULTS = 131072

View File

@ -38,33 +38,60 @@ class BaseCursor(object):
def __del__(self): def __del__(self):
self.close() self.close()
self.errorhandler = None
self._result = None
def close(self): def close(self):
"""Close the cursor. No further queries will be possible.""" """Close the cursor. No further queries will be possible."""
if not self.connection: return if not self.connection: return
del self.messages[:] while self.nextset(): pass
self.nextset()
self.connection = None self.connection = None
self.errorhandler = None
self._result = None
def _check_executed(self): def _check_executed(self):
if not self._executed: if not self._executed:
self.errorhandler(self, ProgrammingError, "execute() first") self.errorhandler(self, ProgrammingError, "execute() first")
def _warning_info(self):
from string import atoi
db = self.connection
info = db.info()
try:
return db.warning_count(), info
except AttributeError:
if info:
return atoi(info.split()[-1]), info
else:
return 0, info
def nextset(self): def nextset(self):
"""Advance to the next result set. """Advance to the next result set.
Returns None if there are no more result sets. Returns None if there are no more result sets.
Note that MySQL does not support multiple result sets at this
time.
""" """
del self.messages[:] del self.messages[:]
if self._executed: if self._executed:
self.fetchall() self.fetchall()
return None
db = self._get_db()
nr = db.next_result()
if nr == -1:
return None
self._do_get_result()
return 1
def _do_get_result(self):
from warnings import warn
db = self.connection
self._result = self._get_result()
self.rowcount = db.affected_rows()
self.rownumber = 0
self.description = self._result and self._result.describe() or None
self.lastrowid = db.insert_id()
warnings, info = self._warning_info()
if warnings:
warn(info, self.Warning, stacklevel=6)
def setinputsizes(self, *args): def setinputsizes(self, *args):
"""Does nothing, required by DB API.""" """Does nothing, required by DB API."""
@ -168,16 +195,7 @@ class BaseCursor(object):
db = self._get_db() db = self._get_db()
db.query(q) db.query(q)
self._result = self._get_result() self._do_get_result()
self.rowcount = db.affected_rows()
self.rownumber = 0
self.description = self._result and self._result.describe() or None
self.lastrowid = db.insert_id()
info = db.info()
if info:
warnings = atoi(info.split()[-1])
if warnings:
warn(info, self.Warning, stacklevel=4)
return self.rowcount return self.rowcount
def _query(self, q): return self._do_query(q) def _query(self, q): return self._do_query(q)
@ -211,11 +229,6 @@ class CursorStoreResultMixIn(object):
def _get_result(self): return self._get_db().store_result() def _get_result(self): return self._get_db().store_result()
def close(self):
"""Close the cursor. Further queries will not be possible."""
self._rows = ()
BaseCursor.close(self)
def _query(self, q): def _query(self, q):
rowcount = self._do_query(q) rowcount = self._do_query(q)
self._rows = self._fetch_row(0) self._rows = self._fetch_row(0)
@ -280,13 +293,6 @@ class CursorUseResultMixIn(object):
close() the cursor before additional queries can be peformed on close() the cursor before additional queries can be peformed on
the connection.""" the connection."""
def close(self):
"""Close the cursor. No further queries can be executed."""
del self.messages[:]
self.nextset()
self._result = None
BaseCursor.close(self)
def _get_result(self): return self._get_db().use_result() def _get_result(self): return self._get_db().use_result()
def fetchone(self): def fetchone(self):

View File

@ -764,15 +764,15 @@ _mysql_ConnectionObject_next_result(
#if MYSQL_VERSION_ID >= 40100 #if MYSQL_VERSION_ID >= 40100
err = mysql_next_result(&(self->connection)); err = mysql_next_result(&(self->connection));
#else #else
err = -1 err = -1;
#endif #endif
Py_END_ALLOW_THREADS Py_END_ALLOW_THREADS
if (err > 0) return _mysql_Exception(self); if (err > 0) return _mysql_Exception(self);
Py_INCREF(Py_None); return PyInt_FromLong(err);
return Py_None;
} }
#if MYSQL_VERSION_ID >= 40100 #if MYSQL_VERSION_ID >= 40100
static char _mysql_ConnectionObject_set_server_option__doc__[] = static char _mysql_ConnectionObject_set_server_option__doc__[] =
"set_server_option(option) -- Enables or disables an option\n\ "set_server_option(option) -- Enables or disables an option\n\
for the connection.\n\ for the connection.\n\
@ -814,6 +814,22 @@ _mysql_ConnectionObject_sqlstate(
if (!PyArg_ParseTuple(args, "")) return NULL; if (!PyArg_ParseTuple(args, "")) return NULL;
return PyString_FromString(mysql_sqlstate(&(self->connection))); return PyString_FromString(mysql_sqlstate(&(self->connection)));
} }
static char _mysql_ConnectionObject_warning_count__doc__[] =
"Returns the number of warnings generated during execution\n\
of the previous SQL statement.\n\
\n\
Non-standard.\n\
";
static PyObject *
_mysql_ConnectionObject_warning_count(
_mysql_ConnectionObject *self,
PyObject *args)
{
if (!PyArg_ParseTuple(args, "")) return NULL;
return PyInt_FromLong(mysql_warning_count(&(self->connection)));
}
#endif #endif
static char _mysql_ConnectionObject_errno__doc__[] = static char _mysql_ConnectionObject_errno__doc__[] =
@ -1956,6 +1972,12 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = {
METH_VARARGS, METH_VARARGS,
_mysql_ConnectionObject_sqlstate__doc__ _mysql_ConnectionObject_sqlstate__doc__
}, },
{
"warning_count",
(PyCFunction)_mysql_ConnectionObject_warning_count,
METH_VARARGS,
_mysql_ConnectionObject_warning_count__doc__
},
#endif #endif
#if MYSQL_VERSION_ID >= 32303 #if MYSQL_VERSION_ID >= 32303
{ {