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
Revert sqlite3_stmt_readonly change
  • Loading branch information
erlend-aasland committed Jun 6, 2022
commit c4f6926b9719f0eeba297390ae809d37f866d02e
7 changes: 0 additions & 7 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -887,13 +887,6 @@ def test_rowcount_executemany(self):
self.cu.executemany("insert into test(name) values (?)", [(1,), (2,), (3,)])
self.assertEqual(self.cu.rowcount, 3)

def test_rowcount_prefixed_with_comment(self):
# gh-79579: rowcount is updated even if query is prefixed with comments
self.cu.execute("/* foo */ insert into test(name) values (?)", ('foo',))
self.assertEqual(self.cu.rowcount, 1)
self.cu.execute("/* bar */ update test set name='bar' where name='foo'")
self.assertEqual(self.cu.rowcount, 2)

@unittest.skipIf(sqlite.sqlite_version_info < (3, 35, 0),
"Requires SQLite 3.35.0 or newer")
def test_rowcount_update_returning(self):
Expand Down
24 changes: 12 additions & 12 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -867,14 +867,6 @@ _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 All @@ -886,6 +878,14 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
}

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

while (1) {
parameters = PyIter_Next(parameters_iter);
if (!parameters) {
Expand Down Expand Up @@ -1315,11 +1315,11 @@ get_rowcount(pysqlite_Cursor *self, void *Py_UNUSED(closure))
if (!check_cursor(self)) {
return NULL;
}
if (self->rowcount != -1L) {
long changes = (long)sqlite3_total_changes(self->connection->db);
return PyLong_FromLong(changes - self->rowcount);
if (self->rowcount == -1L) {
return PyLong_FromLong(-1L);
}
return PyLong_FromLong(-1L);
long changes = (long)sqlite3_total_changes(self->connection->db);
return PyLong_FromLong(changes - self->rowcount);
}

static PyMethodDef cursor_methods[] = {
Expand Down