1 /* connection.c - the connection type
2  *
3  * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
4  *
5  * This file is part of pysqlite.
6  *
7  * This software is provided 'as-is', without any express or implied
8  * warranty.  In no event will the authors be held liable for any damages
9  * arising from the use of this software.
10  *
11  * Permission is granted to anyone to use this software for any purpose,
12  * including commercial applications, and to alter it and redistribute it
13  * freely, subject to the following restrictions:
14  *
15  * 1. The origin of this software must not be misrepresented; you must not
16  *    claim that you wrote the original software. If you use this software
17  *    in a product, an acknowledgment in the product documentation would be
18  *    appreciated but is not required.
19  * 2. Altered source versions must be plainly marked as such, and must not be
20  *    misrepresented as being the original software.
21  * 3. This notice may not be removed or altered from any source distribution.
22  */
23 
24 #include "cache.h"
25 #include "module.h"
26 #include "structmember.h"
27 #include "connection.h"
28 #include "statement.h"
29 #include "cursor.h"
30 #include "prepare_protocol.h"
31 #include "util.h"
32 
33 #include "pythread.h"
34 
35 #define ACTION_FINALIZE 1
36 #define ACTION_RESET 2
37 
38 #if SQLITE_VERSION_NUMBER >= 3003008
39 #ifndef SQLITE_OMIT_LOAD_EXTENSION
40 #define HAVE_LOAD_EXTENSION
41 #endif
42 #endif
43 
44 #if SQLITE_VERSION_NUMBER >= 3006011
45 #define HAVE_BACKUP_API
46 #endif
47 
48 _Py_IDENTIFIER(cursor);
49 
50 static const char * const begin_statements[] = {
51     "BEGIN ",
52     "BEGIN DEFERRED",
53     "BEGIN IMMEDIATE",
54     "BEGIN EXCLUSIVE",
55     NULL
56 };
57 
58 static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored));
59 static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
60 
61 
_sqlite3_result_error(sqlite3_context * ctx,const char * errmsg,int len)62 static void _sqlite3_result_error(sqlite3_context* ctx, const char* errmsg, int len)
63 {
64     /* in older SQLite versions, calling sqlite3_result_error in callbacks
65      * triggers a bug in SQLite that leads either to irritating results or
66      * segfaults, depending on the SQLite version */
67 #if SQLITE_VERSION_NUMBER >= 3003003
68     sqlite3_result_error(ctx, errmsg, len);
69 #else
70     PyErr_SetString(pysqlite_OperationalError, errmsg);
71 #endif
72 }
73 
pysqlite_connection_init(pysqlite_Connection * self,PyObject * args,PyObject * kwargs)74 int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
75 {
76     static char *kwlist[] = {
77         "database", "timeout", "detect_types", "isolation_level",
78         "check_same_thread", "factory", "cached_statements", "uri",
79         NULL
80     };
81 
82     char* database;
83     PyObject* database_obj;
84     int detect_types = 0;
85     PyObject* isolation_level = NULL;
86     PyObject* factory = NULL;
87     int check_same_thread = 1;
88     int cached_statements = 100;
89     int uri = 0;
90     double timeout = 5.0;
91     int rc;
92 
93     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
94                                      PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
95                                      &isolation_level, &check_same_thread,
96                                      &factory, &cached_statements, &uri))
97     {
98         return -1;
99     }
100 
101     if (PySys_Audit("sqlite3.connect", "O", database_obj) < 0) {
102         return -1;
103     }
104 
105     database = PyBytes_AsString(database_obj);
106 
107     self->initialized = 1;
108 
109     self->begin_statement = NULL;
110 
111     Py_CLEAR(self->statement_cache);
112     Py_CLEAR(self->statements);
113     Py_CLEAR(self->cursors);
114 
115     Py_INCREF(Py_None);
116     Py_XSETREF(self->row_factory, Py_None);
117 
118     Py_INCREF(&PyUnicode_Type);
119     Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
120 
121 #ifdef SQLITE_OPEN_URI
122     Py_BEGIN_ALLOW_THREADS
123     rc = sqlite3_open_v2(database, &self->db,
124                          SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
125                          (uri ? SQLITE_OPEN_URI : 0), NULL);
126 #else
127     if (uri) {
128         PyErr_SetString(pysqlite_NotSupportedError, "URIs not supported");
129         return -1;
130     }
131     Py_BEGIN_ALLOW_THREADS
132     /* No need to use sqlite3_open_v2 as sqlite3_open(filename, db) is the
133        same as sqlite3_open_v2(filename, db, SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE, NULL). */
134     rc = sqlite3_open(database, &self->db);
135 #endif
136     Py_END_ALLOW_THREADS
137 
138     Py_DECREF(database_obj);
139 
140     if (rc != SQLITE_OK) {
141         _pysqlite_seterror(self->db, NULL);
142         return -1;
143     }
144 
145     if (!isolation_level) {
146         isolation_level = PyUnicode_FromString("");
147         if (!isolation_level) {
148             return -1;
149         }
150     } else {
151         Py_INCREF(isolation_level);
152     }
153     Py_CLEAR(self->isolation_level);
154     if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
155         Py_DECREF(isolation_level);
156         return -1;
157     }
158     Py_DECREF(isolation_level);
159 
160     self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)&pysqlite_CacheType, "Oi", self, cached_statements);
161     if (PyErr_Occurred()) {
162         return -1;
163     }
164 
165     self->created_statements = 0;
166     self->created_cursors = 0;
167 
168     /* Create lists of weak references to statements/cursors */
169     self->statements = PyList_New(0);
170     self->cursors = PyList_New(0);
171     if (!self->statements || !self->cursors) {
172         return -1;
173     }
174 
175     /* By default, the Cache class INCREFs the factory in its initializer, and
176      * decrefs it in its deallocator method. Since this would create a circular
177      * reference here, we're breaking it by decrementing self, and telling the
178      * cache class to not decref the factory (self) in its deallocator.
179      */
180     self->statement_cache->decref_factory = 0;
181     Py_DECREF(self);
182 
183     self->detect_types = detect_types;
184     self->timeout = timeout;
185     (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
186     self->thread_ident = PyThread_get_thread_ident();
187     if (!check_same_thread && sqlite3_libversion_number() < 3003001) {
188         PyErr_SetString(pysqlite_NotSupportedError, "shared connections not available");
189         return -1;
190     }
191     self->check_same_thread = check_same_thread;
192 
193     self->function_pinboard_trace_callback = NULL;
194     self->function_pinboard_progress_handler = NULL;
195     self->function_pinboard_authorizer_cb = NULL;
196 
197     Py_XSETREF(self->collations, PyDict_New());
198     if (!self->collations) {
199         return -1;
200     }
201 
202     self->Warning               = pysqlite_Warning;
203     self->Error                 = pysqlite_Error;
204     self->InterfaceError        = pysqlite_InterfaceError;
205     self->DatabaseError         = pysqlite_DatabaseError;
206     self->DataError             = pysqlite_DataError;
207     self->OperationalError      = pysqlite_OperationalError;
208     self->IntegrityError        = pysqlite_IntegrityError;
209     self->InternalError         = pysqlite_InternalError;
210     self->ProgrammingError      = pysqlite_ProgrammingError;
211     self->NotSupportedError     = pysqlite_NotSupportedError;
212 
213     return 0;
214 }
215 
216 /* action in (ACTION_RESET, ACTION_FINALIZE) */
pysqlite_do_all_statements(pysqlite_Connection * self,int action,int reset_cursors)217 void pysqlite_do_all_statements(pysqlite_Connection* self, int action, int reset_cursors)
218 {
219     int i;
220     PyObject* weakref;
221     PyObject* statement;
222     pysqlite_Cursor* cursor;
223 
224     for (i = 0; i < PyList_Size(self->statements); i++) {
225         weakref = PyList_GetItem(self->statements, i);
226         statement = PyWeakref_GetObject(weakref);
227         if (statement != Py_None) {
228             Py_INCREF(statement);
229             if (action == ACTION_RESET) {
230                 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
231             } else {
232                 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
233             }
234             Py_DECREF(statement);
235         }
236     }
237 
238     if (reset_cursors) {
239         for (i = 0; i < PyList_Size(self->cursors); i++) {
240             weakref = PyList_GetItem(self->cursors, i);
241             cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
242             if ((PyObject*)cursor != Py_None) {
243                 cursor->reset = 1;
244             }
245         }
246     }
247 }
248 
pysqlite_connection_dealloc(pysqlite_Connection * self)249 void pysqlite_connection_dealloc(pysqlite_Connection* self)
250 {
251     Py_XDECREF(self->statement_cache);
252 
253     /* Clean up if user has not called .close() explicitly. */
254     if (self->db) {
255         SQLITE3_CLOSE(self->db);
256     }
257 
258     Py_XDECREF(self->isolation_level);
259     Py_XDECREF(self->function_pinboard_trace_callback);
260     Py_XDECREF(self->function_pinboard_progress_handler);
261     Py_XDECREF(self->function_pinboard_authorizer_cb);
262     Py_XDECREF(self->row_factory);
263     Py_XDECREF(self->text_factory);
264     Py_XDECREF(self->collations);
265     Py_XDECREF(self->statements);
266     Py_XDECREF(self->cursors);
267     Py_TYPE(self)->tp_free((PyObject*)self);
268 }
269 
270 /*
271  * Registers a cursor with the connection.
272  *
273  * 0 => error; 1 => ok
274  */
pysqlite_connection_register_cursor(pysqlite_Connection * connection,PyObject * cursor)275 int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
276 {
277     PyObject* weakref;
278 
279     weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
280     if (!weakref) {
281         goto error;
282     }
283 
284     if (PyList_Append(connection->cursors, weakref) != 0) {
285         Py_CLEAR(weakref);
286         goto error;
287     }
288 
289     Py_DECREF(weakref);
290 
291     return 1;
292 error:
293     return 0;
294 }
295 
pysqlite_connection_cursor(pysqlite_Connection * self,PyObject * args,PyObject * kwargs)296 PyObject* pysqlite_connection_cursor(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
297 {
298     static char *kwlist[] = {"factory", NULL};
299     PyObject* factory = NULL;
300     PyObject* cursor;
301 
302     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist,
303                                      &factory)) {
304         return NULL;
305     }
306 
307     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
308         return NULL;
309     }
310 
311     if (factory == NULL) {
312         factory = (PyObject*)&pysqlite_CursorType;
313     }
314 
315     cursor = PyObject_CallFunctionObjArgs(factory, (PyObject *)self, NULL);
316     if (cursor == NULL)
317         return NULL;
318     if (!PyObject_TypeCheck(cursor, &pysqlite_CursorType)) {
319         PyErr_Format(PyExc_TypeError,
320                      "factory must return a cursor, not %.100s",
321                      Py_TYPE(cursor)->tp_name);
322         Py_DECREF(cursor);
323         return NULL;
324     }
325 
326     _pysqlite_drop_unused_cursor_references(self);
327 
328     if (cursor && self->row_factory != Py_None) {
329         Py_INCREF(self->row_factory);
330         Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
331     }
332 
333     return cursor;
334 }
335 
pysqlite_connection_close(pysqlite_Connection * self,PyObject * args)336 PyObject* pysqlite_connection_close(pysqlite_Connection* self, PyObject* args)
337 {
338     int rc;
339 
340     if (!pysqlite_check_thread(self)) {
341         return NULL;
342     }
343 
344     pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
345 
346     if (self->db) {
347         rc = SQLITE3_CLOSE(self->db);
348 
349         if (rc != SQLITE_OK) {
350             _pysqlite_seterror(self->db, NULL);
351             return NULL;
352         } else {
353             self->db = NULL;
354         }
355     }
356 
357     Py_RETURN_NONE;
358 }
359 
360 /*
361  * Checks if a connection object is usable (i. e. not closed).
362  *
363  * 0 => error; 1 => ok
364  */
pysqlite_check_connection(pysqlite_Connection * con)365 int pysqlite_check_connection(pysqlite_Connection* con)
366 {
367     if (!con->initialized) {
368         PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
369         return 0;
370     }
371 
372     if (!con->db) {
373         PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
374         return 0;
375     } else {
376         return 1;
377     }
378 }
379 
_pysqlite_connection_begin(pysqlite_Connection * self)380 PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
381 {
382     int rc;
383     const char* tail;
384     sqlite3_stmt* statement;
385 
386     Py_BEGIN_ALLOW_THREADS
387     rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement, &tail);
388     Py_END_ALLOW_THREADS
389 
390     if (rc != SQLITE_OK) {
391         _pysqlite_seterror(self->db, statement);
392         goto error;
393     }
394 
395     rc = pysqlite_step(statement, self);
396     if (rc != SQLITE_DONE) {
397         _pysqlite_seterror(self->db, statement);
398     }
399 
400     Py_BEGIN_ALLOW_THREADS
401     rc = sqlite3_finalize(statement);
402     Py_END_ALLOW_THREADS
403 
404     if (rc != SQLITE_OK && !PyErr_Occurred()) {
405         _pysqlite_seterror(self->db, NULL);
406     }
407 
408 error:
409     if (PyErr_Occurred()) {
410         return NULL;
411     } else {
412         Py_RETURN_NONE;
413     }
414 }
415 
pysqlite_connection_commit(pysqlite_Connection * self,PyObject * args)416 PyObject* pysqlite_connection_commit(pysqlite_Connection* self, PyObject* args)
417 {
418     int rc;
419     const char* tail;
420     sqlite3_stmt* statement;
421 
422     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
423         return NULL;
424     }
425 
426     if (!sqlite3_get_autocommit(self->db)) {
427 
428         Py_BEGIN_ALLOW_THREADS
429         rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, &tail);
430         Py_END_ALLOW_THREADS
431         if (rc != SQLITE_OK) {
432             _pysqlite_seterror(self->db, NULL);
433             goto error;
434         }
435 
436         rc = pysqlite_step(statement, self);
437         if (rc != SQLITE_DONE) {
438             _pysqlite_seterror(self->db, statement);
439         }
440 
441         Py_BEGIN_ALLOW_THREADS
442         rc = sqlite3_finalize(statement);
443         Py_END_ALLOW_THREADS
444         if (rc != SQLITE_OK && !PyErr_Occurred()) {
445             _pysqlite_seterror(self->db, NULL);
446         }
447 
448     }
449 
450 error:
451     if (PyErr_Occurred()) {
452         return NULL;
453     } else {
454         Py_RETURN_NONE;
455     }
456 }
457 
pysqlite_connection_rollback(pysqlite_Connection * self,PyObject * args)458 PyObject* pysqlite_connection_rollback(pysqlite_Connection* self, PyObject* args)
459 {
460     int rc;
461     const char* tail;
462     sqlite3_stmt* statement;
463 
464     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
465         return NULL;
466     }
467 
468     if (!sqlite3_get_autocommit(self->db)) {
469         pysqlite_do_all_statements(self, ACTION_RESET, 1);
470 
471         Py_BEGIN_ALLOW_THREADS
472         rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, &tail);
473         Py_END_ALLOW_THREADS
474         if (rc != SQLITE_OK) {
475             _pysqlite_seterror(self->db, NULL);
476             goto error;
477         }
478 
479         rc = pysqlite_step(statement, self);
480         if (rc != SQLITE_DONE) {
481             _pysqlite_seterror(self->db, statement);
482         }
483 
484         Py_BEGIN_ALLOW_THREADS
485         rc = sqlite3_finalize(statement);
486         Py_END_ALLOW_THREADS
487         if (rc != SQLITE_OK && !PyErr_Occurred()) {
488             _pysqlite_seterror(self->db, NULL);
489         }
490 
491     }
492 
493 error:
494     if (PyErr_Occurred()) {
495         return NULL;
496     } else {
497         Py_RETURN_NONE;
498     }
499 }
500 
501 static int
_pysqlite_set_result(sqlite3_context * context,PyObject * py_val)502 _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
503 {
504     if (py_val == Py_None) {
505         sqlite3_result_null(context);
506     } else if (PyLong_Check(py_val)) {
507         sqlite_int64 value = _pysqlite_long_as_int64(py_val);
508         if (value == -1 && PyErr_Occurred())
509             return -1;
510         sqlite3_result_int64(context, value);
511     } else if (PyFloat_Check(py_val)) {
512         sqlite3_result_double(context, PyFloat_AsDouble(py_val));
513     } else if (PyUnicode_Check(py_val)) {
514         const char *str = PyUnicode_AsUTF8(py_val);
515         if (str == NULL)
516             return -1;
517         sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
518     } else if (PyObject_CheckBuffer(py_val)) {
519         Py_buffer view;
520         if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
521             PyErr_SetString(PyExc_ValueError,
522                             "could not convert BLOB to buffer");
523             return -1;
524         }
525         if (view.len > INT_MAX) {
526             PyErr_SetString(PyExc_OverflowError,
527                             "BLOB longer than INT_MAX bytes");
528             PyBuffer_Release(&view);
529             return -1;
530         }
531         sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
532         PyBuffer_Release(&view);
533     } else {
534         return -1;
535     }
536     return 0;
537 }
538 
_pysqlite_build_py_params(sqlite3_context * context,int argc,sqlite3_value ** argv)539 PyObject* _pysqlite_build_py_params(sqlite3_context *context, int argc, sqlite3_value** argv)
540 {
541     PyObject* args;
542     int i;
543     sqlite3_value* cur_value;
544     PyObject* cur_py_value;
545     const char* val_str;
546     Py_ssize_t buflen;
547 
548     args = PyTuple_New(argc);
549     if (!args) {
550         return NULL;
551     }
552 
553     for (i = 0; i < argc; i++) {
554         cur_value = argv[i];
555         switch (sqlite3_value_type(argv[i])) {
556             case SQLITE_INTEGER:
557                 cur_py_value = _pysqlite_long_from_int64(sqlite3_value_int64(cur_value));
558                 break;
559             case SQLITE_FLOAT:
560                 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
561                 break;
562             case SQLITE_TEXT:
563                 val_str = (const char*)sqlite3_value_text(cur_value);
564                 cur_py_value = PyUnicode_FromString(val_str);
565                 /* TODO: have a way to show errors here */
566                 if (!cur_py_value) {
567                     PyErr_Clear();
568                     Py_INCREF(Py_None);
569                     cur_py_value = Py_None;
570                 }
571                 break;
572             case SQLITE_BLOB:
573                 buflen = sqlite3_value_bytes(cur_value);
574                 cur_py_value = PyBytes_FromStringAndSize(
575                     sqlite3_value_blob(cur_value), buflen);
576                 break;
577             case SQLITE_NULL:
578             default:
579                 Py_INCREF(Py_None);
580                 cur_py_value = Py_None;
581         }
582 
583         if (!cur_py_value) {
584             Py_DECREF(args);
585             return NULL;
586         }
587 
588         PyTuple_SetItem(args, i, cur_py_value);
589 
590     }
591 
592     return args;
593 }
594 
_pysqlite_func_callback(sqlite3_context * context,int argc,sqlite3_value ** argv)595 void _pysqlite_func_callback(sqlite3_context* context, int argc, sqlite3_value** argv)
596 {
597     PyObject* args;
598     PyObject* py_func;
599     PyObject* py_retval = NULL;
600     int ok;
601 
602     PyGILState_STATE threadstate;
603 
604     threadstate = PyGILState_Ensure();
605 
606     py_func = (PyObject*)sqlite3_user_data(context);
607 
608     args = _pysqlite_build_py_params(context, argc, argv);
609     if (args) {
610         py_retval = PyObject_CallObject(py_func, args);
611         Py_DECREF(args);
612     }
613 
614     ok = 0;
615     if (py_retval) {
616         ok = _pysqlite_set_result(context, py_retval) == 0;
617         Py_DECREF(py_retval);
618     }
619     if (!ok) {
620         if (_pysqlite_enable_callback_tracebacks) {
621             PyErr_Print();
622         } else {
623             PyErr_Clear();
624         }
625         _sqlite3_result_error(context, "user-defined function raised exception", -1);
626     }
627 
628     PyGILState_Release(threadstate);
629 }
630 
_pysqlite_step_callback(sqlite3_context * context,int argc,sqlite3_value ** params)631 static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
632 {
633     PyObject* args;
634     PyObject* function_result = NULL;
635     PyObject* aggregate_class;
636     PyObject** aggregate_instance;
637     PyObject* stepmethod = NULL;
638 
639     PyGILState_STATE threadstate;
640 
641     threadstate = PyGILState_Ensure();
642 
643     aggregate_class = (PyObject*)sqlite3_user_data(context);
644 
645     aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
646 
647     if (*aggregate_instance == NULL) {
648         *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
649 
650         if (PyErr_Occurred()) {
651             *aggregate_instance = 0;
652             if (_pysqlite_enable_callback_tracebacks) {
653                 PyErr_Print();
654             } else {
655                 PyErr_Clear();
656             }
657             _sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
658             goto error;
659         }
660     }
661 
662     stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
663     if (!stepmethod) {
664         goto error;
665     }
666 
667     args = _pysqlite_build_py_params(context, argc, params);
668     if (!args) {
669         goto error;
670     }
671 
672     function_result = PyObject_CallObject(stepmethod, args);
673     Py_DECREF(args);
674 
675     if (!function_result) {
676         if (_pysqlite_enable_callback_tracebacks) {
677             PyErr_Print();
678         } else {
679             PyErr_Clear();
680         }
681         _sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
682     }
683 
684 error:
685     Py_XDECREF(stepmethod);
686     Py_XDECREF(function_result);
687 
688     PyGILState_Release(threadstate);
689 }
690 
_pysqlite_final_callback(sqlite3_context * context)691 void _pysqlite_final_callback(sqlite3_context* context)
692 {
693     PyObject* function_result;
694     PyObject** aggregate_instance;
695     _Py_IDENTIFIER(finalize);
696     int ok;
697     PyObject *exception, *value, *tb;
698     int restore;
699 
700     PyGILState_STATE threadstate;
701 
702     threadstate = PyGILState_Ensure();
703 
704     aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
705     if (!*aggregate_instance) {
706         /* this branch is executed if there was an exception in the aggregate's
707          * __init__ */
708 
709         goto error;
710     }
711 
712     /* Keep the exception (if any) of the last call to step() */
713     PyErr_Fetch(&exception, &value, &tb);
714     restore = 1;
715 
716     function_result = _PyObject_CallMethodId(*aggregate_instance, &PyId_finalize, NULL);
717 
718     Py_DECREF(*aggregate_instance);
719 
720     ok = 0;
721     if (function_result) {
722         ok = _pysqlite_set_result(context, function_result) == 0;
723         Py_DECREF(function_result);
724     }
725     if (!ok) {
726         if (_pysqlite_enable_callback_tracebacks) {
727             PyErr_Print();
728         } else {
729             PyErr_Clear();
730         }
731         _sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
732 #if SQLITE_VERSION_NUMBER < 3003003
733         /* with old SQLite versions, _sqlite3_result_error() sets a new Python
734            exception, so don't restore the previous exception */
735         restore = 0;
736 #endif
737     }
738 
739     if (restore) {
740         /* Restore the exception (if any) of the last call to step(),
741            but clear also the current exception if finalize() failed */
742         PyErr_Restore(exception, value, tb);
743     }
744 
745 error:
746     PyGILState_Release(threadstate);
747 }
748 
_pysqlite_drop_unused_statement_references(pysqlite_Connection * self)749 static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
750 {
751     PyObject* new_list;
752     PyObject* weakref;
753     int i;
754 
755     /* we only need to do this once in a while */
756     if (self->created_statements++ < 200) {
757         return;
758     }
759 
760     self->created_statements = 0;
761 
762     new_list = PyList_New(0);
763     if (!new_list) {
764         return;
765     }
766 
767     for (i = 0; i < PyList_Size(self->statements); i++) {
768         weakref = PyList_GetItem(self->statements, i);
769         if (PyWeakref_GetObject(weakref) != Py_None) {
770             if (PyList_Append(new_list, weakref) != 0) {
771                 Py_DECREF(new_list);
772                 return;
773             }
774         }
775     }
776 
777     Py_SETREF(self->statements, new_list);
778 }
779 
_pysqlite_drop_unused_cursor_references(pysqlite_Connection * self)780 static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
781 {
782     PyObject* new_list;
783     PyObject* weakref;
784     int i;
785 
786     /* we only need to do this once in a while */
787     if (self->created_cursors++ < 200) {
788         return;
789     }
790 
791     self->created_cursors = 0;
792 
793     new_list = PyList_New(0);
794     if (!new_list) {
795         return;
796     }
797 
798     for (i = 0; i < PyList_Size(self->cursors); i++) {
799         weakref = PyList_GetItem(self->cursors, i);
800         if (PyWeakref_GetObject(weakref) != Py_None) {
801             if (PyList_Append(new_list, weakref) != 0) {
802                 Py_DECREF(new_list);
803                 return;
804             }
805         }
806     }
807 
808     Py_SETREF(self->cursors, new_list);
809 }
810 
_destructor(void * args)811 static void _destructor(void* args)
812 {
813     Py_DECREF((PyObject*)args);
814 }
815 
pysqlite_connection_create_function(pysqlite_Connection * self,PyObject * args,PyObject * kwargs)816 PyObject* pysqlite_connection_create_function(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
817 {
818     static char *kwlist[] = {"name", "narg", "func", "deterministic", NULL};
819 
820     PyObject* func;
821     char* name;
822     int narg;
823     int rc;
824     int deterministic = 0;
825     int flags = SQLITE_UTF8;
826 
827     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
828         return NULL;
829     }
830 
831     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO|$p", kwlist,
832                                      &name, &narg, &func, &deterministic))
833     {
834         return NULL;
835     }
836 
837     if (deterministic) {
838 #if SQLITE_VERSION_NUMBER < 3008003
839         PyErr_SetString(pysqlite_NotSupportedError,
840                         "deterministic=True requires SQLite 3.8.3 or higher");
841         return NULL;
842 #else
843         if (sqlite3_libversion_number() < 3008003) {
844             PyErr_SetString(pysqlite_NotSupportedError,
845                             "deterministic=True requires SQLite 3.8.3 or higher");
846             return NULL;
847         }
848         flags |= SQLITE_DETERMINISTIC;
849 #endif
850     }
851     Py_INCREF(func);
852     rc = sqlite3_create_function_v2(self->db,
853                                     name,
854                                     narg,
855                                     flags,
856                                     (void*)func,
857                                     _pysqlite_func_callback,
858                                     NULL,
859                                     NULL,
860                                     &_destructor);  // will decref func
861 
862     if (rc != SQLITE_OK) {
863         /* Workaround for SQLite bug: no error code or string is available here */
864         PyErr_SetString(pysqlite_OperationalError, "Error creating function");
865         return NULL;
866     }
867     Py_RETURN_NONE;
868 }
869 
pysqlite_connection_create_aggregate(pysqlite_Connection * self,PyObject * args,PyObject * kwargs)870 PyObject* pysqlite_connection_create_aggregate(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
871 {
872     PyObject* aggregate_class;
873 
874     int n_arg;
875     char* name;
876     static char *kwlist[] = { "name", "n_arg", "aggregate_class", NULL };
877     int rc;
878 
879     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
880         return NULL;
881     }
882 
883     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "siO:create_aggregate",
884                                       kwlist, &name, &n_arg, &aggregate_class)) {
885         return NULL;
886     }
887     Py_INCREF(aggregate_class);
888     rc = sqlite3_create_function_v2(self->db,
889                                     name,
890                                     n_arg,
891                                     SQLITE_UTF8,
892                                     (void*)aggregate_class,
893                                     0,
894                                     &_pysqlite_step_callback,
895                                     &_pysqlite_final_callback,
896                                     &_destructor); // will decref func
897     if (rc != SQLITE_OK) {
898         /* Workaround for SQLite bug: no error code or string is available here */
899         PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
900         return NULL;
901     }
902     Py_RETURN_NONE;
903 }
904 
_authorizer_callback(void * user_arg,int action,const char * arg1,const char * arg2,const char * dbname,const char * access_attempt_source)905 static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
906 {
907     PyObject *ret;
908     int rc;
909     PyGILState_STATE gilstate;
910 
911     gilstate = PyGILState_Ensure();
912 
913     ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
914 
915     if (ret == NULL) {
916         if (_pysqlite_enable_callback_tracebacks)
917             PyErr_Print();
918         else
919             PyErr_Clear();
920 
921         rc = SQLITE_DENY;
922     }
923     else {
924         if (PyLong_Check(ret)) {
925             rc = _PyLong_AsInt(ret);
926             if (rc == -1 && PyErr_Occurred()) {
927                 if (_pysqlite_enable_callback_tracebacks)
928                     PyErr_Print();
929                 else
930                     PyErr_Clear();
931                 rc = SQLITE_DENY;
932             }
933         }
934         else {
935             rc = SQLITE_DENY;
936         }
937         Py_DECREF(ret);
938     }
939 
940     PyGILState_Release(gilstate);
941     return rc;
942 }
943 
_progress_handler(void * user_arg)944 static int _progress_handler(void* user_arg)
945 {
946     int rc;
947     PyObject *ret;
948     PyGILState_STATE gilstate;
949 
950     gilstate = PyGILState_Ensure();
951     ret = _PyObject_CallNoArg((PyObject*)user_arg);
952 
953     if (!ret) {
954         if (_pysqlite_enable_callback_tracebacks) {
955             PyErr_Print();
956         } else {
957             PyErr_Clear();
958         }
959 
960         /* abort query if error occurred */
961         rc = 1;
962     } else {
963         rc = (int)PyObject_IsTrue(ret);
964         Py_DECREF(ret);
965     }
966 
967     PyGILState_Release(gilstate);
968     return rc;
969 }
970 
_trace_callback(void * user_arg,const char * statement_string)971 static void _trace_callback(void* user_arg, const char* statement_string)
972 {
973     PyObject *py_statement = NULL;
974     PyObject *ret = NULL;
975 
976     PyGILState_STATE gilstate;
977 
978     gilstate = PyGILState_Ensure();
979     py_statement = PyUnicode_DecodeUTF8(statement_string,
980             strlen(statement_string), "replace");
981     if (py_statement) {
982         ret = PyObject_CallFunctionObjArgs((PyObject*)user_arg, py_statement, NULL);
983         Py_DECREF(py_statement);
984     }
985 
986     if (ret) {
987         Py_DECREF(ret);
988     } else {
989         if (_pysqlite_enable_callback_tracebacks) {
990             PyErr_Print();
991         } else {
992             PyErr_Clear();
993         }
994     }
995 
996     PyGILState_Release(gilstate);
997 }
998 
pysqlite_connection_set_authorizer(pysqlite_Connection * self,PyObject * args,PyObject * kwargs)999 static PyObject* pysqlite_connection_set_authorizer(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1000 {
1001     PyObject* authorizer_cb;
1002 
1003     static char *kwlist[] = { "authorizer_callback", NULL };
1004     int rc;
1005 
1006     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1007         return NULL;
1008     }
1009 
1010     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_authorizer",
1011                                       kwlist, &authorizer_cb)) {
1012         return NULL;
1013     }
1014 
1015     rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
1016     if (rc != SQLITE_OK) {
1017         PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
1018         Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
1019         return NULL;
1020     } else {
1021         Py_INCREF(authorizer_cb);
1022         Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
1023     }
1024     Py_RETURN_NONE;
1025 }
1026 
pysqlite_connection_set_progress_handler(pysqlite_Connection * self,PyObject * args,PyObject * kwargs)1027 static PyObject* pysqlite_connection_set_progress_handler(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1028 {
1029     PyObject* progress_handler;
1030     int n;
1031 
1032     static char *kwlist[] = { "progress_handler", "n", NULL };
1033 
1034     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1035         return NULL;
1036     }
1037 
1038     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi:set_progress_handler",
1039                                       kwlist, &progress_handler, &n)) {
1040         return NULL;
1041     }
1042 
1043     if (progress_handler == Py_None) {
1044         /* None clears the progress handler previously set */
1045         sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1046         Py_XSETREF(self->function_pinboard_progress_handler, NULL);
1047     } else {
1048         sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
1049         Py_INCREF(progress_handler);
1050         Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
1051     }
1052     Py_RETURN_NONE;
1053 }
1054 
pysqlite_connection_set_trace_callback(pysqlite_Connection * self,PyObject * args,PyObject * kwargs)1055 static PyObject* pysqlite_connection_set_trace_callback(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1056 {
1057     PyObject* trace_callback;
1058 
1059     static char *kwlist[] = { "trace_callback", NULL };
1060 
1061     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1062         return NULL;
1063     }
1064 
1065     if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O:set_trace_callback",
1066                                       kwlist, &trace_callback)) {
1067         return NULL;
1068     }
1069 
1070     if (trace_callback == Py_None) {
1071         /* None clears the trace callback previously set */
1072         sqlite3_trace(self->db, 0, (void*)0);
1073         Py_XSETREF(self->function_pinboard_trace_callback, NULL);
1074     } else {
1075         sqlite3_trace(self->db, _trace_callback, trace_callback);
1076         Py_INCREF(trace_callback);
1077         Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
1078     }
1079 
1080     Py_RETURN_NONE;
1081 }
1082 
1083 #ifdef HAVE_LOAD_EXTENSION
pysqlite_enable_load_extension(pysqlite_Connection * self,PyObject * args)1084 static PyObject* pysqlite_enable_load_extension(pysqlite_Connection* self, PyObject* args)
1085 {
1086     int rc;
1087     int onoff;
1088 
1089     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1090         return NULL;
1091     }
1092 
1093     if (!PyArg_ParseTuple(args, "i", &onoff)) {
1094         return NULL;
1095     }
1096 
1097     rc = sqlite3_enable_load_extension(self->db, onoff);
1098 
1099     if (rc != SQLITE_OK) {
1100         PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1101         return NULL;
1102     } else {
1103         Py_RETURN_NONE;
1104     }
1105 }
1106 
pysqlite_load_extension(pysqlite_Connection * self,PyObject * args)1107 static PyObject* pysqlite_load_extension(pysqlite_Connection* self, PyObject* args)
1108 {
1109     int rc;
1110     char* extension_name;
1111     char* errmsg;
1112 
1113     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1114         return NULL;
1115     }
1116 
1117     if (!PyArg_ParseTuple(args, "s", &extension_name)) {
1118         return NULL;
1119     }
1120 
1121     rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1122     if (rc != 0) {
1123         PyErr_SetString(pysqlite_OperationalError, errmsg);
1124         return NULL;
1125     } else {
1126         Py_RETURN_NONE;
1127     }
1128 }
1129 #endif
1130 
pysqlite_check_thread(pysqlite_Connection * self)1131 int pysqlite_check_thread(pysqlite_Connection* self)
1132 {
1133     if (self->check_same_thread) {
1134         if (PyThread_get_thread_ident() != self->thread_ident) {
1135             PyErr_Format(pysqlite_ProgrammingError,
1136                         "SQLite objects created in a thread can only be used in that same thread. "
1137                         "The object was created in thread id %lu and this is thread id %lu.",
1138                         self->thread_ident, PyThread_get_thread_ident());
1139             return 0;
1140         }
1141 
1142     }
1143     return 1;
1144 }
1145 
pysqlite_connection_get_isolation_level(pysqlite_Connection * self,void * unused)1146 static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
1147 {
1148     Py_INCREF(self->isolation_level);
1149     return self->isolation_level;
1150 }
1151 
pysqlite_connection_get_total_changes(pysqlite_Connection * self,void * unused)1152 static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
1153 {
1154     if (!pysqlite_check_connection(self)) {
1155         return NULL;
1156     } else {
1157         return Py_BuildValue("i", sqlite3_total_changes(self->db));
1158     }
1159 }
1160 
pysqlite_connection_get_in_transaction(pysqlite_Connection * self,void * unused)1161 static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1162 {
1163     if (!pysqlite_check_connection(self)) {
1164         return NULL;
1165     }
1166     if (!sqlite3_get_autocommit(self->db)) {
1167         Py_RETURN_TRUE;
1168     }
1169     Py_RETURN_FALSE;
1170 }
1171 
1172 static int
pysqlite_connection_set_isolation_level(pysqlite_Connection * self,PyObject * isolation_level,void * Py_UNUSED (ignored))1173 pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
1174 {
1175     if (isolation_level == NULL) {
1176         PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1177         return -1;
1178     }
1179     if (isolation_level == Py_None) {
1180         PyObject *res = pysqlite_connection_commit(self, NULL);
1181         if (!res) {
1182             return -1;
1183         }
1184         Py_DECREF(res);
1185 
1186         self->begin_statement = NULL;
1187     } else {
1188         const char * const *candidate;
1189         PyObject *uppercase_level;
1190         _Py_IDENTIFIER(upper);
1191 
1192         if (!PyUnicode_Check(isolation_level)) {
1193             PyErr_Format(PyExc_TypeError,
1194                          "isolation_level must be a string or None, not %.100s",
1195                          Py_TYPE(isolation_level)->tp_name);
1196             return -1;
1197         }
1198 
1199         uppercase_level = _PyObject_CallMethodIdObjArgs(
1200                         (PyObject *)&PyUnicode_Type, &PyId_upper,
1201                         isolation_level, NULL);
1202         if (!uppercase_level) {
1203             return -1;
1204         }
1205         for (candidate = begin_statements; *candidate; candidate++) {
1206             if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
1207                 break;
1208         }
1209         Py_DECREF(uppercase_level);
1210         if (!*candidate) {
1211             PyErr_SetString(PyExc_ValueError,
1212                             "invalid value for isolation_level");
1213             return -1;
1214         }
1215         self->begin_statement = *candidate;
1216     }
1217 
1218     Py_INCREF(isolation_level);
1219     Py_XSETREF(self->isolation_level, isolation_level);
1220     return 0;
1221 }
1222 
pysqlite_connection_call(pysqlite_Connection * self,PyObject * args,PyObject * kwargs)1223 PyObject* pysqlite_connection_call(pysqlite_Connection* self, PyObject* args, PyObject* kwargs)
1224 {
1225     PyObject* sql;
1226     pysqlite_Statement* statement;
1227     PyObject* weakref;
1228     int rc;
1229 
1230     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1231         return NULL;
1232     }
1233 
1234     if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
1235         return NULL;
1236 
1237     if (!PyArg_ParseTuple(args, "O", &sql))
1238         return NULL;
1239 
1240     _pysqlite_drop_unused_statement_references(self);
1241 
1242     statement = PyObject_New(pysqlite_Statement, &pysqlite_StatementType);
1243     if (!statement) {
1244         return NULL;
1245     }
1246 
1247     statement->db = NULL;
1248     statement->st = NULL;
1249     statement->sql = NULL;
1250     statement->in_use = 0;
1251     statement->in_weakreflist = NULL;
1252 
1253     rc = pysqlite_statement_create(statement, self, sql);
1254     if (rc != SQLITE_OK) {
1255         if (rc == PYSQLITE_TOO_MUCH_SQL) {
1256             PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
1257         } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
1258             if (PyErr_ExceptionMatches(PyExc_TypeError))
1259                 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
1260         } else {
1261             (void)pysqlite_statement_reset(statement);
1262             _pysqlite_seterror(self->db, NULL);
1263         }
1264         goto error;
1265     }
1266 
1267     weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1268     if (weakref == NULL)
1269         goto error;
1270     if (PyList_Append(self->statements, weakref) != 0) {
1271         Py_DECREF(weakref);
1272         goto error;
1273     }
1274     Py_DECREF(weakref);
1275 
1276     return (PyObject*)statement;
1277 
1278 error:
1279     Py_DECREF(statement);
1280     return NULL;
1281 }
1282 
pysqlite_connection_execute(pysqlite_Connection * self,PyObject * args)1283 PyObject* pysqlite_connection_execute(pysqlite_Connection* self, PyObject* args)
1284 {
1285     PyObject* cursor = 0;
1286     PyObject* result = 0;
1287     PyObject* method = 0;
1288 
1289     cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
1290     if (!cursor) {
1291         goto error;
1292     }
1293 
1294     method = PyObject_GetAttrString(cursor, "execute");
1295     if (!method) {
1296         Py_CLEAR(cursor);
1297         goto error;
1298     }
1299 
1300     result = PyObject_CallObject(method, args);
1301     if (!result) {
1302         Py_CLEAR(cursor);
1303     }
1304 
1305 error:
1306     Py_XDECREF(result);
1307     Py_XDECREF(method);
1308 
1309     return cursor;
1310 }
1311 
pysqlite_connection_executemany(pysqlite_Connection * self,PyObject * args)1312 PyObject* pysqlite_connection_executemany(pysqlite_Connection* self, PyObject* args)
1313 {
1314     PyObject* cursor = 0;
1315     PyObject* result = 0;
1316     PyObject* method = 0;
1317 
1318     cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
1319     if (!cursor) {
1320         goto error;
1321     }
1322 
1323     method = PyObject_GetAttrString(cursor, "executemany");
1324     if (!method) {
1325         Py_CLEAR(cursor);
1326         goto error;
1327     }
1328 
1329     result = PyObject_CallObject(method, args);
1330     if (!result) {
1331         Py_CLEAR(cursor);
1332     }
1333 
1334 error:
1335     Py_XDECREF(result);
1336     Py_XDECREF(method);
1337 
1338     return cursor;
1339 }
1340 
pysqlite_connection_executescript(pysqlite_Connection * self,PyObject * args)1341 PyObject* pysqlite_connection_executescript(pysqlite_Connection* self, PyObject* args)
1342 {
1343     PyObject* cursor = 0;
1344     PyObject* result = 0;
1345     PyObject* method = 0;
1346 
1347     cursor = _PyObject_CallMethodId((PyObject*)self, &PyId_cursor, NULL);
1348     if (!cursor) {
1349         goto error;
1350     }
1351 
1352     method = PyObject_GetAttrString(cursor, "executescript");
1353     if (!method) {
1354         Py_CLEAR(cursor);
1355         goto error;
1356     }
1357 
1358     result = PyObject_CallObject(method, args);
1359     if (!result) {
1360         Py_CLEAR(cursor);
1361     }
1362 
1363 error:
1364     Py_XDECREF(result);
1365     Py_XDECREF(method);
1366 
1367     return cursor;
1368 }
1369 
1370 /* ------------------------- COLLATION CODE ------------------------ */
1371 
1372 static int
pysqlite_collation_callback(void * context,int text1_length,const void * text1_data,int text2_length,const void * text2_data)1373 pysqlite_collation_callback(
1374         void* context,
1375         int text1_length, const void* text1_data,
1376         int text2_length, const void* text2_data)
1377 {
1378     PyObject* callback = (PyObject*)context;
1379     PyObject* string1 = 0;
1380     PyObject* string2 = 0;
1381     PyGILState_STATE gilstate;
1382     PyObject* retval = NULL;
1383     long longval;
1384     int result = 0;
1385     gilstate = PyGILState_Ensure();
1386 
1387     if (PyErr_Occurred()) {
1388         goto finally;
1389     }
1390 
1391     string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1392     string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
1393 
1394     if (!string1 || !string2) {
1395         goto finally; /* failed to allocate strings */
1396     }
1397 
1398     retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1399 
1400     if (!retval) {
1401         /* execution failed */
1402         goto finally;
1403     }
1404 
1405     longval = PyLong_AsLongAndOverflow(retval, &result);
1406     if (longval == -1 && PyErr_Occurred()) {
1407         PyErr_Clear();
1408         result = 0;
1409     }
1410     else if (!result) {
1411         if (longval > 0)
1412             result = 1;
1413         else if (longval < 0)
1414             result = -1;
1415     }
1416 
1417 finally:
1418     Py_XDECREF(string1);
1419     Py_XDECREF(string2);
1420     Py_XDECREF(retval);
1421     PyGILState_Release(gilstate);
1422     return result;
1423 }
1424 
1425 static PyObject *
pysqlite_connection_interrupt(pysqlite_Connection * self,PyObject * args)1426 pysqlite_connection_interrupt(pysqlite_Connection* self, PyObject* args)
1427 {
1428     PyObject* retval = NULL;
1429 
1430     if (!pysqlite_check_connection(self)) {
1431         goto finally;
1432     }
1433 
1434     sqlite3_interrupt(self->db);
1435 
1436     Py_INCREF(Py_None);
1437     retval = Py_None;
1438 
1439 finally:
1440     return retval;
1441 }
1442 
1443 /* Function author: Paul Kippes <kippesp@gmail.com>
1444  * Class method of Connection to call the Python function _iterdump
1445  * of the sqlite3 module.
1446  */
1447 static PyObject *
pysqlite_connection_iterdump(pysqlite_Connection * self,PyObject * args)1448 pysqlite_connection_iterdump(pysqlite_Connection* self, PyObject* args)
1449 {
1450     _Py_IDENTIFIER(_iterdump);
1451     PyObject* retval = NULL;
1452     PyObject* module = NULL;
1453     PyObject* module_dict;
1454     PyObject* pyfn_iterdump;
1455 
1456     if (!pysqlite_check_connection(self)) {
1457         goto finally;
1458     }
1459 
1460     module = PyImport_ImportModule(MODULE_NAME ".dump");
1461     if (!module) {
1462         goto finally;
1463     }
1464 
1465     module_dict = PyModule_GetDict(module);
1466     if (!module_dict) {
1467         goto finally;
1468     }
1469 
1470     pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
1471     if (!pyfn_iterdump) {
1472         if (!PyErr_Occurred()) {
1473             PyErr_SetString(pysqlite_OperationalError,
1474                             "Failed to obtain _iterdump() reference");
1475         }
1476         goto finally;
1477     }
1478 
1479     args = PyTuple_New(1);
1480     if (!args) {
1481         goto finally;
1482     }
1483     Py_INCREF(self);
1484     PyTuple_SetItem(args, 0, (PyObject*)self);
1485     retval = PyObject_CallObject(pyfn_iterdump, args);
1486 
1487 finally:
1488     Py_XDECREF(args);
1489     Py_XDECREF(module);
1490     return retval;
1491 }
1492 
1493 #ifdef HAVE_BACKUP_API
1494 static PyObject *
pysqlite_connection_backup(pysqlite_Connection * self,PyObject * args,PyObject * kwds)1495 pysqlite_connection_backup(pysqlite_Connection *self, PyObject *args, PyObject *kwds)
1496 {
1497     PyObject *target = NULL;
1498     int pages = -1;
1499     PyObject *progress = Py_None;
1500     const char *name = "main";
1501     int rc;
1502     int callback_error = 0;
1503     PyObject *sleep_obj = NULL;
1504     int sleep_ms = 250;
1505     sqlite3 *bck_conn;
1506     sqlite3_backup *bck_handle;
1507     static char *keywords[] = {"target", "pages", "progress", "name", "sleep", NULL};
1508 
1509     if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!|$iOsO:backup", keywords,
1510                                      &pysqlite_ConnectionType, &target,
1511                                      &pages, &progress, &name, &sleep_obj)) {
1512         return NULL;
1513     }
1514 
1515     if (sleep_obj != NULL) {
1516         _PyTime_t sleep_secs;
1517         if (_PyTime_FromSecondsObject(&sleep_secs, sleep_obj,
1518                                       _PyTime_ROUND_TIMEOUT)) {
1519             return NULL;
1520         }
1521         _PyTime_t ms = _PyTime_AsMilliseconds(sleep_secs,
1522                                               _PyTime_ROUND_TIMEOUT);
1523         if (ms < INT_MIN || ms > INT_MAX) {
1524             PyErr_SetString(PyExc_OverflowError, "sleep is too large");
1525             return NULL;
1526         }
1527         sleep_ms = (int)ms;
1528     }
1529 
1530     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1531         return NULL;
1532     }
1533 
1534     if (!pysqlite_check_connection((pysqlite_Connection *)target)) {
1535         return NULL;
1536     }
1537 
1538     if ((pysqlite_Connection *)target == self) {
1539         PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1540         return NULL;
1541     }
1542 
1543 #if SQLITE_VERSION_NUMBER < 3008008
1544     /* Since 3.8.8 this is already done, per commit
1545        https://www.sqlite.org/src/info/169b5505498c0a7e */
1546     if (!sqlite3_get_autocommit(((pysqlite_Connection *)target)->db)) {
1547         PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1548         return NULL;
1549     }
1550 #endif
1551 
1552     if (progress != Py_None && !PyCallable_Check(progress)) {
1553         PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1554         return NULL;
1555     }
1556 
1557     if (pages == 0) {
1558         pages = -1;
1559     }
1560 
1561     bck_conn = ((pysqlite_Connection *)target)->db;
1562 
1563     Py_BEGIN_ALLOW_THREADS
1564     bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1565     Py_END_ALLOW_THREADS
1566 
1567     if (bck_handle) {
1568         do {
1569             Py_BEGIN_ALLOW_THREADS
1570             rc = sqlite3_backup_step(bck_handle, pages);
1571             Py_END_ALLOW_THREADS
1572 
1573             if (progress != Py_None) {
1574                 PyObject *res;
1575 
1576                 res = PyObject_CallFunction(progress, "iii", rc,
1577                                             sqlite3_backup_remaining(bck_handle),
1578                                             sqlite3_backup_pagecount(bck_handle));
1579                 if (res == NULL) {
1580                     /* User's callback raised an error: interrupt the loop and
1581                        propagate it. */
1582                     callback_error = 1;
1583                     rc = -1;
1584                 } else {
1585                     Py_DECREF(res);
1586                 }
1587             }
1588 
1589             /* Sleep for a while if there are still further pages to copy and
1590                the engine could not make any progress */
1591             if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1592                 Py_BEGIN_ALLOW_THREADS
1593                 sqlite3_sleep(sleep_ms);
1594                 Py_END_ALLOW_THREADS
1595             }
1596         } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1597 
1598         Py_BEGIN_ALLOW_THREADS
1599         rc = sqlite3_backup_finish(bck_handle);
1600         Py_END_ALLOW_THREADS
1601     } else {
1602         rc = _pysqlite_seterror(bck_conn, NULL);
1603     }
1604 
1605     if (!callback_error && rc != SQLITE_OK) {
1606         /* We cannot use _pysqlite_seterror() here because the backup APIs do
1607            not set the error status on the connection object, but rather on
1608            the backup handle. */
1609         if (rc == SQLITE_NOMEM) {
1610             (void)PyErr_NoMemory();
1611         } else {
1612 #if SQLITE_VERSION_NUMBER > 3007015
1613             PyErr_SetString(pysqlite_OperationalError, sqlite3_errstr(rc));
1614 #else
1615             switch (rc) {
1616                 case SQLITE_ERROR:
1617                     /* Description of SQLITE_ERROR in SQLite 3.7.14 and older
1618                        releases. */
1619                     PyErr_SetString(pysqlite_OperationalError,
1620                                     "SQL logic error or missing database");
1621                     break;
1622                 case SQLITE_READONLY:
1623                     PyErr_SetString(pysqlite_OperationalError,
1624                                     "attempt to write a readonly database");
1625                     break;
1626                 case SQLITE_BUSY:
1627                     PyErr_SetString(pysqlite_OperationalError, "database is locked");
1628                     break;
1629                 case SQLITE_LOCKED:
1630                     PyErr_SetString(pysqlite_OperationalError,
1631                                     "database table is locked");
1632                     break;
1633                 default:
1634                     PyErr_Format(pysqlite_OperationalError,
1635                                  "unrecognized error code: %d", rc);
1636                     break;
1637             }
1638 #endif
1639         }
1640     }
1641 
1642     if (!callback_error && rc == SQLITE_OK) {
1643         Py_RETURN_NONE;
1644     } else {
1645         return NULL;
1646     }
1647 }
1648 #endif
1649 
1650 static PyObject *
pysqlite_connection_create_collation(pysqlite_Connection * self,PyObject * args)1651 pysqlite_connection_create_collation(pysqlite_Connection* self, PyObject* args)
1652 {
1653     PyObject* callable;
1654     PyObject* uppercase_name = 0;
1655     PyObject* name;
1656     PyObject* retval;
1657     Py_ssize_t i, len;
1658     _Py_IDENTIFIER(upper);
1659     const char *uppercase_name_str;
1660     int rc;
1661     unsigned int kind;
1662     void *data;
1663 
1664     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1665         goto finally;
1666     }
1667 
1668     if (!PyArg_ParseTuple(args, "UO:create_collation(name, callback)",
1669                           &name, &callable)) {
1670         goto finally;
1671     }
1672 
1673     uppercase_name = _PyObject_CallMethodIdObjArgs((PyObject *)&PyUnicode_Type,
1674                                                    &PyId_upper, name, NULL);
1675     if (!uppercase_name) {
1676         goto finally;
1677     }
1678 
1679     if (PyUnicode_READY(uppercase_name))
1680         goto finally;
1681     len = PyUnicode_GET_LENGTH(uppercase_name);
1682     kind = PyUnicode_KIND(uppercase_name);
1683     data = PyUnicode_DATA(uppercase_name);
1684     for (i=0; i<len; i++) {
1685         Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1686         if ((ch >= '0' && ch <= '9')
1687          || (ch >= 'A' && ch <= 'Z')
1688          || (ch == '_'))
1689         {
1690             continue;
1691         } else {
1692             PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
1693             goto finally;
1694         }
1695     }
1696 
1697     uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
1698     if (!uppercase_name_str)
1699         goto finally;
1700 
1701     if (callable != Py_None && !PyCallable_Check(callable)) {
1702         PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1703         goto finally;
1704     }
1705 
1706     if (callable != Py_None) {
1707         if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1708             goto finally;
1709     } else {
1710         if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1711             goto finally;
1712     }
1713 
1714     rc = sqlite3_create_collation(self->db,
1715                                   uppercase_name_str,
1716                                   SQLITE_UTF8,
1717                                   (callable != Py_None) ? callable : NULL,
1718                                   (callable != Py_None) ? pysqlite_collation_callback : NULL);
1719     if (rc != SQLITE_OK) {
1720         PyDict_DelItem(self->collations, uppercase_name);
1721         _pysqlite_seterror(self->db, NULL);
1722         goto finally;
1723     }
1724 
1725 finally:
1726     Py_XDECREF(uppercase_name);
1727 
1728     if (PyErr_Occurred()) {
1729         retval = NULL;
1730     } else {
1731         Py_INCREF(Py_None);
1732         retval = Py_None;
1733     }
1734 
1735     return retval;
1736 }
1737 
1738 /* Called when the connection is used as a context manager. Returns itself as a
1739  * convenience to the caller. */
1740 static PyObject *
pysqlite_connection_enter(pysqlite_Connection * self,PyObject * args)1741 pysqlite_connection_enter(pysqlite_Connection* self, PyObject* args)
1742 {
1743     Py_INCREF(self);
1744     return (PyObject*)self;
1745 }
1746 
1747 /** Called when the connection is used as a context manager. If there was any
1748  * exception, a rollback takes place; otherwise we commit. */
1749 static PyObject *
pysqlite_connection_exit(pysqlite_Connection * self,PyObject * args)1750 pysqlite_connection_exit(pysqlite_Connection* self, PyObject* args)
1751 {
1752     PyObject* exc_type, *exc_value, *exc_tb;
1753     const char* method_name;
1754     PyObject* result;
1755 
1756     if (!PyArg_ParseTuple(args, "OOO", &exc_type, &exc_value, &exc_tb)) {
1757         return NULL;
1758     }
1759 
1760     if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1761         method_name = "commit";
1762     } else {
1763         method_name = "rollback";
1764     }
1765 
1766     result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
1767     if (!result) {
1768         return NULL;
1769     }
1770     Py_DECREF(result);
1771 
1772     Py_RETURN_FALSE;
1773 }
1774 
1775 static const char connection_doc[] =
1776 PyDoc_STR("SQLite database connection object.");
1777 
1778 static PyGetSetDef connection_getset[] = {
1779     {"isolation_level",  (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1780     {"total_changes",  (getter)pysqlite_connection_get_total_changes, (setter)0},
1781     {"in_transaction",  (getter)pysqlite_connection_get_in_transaction, (setter)0},
1782     {NULL}
1783 };
1784 
1785 static PyMethodDef connection_methods[] = {
1786     {"cursor", (PyCFunction)(void(*)(void))pysqlite_connection_cursor, METH_VARARGS|METH_KEYWORDS,
1787         PyDoc_STR("Return a cursor for the connection.")},
1788     {"close", (PyCFunction)pysqlite_connection_close, METH_NOARGS,
1789         PyDoc_STR("Closes the connection.")},
1790     {"commit", (PyCFunction)pysqlite_connection_commit, METH_NOARGS,
1791         PyDoc_STR("Commit the current transaction.")},
1792     {"rollback", (PyCFunction)pysqlite_connection_rollback, METH_NOARGS,
1793         PyDoc_STR("Roll back the current transaction.")},
1794     {"create_function", (PyCFunction)(void(*)(void))pysqlite_connection_create_function, METH_VARARGS|METH_KEYWORDS,
1795         PyDoc_STR("Creates a new function. Non-standard.")},
1796     {"create_aggregate", (PyCFunction)(void(*)(void))pysqlite_connection_create_aggregate, METH_VARARGS|METH_KEYWORDS,
1797         PyDoc_STR("Creates a new aggregate. Non-standard.")},
1798     {"set_authorizer", (PyCFunction)(void(*)(void))pysqlite_connection_set_authorizer, METH_VARARGS|METH_KEYWORDS,
1799         PyDoc_STR("Sets authorizer callback. Non-standard.")},
1800     #ifdef HAVE_LOAD_EXTENSION
1801     {"enable_load_extension", (PyCFunction)pysqlite_enable_load_extension, METH_VARARGS,
1802         PyDoc_STR("Enable dynamic loading of SQLite extension modules. Non-standard.")},
1803     {"load_extension", (PyCFunction)pysqlite_load_extension, METH_VARARGS,
1804         PyDoc_STR("Load SQLite extension module. Non-standard.")},
1805     #endif
1806     {"set_progress_handler", (PyCFunction)(void(*)(void))pysqlite_connection_set_progress_handler, METH_VARARGS|METH_KEYWORDS,
1807         PyDoc_STR("Sets progress handler callback. Non-standard.")},
1808     {"set_trace_callback", (PyCFunction)(void(*)(void))pysqlite_connection_set_trace_callback, METH_VARARGS|METH_KEYWORDS,
1809         PyDoc_STR("Sets a trace callback called for each SQL statement (passed as unicode). Non-standard.")},
1810     {"execute", (PyCFunction)pysqlite_connection_execute, METH_VARARGS,
1811         PyDoc_STR("Executes a SQL statement. Non-standard.")},
1812     {"executemany", (PyCFunction)pysqlite_connection_executemany, METH_VARARGS,
1813         PyDoc_STR("Repeatedly executes a SQL statement. Non-standard.")},
1814     {"executescript", (PyCFunction)pysqlite_connection_executescript, METH_VARARGS,
1815         PyDoc_STR("Executes a multiple SQL statements at once. Non-standard.")},
1816     {"create_collation", (PyCFunction)pysqlite_connection_create_collation, METH_VARARGS,
1817         PyDoc_STR("Creates a collation function. Non-standard.")},
1818     {"interrupt", (PyCFunction)pysqlite_connection_interrupt, METH_NOARGS,
1819         PyDoc_STR("Abort any pending database operation. Non-standard.")},
1820     {"iterdump", (PyCFunction)pysqlite_connection_iterdump, METH_NOARGS,
1821         PyDoc_STR("Returns iterator to the dump of the database in an SQL text format. Non-standard.")},
1822     #ifdef HAVE_BACKUP_API
1823     {"backup", (PyCFunction)(void(*)(void))pysqlite_connection_backup, METH_VARARGS | METH_KEYWORDS,
1824         PyDoc_STR("Makes a backup of the database. Non-standard.")},
1825     #endif
1826     {"__enter__", (PyCFunction)pysqlite_connection_enter, METH_NOARGS,
1827         PyDoc_STR("For context manager. Non-standard.")},
1828     {"__exit__", (PyCFunction)pysqlite_connection_exit, METH_VARARGS,
1829         PyDoc_STR("For context manager. Non-standard.")},
1830     {NULL, NULL}
1831 };
1832 
1833 static struct PyMemberDef connection_members[] =
1834 {
1835     {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1836     {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1837     {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1838     {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1839     {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1840     {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1841     {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1842     {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1843     {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1844     {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
1845     {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1846     {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
1847     {NULL}
1848 };
1849 
1850 PyTypeObject pysqlite_ConnectionType = {
1851         PyVarObject_HEAD_INIT(NULL, 0)
1852         MODULE_NAME ".Connection",                      /* tp_name */
1853         sizeof(pysqlite_Connection),                    /* tp_basicsize */
1854         0,                                              /* tp_itemsize */
1855         (destructor)pysqlite_connection_dealloc,        /* tp_dealloc */
1856         0,                                              /* tp_vectorcall_offset */
1857         0,                                              /* tp_getattr */
1858         0,                                              /* tp_setattr */
1859         0,                                              /* tp_as_async */
1860         0,                                              /* tp_repr */
1861         0,                                              /* tp_as_number */
1862         0,                                              /* tp_as_sequence */
1863         0,                                              /* tp_as_mapping */
1864         0,                                              /* tp_hash */
1865         (ternaryfunc)pysqlite_connection_call,          /* tp_call */
1866         0,                                              /* tp_str */
1867         0,                                              /* tp_getattro */
1868         0,                                              /* tp_setattro */
1869         0,                                              /* tp_as_buffer */
1870         Py_TPFLAGS_DEFAULT|Py_TPFLAGS_BASETYPE,         /* tp_flags */
1871         connection_doc,                                 /* tp_doc */
1872         0,                                              /* tp_traverse */
1873         0,                                              /* tp_clear */
1874         0,                                              /* tp_richcompare */
1875         0,                                              /* tp_weaklistoffset */
1876         0,                                              /* tp_iter */
1877         0,                                              /* tp_iternext */
1878         connection_methods,                             /* tp_methods */
1879         connection_members,                             /* tp_members */
1880         connection_getset,                              /* tp_getset */
1881         0,                                              /* tp_base */
1882         0,                                              /* tp_dict */
1883         0,                                              /* tp_descr_get */
1884         0,                                              /* tp_descr_set */
1885         0,                                              /* tp_dictoffset */
1886         (initproc)pysqlite_connection_init,             /* tp_init */
1887         0,                                              /* tp_alloc */
1888         0,                                              /* tp_new */
1889         0                                               /* tp_free */
1890 };
1891 
pysqlite_connection_setup_types(void)1892 extern int pysqlite_connection_setup_types(void)
1893 {
1894     pysqlite_ConnectionType.tp_new = PyType_GenericNew;
1895     return PyType_Ready(&pysqlite_ConnectionType);
1896 }
1897