2929#include "prepare_protocol.h"
3030#include "util.h"
3131
32- #define ACTION_FINALIZE 1
33- #define ACTION_RESET 2
34-
3532#if SQLITE_VERSION_NUMBER >= 3014000
3633#define HAVE_TRACE_V2
3734#endif
@@ -114,7 +111,6 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
114111 self -> begin_statement = NULL ;
115112
116113 Py_CLEAR (self -> statement_cache );
117- Py_CLEAR (self -> statements );
118114 Py_CLEAR (self -> cursors );
119115
120116 Py_INCREF (Py_None );
@@ -159,13 +155,11 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
159155 return -1 ;
160156 }
161157
162- self -> created_statements = 0 ;
163158 self -> created_cursors = 0 ;
164159
165- /* Create lists of weak references to statements/cursors */
166- self -> statements = PyList_New (0 );
160+ /* Create list of weak references to cursors */
167161 self -> cursors = PyList_New (0 );
168- if (! self -> statements || ! self -> cursors ) {
162+ if (self -> cursors == NULL ) {
169163 return -1 ;
170164 }
171165
@@ -198,37 +192,24 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
198192 return 0 ;
199193}
200194
201- /* action in (ACTION_RESET, ACTION_FINALIZE) */
202195static void
203- pysqlite_do_all_statements (pysqlite_Connection * self , int action ,
204- int reset_cursors )
196+ pysqlite_do_all_statements (pysqlite_Connection * self )
205197{
206- int i ;
207- PyObject * weakref ;
208- PyObject * statement ;
209- pysqlite_Cursor * cursor ;
210-
211- for (i = 0 ; i < PyList_Size (self -> statements ); i ++ ) {
212- weakref = PyList_GetItem (self -> statements , i );
213- statement = PyWeakref_GetObject (weakref );
214- if (statement != Py_None ) {
215- Py_INCREF (statement );
216- if (action == ACTION_RESET ) {
217- (void )pysqlite_statement_reset ((pysqlite_Statement * )statement );
218- } else {
219- (void )pysqlite_statement_finalize ((pysqlite_Statement * )statement );
220- }
221- Py_DECREF (statement );
198+ // Reset all statements
199+ sqlite3_stmt * stmt = NULL ;
200+ while ((stmt = sqlite3_next_stmt (self -> db , stmt ))) {
201+ if (sqlite3_stmt_busy (stmt )) {
202+ (void )sqlite3_reset (stmt );
222203 }
223204 }
224205
225- if ( reset_cursors ) {
226- for (i = 0 ; i < PyList_Size (self -> cursors ); i ++ ) {
227- weakref = PyList_GetItem (self -> cursors , i );
228- cursor = ( pysqlite_Cursor * ) PyWeakref_GetObject (weakref );
229- if (( PyObject * ) cursor != Py_None ) {
230- cursor -> reset = 1 ;
231- }
206+ // Reset all cursors
207+ for (int i = 0 ; i < PyList_Size (self -> cursors ); i ++ ) {
208+ PyObject * weakref = PyList_GetItem (self -> cursors , i );
209+ PyObject * object = PyWeakref_GetObject (weakref );
210+ if (object != Py_None ) {
211+ pysqlite_Cursor * cursor = ( pysqlite_Cursor * ) object ;
212+ cursor -> reset = 1 ;
232213 }
233214 }
234215}
@@ -239,7 +220,6 @@ connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
239220 Py_VISIT (Py_TYPE (self ));
240221 Py_VISIT (self -> isolation_level );
241222 Py_VISIT (self -> statement_cache );
242- Py_VISIT (self -> statements );
243223 Py_VISIT (self -> cursors );
244224 Py_VISIT (self -> row_factory );
245225 Py_VISIT (self -> text_factory );
@@ -254,7 +234,6 @@ connection_clear(pysqlite_Connection *self)
254234{
255235 Py_CLEAR (self -> isolation_level );
256236 Py_CLEAR (self -> statement_cache );
257- Py_CLEAR (self -> statements );
258237 Py_CLEAR (self -> cursors );
259238 Py_CLEAR (self -> row_factory );
260239 Py_CLEAR (self -> text_factory );
@@ -378,7 +357,7 @@ pysqlite_connection_close_impl(pysqlite_Connection *self)
378357 return NULL ;
379358 }
380359
381- pysqlite_do_all_statements (self , ACTION_FINALIZE , 1 );
360+ Py_CLEAR (self -> statement_cache );
382361 connection_close (self );
383362
384363 Py_RETURN_NONE ;
@@ -474,7 +453,7 @@ pysqlite_connection_rollback_impl(pysqlite_Connection *self)
474453 }
475454
476455 if (!sqlite3_get_autocommit (self -> db )) {
477- pysqlite_do_all_statements (self , ACTION_RESET , 1 );
456+ pysqlite_do_all_statements (self );
478457
479458 Py_BEGIN_ALLOW_THREADS
480459 rc = sqlite3_prepare_v2 (self -> db , "ROLLBACK" , 9 , & statement , NULL );
@@ -774,37 +753,6 @@ _pysqlite_final_callback(sqlite3_context *context)
774753 PyGILState_Release (threadstate );
775754}
776755
777- static void _pysqlite_drop_unused_statement_references (pysqlite_Connection * self )
778- {
779- PyObject * new_list ;
780- PyObject * weakref ;
781- int i ;
782-
783- /* we only need to do this once in a while */
784- if (self -> created_statements ++ < 200 ) {
785- return ;
786- }
787-
788- self -> created_statements = 0 ;
789-
790- new_list = PyList_New (0 );
791- if (!new_list ) {
792- return ;
793- }
794-
795- for (i = 0 ; i < PyList_Size (self -> statements ); i ++ ) {
796- weakref = PyList_GetItem (self -> statements , i );
797- if (PyWeakref_GetObject (weakref ) != Py_None ) {
798- if (PyList_Append (new_list , weakref ) != 0 ) {
799- Py_DECREF (new_list );
800- return ;
801- }
802- }
803- }
804-
805- Py_SETREF (self -> statements , new_list );
806- }
807-
808756static void _pysqlite_drop_unused_cursor_references (pysqlite_Connection * self )
809757{
810758 PyObject * new_list ;
@@ -1358,7 +1306,6 @@ pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
13581306{
13591307 PyObject * sql ;
13601308 pysqlite_Statement * statement ;
1361- PyObject * weakref ;
13621309
13631310 if (!pysqlite_check_thread (self ) || !pysqlite_check_connection (self )) {
13641311 return NULL ;
@@ -1370,27 +1317,12 @@ pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
13701317 if (!PyArg_ParseTuple (args , "U" , & sql ))
13711318 return NULL ;
13721319
1373- _pysqlite_drop_unused_statement_references (self );
1374-
13751320 statement = pysqlite_statement_create (self , sql );
13761321 if (statement == NULL ) {
13771322 return NULL ;
13781323 }
13791324
1380- weakref = PyWeakref_NewRef ((PyObject * )statement , NULL );
1381- if (weakref == NULL )
1382- goto error ;
1383- if (PyList_Append (self -> statements , weakref ) != 0 ) {
1384- Py_DECREF (weakref );
1385- goto error ;
1386- }
1387- Py_DECREF (weakref );
1388-
13891325 return (PyObject * )statement ;
1390-
1391- error :
1392- Py_DECREF (statement );
1393- return NULL ;
13941326}
13951327
13961328/*[clinic input]
0 commit comments