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) psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *keywds)
{ {
const char *name = NULL; 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, if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sOO", kwlist,
&name, &factory)) { &name, &factory, &withhold)) {
return NULL; 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); EXC_IF_CONN_CLOSED(self);
if (self->status != CONN_STATUS_READY && if (self->status != CONN_STATUS_READY &&
@ -95,6 +108,9 @@ psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *keywds)
Py_DECREF(obj); Py_DECREF(obj);
return NULL; return NULL;
} }
if (withhold == Py_True)
((cursorObject*)obj)->withhold = 1;
Dprintf("psyco_conn_cursor: new cursor at %p: refcnt = " Dprintf("psyco_conn_cursor: new cursor at %p: refcnt = "
FORMAT_CODE_PY_SSIZE_T, FORMAT_CODE_PY_SSIZE_T,
@ -525,7 +541,7 @@ psyco_conn_set_session(connectionObject *self, PyObject *args, PyObject *kwargs)
#define psyco_conn_autocommit_doc \ #define psyco_conn_autocommit_doc \
"set or return the autocommit status." "Set or return the autocommit status."
static PyObject * static PyObject *
psyco_conn_autocommit_get(connectionObject *self) psyco_conn_autocommit_get(connectionObject *self)

View File

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

View File

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

View File

@ -159,6 +159,11 @@ class CursorTests(unittest.TestCase):
self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)]) self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)])
def test_withhold(self): 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 = self.conn.cursor()
curs.execute("drop table if exists withhold") curs.execute("drop table if exists withhold")
curs.execute("create table withhold (data int)") curs.execute("create table withhold (data int)")
@ -173,7 +178,14 @@ class CursorTests(unittest.TestCase):
curs.execute("select data from withhold order by data") curs.execute("select data from withhold order by data")
self.conn.commit() self.conn.commit()
self.assertEqual(curs.fetchall(), [(10,), (20,), (30,)]) 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 = self.conn.cursor()
curs.execute("drop table withhold") curs.execute("drop table withhold")
self.conn.commit() self.conn.commit()