Skip to content

Commit 7b88f63

Browse files
author
Erlend Egeberg Aasland
authored
This reverts commit 050d103, but keeps the tests.
1 parent f56268a commit 7b88f63

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

Modules/_sqlite/cursor.c

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ cursor_clear(pysqlite_Cursor *self)
104104
Py_CLEAR(self->row_cast_map);
105105
Py_CLEAR(self->lastrowid);
106106
Py_CLEAR(self->row_factory);
107-
Py_CLEAR(self->statement);
107+
if (self->statement) {
108+
/* Reset the statement if the user has not closed the cursor */
109+
pysqlite_statement_reset(self->statement);
110+
Py_CLEAR(self->statement);
111+
}
112+
108113
return 0;
109114
}
110115

@@ -116,14 +121,6 @@ cursor_dealloc(pysqlite_Cursor *self)
116121
if (self->in_weakreflist != NULL) {
117122
PyObject_ClearWeakRefs((PyObject*)self);
118123
}
119-
if (self->statement) {
120-
/* A SELECT query will lock the affected database table(s), so we need
121-
* to reset the statement to unlock the database before disappearing */
122-
sqlite3_stmt *stmt = self->statement->st;
123-
if (sqlite3_stmt_readonly(stmt)) {
124-
pysqlite_statement_reset(self->statement);
125-
}
126-
}
127124
tp->tp_clear((PyObject *)self);
128125
tp->tp_free(self);
129126
Py_DECREF(tp);
@@ -518,19 +515,18 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
518515
}
519516
}
520517

518+
if (self->statement != NULL) {
519+
/* There is an active statement */
520+
pysqlite_statement_reset(self->statement);
521+
}
522+
521523
/* reset description and rowcount */
522524
Py_INCREF(Py_None);
523525
Py_SETREF(self->description, Py_None);
524526
self->rowcount = 0L;
525527

526528
if (self->statement) {
527-
/* A SELECT query will lock the affected database table(s), so we need
528-
* to reset the statement to unlock the database before switching
529-
* statements */
530-
sqlite3_stmt *stmt = self->statement->st;
531-
if (sqlite3_stmt_readonly(stmt)) {
532-
pysqlite_statement_reset(self->statement);
533-
}
529+
(void)pysqlite_statement_reset(self->statement);
534530
}
535531

536532
PyObject *stmt = get_statement_from_cache(self, operation);
@@ -553,6 +549,8 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
553549
goto error;
554550
}
555551
}
552+
553+
pysqlite_statement_reset(self->statement);
556554
pysqlite_statement_mark_dirty(self->statement);
557555

558556
/* We start a transaction implicitly before a DML statement.
@@ -572,7 +570,6 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
572570
break;
573571
}
574572

575-
pysqlite_statement_reset(self->statement);
576573
pysqlite_statement_mark_dirty(self->statement);
577574

578575
pysqlite_statement_bind_parameters(state, self->statement, parameters);
@@ -590,6 +587,7 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
590587
PyErr_Clear();
591588
}
592589
}
590+
(void)pysqlite_statement_reset(self->statement);
593591
_pysqlite_seterror(state, self->connection->db);
594592
goto error;
595593
}
@@ -648,9 +646,13 @@ _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* operation
648646
}
649647

650648
if (rc == SQLITE_DONE && !multiple) {
649+
pysqlite_statement_reset(self->statement);
651650
Py_CLEAR(self->statement);
652651
}
653652

653+
if (multiple) {
654+
pysqlite_statement_reset(self->statement);
655+
}
654656
Py_XDECREF(parameters);
655657
}
656658

@@ -802,6 +804,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
802804
sqlite3_stmt *stmt = self->statement->st;
803805
assert(stmt != NULL);
804806
if (sqlite3_data_count(stmt) == 0) {
807+
(void)pysqlite_statement_reset(self->statement);
805808
Py_CLEAR(self->statement);
806809
return NULL;
807810
}
@@ -812,7 +815,7 @@ pysqlite_cursor_iternext(pysqlite_Cursor *self)
812815
}
813816
int rc = pysqlite_step(stmt);
814817
if (rc == SQLITE_DONE) {
815-
Py_CLEAR(self->statement);
818+
(void)pysqlite_statement_reset(self->statement);
816819
}
817820
else if (rc != SQLITE_ROW) {
818821
(void)_pysqlite_seterror(self->connection->state,
@@ -982,7 +985,11 @@ pysqlite_cursor_close_impl(pysqlite_Cursor *self, PyTypeObject *cls)
982985
return NULL;
983986
}
984987

985-
Py_CLEAR(self->statement);
988+
if (self->statement) {
989+
(void)pysqlite_statement_reset(self->statement);
990+
Py_CLEAR(self->statement);
991+
}
992+
986993
self->closed = 1;
987994

988995
Py_RETURN_NONE;

Modules/_sqlite/statement.c

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -360,31 +360,23 @@ pysqlite_statement_bind_parameters(pysqlite_state *state,
360360
}
361361
}
362362

363-
void
364-
pysqlite_statement_reset(pysqlite_Statement *self)
363+
int pysqlite_statement_reset(pysqlite_Statement* self)
365364
{
366-
sqlite3_stmt *stmt = self->st;
367-
if (stmt == NULL || self->in_use == 0) {
368-
return;
369-
}
365+
int rc;
370366

371-
#if SQLITE_VERSION_NUMBER >= 3020000
372-
/* Check if the statement has been run (that is, sqlite3_step() has been
373-
* called at least once). Third parameter is non-zero in order to reset the
374-
* run count. */
375-
if (sqlite3_stmt_status(stmt, SQLITE_STMTSTATUS_RUN, 1) == 0) {
376-
return;
377-
}
378-
#endif
367+
rc = SQLITE_OK;
379368

380-
int rc;
381-
Py_BEGIN_ALLOW_THREADS
382-
rc = sqlite3_reset(stmt);
383-
Py_END_ALLOW_THREADS
369+
if (self->in_use && self->st) {
370+
Py_BEGIN_ALLOW_THREADS
371+
rc = sqlite3_reset(self->st);
372+
Py_END_ALLOW_THREADS
384373

385-
if (rc == SQLITE_OK) {
386-
self->in_use = 0;
374+
if (rc == SQLITE_OK) {
375+
self->in_use = 0;
376+
}
387377
}
378+
379+
return rc;
388380
}
389381

390382
void pysqlite_statement_mark_dirty(pysqlite_Statement* self)

Modules/_sqlite/statement.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ void pysqlite_statement_bind_parameters(pysqlite_state *state,
4444
pysqlite_Statement *self,
4545
PyObject *parameters);
4646

47-
void pysqlite_statement_reset(pysqlite_Statement *self);
47+
int pysqlite_statement_reset(pysqlite_Statement* self);
4848
void pysqlite_statement_mark_dirty(pysqlite_Statement* self);
4949

5050
int pysqlite_statement_setup_types(PyObject *module);

0 commit comments

Comments
 (0)