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