Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Calculate rowcount using sqlite3_total_changes() diff
  • Loading branch information
erlend-aasland committed Jun 5, 2022
commit 84891c6fffc6f44d92bc1f9d3cbad00cf3593fe1
45 changes: 29 additions & 16 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self,

self->arraysize = 1;
self->closed = 0;
self->rowcount = -1L;

Py_INCREF(Py_None);
Py_XSETREF(self->row_factory, Py_None);
Expand Down Expand Up @@ -855,7 +856,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
goto error;
}

if (self->statement->in_use) {
if (sqlite3_stmt_readonly(self->statement->st)) {
Py_SETREF(self->statement,
pysqlite_statement_create(self->connection, operation));
if (self->statement == NULL) {
Expand All @@ -866,6 +867,14 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
stmt_reset(self->statement);
stmt_mark_dirty(self->statement);

// Save current row count
if (sqlite3_stmt_readonly(self->statement->st)) {
self->rowcount = -1L;
}
else {
self->rowcount = (long)sqlite3_total_changes(self->connection->db);
}

/* We start a transaction implicitly before a DML statement.
SELECT is the only exception. See #9924. */
if (self->connection->isolation_level
Expand Down Expand Up @@ -972,6 +981,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
self->locked = 0;

if (PyErr_Occurred()) {
self->rowcount = -1L;
return NULL;
} else {
return Py_NewRef((PyObject *)self);
Expand Down Expand Up @@ -1299,6 +1309,19 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self)
Py_RETURN_NONE;
}

static PyObject *
get_rowcount(pysqlite_Cursor *self, void *Py_UNUSED(closure))
{
if (!check_cursor(self)) {
return -1;
}
if (self->rowcount != -1L) {
long changes = (long)sqlite3_total_changes(self->connection->db);
return PyLong_FromLong(changes - self->rowcount);
}
return PyLong_FromLong(-1L);
}

static PyMethodDef cursor_methods[] = {
PYSQLITE_CURSOR_CLOSE_METHODDEF
PYSQLITE_CURSOR_EXECUTEMANY_METHODDEF
Expand All @@ -1312,21 +1335,6 @@ static PyMethodDef cursor_methods[] = {
{NULL, NULL}
};

static PyObject *
get_rowcount(pysqlite_Cursor *self, void *Py_UNUSED(unused))
{
if (!check_cursor(self)) {
return NULL;
}
int changes = sqlite3_changes(self->connection->db);
return PyLong_FromLong(changes);
}

static PyGetSetDef cursor_getset[] = {
{"rowcount", (getter)get_rowcount, (setter)NULL},
{NULL},
};

static struct PyMemberDef cursor_members[] =
{
{"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
Expand All @@ -1338,6 +1346,11 @@ static struct PyMemberDef cursor_members[] =
{NULL}
};

static PyGetSetDef cursor_getset[] = {
{"rowcount", (getter)get_rowcount, (setter)NULL},
{NULL},
};

static const char cursor_doc[] =
PyDoc_STR("SQLite database cursor class.");

Expand Down
1 change: 1 addition & 0 deletions Modules/_sqlite/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ typedef struct
PyObject* row_cast_map;
int arraysize;
PyObject* lastrowid;
long rowcount; // saved row count
PyObject* row_factory;
pysqlite_Statement* statement;
int closed;
Expand Down