Building rows simplified

Dropped repeated checks for tuple_factory.
Internal functions refactored a bit.
This commit is contained in:
Daniele Varrazzo 2012-02-24 21:52:36 +00:00
parent efee049338
commit 94a53b48df

View File

@ -664,13 +664,14 @@ _psyco_curs_prefetch(cursorObject *self)
return i; return i;
} }
static PyObject * static int
_psyco_curs_buildrow_fill(cursorObject *self, PyObject *res, _psyco_curs_buildrow_fill(cursorObject *self, PyObject *res,
int row, int n, int istuple) int row, int n, int istuple)
{ {
int i, len, err; int i, len, err;
const char *str; const char *str;
PyObject *val; PyObject *val;
int rv = -1;
for (i=0; i < n; i++) { for (i=0; i < n; i++) {
if (PQgetisnull(self->pgres, row, i)) { if (PQgetisnull(self->pgres, row, i)) {
@ -685,59 +686,59 @@ _psyco_curs_buildrow_fill(cursorObject *self, PyObject *res,
Dprintf("_psyco_curs_buildrow: row %ld, element %d, len %d", Dprintf("_psyco_curs_buildrow: row %ld, element %d, len %d",
self->row, i, len); self->row, i, len);
val = typecast_cast(PyTuple_GET_ITEM(self->casts, i), str, len, if (!(val = typecast_cast(PyTuple_GET_ITEM(self->casts, i), str, len,
(PyObject*)self); (PyObject*)self))) {
goto exit;
}
if (val) { Dprintf("_psyco_curs_buildrow: val->refcnt = "
Dprintf("_psyco_curs_buildrow: val->refcnt = " FORMAT_CODE_PY_SSIZE_T,
FORMAT_CODE_PY_SSIZE_T, Py_REFCNT(val)
Py_REFCNT(val) );
); if (istuple) {
if (istuple) { PyTuple_SET_ITEM(res, i, val);
PyTuple_SET_ITEM(res, i, val);
}
else {
err = PySequence_SetItem(res, i, val);
Py_DECREF(val);
if (err == -1) {
Py_DECREF(res);
res = NULL;
break;
}
}
} }
else { else {
/* an error occurred in the type system, we return NULL to raise err = PySequence_SetItem(res, i, val);
an exception. the typecast code should already have set the Py_DECREF(val);
exception type and text */ if (err == -1) { goto exit; }
Py_DECREF(res);
res = NULL;
break;
} }
} }
return res;
rv = 0;
exit:
return rv;
} }
static PyObject * static PyObject *
_psyco_curs_buildrow(cursorObject *self, int row) _psyco_curs_buildrow(cursorObject *self, int row)
{ {
int n; int n;
int istuple;
PyObject *t = NULL;
PyObject *rv = NULL;
n = PQnfields(self->pgres); n = PQnfields(self->pgres);
return _psyco_curs_buildrow_fill(self, PyTuple_New(n), row, n, 1); istuple = (self->tuple_factory == Py_None);
}
static PyObject * if (istuple) {
_psyco_curs_buildrow_with_factory(cursorObject *self, int row) t = PyTuple_New(n);
{ }
int n; else {
PyObject *res; t = PyObject_CallFunctionObjArgs(self->tuple_factory, self, NULL);
}
if (!t) { goto exit; }
n = PQnfields(self->pgres); if (0 == _psyco_curs_buildrow_fill(self, t, row, n, istuple)) {
if (!(res = PyObject_CallFunctionObjArgs(self->tuple_factory, self, NULL))) rv = t;
return NULL; t = NULL;
}
exit:
Py_XDECREF(t);
return rv;
return _psyco_curs_buildrow_fill(self, res, row, n, 0);
} }
static PyObject * static PyObject *
@ -769,11 +770,7 @@ psyco_curs_fetchone(cursorObject *self, PyObject *args)
return Py_None; return Py_None;
} }
if (self->tuple_factory == Py_None) res = _psyco_curs_buildrow(self, self->row);
res = _psyco_curs_buildrow(self, self->row);
else
res = _psyco_curs_buildrow_with_factory(self, self->row);
self->row++; /* move the counter to next line */ self->row++; /* move the counter to next line */
/* if the query was async aggresively free pgres, to allow /* if the query was async aggresively free pgres, to allow
@ -820,11 +817,7 @@ psyco_curs_next_named(cursorObject *self)
return NULL; return NULL;
} }
if (self->tuple_factory == Py_None) res = _psyco_curs_buildrow(self, self->row);
res = _psyco_curs_buildrow(self, self->row);
else
res = _psyco_curs_buildrow_with_factory(self, self->row);
self->row++; /* move the counter to next line */ self->row++; /* move the counter to next line */
/* if the query was async aggresively free pgres, to allow /* if the query was async aggresively free pgres, to allow
@ -900,10 +893,7 @@ psyco_curs_fetchmany(cursorObject *self, PyObject *args, PyObject *kwords)
list = PyList_New(size); list = PyList_New(size);
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
if (self->tuple_factory == Py_None) res = _psyco_curs_buildrow(self, self->row);
res = _psyco_curs_buildrow(self, self->row);
else
res = _psyco_curs_buildrow_with_factory(self, self->row);
self->row++; self->row++;
@ -965,10 +955,7 @@ psyco_curs_fetchall(cursorObject *self, PyObject *args)
list = PyList_New(size); list = PyList_New(size);
for (i = 0; i < size; i++) { for (i = 0; i < size; i++) {
if (self->tuple_factory == Py_None) res = _psyco_curs_buildrow(self, self->row);
res = _psyco_curs_buildrow(self, self->row);
else
res = _psyco_curs_buildrow_with_factory(self, self->row);
self->row++; self->row++;