summaryrefslogtreecommitdiffstats
path: root/Modules/_sqlite/cursor.c
diff options
context:
space:
mode:
authorErlend Egeberg Aasland <erlend.aasland@innova.no>2021-09-21 11:20:34 (GMT)
committerGitHub <noreply@github.com>2021-09-21 11:20:34 (GMT)
commit050d1035957379d70e8601e6f5636637716a264b (patch)
treee6d21a1cc20b46585e1da7433556f8ce5107b594 /Modules/_sqlite/cursor.c
parentdebd80403721b00423680328d6adf160a28fbff4 (diff)
downloadcpython-050d1035957379d70e8601e6f5636637716a264b.zip
cpython-050d1035957379d70e8601e6f5636637716a264b.tar.gz
cpython-050d1035957379d70e8601e6f5636637716a264b.tar.bz2
bpo-44958: Only reset `sqlite3` statements when needed (GH-27844)
Diffstat (limited to 'Modules/_sqlite/cursor.c')
-rw-r--r--Modules/_sqlite/cursor.c45
1 files changed, 19 insertions, 26 deletions
diff --git a/Modules/_sqlite/cursor.c b/Modules/_sqlite/cursor.c
index 38ccdcf..9bac607 100644
--- a/Modules/_sqlite/cursor.c
+++ b/Modules/_sqlite/cursor.c
@@ -104,12 +104,7 @@ cursor_clear(pysqlite_Cursor *self)
Py_CLEAR(self->row_cast_map);
Py_CLEAR(self->lastrowid);
Py_CLEAR(self->row_factory);
- if (self->statement) {
- /* Reset the statement if the user has not closed the cursor */
- pysqlite_statement_reset(self->statement);
- Py_CLEAR(self->statement);
- }
-
+ Py_CLEAR(self->statement);
return 0;
}
@@ -121,6 +116,14 @@ cursor_dealloc(pysqlite_Cursor *self)
if (self->in_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)self);
}
+ if (self->statement) {
+ /* A SELECT query will lock the affected database table(s), so we need
+ * to reset the statement to unlock the database before disappearing */
+ sqlite3_stmt *stmt = self->statement->st;
+ if (sqlite3_stmt_readonly(stmt)) {
+ pysqlite_statement_reset(self->statement);
+ }
+ }
tp->tp_clear((PyObject *)self);
tp->tp_free(self);
Py_DECREF(tp);
@@ -515,18 +518,19 @@ _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) {
- (void)pysqlite_statement_reset(self->statement);
+ /* A SELECT query will lock the affected database table(s), so we need
+ * to reset the statement to unlock the database before switching
+ * statements */
+ sqlite3_stmt *stmt = self->statement->st;
+ if (sqlite3_stmt_readonly(stmt)) {
+ pysqlite_statement_reset(self->statement);
+ }
}
PyObject *stmt = get_statement_from_cache(self, operation);
@@ -549,8 +553,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
goto error;
}
}
-
- pysqlite_statement_reset(self->statement);
pysqlite_statement_mark_dirty(self->statement);
/* We start a transaction implicitly before a DML statement.
@@ -570,6 +572,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
break;
}
+ pysqlite_statement_reset(self->statement);
pysqlite_statement_mark_dirty(self->statement);
pysqlite_statement_bind_parameters(state, self->statement, parameters);
@@ -587,7 +590,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;
}
@@ -646,13 +648,9 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
}
if (rc == SQLITE_DONE && !multiple) {
- pysqlite_statement_reset(self->statement);
Py_CLEAR(self->statement);
}
- if (multiple) {
- pysqlite_statement_reset(self->statement);
- }
Py_XDECREF(parameters);
}
@@ -804,7 +802,6 @@ 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;
}
@@ -815,7 +812,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
}
int rc = pysqlite_step(stmt);
if (rc == SQLITE_DONE) {
- (void)pysqlite_statement_reset(self->statement);
+ Py_CLEAR(self->statement);
}
else if (rc != SQLITE_ROW) {
(void)_pysqlite_seterror(self->connection->state,
@@ -985,11 +982,7 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
return NULL;
}
- if (self->statement) {
- (void)pysqlite_statement_reset(self->statement);
- Py_CLEAR(self->statement);
- }
-
+ Py_CLEAR(self->statement);
self->closed = 1;
Py_RETURN_NONE;