29
29
#include "prepare_protocol.h"
30
30
#include "util.h"
31
31
32
- #define ACTION_FINALIZE 1
33
- #define ACTION_RESET 2
34
-
35
32
#if SQLITE_VERSION_NUMBER >= 3014000
36
33
#define HAVE_TRACE_V2
37
34
#endif
@@ -114,7 +111,6 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
114
111
self -> begin_statement = NULL ;
115
112
116
113
Py_CLEAR (self -> statement_cache );
117
- Py_CLEAR (self -> statements );
118
114
Py_CLEAR (self -> cursors );
119
115
120
116
Py_INCREF (Py_None );
@@ -159,13 +155,11 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
159
155
return -1 ;
160
156
}
161
157
162
- self -> created_statements = 0 ;
163
158
self -> created_cursors = 0 ;
164
159
165
- /* Create lists of weak references to statements/cursors */
166
- self -> statements = PyList_New (0 );
160
+ /* Create list of weak references to cursors */
167
161
self -> cursors = PyList_New (0 );
168
- if (! self -> statements || ! self -> cursors ) {
162
+ if (self -> cursors == NULL ) {
169
163
return -1 ;
170
164
}
171
165
@@ -198,37 +192,24 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
198
192
return 0 ;
199
193
}
200
194
201
- /* action in (ACTION_RESET, ACTION_FINALIZE) */
202
195
static void
203
- pysqlite_do_all_statements (pysqlite_Connection * self , int action ,
204
- int reset_cursors )
196
+ pysqlite_do_all_statements (pysqlite_Connection * self )
205
197
{
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 );
222
203
}
223
204
}
224
205
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 ;
232
213
}
233
214
}
234
215
}
@@ -239,7 +220,6 @@ connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
239
220
Py_VISIT (Py_TYPE (self ));
240
221
Py_VISIT (self -> isolation_level );
241
222
Py_VISIT (self -> statement_cache );
242
- Py_VISIT (self -> statements );
243
223
Py_VISIT (self -> cursors );
244
224
Py_VISIT (self -> row_factory );
245
225
Py_VISIT (self -> text_factory );
@@ -254,7 +234,6 @@ connection_clear(pysqlite_Connection *self)
254
234
{
255
235
Py_CLEAR (self -> isolation_level );
256
236
Py_CLEAR (self -> statement_cache );
257
- Py_CLEAR (self -> statements );
258
237
Py_CLEAR (self -> cursors );
259
238
Py_CLEAR (self -> row_factory );
260
239
Py_CLEAR (self -> text_factory );
@@ -378,7 +357,7 @@ pysqlite_connection_close_impl(pysqlite_Connection *self)
378
357
return NULL ;
379
358
}
380
359
381
- pysqlite_do_all_statements (self , ACTION_FINALIZE , 1 );
360
+ Py_CLEAR (self -> statement_cache );
382
361
connection_close (self );
383
362
384
363
Py_RETURN_NONE ;
@@ -474,7 +453,7 @@ pysqlite_connection_rollback_impl(pysqlite_Connection *self)
474
453
}
475
454
476
455
if (!sqlite3_get_autocommit (self -> db )) {
477
- pysqlite_do_all_statements (self , ACTION_RESET , 1 );
456
+ pysqlite_do_all_statements (self );
478
457
479
458
Py_BEGIN_ALLOW_THREADS
480
459
rc = sqlite3_prepare_v2 (self -> db , "ROLLBACK" , 9 , & statement , NULL );
@@ -774,37 +753,6 @@ _pysqlite_final_callback(sqlite3_context *context)
774
753
PyGILState_Release (threadstate );
775
754
}
776
755
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
-
808
756
static void _pysqlite_drop_unused_cursor_references (pysqlite_Connection * self )
809
757
{
810
758
PyObject * new_list ;
@@ -1358,7 +1306,6 @@ pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1358
1306
{
1359
1307
PyObject * sql ;
1360
1308
pysqlite_Statement * statement ;
1361
- PyObject * weakref ;
1362
1309
1363
1310
if (!pysqlite_check_thread (self ) || !pysqlite_check_connection (self )) {
1364
1311
return NULL ;
@@ -1370,27 +1317,12 @@ pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1370
1317
if (!PyArg_ParseTuple (args , "U" , & sql ))
1371
1318
return NULL ;
1372
1319
1373
- _pysqlite_drop_unused_statement_references (self );
1374
-
1375
1320
statement = pysqlite_statement_create (self , sql );
1376
1321
if (statement == NULL ) {
1377
1322
return NULL ;
1378
1323
}
1379
1324
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
-
1389
1325
return (PyObject * )statement ;
1390
-
1391
- error :
1392
- Py_DECREF (statement );
1393
- return NULL ;
1394
1326
}
1395
1327
1396
1328
/*[clinic input]
0 commit comments