mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-26 02:43:43 +03:00
Make asynchronous connections produce asynchronous cursors by default
Drop the async kwarg from cursor.execute(), cursors created by asynchronous connections will be asynchronous by default, ones created by synchronous connections will be synchronous. Mind that this might break third party subclasses of psycopg2.extensions.cursor, if they try to chain to the superclass in their execute() implementation and are passing the async kwarg. The example cursors in psycopg2.extras have been fixed no to do that.
This commit is contained in:
parent
d8ab5ac8a1
commit
01799e9137
|
@ -112,10 +112,10 @@ class DictCursor(DictCursorBase):
|
||||||
DictCursorBase.__init__(self, *args, **kwargs)
|
DictCursorBase.__init__(self, *args, **kwargs)
|
||||||
self._prefetch = 1
|
self._prefetch = 1
|
||||||
|
|
||||||
def execute(self, query, vars=None, async=0):
|
def execute(self, query, vars=None):
|
||||||
self.index = {}
|
self.index = {}
|
||||||
self._query_executed = 1
|
self._query_executed = 1
|
||||||
return _cursor.execute(self, query, vars, async)
|
return _cursor.execute(self, query, vars)
|
||||||
|
|
||||||
def callproc(self, procname, vars=None):
|
def callproc(self, procname, vars=None):
|
||||||
self.index = {}
|
self.index = {}
|
||||||
|
@ -201,10 +201,10 @@ class RealDictCursor(DictCursorBase):
|
||||||
DictCursorBase.__init__(self, *args, **kwargs)
|
DictCursorBase.__init__(self, *args, **kwargs)
|
||||||
self._prefetch = 0
|
self._prefetch = 0
|
||||||
|
|
||||||
def execute(self, query, vars=None, async=0):
|
def execute(self, query, vars=None):
|
||||||
self.column_mapping = []
|
self.column_mapping = []
|
||||||
self._query_executed = 1
|
self._query_executed = 1
|
||||||
return _cursor.execute(self, query, vars, async)
|
return _cursor.execute(self, query, vars)
|
||||||
|
|
||||||
def callproc(self, procname, vars=None):
|
def callproc(self, procname, vars=None):
|
||||||
self.column_mapping = []
|
self.column_mapping = []
|
||||||
|
@ -282,9 +282,9 @@ class LoggingConnection(_connection):
|
||||||
class LoggingCursor(_cursor):
|
class LoggingCursor(_cursor):
|
||||||
"""A cursor that logs queries using its connection logging facilities."""
|
"""A cursor that logs queries using its connection logging facilities."""
|
||||||
|
|
||||||
def execute(self, query, vars=None, async=0):
|
def execute(self, query, vars=None):
|
||||||
try:
|
try:
|
||||||
return _cursor.execute(self, query, vars, async)
|
return _cursor.execute(self, query, vars)
|
||||||
finally:
|
finally:
|
||||||
self.connection.log(self.query, self)
|
self.connection.log(self.query, self)
|
||||||
|
|
||||||
|
@ -325,9 +325,9 @@ class MinTimeLoggingConnection(LoggingConnection):
|
||||||
class MinTimeLoggingCursor(LoggingCursor):
|
class MinTimeLoggingCursor(LoggingCursor):
|
||||||
"""The cursor sub-class companion to `MinTimeLoggingConnection`."""
|
"""The cursor sub-class companion to `MinTimeLoggingConnection`."""
|
||||||
|
|
||||||
def execute(self, query, vars=None, async=0):
|
def execute(self, query, vars=None):
|
||||||
self.timestamp = time.time()
|
self.timestamp = time.time()
|
||||||
return LoggingCursor.execute(self, query, vars, async)
|
return LoggingCursor.execute(self, query, vars)
|
||||||
|
|
||||||
def callproc(self, procname, vars=None):
|
def callproc(self, procname, vars=None):
|
||||||
self.timestamp = time.time()
|
self.timestamp = time.time()
|
||||||
|
|
|
@ -305,7 +305,7 @@ static PyObject *_psyco_curs_validate_sql_basic(
|
||||||
}
|
}
|
||||||
|
|
||||||
#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) -- Execute query with bound vars."
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_psyco_curs_execute(cursorObject *self,
|
_psyco_curs_execute(cursorObject *self,
|
||||||
|
@ -442,29 +442,12 @@ _psyco_curs_execute(cursorObject *self,
|
||||||
static PyObject *
|
static PyObject *
|
||||||
psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
{
|
{
|
||||||
long int async;
|
|
||||||
PyObject *vars = NULL, *operation = NULL;
|
PyObject *vars = NULL, *operation = NULL;
|
||||||
|
|
||||||
static char *kwlist[] = {"query", "vars", "async", NULL};
|
static char *kwlist[] = {"query", "vars", NULL};
|
||||||
|
|
||||||
async = self->conn->async;
|
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist,
|
||||||
|
&operation, &vars)) {
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|Ol", kwlist,
|
|
||||||
&operation, &vars, &async)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (async != self->conn->async) {
|
|
||||||
if (async == 0)
|
|
||||||
psyco_set_error(ProgrammingError, (PyObject*)self,
|
|
||||||
"can't execute a synchronous query "
|
|
||||||
"from an asynchronous cursor",
|
|
||||||
NULL, NULL);
|
|
||||||
else
|
|
||||||
psyco_set_error(ProgrammingError, (PyObject*)self,
|
|
||||||
"can't execute an asynchronous query "
|
|
||||||
"from a synchronous cursor",
|
|
||||||
NULL, NULL);
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -489,7 +472,7 @@ psyco_curs_execute(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
|
|
||||||
EXC_IF_CURS_CLOSED(self);
|
EXC_IF_CURS_CLOSED(self);
|
||||||
|
|
||||||
if (_psyco_curs_execute(self, operation, vars, async)) {
|
if (_psyco_curs_execute(self, operation, vars, self->conn->async)) {
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return Py_None;
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
@ -958,41 +941,23 @@ psyco_curs_fetchall(cursorObject *self, PyObject *args)
|
||||||
/* callproc method - execute a stored procedure */
|
/* callproc method - execute a stored procedure */
|
||||||
|
|
||||||
#define psyco_curs_callproc_doc \
|
#define psyco_curs_callproc_doc \
|
||||||
"callproc(procname, parameters=None, async=0) -- Execute stored procedure."
|
"callproc(procname, parameters=None) -- Execute stored procedure."
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
psyco_curs_callproc(cursorObject *self, PyObject *args, PyObject *kwargs)
|
psyco_curs_callproc(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
{
|
{
|
||||||
const char *procname = NULL;
|
const char *procname = NULL;
|
||||||
char *sql = NULL;
|
char *sql = NULL;
|
||||||
long int async;
|
|
||||||
Py_ssize_t procname_len, i, nparameters = 0, sl = 0;
|
Py_ssize_t procname_len, i, nparameters = 0, sl = 0;
|
||||||
PyObject *parameters = Py_None;
|
PyObject *parameters = Py_None;
|
||||||
PyObject *operation = NULL;
|
PyObject *operation = NULL;
|
||||||
PyObject *res = NULL;
|
PyObject *res = NULL;
|
||||||
|
|
||||||
async = self->conn->async;
|
if (!PyArg_ParseTuple(args, "s#|O",
|
||||||
|
&procname, &procname_len, ¶meters
|
||||||
if (!PyArg_ParseTuple(args, "s#|Ol",
|
|
||||||
&procname, &procname_len, ¶meters, &async
|
|
||||||
))
|
))
|
||||||
{ return NULL; }
|
{ return NULL; }
|
||||||
|
|
||||||
if (async != self->conn->async) {
|
|
||||||
if (async == 0)
|
|
||||||
psyco_set_error(ProgrammingError, (PyObject*)self,
|
|
||||||
"can't do a synchronous function call "
|
|
||||||
"from an asynchronous cursor",
|
|
||||||
NULL, NULL);
|
|
||||||
else
|
|
||||||
psyco_set_error(ProgrammingError, (PyObject*)self,
|
|
||||||
"can't do an asynchronous function call "
|
|
||||||
"from a synchronous cursor",
|
|
||||||
NULL, NULL);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
EXC_IF_CURS_CLOSED(self);
|
EXC_IF_CURS_CLOSED(self);
|
||||||
|
|
||||||
if (self->name != NULL) {
|
if (self->name != NULL) {
|
||||||
|
@ -1021,7 +986,7 @@ psyco_curs_callproc(cursorObject *self, PyObject *args, PyObject *kwargs)
|
||||||
operation = PyString_FromString(sql);
|
operation = PyString_FromString(sql);
|
||||||
PyMem_Free((void*)sql);
|
PyMem_Free((void*)sql);
|
||||||
|
|
||||||
if (_psyco_curs_execute(self, operation, parameters, async)) {
|
if (_psyco_curs_execute(self, operation, parameters, self->conn->async)) {
|
||||||
Py_INCREF(parameters);
|
Py_INCREF(parameters);
|
||||||
res = parameters;
|
res = parameters;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user