@@ -107,6 +107,7 @@ pysqlite_cursor_init_impl(pysqlite_Cursor *self,
107
107
108
108
self -> arraysize = 1 ;
109
109
self -> closed = 0 ;
110
+ self -> rowcount = -1L ;
110
111
111
112
Py_INCREF (Py_None );
112
113
Py_XSETREF (self -> row_factory , Py_None );
@@ -855,7 +856,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
855
856
goto error ;
856
857
}
857
858
858
- if (self -> statement -> in_use ) {
859
+ if (sqlite3_stmt_readonly ( self -> statement -> st ) ) {
859
860
Py_SETREF (self -> statement ,
860
861
pysqlite_statement_create (self -> connection , operation ));
861
862
if (self -> statement == NULL ) {
@@ -866,6 +867,14 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
866
867
stmt_reset (self -> statement );
867
868
stmt_mark_dirty (self -> statement );
868
869
870
+ // Save current row count
871
+ if (sqlite3_stmt_readonly (self -> statement -> st )) {
872
+ self -> rowcount = -1L ;
873
+ }
874
+ else {
875
+ self -> rowcount = (long )sqlite3_total_changes (self -> connection -> db );
876
+ }
877
+
869
878
/* We start a transaction implicitly before a DML statement.
870
879
SELECT is the only exception. See #9924. */
871
880
if (self -> connection -> isolation_level
@@ -972,6 +981,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
972
981
self -> locked = 0 ;
973
982
974
983
if (PyErr_Occurred ()) {
984
+ self -> rowcount = -1L ;
975
985
return NULL ;
976
986
} else {
977
987
return Py_NewRef ((PyObject * )self );
@@ -1299,6 +1309,19 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self)
1299
1309
Py_RETURN_NONE ;
1300
1310
}
1301
1311
1312
+ static PyObject *
1313
+ get_rowcount (pysqlite_Cursor * self , void * Py_UNUSED (closure ))
1314
+ {
1315
+ if (!check_cursor (self )) {
1316
+ return -1 ;
1317
+ }
1318
+ if (self -> rowcount != -1L ) {
1319
+ long changes = (long )sqlite3_total_changes (self -> connection -> db );
1320
+ return PyLong_FromLong (changes - self -> rowcount );
1321
+ }
1322
+ return PyLong_FromLong (-1L );
1323
+ }
1324
+
1302
1325
static PyMethodDef cursor_methods [] = {
1303
1326
PYSQLITE_CURSOR_CLOSE_METHODDEF
1304
1327
PYSQLITE_CURSOR_EXECUTEMANY_METHODDEF
@@ -1312,21 +1335,6 @@ static PyMethodDef cursor_methods[] = {
1312
1335
{NULL , NULL }
1313
1336
};
1314
1337
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
-
1330
1338
static struct PyMemberDef cursor_members [] =
1331
1339
{
1332
1340
{"connection" , T_OBJECT , offsetof(pysqlite_Cursor , connection ), READONLY },
@@ -1338,6 +1346,11 @@ static struct PyMemberDef cursor_members[] =
1338
1346
{NULL }
1339
1347
};
1340
1348
1349
+ static PyGetSetDef cursor_getset [] = {
1350
+ {"rowcount" , (getter )get_rowcount , (setter )NULL },
1351
+ {NULL },
1352
+ };
1353
+
1341
1354
static const char cursor_doc [] =
1342
1355
PyDoc_STR ("SQLite database cursor class." );
1343
1356
0 commit comments