executemany rowcount
Markus Demleitner <[email protected]>
| Newsgroups | gmane.comp.python.db.psycopg.devel |
|---|---|
| Message-ID | <[email protected]> |
Hi,
At some point between 2.0.6 and 2.0.8 the old executemany/rowcount
behaviour of just leaving the rowcount of the last execute alone got
fixed to have executemany always leave a rowcount of -1.
This somewhat rains on my parade since I'd have to special-case
update operations where users expect to see whether they've changed
anything. They used to work by chance since I'd execute only one
statement in that case anyway.
Now, I'd suggest the following behaviour for executemany and
rowcount:
We want meaningful rowcounts for an executemany. The rules are:
The rowcount is the sum of all individual rowcounts, except when
a single rowcount is -1. In that case, and when an error occurs,
the whole rowcount is -1.
I believe that either all or no operation in an executemany have a
rowcount of -1, so the somewhat nonsensical consequence of having
individual operations set real rowcounts and the whole operation
having -1 probably is not relevant, but if it is, I think the
proposed behaviour is still preferable to possible alternatives.
Attached is a patch against bzr trunk that (I think:-) would do this.
Cheers,
Markus
_______________________________________________
Psycopg mailing list
Psycopg-IAPFreCvJWPBWskQ1e/[email protected]
http://lists.initd.org/mailman/listinfo/psycopg
execmany-rowcount.patch
(text/plain, 1.4 KB)
=== modified file 'psycopg/cursor_type.c'
--- psycopg/cursor_type.c 2009-01-10 17:48:08 +0000
+++ psycopg/cursor_type.c 2009-02-06 10:52:51 +0000
@@ -498,6 +498,15 @@
{
PyObject *operation = NULL, *vars = NULL;
PyObject *v, *iter = NULL;
+ /* We want meaningful rowcounts for an executemany. The rules are:
+ * The rowcount is the sum of all individual rowcounts, except when
+ * a single rowcount is -1. In that case, and when an error occurs,
+ * the whole rowcount is -1.
+ *
+ * I believe that either all or no operation in an executemany have a
+ * rowcount of -1.
+ */
+ int rowcount_sum = 0;
static char *kwlist[] = {"query", "vars_list", NULL};
@@ -521,6 +530,7 @@
while ((v = PyIter_Next(vars)) != NULL) {
if (_psyco_curs_execute(self, operation, v, 0) == 0) {
+ self->rowcount = -1;
Py_DECREF(v);
Py_XDECREF(iter);
return NULL;
@@ -528,9 +538,15 @@
else {
Py_DECREF(v);
}
+ if (self->rowcount==-1) {
+ rowcount_sum = -1;
+ } else if (rowcount_sum>=0) {
+ rowcount_sum += self->rowcount;
+ } /* fall through: there's been a negative rowcount before, ignore
+ further rowcounts even if they are non-negative */
}
Py_XDECREF(iter);
- self->rowcount = -1;
+ self->rowcount = rowcount_sum;
Py_INCREF(Py_None);
return Py_None;