mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-01-31 09:24:07 +03:00
First try at .executemany() implementation.
This commit is contained in:
parent
9316c6af53
commit
19c5129d5b
|
@ -1,5 +1,13 @@
|
||||||
2005-03-12 Federico Di Gregorio <fog@debian.org>
|
2005-03-12 Federico Di Gregorio <fog@debian.org>
|
||||||
|
|
||||||
|
* psycopg/cursor_type.c (psyco_curs_executemany): implemented as a
|
||||||
|
wrapper to extract python arguments and then call
|
||||||
|
_psyco_curs_execute().
|
||||||
|
|
||||||
|
* psycopg/cursor_type.c (_psyco_curs_execute): splitted away
|
||||||
|
python argument parsing from the real execute code, to later allow
|
||||||
|
for .executemany().
|
||||||
|
|
||||||
* psycopg/cursor_type.c (_psyco_curs_buildrow_fill): modified to
|
* psycopg/cursor_type.c (_psyco_curs_buildrow_fill): modified to
|
||||||
call typecast_cast().
|
call typecast_cast().
|
||||||
|
|
||||||
|
|
|
@ -252,20 +252,12 @@ _mogrify(PyObject *var, PyObject *fmt, connectionObject *conn, PyObject **new)
|
||||||
#define psyco_curs_execute_doc \
|
#define psyco_curs_execute_doc \
|
||||||
"execute(query, vars=None, async=0) -> execute query with bound vars"
|
"execute(query, vars=None, async=0) -> execute query with bound vars"
|
||||||
|
|
||||||
static PyObject *
|
static int
|
||||||
psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
_psyco_curs_execute(cursorObject *self,
|
||||||
|
PyObject *operation, PyObject *vars, long int async)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
long int async = 0;
|
PyObject *fquery, *cvt = NULL, *uoperation = NULL;
|
||||||
PyObject *vars = NULL, *cvt = NULL, *operation = NULL;
|
|
||||||
PyObject *fquery, *uoperation = NULL;
|
|
||||||
|
|
||||||
static char *kwlist[] = {"query", "vars", "async", NULL};
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi", kwlist,
|
|
||||||
&operation, &vars, &async)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
pthread_mutex_lock(&(self->conn->lock));
|
pthread_mutex_lock(&(self->conn->lock));
|
||||||
if (self->conn->async_cursor != NULL
|
if (self->conn->async_cursor != NULL
|
||||||
|
@ -273,7 +265,7 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
pthread_mutex_unlock(&(self->conn->lock));
|
pthread_mutex_unlock(&(self->conn->lock));
|
||||||
PyErr_SetString(ProgrammingError,
|
PyErr_SetString(ProgrammingError,
|
||||||
"asynchronous query already in execution");
|
"asynchronous query already in execution");
|
||||||
return NULL;
|
return 0;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&(self->conn->lock));
|
pthread_mutex_unlock(&(self->conn->lock));
|
||||||
|
|
||||||
|
@ -291,11 +283,10 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
else {
|
else {
|
||||||
PyErr_Format(InterfaceError, "can't encode unicode query to %s",
|
PyErr_Format(InterfaceError, "can't encode unicode query to %s",
|
||||||
self->conn->encoding);
|
self->conn->encoding);
|
||||||
return NULL;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
EXC_IF_CURS_CLOSED(self);
|
|
||||||
IFCLEARPGRES(self->pgres);
|
IFCLEARPGRES(self->pgres);
|
||||||
|
|
||||||
if (self->query) {
|
if (self->query) {
|
||||||
|
@ -331,7 +322,7 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
{
|
{
|
||||||
if(_mogrify(vars, operation, self->conn, &cvt) == -1) {
|
if(_mogrify(vars, operation, self->conn, &cvt) == -1) {
|
||||||
Py_XDECREF(uoperation);
|
Py_XDECREF(uoperation);
|
||||||
return NULL;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -384,7 +375,7 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
PyErr_Restore(err, arg, trace);
|
PyErr_Restore(err, arg, trace);
|
||||||
}
|
}
|
||||||
Py_XDECREF(uoperation);
|
Py_XDECREF(uoperation);
|
||||||
return NULL;
|
return 0;
|
||||||
}
|
}
|
||||||
self->query = strdup(PyString_AS_STRING(fquery));
|
self->query = strdup(PyString_AS_STRING(fquery));
|
||||||
|
|
||||||
|
@ -403,15 +394,69 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
|
|
||||||
Py_XDECREF(uoperation);
|
Py_XDECREF(uoperation);
|
||||||
|
|
||||||
if (res == -1) {
|
return res == -1 ? 0 : 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
|
{
|
||||||
|
long int async = 0;
|
||||||
|
PyObject *vars = NULL, *operation = NULL;
|
||||||
|
|
||||||
|
static char *kwlist[] = {"query", "vars", "async", NULL};
|
||||||
|
|
||||||
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi", kwlist,
|
||||||
|
&operation, &vars, &async)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
|
EXC_IF_CURS_CLOSED(self);
|
||||||
|
|
||||||
|
if (_psyco_curs_execute(self, operation, vars, async)) {
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return Py_None;
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define psyco_curs_executemany_doc \
|
||||||
|
"execute(query, vars_list=(), async=0) -> execute many queries with bound vars"
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
psyco_curs_executemany(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
PyObject *operation = NULL, *vars = NULL;
|
||||||
|
|
||||||
|
|
||||||
|
static char *kwlist[] = {"query", "vars", NULL};
|
||||||
|
|
||||||
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Oi", kwlist,
|
||||||
|
&operation, &vars)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
EXC_IF_CURS_CLOSED(self);
|
||||||
|
|
||||||
|
for (i = 0; i < PySequence_Size(vars); i++) {
|
||||||
|
PyObject *v = PySequence_GetItem(vars, i);
|
||||||
|
if (!v || _psyco_curs_execute(self, operation, vars, 0) == 0) {
|
||||||
|
Py_XDECREF(v);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Py_DECREF(v);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Py_INCREF(Py_None);
|
||||||
|
return Py_None;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#ifdef PSYCOPG_EXTENSIONS
|
#ifdef PSYCOPG_EXTENSIONS
|
||||||
#define psyco_curs_mogrify_doc \
|
#define psyco_curs_mogrify_doc \
|
||||||
"mogrify(query, vars=None) -> return query after binding vars"
|
"mogrify(query, vars=None) -> return query after binding vars"
|
||||||
|
@ -1119,6 +1164,8 @@ static struct PyMethodDef cursorObject_methods[] = {
|
||||||
METH_VARARGS, psyco_curs_close_doc},
|
METH_VARARGS, psyco_curs_close_doc},
|
||||||
{"execute", (PyCFunction)psyco_curs_execute,
|
{"execute", (PyCFunction)psyco_curs_execute,
|
||||||
METH_VARARGS|METH_KEYWORDS, psyco_curs_execute_doc},
|
METH_VARARGS|METH_KEYWORDS, psyco_curs_execute_doc},
|
||||||
|
{"executemany", (PyCFunction)psyco_curs_executemany,
|
||||||
|
METH_VARARGS|METH_KEYWORDS, psyco_curs_executemany_doc},
|
||||||
{"fetchone", (PyCFunction)psyco_curs_fetchone,
|
{"fetchone", (PyCFunction)psyco_curs_fetchone,
|
||||||
METH_VARARGS, psyco_curs_fetchone_doc},
|
METH_VARARGS, psyco_curs_fetchone_doc},
|
||||||
{"fetchmany", (PyCFunction)psyco_curs_fetchmany,
|
{"fetchmany", (PyCFunction)psyco_curs_fetchmany,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user