New 'withhold' parameter for connection.cursor()

This commit is contained in:
Federico Di Gregorio 2011-08-10 18:25:46 +02:00
parent 30a046c602
commit 479bdf7458
4 changed files with 39 additions and 6 deletions

View File

@ -53,15 +53,28 @@ static PyObject *
psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *keywds)
{
const char *name = NULL;
PyObject *obj, *factory = NULL;
PyObject *obj, *factory = NULL, *withhold = NULL;
static char *kwlist[] = {"name", "cursor_factory", NULL};
static char *kwlist[] = {"name", "cursor_factory", "withhold", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sO", kwlist,
&name, &factory)) {
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sOO", kwlist,
&name, &factory, &withhold)) {
return NULL;
}
if (withhold != NULL) {
if (withhold == Py_True && name == NULL) {
PyErr_SetString(ProgrammingError,
"'withhold=True can be specified only for named cursors");
return NULL;
}
if (withhold != NULL && withhold != Py_True && withhold != Py_False) {
PyErr_SetString(ProgrammingError,
"'withhold should be True or False");
return NULL;
}
}
EXC_IF_CONN_CLOSED(self);
if (self->status != CONN_STATUS_READY &&
@ -96,6 +109,9 @@ psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *keywds)
return NULL;
}
if (withhold == Py_True)
((cursorObject*)obj)->withhold = 1;
Dprintf("psyco_conn_cursor: new cursor at %p: refcnt = "
FORMAT_CODE_PY_SSIZE_T,
obj, Py_REFCNT(obj)
@ -525,7 +541,7 @@ psyco_conn_set_session(connectionObject *self, PyObject *args, PyObject *kwargs)
#define psyco_conn_autocommit_doc \
"set or return the autocommit status."
"Set or return the autocommit status."
static PyObject *
psyco_conn_autocommit_get(connectionObject *self)

View File

@ -237,6 +237,7 @@
<Compile Include="psycopg\green.c" />
<Compile Include="psycopg\notify_type.c" />
<Compile Include="psycopg\xid_type.c" />
<Compile Include="psycopg\bytes_format.c" />
</ItemGroup>
<ProjectExtensions>
<MonoDevelop>

View File

@ -35,6 +35,10 @@ Global
$4.inheritsSet = VisualStudio
$4.inheritsScope = text/plain
$4.scope = text/x-readme
$0.TextStylePolicy = $5
$5.inheritsSet = VisualStudio
$5.inheritsScope = text/plain
$5.scope = text/plain
name = psycopg2
EndGlobalSection
EndGlobal

View File

@ -159,6 +159,11 @@ class CursorTests(unittest.TestCase):
self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)])
def test_withhold(self):
self.assertRaises(psycopg2.ProgrammingError, self.conn.cursor,
withhold=True)
self.assertRaises(psycopg2.ProgrammingError, self.conn.cursor,
"NAME", withhold="")
curs = self.conn.cursor()
curs.execute("drop table if exists withhold")
curs.execute("create table withhold (data int)")
@ -173,6 +178,13 @@ class CursorTests(unittest.TestCase):
curs.execute("select data from withhold order by data")
self.conn.commit()
self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)])
curs.close()
curs = self.conn.cursor("W", withhold=True)
self.assertEqual(curs.withhold, True);
curs.execute("select data from withhold order by data")
self.conn.commit()
self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)])
curs = self.conn.cursor()
curs.execute("drop table withhold")