Skip to content

gh-88239: Use sqlite3_stmt_busy() to determine if statements are in use #25984

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 16 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
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
19 changes: 3 additions & 16 deletions Modules/_sqlite/cursor.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,10 @@ stmt_reset(pysqlite_Statement *self)
{
int rc = SQLITE_OK;

if (self->in_use && self->st) {
if (self->st != NULL) {
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_reset(self->st);
Py_END_ALLOW_THREADS

if (rc == SQLITE_OK) {
self->in_use = 0;
}
}

return rc;
Expand Down Expand Up @@ -770,12 +766,6 @@ bind_parameters(pysqlite_state *state, pysqlite_Statement *self,
}
}

static inline void
stmt_mark_dirty(pysqlite_Statement *self)
{
self->in_use = 1;
}

PyObject *
_pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation, PyObject* second_argument)
{
Expand Down Expand Up @@ -852,16 +842,15 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
goto error;
}

if (self->statement->in_use) {
if (sqlite3_stmt_busy(self->statement->st)) {
Py_SETREF(self->statement,
pysqlite_statement_create(self->connection, operation));
if (self->statement == NULL) {
goto error;
}
}

stmt_reset(self->statement);
stmt_mark_dirty(self->statement);
(void)stmt_reset(self->statement);
self->rowcount = self->statement->is_dml ? 0L : -1L;

/* We start a transaction implicitly before a DML statement.
Expand All @@ -882,8 +871,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
break;
}

stmt_mark_dirty(self->statement);

bind_parameters(state, self->statement, parameters);
if (PyErr_Occurred()) {
goto error;
Expand Down
1 change: 0 additions & 1 deletion Modules/_sqlite/statement.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ pysqlite_statement_create(pysqlite_Connection *connection, PyObject *sql)
}

self->st = stmt;
self->in_use = 0;
self->is_dml = is_dml;

PyObject_GC_Track(self);
Expand Down
1 change: 0 additions & 1 deletion Modules/_sqlite/statement.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ typedef struct
{
PyObject_HEAD
sqlite3_stmt* st;
int in_use;
int is_dml;
} pysqlite_Statement;

Expand Down