Skip to content

gh-89121: Keep the number of pending SQLite statements to a minimum #30379

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 8 commits into from
Jun 23, 2022
49 changes: 22 additions & 27 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,17 +508,13 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
}

if (self->statement != NULL) {
/* There is an active statement */
pysqlite_statement_reset(self->statement);
}

/* reset description and rowcount */
Py_INCREF(Py_None);
Py_SETREF(self->description, Py_None);
self->rowcount = 0L;

if (self->statement) {
// Reset pending statements on this cursor.
(void)pysqlite_statement_reset(self->statement);
}

Expand Down Expand Up @@ -557,6 +553,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
}

assert(!sqlite3_stmt_busy(self->statement->st));
while (1) {
parameters = PyIter_Next(parameters_iter);
if (!parameters) {
Expand All @@ -580,7 +577,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
PyErr_Clear();
}
}
(void)pysqlite_statement_reset(self->statement);
_pysqlite_seterror(state, self->connection->db);
goto error;
}
Expand Down Expand Up @@ -628,13 +624,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
self->rowcount= -1L;
}

if (rc == SQLITE_DONE && !multiple) {
pysqlite_statement_reset(self->statement);
Py_CLEAR(self->statement);
}

if (multiple) {
pysqlite_statement_reset(self->statement);
if (rc == SQLITE_DONE) {
(void)pysqlite_statement_reset(self->statement);
}
Py_XDECREF(parameters);
}
Expand All @@ -658,11 +649,17 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
self->locked = 0;

if (PyErr_Occurred()) {
if (self->statement) {
(void)pysqlite_statement_reset(self->statement);
Py_CLEAR(self->statement);
}
self->rowcount = -1L;
return NULL;
} else {
return Py_NewRef((PyObject *)self);
}
if (self->statement && !sqlite3_stmt_busy(self->statement->st)) {
Py_CLEAR(self->statement);
}
return Py_NewRef((PyObject *)self);
}

/*[clinic input]
Expand Down Expand Up @@ -789,25 +786,23 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)

sqlite3_stmt *stmt = self->statement->st;
assert(stmt != NULL);
if (sqlite3_data_count(stmt) == 0) {
(void)pysqlite_statement_reset(self->statement);
Py_CLEAR(self->statement);
return NULL;
}
assert(sqlite3_data_count(stmt) != 0);

PyObject *row = _pysqlite_fetch_one_row(self);
if (row == NULL) {
return NULL;
}
int rc = pysqlite_step(stmt);
if (rc == SQLITE_DONE) {
if (rc != SQLITE_ROW) {
(void)pysqlite_statement_reset(self->statement);
}
else if (rc != SQLITE_ROW) {
(void)_pysqlite_seterror(self->connection->state,
self->connection->db);
Py_DECREF(row);
return NULL;
Py_CLEAR(self->statement);

if (rc != SQLITE_DONE) {
(void)_pysqlite_seterror(self->connection->state,
self->connection->db);
Py_DECREF(row);
return NULL;
}
}
if (!Py_IsNone(self->row_factory)) {
PyObject *factory = self->row_factory;
Expand Down