Skip to content

Commit 5436db7

Browse files
Try lazy rowcount approach
Does not work bca. executemany, but it solves the repro
1 parent 4082c8e commit 5436db7

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

Modules/_sqlite/cursor.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self,
107107

108108
self->arraysize = 1;
109109
self->closed = 0;
110-
self->rowcount = -1L;
111110

112111
Py_INCREF(Py_None);
113112
Py_XSETREF(self->row_factory, Py_None);
@@ -835,10 +834,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
835834
stmt_reset(self->statement);
836835
}
837836

838-
/* reset description and rowcount */
837+
/* reset description */
839838
Py_INCREF(Py_None);
840839
Py_SETREF(self->description, Py_None);
841-
self->rowcount = 0L;
842840

843841
if (self->statement) {
844842
(void)stmt_reset(self->statement);
@@ -944,12 +942,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
944942
}
945943
}
946944

947-
if (self->statement->is_dml) {
948-
self->rowcount += (long)sqlite3_changes(self->connection->db);
949-
} else {
950-
self->rowcount= -1L;
951-
}
952-
953945
if (rc == SQLITE_DONE && !multiple) {
954946
stmt_reset(self->statement);
955947
Py_CLEAR(self->statement);
@@ -980,7 +972,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
980972
self->locked = 0;
981973

982974
if (PyErr_Occurred()) {
983-
self->rowcount = -1L;
984975
return NULL;
985976
} else {
986977
return Py_NewRef((PyObject *)self);
@@ -1321,13 +1312,27 @@ static PyMethodDef cursor_methods[] = {
13211312
{NULL, NULL}
13221313
};
13231314

1315+
static PyObject *
1316+
get_rowcount(pysqlite_Cursor *self, void *Py_UNUSED(unused))
1317+
{
1318+
if (!check_cursor(self)) {
1319+
return NULL;
1320+
}
1321+
int changes = sqlite3_changes(self->connection->db);
1322+
return PyLong_FromLong(changes);
1323+
}
1324+
1325+
static PyGetSetDef cursor_getset[] = {
1326+
{"rowcount", (getter)get_rowcount, (setter)NULL},
1327+
{NULL},
1328+
};
1329+
13241330
static struct PyMemberDef cursor_members[] =
13251331
{
13261332
{"connection", T_OBJECT, offsetof(pysqlite_Cursor, connection), READONLY},
13271333
{"description", T_OBJECT, offsetof(pysqlite_Cursor, description), READONLY},
13281334
{"arraysize", T_INT, offsetof(pysqlite_Cursor, arraysize), 0},
13291335
{"lastrowid", T_OBJECT, offsetof(pysqlite_Cursor, lastrowid), READONLY},
1330-
{"rowcount", T_LONG, offsetof(pysqlite_Cursor, rowcount), READONLY},
13311336
{"row_factory", T_OBJECT, offsetof(pysqlite_Cursor, row_factory), 0},
13321337
{"__weaklistoffset__", T_PYSSIZET, offsetof(pysqlite_Cursor, in_weakreflist), READONLY},
13331338
{NULL}
@@ -1346,6 +1351,7 @@ static PyType_Slot cursor_slots[] = {
13461351
{Py_tp_init, pysqlite_cursor_init},
13471352
{Py_tp_traverse, cursor_traverse},
13481353
{Py_tp_clear, cursor_clear},
1354+
{Py_tp_getset, cursor_getset},
13491355
{0, NULL},
13501356
};
13511357

Modules/_sqlite/cursor.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ typedef struct
3838
PyObject* row_cast_map;
3939
int arraysize;
4040
PyObject* lastrowid;
41-
long rowcount;
4241
PyObject* row_factory;
4342
pysqlite_Statement* statement;
4443
int closed;

0 commit comments

Comments
 (0)