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
IGNORE_SIGPIPE = 4096
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):
self.close()
self.errorhandler = None
self._result = None
def close(self):
"""Close the cursor. No further queries will be possible."""
if not self.connection: return
del self.messages[:]
self.nextset()
while self.nextset(): pass
self.connection = None
self.errorhandler = None
self._result = None
def _check_executed(self):
if not self._executed:
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):
"""Advance to the next result set.
Returns None if there are no more result sets.
Note that MySQL does not support multiple result sets at this
time.
"""
del self.messages[:]
if self._executed:
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):
"""Does nothing, required by DB API."""
@ -168,16 +195,7 @@ class BaseCursor(object):
db = self._get_db()
db.query(q)
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()
info = db.info()
if info:
warnings = atoi(info.split()[-1])
if warnings:
warn(info, self.Warning, stacklevel=4)
self._do_get_result()
return self.rowcount
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 close(self):
"""Close the cursor. Further queries will not be possible."""
self._rows = ()
BaseCursor.close(self)
def _query(self, q):
rowcount = self._do_query(q)
self._rows = self._fetch_row(0)
@ -280,13 +293,6 @@ class CursorUseResultMixIn(object):
close() the cursor before additional queries can be peformed on
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 fetchone(self):

View File

@ -764,15 +764,15 @@ _mysql_ConnectionObject_next_result(
#if MYSQL_VERSION_ID >= 40100
err = mysql_next_result(&(self->connection));
#else
err = -1
err = -1;
#endif
Py_END_ALLOW_THREADS
if (err > 0) return _mysql_Exception(self);
Py_INCREF(Py_None);
return Py_None;
return PyInt_FromLong(err);
}
#if MYSQL_VERSION_ID >= 40100
static char _mysql_ConnectionObject_set_server_option__doc__[] =
"set_server_option(option) -- Enables or disables an option\n\
for the connection.\n\
@ -814,6 +814,22 @@ _mysql_ConnectionObject_sqlstate(
if (!PyArg_ParseTuple(args, "")) return NULL;
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
static char _mysql_ConnectionObject_errno__doc__[] =
@ -1956,6 +1972,12 @@ static PyMethodDef _mysql_ConnectionObject_methods[] = {
METH_VARARGS,
_mysql_ConnectionObject_sqlstate__doc__
},
{
"warning_count",
(PyCFunction)_mysql_ConnectionObject_warning_count,
METH_VARARGS,
_mysql_ConnectionObject_warning_count__doc__
},
#endif
#if MYSQL_VERSION_ID >= 32303
{