Skip to content

Commit 79902e1

Browse files
Added support for returning the rowid of the last row modified by an operation
on a cursor (or None if no row was modified).
1 parent 5f70edd commit 79902e1

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

doc/src/api_manual/cursor.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,15 @@ Cursor Object
396396
mentioned in PEP 249 as an optional extension.
397397

398398

399+
.. data:: Cursor.lastrowid
400+
401+
This read-only attribute returns the rowid of the last row modified by the
402+
cursor. If no row was modified by the last operation performed on the
403+
cursor, the value None is returned.
404+
405+
.. versionadded:: 7.3
406+
407+
399408
.. attribute:: Cursor.outputtypehandler
400409

401410
This read-write attribute specifies a method called for each column that is

src/cxoCursor.c

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ static PyObject *cxoCursor_var(cxoCursor*, PyObject*, PyObject*);
3939
static PyObject *cxoCursor_arrayVar(cxoCursor*, PyObject*);
4040
static PyObject *cxoCursor_bindNames(cxoCursor*, PyObject*);
4141
static PyObject *cxoCursor_getDescription(cxoCursor*, void*);
42+
static PyObject *cxoCursor_getLastRowid(cxoCursor*, void*);
4243
static PyObject *cxoCursor_new(PyTypeObject*, PyObject*, PyObject*);
4344
static int cxoCursor_init(cxoCursor*, PyObject*, PyObject*);
4445
static PyObject *cxoCursor_repr(cxoCursor*);
@@ -117,6 +118,7 @@ static PyMemberDef cxoCursorMembers[] = {
117118
//-----------------------------------------------------------------------------
118119
static PyGetSetDef cxoCursorCalcMembers[] = {
119120
{ "description", (getter) cxoCursor_getDescription, 0, 0, 0 },
121+
{ "lastrowid", (getter) cxoCursor_getLastRowid, 0, 0, 0 },
120122
{ NULL }
121123
};
122124

@@ -608,6 +610,37 @@ static PyObject *cxoCursor_getDescription(cxoCursor *cursor, void *unused)
608610
}
609611

610612

613+
//-----------------------------------------------------------------------------
614+
// cxoCursor_getLastRowid()
615+
// Return the rowid of the last modified row if applicable. If no row was
616+
// modified the value None is returned.
617+
//-----------------------------------------------------------------------------
618+
static PyObject *cxoCursor_getLastRowid(cxoCursor *cursor, void *unused)
619+
{
620+
uint32_t rowidStrLength;
621+
const char *rowidStr;
622+
dpiRowid *rowid;
623+
624+
// make sure the cursor is open
625+
if (cxoCursor_isOpen(cursor) < 0)
626+
return NULL;
627+
628+
// get the value, if applicable
629+
if (cursor->handle) {
630+
if (dpiStmt_getLastRowid(cursor->handle, &rowid) < 0)
631+
return cxoError_raiseAndReturnNull();
632+
if (rowid) {
633+
if (dpiRowid_getStringValue(rowid, &rowidStr, &rowidStrLength) < 0)
634+
return cxoError_raiseAndReturnNull();
635+
return cxoPyString_fromEncodedString(rowidStr, rowidStrLength,
636+
cursor->connection->encodingInfo.encoding, NULL);
637+
}
638+
}
639+
640+
Py_RETURN_NONE;
641+
}
642+
643+
611644
//-----------------------------------------------------------------------------
612645
// cxoCursor_close()
613646
// Close the cursor. Any action taken on this cursor from this point forward

0 commit comments

Comments
 (0)