Skip to content

Commit

Permalink
New 'withhold' parameter for connection.cursor()
Browse files Browse the repository at this point in the history
  • Loading branch information
fogzot committed Aug 10, 2011
1 parent 30a046c commit 479bdf7
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
26 changes: 21 additions & 5 deletions psycopg/connection_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down Expand Up @@ -95,6 +108,9 @@ psyco_conn_cursor(connectionObject *self, PyObject *args, PyObject *keywds)
Py_DECREF(obj);
return NULL;
}

if (withhold == Py_True)
((cursorObject*)obj)->withhold = 1;

Dprintf("psyco_conn_cursor: new cursor at %p: refcnt = "
FORMAT_CODE_PY_SSIZE_T,
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions psycopg2.cproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
4 changes: 4 additions & 0 deletions psycopg2.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 13 additions & 1 deletion tests/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,11 @@ def test_invalid_name(self):
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)")
Expand All @@ -173,7 +178,14 @@ def test_withhold(self):
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")
self.conn.commit()
Expand Down

0 comments on commit 479bdf7

Please sign in to comment.