Skip to content

bpo-46249: Move set lastrowid out of the sqlite3 query loop #30489

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
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
22 changes: 11 additions & 11 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
int rc;
int numcols;
PyObject* column_name;
sqlite_int64 lastrowid;

if (!check_cursor(self)) {
goto error;
Expand Down Expand Up @@ -630,16 +629,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
self->rowcount= -1L;
}

if (!multiple) {
Py_BEGIN_ALLOW_THREADS
lastrowid = sqlite3_last_insert_rowid(self->connection->db);
Py_END_ALLOW_THREADS
Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid));
if (self->lastrowid == NULL) {
goto error;
}
}

if (rc == SQLITE_DONE && !multiple) {
pysqlite_statement_reset(self->statement);
Py_CLEAR(self->statement);
Expand All @@ -651,6 +640,17 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
Py_XDECREF(parameters);
}

if (!multiple) {
sqlite_int64 lastrowid;

Py_BEGIN_ALLOW_THREADS
lastrowid = sqlite3_last_insert_rowid(self->connection->db);
Py_END_ALLOW_THREADS

Py_SETREF(self->lastrowid, PyLong_FromLongLong(lastrowid));
// Fall through on error.
}

error:
Py_XDECREF(parameters);
Py_XDECREF(parameters_iter);
Expand Down