1 /* connection.c - the connection type
2  *
3  * Copyright (C) 2004-2010 Gerhard Häring <[email protected]>
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 "module.h"
25 #include "structmember.h"         // PyMemberDef
26 #include "connection.h"
27 #include "statement.h"
28 #include "cursor.h"
29 #include "blob.h"
30 #include "prepare_protocol.h"
31 #include "util.h"
32 
33 #if SQLITE_VERSION_NUMBER >= 3014000
34 #define HAVE_TRACE_V2
35 #endif
36 
37 #if SQLITE_VERSION_NUMBER >= 3025000
38 #define HAVE_WINDOW_FUNCTIONS
39 #endif
40 
41 static const char *
get_isolation_level(const char * level)42 get_isolation_level(const char *level)
43 {
44     assert(level != NULL);
45     static const char *const allowed_levels[] = {
46         "",
47         "DEFERRED",
48         "IMMEDIATE",
49         "EXCLUSIVE",
50         NULL
51     };
52     for (int i = 0; allowed_levels[i] != NULL; i++) {
53         const char *candidate = allowed_levels[i];
54         if (sqlite3_stricmp(level, candidate) == 0) {
55             return candidate;
56         }
57     }
58     PyErr_SetString(PyExc_ValueError,
59                     "isolation_level string must be '', 'DEFERRED', "
60                     "'IMMEDIATE', or 'EXCLUSIVE'");
61     return NULL;
62 }
63 
64 static int
isolation_level_converter(PyObject * str_or_none,const char ** result)65 isolation_level_converter(PyObject *str_or_none, const char **result)
66 {
67     if (Py_IsNone(str_or_none)) {
68         *result = NULL;
69     }
70     else if (PyUnicode_Check(str_or_none)) {
71         Py_ssize_t sz;
72         const char *str = PyUnicode_AsUTF8AndSize(str_or_none, &sz);
73         if (str == NULL) {
74             return 0;
75         }
76         if (strlen(str) != (size_t)sz) {
77             PyErr_SetString(PyExc_ValueError, "embedded null character");
78             return 0;
79         }
80 
81         const char *level = get_isolation_level(str);
82         if (level == NULL) {
83             return 0;
84         }
85         *result = level;
86     }
87     else {
88         PyErr_SetString(PyExc_TypeError,
89                         "isolation_level must be str or None");
90         return 0;
91     }
92     return 1;
93 }
94 
95 static int
sqlite3_int64_converter(PyObject * obj,sqlite3_int64 * result)96 sqlite3_int64_converter(PyObject *obj, sqlite3_int64 *result)
97 {
98     if (!PyLong_Check(obj)) {
99         PyErr_SetString(PyExc_TypeError, "expected 'int'");
100         return 0;
101     }
102     *result = _pysqlite_long_as_int64(obj);
103     if (PyErr_Occurred()) {
104         return 0;
105     }
106     return 1;
107 }
108 
109 #define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
110 #include "clinic/connection.c.h"
111 #undef clinic_state
112 
113 /*[clinic input]
114 module _sqlite3
115 class _sqlite3.Connection "pysqlite_Connection *" "clinic_state()->ConnectionType"
116 [clinic start generated code]*/
117 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=67369db2faf80891]*/
118 
119 static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
120 static void free_callback_context(callback_context *ctx);
121 static void set_callback_context(callback_context **ctx_pp,
122                                  callback_context *ctx);
123 static void connection_close(pysqlite_Connection *self);
124 PyObject *_pysqlite_query_execute(pysqlite_Cursor *, int, PyObject *, PyObject *);
125 
126 static PyObject *
new_statement_cache(pysqlite_Connection * self,pysqlite_state * state,int maxsize)127 new_statement_cache(pysqlite_Connection *self, pysqlite_state *state,
128                     int maxsize)
129 {
130     PyObject *args[] = { NULL, PyLong_FromLong(maxsize), };
131     if (args[1] == NULL) {
132         return NULL;
133     }
134     PyObject *lru_cache = state->lru_cache;
135     size_t nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
136     PyObject *inner = PyObject_Vectorcall(lru_cache, args + 1, nargsf, NULL);
137     Py_DECREF(args[1]);
138     if (inner == NULL) {
139         return NULL;
140     }
141 
142     args[1] = (PyObject *)self;  // Borrowed ref.
143     nargsf = 1 | PY_VECTORCALL_ARGUMENTS_OFFSET;
144     PyObject *res = PyObject_Vectorcall(inner, args + 1, nargsf, NULL);
145     Py_DECREF(inner);
146     return res;
147 }
148 
149 /*[python input]
150 class IsolationLevel_converter(CConverter):
151     type = "const char *"
152     converter = "isolation_level_converter"
153 
154 class sqlite3_int64_converter(CConverter):
155     type = "sqlite3_int64"
156     converter = "sqlite3_int64_converter"
157 
158 [python start generated code]*/
159 /*[python end generated code: output=da39a3ee5e6b4b0d input=e9bee126e0500e61]*/
160 
161 // NB: This needs to be in sync with the sqlite3.connect docstring
162 /*[clinic input]
163 _sqlite3.Connection.__init__ as pysqlite_connection_init
164 
165     database: object
166     timeout: double = 5.0
167     detect_types: int = 0
168     isolation_level: IsolationLevel = ""
169     check_same_thread: bool(accept={int}) = True
170     factory: object(c_default='(PyObject*)clinic_state()->ConnectionType') = ConnectionType
171     cached_statements as cache_size: int = 128
172     uri: bool = False
173 [clinic start generated code]*/
174 
175 static int
pysqlite_connection_init_impl(pysqlite_Connection * self,PyObject * database,double timeout,int detect_types,const char * isolation_level,int check_same_thread,PyObject * factory,int cache_size,int uri)176 pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
177                               double timeout, int detect_types,
178                               const char *isolation_level,
179                               int check_same_thread, PyObject *factory,
180                               int cache_size, int uri)
181 /*[clinic end generated code: output=839eb2fee4293bda input=b8ce63dc6f70a383]*/
182 {
183     if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
184         return -1;
185     }
186 
187     PyObject *bytes;
188     if (!PyUnicode_FSConverter(database, &bytes)) {
189         return -1;
190     }
191 
192     if (self->initialized) {
193         PyTypeObject *tp = Py_TYPE(self);
194         tp->tp_clear((PyObject *)self);
195         connection_close(self);
196         self->initialized = 0;
197     }
198 
199     // Create and configure SQLite database object.
200     sqlite3 *db;
201     int rc;
202     Py_BEGIN_ALLOW_THREADS
203     rc = sqlite3_open_v2(PyBytes_AS_STRING(bytes), &db,
204                          SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
205                          (uri ? SQLITE_OPEN_URI : 0), NULL);
206     if (rc == SQLITE_OK) {
207         (void)sqlite3_busy_timeout(db, (int)(timeout*1000));
208     }
209     Py_END_ALLOW_THREADS
210 
211     Py_DECREF(bytes);
212     if (db == NULL && rc == SQLITE_NOMEM) {
213         PyErr_NoMemory();
214         return -1;
215     }
216 
217     pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(self));
218     if (rc != SQLITE_OK) {
219         _pysqlite_seterror(state, db);
220         goto error;
221     }
222 
223     // Create LRU statement cache; returns a new reference.
224     PyObject *statement_cache = new_statement_cache(self, state, cache_size);
225     if (statement_cache == NULL) {
226         goto error;
227     }
228 
229     /* Create lists of weak references to cursors and blobs */
230     PyObject *cursors = PyList_New(0);
231     if (cursors == NULL) {
232         Py_DECREF(statement_cache);
233         goto error;
234     }
235 
236     PyObject *blobs = PyList_New(0);
237     if (blobs == NULL) {
238         Py_DECREF(statement_cache);
239         Py_DECREF(cursors);
240         goto error;
241     }
242 
243     // Init connection state members.
244     self->db = db;
245     self->state = state;
246     self->detect_types = detect_types;
247     self->isolation_level = isolation_level;
248     self->check_same_thread = check_same_thread;
249     self->thread_ident = PyThread_get_thread_ident();
250     self->statement_cache = statement_cache;
251     self->cursors = cursors;
252     self->blobs = blobs;
253     self->created_cursors = 0;
254     self->row_factory = Py_NewRef(Py_None);
255     self->text_factory = Py_NewRef(&PyUnicode_Type);
256     self->trace_ctx = NULL;
257     self->progress_ctx = NULL;
258     self->authorizer_ctx = NULL;
259 
260     // Borrowed refs
261     self->Warning               = state->Warning;
262     self->Error                 = state->Error;
263     self->InterfaceError        = state->InterfaceError;
264     self->DatabaseError         = state->DatabaseError;
265     self->DataError             = state->DataError;
266     self->OperationalError      = state->OperationalError;
267     self->IntegrityError        = state->IntegrityError;
268     self->InternalError         = state->InternalError;
269     self->ProgrammingError      = state->ProgrammingError;
270     self->NotSupportedError     = state->NotSupportedError;
271 
272     if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
273         return -1;  // Don't goto error; at this point, dealloc will clean up.
274     }
275 
276     self->initialized = 1;
277     return 0;
278 
279 error:
280     // There are no statements or other SQLite objects attached to the
281     // database, so sqlite3_close() should always return SQLITE_OK.
282     rc = sqlite3_close(db);
283     assert(rc == SQLITE_OK);
284     return -1;
285 }
286 
287 #define VISIT_CALLBACK_CONTEXT(ctx) \
288 do {                                \
289     if (ctx) {                      \
290         Py_VISIT(ctx->callable);    \
291         Py_VISIT(ctx->module);      \
292     }                               \
293 } while (0)
294 
295 static int
connection_traverse(pysqlite_Connection * self,visitproc visit,void * arg)296 connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
297 {
298     Py_VISIT(Py_TYPE(self));
299     Py_VISIT(self->statement_cache);
300     Py_VISIT(self->cursors);
301     Py_VISIT(self->blobs);
302     Py_VISIT(self->row_factory);
303     Py_VISIT(self->text_factory);
304     VISIT_CALLBACK_CONTEXT(self->trace_ctx);
305     VISIT_CALLBACK_CONTEXT(self->progress_ctx);
306     VISIT_CALLBACK_CONTEXT(self->authorizer_ctx);
307 #undef VISIT_CALLBACK_CONTEXT
308     return 0;
309 }
310 
311 static inline void
clear_callback_context(callback_context * ctx)312 clear_callback_context(callback_context *ctx)
313 {
314     if (ctx != NULL) {
315         Py_CLEAR(ctx->callable);
316         Py_CLEAR(ctx->module);
317     }
318 }
319 
320 static int
connection_clear(pysqlite_Connection * self)321 connection_clear(pysqlite_Connection *self)
322 {
323     Py_CLEAR(self->statement_cache);
324     Py_CLEAR(self->cursors);
325     Py_CLEAR(self->blobs);
326     Py_CLEAR(self->row_factory);
327     Py_CLEAR(self->text_factory);
328     clear_callback_context(self->trace_ctx);
329     clear_callback_context(self->progress_ctx);
330     clear_callback_context(self->authorizer_ctx);
331     return 0;
332 }
333 
334 static void
free_callback_contexts(pysqlite_Connection * self)335 free_callback_contexts(pysqlite_Connection *self)
336 {
337     set_callback_context(&self->trace_ctx, NULL);
338     set_callback_context(&self->progress_ctx, NULL);
339     set_callback_context(&self->authorizer_ctx, NULL);
340 }
341 
342 static void
connection_close(pysqlite_Connection * self)343 connection_close(pysqlite_Connection *self)
344 {
345     if (self->db) {
346         free_callback_contexts(self);
347 
348         sqlite3 *db = self->db;
349         self->db = NULL;
350 
351         Py_BEGIN_ALLOW_THREADS
352         int rc = sqlite3_close_v2(db);
353         assert(rc == SQLITE_OK), (void)rc;
354         Py_END_ALLOW_THREADS
355     }
356 }
357 
358 static void
connection_dealloc(pysqlite_Connection * self)359 connection_dealloc(pysqlite_Connection *self)
360 {
361     PyTypeObject *tp = Py_TYPE(self);
362     PyObject_GC_UnTrack(self);
363     tp->tp_clear((PyObject *)self);
364 
365     /* Clean up if user has not called .close() explicitly. */
366     connection_close(self);
367 
368     tp->tp_free(self);
369     Py_DECREF(tp);
370 }
371 
372 /*[clinic input]
373 _sqlite3.Connection.cursor as pysqlite_connection_cursor
374 
375     factory: object = NULL
376 
377 Return a cursor for the connection.
378 [clinic start generated code]*/
379 
380 static PyObject *
pysqlite_connection_cursor_impl(pysqlite_Connection * self,PyObject * factory)381 pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
382 /*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/
383 {
384     PyObject* cursor;
385 
386     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
387         return NULL;
388     }
389 
390     if (factory == NULL) {
391         factory = (PyObject *)self->state->CursorType;
392     }
393 
394     cursor = PyObject_CallOneArg(factory, (PyObject *)self);
395     if (cursor == NULL)
396         return NULL;
397     if (!PyObject_TypeCheck(cursor, self->state->CursorType)) {
398         PyErr_Format(PyExc_TypeError,
399                      "factory must return a cursor, not %.100s",
400                      Py_TYPE(cursor)->tp_name);
401         Py_DECREF(cursor);
402         return NULL;
403     }
404 
405     _pysqlite_drop_unused_cursor_references(self);
406 
407     if (cursor && self->row_factory != Py_None) {
408         Py_INCREF(self->row_factory);
409         Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
410     }
411 
412     return cursor;
413 }
414 
415 /*[clinic input]
416 _sqlite3.Connection.blobopen as blobopen
417 
418     table: str
419         Table name.
420     column as col: str
421         Column name.
422     row: sqlite3_int64
423         Row index.
424     /
425     *
426     readonly: bool(accept={int}) = False
427         Open the BLOB without write permissions.
428     name: str = "main"
429         Database name.
430 
431 Open and return a BLOB object.
432 [clinic start generated code]*/
433 
434 static PyObject *
blobopen_impl(pysqlite_Connection * self,const char * table,const char * col,sqlite3_int64 row,int readonly,const char * name)435 blobopen_impl(pysqlite_Connection *self, const char *table, const char *col,
436               sqlite3_int64 row, int readonly, const char *name)
437 /*[clinic end generated code: output=6a02d43efb885d1c input=4180b11a0591d80d]*/
438 {
439     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
440         return NULL;
441     }
442 
443     int rc;
444     sqlite3_blob *blob;
445 
446     Py_BEGIN_ALLOW_THREADS
447     rc = sqlite3_blob_open(self->db, name, table, col, row, !readonly, &blob);
448     Py_END_ALLOW_THREADS
449 
450     if (rc == SQLITE_MISUSE) {
451         PyErr_Format(self->state->InterfaceError, sqlite3_errstr(rc));
452         return NULL;
453     }
454     else if (rc != SQLITE_OK) {
455         _pysqlite_seterror(self->state, self->db);
456         return NULL;
457     }
458 
459     pysqlite_Blob *obj = PyObject_GC_New(pysqlite_Blob, self->state->BlobType);
460     if (obj == NULL) {
461         goto error;
462     }
463 
464     obj->connection = (pysqlite_Connection *)Py_NewRef(self);
465     obj->blob = blob;
466     obj->offset = 0;
467     obj->in_weakreflist = NULL;
468 
469     PyObject_GC_Track(obj);
470 
471     // Add our blob to connection blobs list
472     PyObject *weakref = PyWeakref_NewRef((PyObject *)obj, NULL);
473     if (weakref == NULL) {
474         goto error;
475     }
476     rc = PyList_Append(self->blobs, weakref);
477     Py_DECREF(weakref);
478     if (rc < 0) {
479         goto error;
480     }
481 
482     return (PyObject *)obj;
483 
484 error:
485     Py_XDECREF(obj);
486     return NULL;
487 }
488 
489 /*[clinic input]
490 _sqlite3.Connection.close as pysqlite_connection_close
491 
492 Close the database connection.
493 
494 Any pending transaction is not committed implicitly.
495 [clinic start generated code]*/
496 
497 static PyObject *
pysqlite_connection_close_impl(pysqlite_Connection * self)498 pysqlite_connection_close_impl(pysqlite_Connection *self)
499 /*[clinic end generated code: output=a546a0da212c9b97 input=b3ed5b74f6fefc06]*/
500 {
501     if (!pysqlite_check_thread(self)) {
502         return NULL;
503     }
504 
505     if (!self->initialized) {
506         PyTypeObject *tp = Py_TYPE(self);
507         pysqlite_state *state = pysqlite_get_state_by_type(tp);
508         PyErr_SetString(state->ProgrammingError,
509                         "Base Connection.__init__ not called.");
510         return NULL;
511     }
512 
513     pysqlite_close_all_blobs(self);
514     Py_CLEAR(self->statement_cache);
515     connection_close(self);
516 
517     Py_RETURN_NONE;
518 }
519 
520 /*
521  * Checks if a connection object is usable (i. e. not closed).
522  *
523  * 0 => error; 1 => ok
524  */
pysqlite_check_connection(pysqlite_Connection * con)525 int pysqlite_check_connection(pysqlite_Connection* con)
526 {
527     if (!con->initialized) {
528         pysqlite_state *state = pysqlite_get_state_by_type(Py_TYPE(con));
529         PyErr_SetString(state->ProgrammingError,
530                         "Base Connection.__init__ not called.");
531         return 0;
532     }
533 
534     if (!con->db) {
535         PyErr_SetString(con->state->ProgrammingError,
536                         "Cannot operate on a closed database.");
537         return 0;
538     } else {
539         return 1;
540     }
541 }
542 
543 /*[clinic input]
544 _sqlite3.Connection.commit as pysqlite_connection_commit
545 
546 Commit any pending transaction to the database.
547 
548 If there is no open transaction, this method is a no-op.
549 [clinic start generated code]*/
550 
551 static PyObject *
pysqlite_connection_commit_impl(pysqlite_Connection * self)552 pysqlite_connection_commit_impl(pysqlite_Connection *self)
553 /*[clinic end generated code: output=3da45579e89407f2 input=c8793c97c3446065]*/
554 {
555     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
556         return NULL;
557     }
558 
559     if (!sqlite3_get_autocommit(self->db)) {
560         int rc;
561 
562         Py_BEGIN_ALLOW_THREADS
563         sqlite3_stmt *statement;
564         rc = sqlite3_prepare_v2(self->db, "COMMIT", 7, &statement, NULL);
565         if (rc == SQLITE_OK) {
566             (void)sqlite3_step(statement);
567             rc = sqlite3_finalize(statement);
568         }
569         Py_END_ALLOW_THREADS
570 
571         if (rc != SQLITE_OK) {
572             (void)_pysqlite_seterror(self->state, self->db);
573             return NULL;
574         }
575     }
576 
577     Py_RETURN_NONE;
578 }
579 
580 /*[clinic input]
581 _sqlite3.Connection.rollback as pysqlite_connection_rollback
582 
583 Roll back to the start of any pending transaction.
584 
585 If there is no open transaction, this method is a no-op.
586 [clinic start generated code]*/
587 
588 static PyObject *
pysqlite_connection_rollback_impl(pysqlite_Connection * self)589 pysqlite_connection_rollback_impl(pysqlite_Connection *self)
590 /*[clinic end generated code: output=b66fa0d43e7ef305 input=7f60a2f1076f16b3]*/
591 {
592     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
593         return NULL;
594     }
595 
596     if (!sqlite3_get_autocommit(self->db)) {
597         int rc;
598 
599         Py_BEGIN_ALLOW_THREADS
600         sqlite3_stmt *statement;
601         rc = sqlite3_prepare_v2(self->db, "ROLLBACK", 9, &statement, NULL);
602         if (rc == SQLITE_OK) {
603             (void)sqlite3_step(statement);
604             rc = sqlite3_finalize(statement);
605         }
606         Py_END_ALLOW_THREADS
607 
608         if (rc != SQLITE_OK) {
609             (void)_pysqlite_seterror(self->state, self->db);
610             return NULL;
611         }
612 
613     }
614 
615     Py_RETURN_NONE;
616 }
617 
618 static int
_pysqlite_set_result(sqlite3_context * context,PyObject * py_val)619 _pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
620 {
621     if (py_val == Py_None) {
622         sqlite3_result_null(context);
623     } else if (PyLong_Check(py_val)) {
624         sqlite_int64 value = _pysqlite_long_as_int64(py_val);
625         if (value == -1 && PyErr_Occurred())
626             return -1;
627         sqlite3_result_int64(context, value);
628     } else if (PyFloat_Check(py_val)) {
629         double value = PyFloat_AsDouble(py_val);
630         if (value == -1 && PyErr_Occurred()) {
631             return -1;
632         }
633         sqlite3_result_double(context, value);
634     } else if (PyUnicode_Check(py_val)) {
635         Py_ssize_t sz;
636         const char *str = PyUnicode_AsUTF8AndSize(py_val, &sz);
637         if (str == NULL) {
638             return -1;
639         }
640         if (sz > INT_MAX) {
641             PyErr_SetString(PyExc_OverflowError,
642                             "string is longer than INT_MAX bytes");
643             return -1;
644         }
645         sqlite3_result_text(context, str, (int)sz, SQLITE_TRANSIENT);
646     } else if (PyObject_CheckBuffer(py_val)) {
647         Py_buffer view;
648         if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
649             return -1;
650         }
651         if (view.len > INT_MAX) {
652             PyErr_SetString(PyExc_OverflowError,
653                             "BLOB longer than INT_MAX bytes");
654             PyBuffer_Release(&view);
655             return -1;
656         }
657         sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
658         PyBuffer_Release(&view);
659     } else {
660         callback_context *ctx = (callback_context *)sqlite3_user_data(context);
661         PyErr_Format(ctx->state->ProgrammingError,
662                      "User-defined functions cannot return '%s' values to "
663                      "SQLite",
664                      Py_TYPE(py_val)->tp_name);
665         return -1;
666     }
667     return 0;
668 }
669 
670 static PyObject *
_pysqlite_build_py_params(sqlite3_context * context,int argc,sqlite3_value ** argv)671 _pysqlite_build_py_params(sqlite3_context *context, int argc,
672                           sqlite3_value **argv)
673 {
674     PyObject* args;
675     int i;
676     sqlite3_value* cur_value;
677     PyObject* cur_py_value;
678 
679     args = PyTuple_New(argc);
680     if (!args) {
681         return NULL;
682     }
683 
684     for (i = 0; i < argc; i++) {
685         cur_value = argv[i];
686         switch (sqlite3_value_type(argv[i])) {
687             case SQLITE_INTEGER:
688                 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
689                 break;
690             case SQLITE_FLOAT:
691                 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
692                 break;
693             case SQLITE_TEXT: {
694                 sqlite3 *db = sqlite3_context_db_handle(context);
695                 const char *text = (const char *)sqlite3_value_text(cur_value);
696 
697                 if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
698                     PyErr_NoMemory();
699                     goto error;
700                 }
701 
702                 Py_ssize_t size = sqlite3_value_bytes(cur_value);
703                 cur_py_value = PyUnicode_FromStringAndSize(text, size);
704                 break;
705             }
706             case SQLITE_BLOB: {
707                 sqlite3 *db = sqlite3_context_db_handle(context);
708                 const void *blob = sqlite3_value_blob(cur_value);
709 
710                 if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
711                     PyErr_NoMemory();
712                     goto error;
713                 }
714 
715                 Py_ssize_t size = sqlite3_value_bytes(cur_value);
716                 cur_py_value = PyBytes_FromStringAndSize(blob, size);
717                 break;
718             }
719             case SQLITE_NULL:
720             default:
721                 cur_py_value = Py_NewRef(Py_None);
722         }
723 
724         if (!cur_py_value) {
725             goto error;
726         }
727 
728         PyTuple_SET_ITEM(args, i, cur_py_value);
729     }
730 
731     return args;
732 
733 error:
734     Py_DECREF(args);
735     return NULL;
736 }
737 
738 static void
print_or_clear_traceback(callback_context * ctx)739 print_or_clear_traceback(callback_context *ctx)
740 {
741     assert(ctx != NULL);
742     assert(ctx->state != NULL);
743     if (ctx->state->enable_callback_tracebacks) {
744         PyErr_WriteUnraisable(ctx->callable);
745     }
746     else {
747         PyErr_Clear();
748     }
749 }
750 
751 // Checks the Python exception and sets the appropriate SQLite error code.
752 static void
set_sqlite_error(sqlite3_context * context,const char * msg)753 set_sqlite_error(sqlite3_context *context, const char *msg)
754 {
755     assert(PyErr_Occurred());
756     if (PyErr_ExceptionMatches(PyExc_MemoryError)) {
757         sqlite3_result_error_nomem(context);
758     }
759     else if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
760         sqlite3_result_error_toobig(context);
761     }
762     else {
763         sqlite3_result_error(context, msg, -1);
764     }
765     callback_context *ctx = (callback_context *)sqlite3_user_data(context);
766     print_or_clear_traceback(ctx);
767 }
768 
769 static void
func_callback(sqlite3_context * context,int argc,sqlite3_value ** argv)770 func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
771 {
772     PyGILState_STATE threadstate = PyGILState_Ensure();
773 
774     PyObject* args;
775     PyObject* py_retval = NULL;
776     int ok;
777 
778     args = _pysqlite_build_py_params(context, argc, argv);
779     if (args) {
780         callback_context *ctx = (callback_context *)sqlite3_user_data(context);
781         assert(ctx != NULL);
782         py_retval = PyObject_CallObject(ctx->callable, args);
783         Py_DECREF(args);
784     }
785 
786     ok = 0;
787     if (py_retval) {
788         ok = _pysqlite_set_result(context, py_retval) == 0;
789         Py_DECREF(py_retval);
790     }
791     if (!ok) {
792         set_sqlite_error(context, "user-defined function raised exception");
793     }
794 
795     PyGILState_Release(threadstate);
796 }
797 
798 static void
step_callback(sqlite3_context * context,int argc,sqlite3_value ** params)799 step_callback(sqlite3_context *context, int argc, sqlite3_value **params)
800 {
801     PyGILState_STATE threadstate = PyGILState_Ensure();
802 
803     PyObject* args;
804     PyObject* function_result = NULL;
805     PyObject** aggregate_instance;
806     PyObject* stepmethod = NULL;
807 
808     callback_context *ctx = (callback_context *)sqlite3_user_data(context);
809     assert(ctx != NULL);
810 
811     aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
812     if (*aggregate_instance == NULL) {
813         *aggregate_instance = PyObject_CallNoArgs(ctx->callable);
814         if (!*aggregate_instance) {
815             set_sqlite_error(context,
816                     "user-defined aggregate's '__init__' method raised error");
817             goto error;
818         }
819     }
820 
821     stepmethod = PyObject_GetAttr(*aggregate_instance, ctx->state->str_step);
822     if (!stepmethod) {
823         set_sqlite_error(context,
824                 "user-defined aggregate's 'step' method not defined");
825         goto error;
826     }
827 
828     args = _pysqlite_build_py_params(context, argc, params);
829     if (!args) {
830         goto error;
831     }
832 
833     function_result = PyObject_CallObject(stepmethod, args);
834     Py_DECREF(args);
835 
836     if (!function_result) {
837         set_sqlite_error(context,
838                 "user-defined aggregate's 'step' method raised error");
839     }
840 
841 error:
842     Py_XDECREF(stepmethod);
843     Py_XDECREF(function_result);
844 
845     PyGILState_Release(threadstate);
846 }
847 
848 static void
final_callback(sqlite3_context * context)849 final_callback(sqlite3_context *context)
850 {
851     PyGILState_STATE threadstate = PyGILState_Ensure();
852 
853     PyObject* function_result;
854     PyObject** aggregate_instance;
855     int ok;
856     PyObject *exception, *value, *tb;
857 
858     aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
859     if (aggregate_instance == NULL) {
860         /* No rows matched the query; the step handler was never called. */
861         goto error;
862     }
863     else if (!*aggregate_instance) {
864         /* this branch is executed if there was an exception in the aggregate's
865          * __init__ */
866 
867         goto error;
868     }
869 
870     // Keep the exception (if any) of the last call to step, value, or inverse
871     PyErr_Fetch(&exception, &value, &tb);
872 
873     callback_context *ctx = (callback_context *)sqlite3_user_data(context);
874     assert(ctx != NULL);
875     function_result = PyObject_CallMethodNoArgs(*aggregate_instance,
876                                                 ctx->state->str_finalize);
877     Py_DECREF(*aggregate_instance);
878 
879     ok = 0;
880     if (function_result) {
881         ok = _pysqlite_set_result(context, function_result) == 0;
882         Py_DECREF(function_result);
883     }
884     if (!ok) {
885         int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError);
886         _PyErr_ChainExceptions(exception, value, tb);
887 
888         /* Note: contrary to the step, value, and inverse callbacks, SQLite
889          * does _not_, as of SQLite 3.38.0, propagate errors to sqlite3_step()
890          * from the finalize callback. This implies that execute*() will not
891          * raise OperationalError, as it normally would. */
892         set_sqlite_error(context, attr_err
893                 ? "user-defined aggregate's 'finalize' method not defined"
894                 : "user-defined aggregate's 'finalize' method raised error");
895     }
896     else {
897         PyErr_Restore(exception, value, tb);
898     }
899 
900 error:
901     PyGILState_Release(threadstate);
902 }
903 
_pysqlite_drop_unused_cursor_references(pysqlite_Connection * self)904 static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
905 {
906     PyObject* new_list;
907     PyObject* weakref;
908     int i;
909 
910     /* we only need to do this once in a while */
911     if (self->created_cursors++ < 200) {
912         return;
913     }
914 
915     self->created_cursors = 0;
916 
917     new_list = PyList_New(0);
918     if (!new_list) {
919         return;
920     }
921 
922     for (i = 0; i < PyList_Size(self->cursors); i++) {
923         weakref = PyList_GetItem(self->cursors, i);
924         if (PyWeakref_GetObject(weakref) != Py_None) {
925             if (PyList_Append(new_list, weakref) != 0) {
926                 Py_DECREF(new_list);
927                 return;
928             }
929         }
930     }
931 
932     Py_SETREF(self->cursors, new_list);
933 }
934 
935 /* Allocate a UDF/callback context structure. In order to ensure that the state
936  * pointer always outlives the callback context, we make sure it owns a
937  * reference to the module itself. create_callback_context() is always called
938  * from connection methods, so we use the defining class to fetch the module
939  * pointer.
940  */
941 static callback_context *
create_callback_context(PyTypeObject * cls,PyObject * callable)942 create_callback_context(PyTypeObject *cls, PyObject *callable)
943 {
944     callback_context *ctx = PyMem_Malloc(sizeof(callback_context));
945     if (ctx != NULL) {
946         PyObject *module = PyType_GetModule(cls);
947         ctx->callable = Py_NewRef(callable);
948         ctx->module = Py_NewRef(module);
949         ctx->state = pysqlite_get_state(module);
950     }
951     return ctx;
952 }
953 
954 static void
free_callback_context(callback_context * ctx)955 free_callback_context(callback_context *ctx)
956 {
957     assert(ctx != NULL);
958     Py_XDECREF(ctx->callable);
959     Py_XDECREF(ctx->module);
960     PyMem_Free(ctx);
961 }
962 
963 static void
set_callback_context(callback_context ** ctx_pp,callback_context * ctx)964 set_callback_context(callback_context **ctx_pp, callback_context *ctx)
965 {
966     assert(ctx_pp != NULL);
967     callback_context *tmp = *ctx_pp;
968     *ctx_pp = ctx;
969     if (tmp != NULL) {
970         free_callback_context(tmp);
971     }
972 }
973 
974 static void
destructor_callback(void * ctx)975 destructor_callback(void *ctx)
976 {
977     if (ctx != NULL) {
978         // This function may be called without the GIL held, so we need to
979         // ensure that we destroy 'ctx' with the GIL held.
980         PyGILState_STATE gstate = PyGILState_Ensure();
981         free_callback_context((callback_context *)ctx);
982         PyGILState_Release(gstate);
983     }
984 }
985 
986 /*[clinic input]
987 _sqlite3.Connection.create_function as pysqlite_connection_create_function
988 
989     cls: defining_class
990     /
991     name: str
992     narg: int
993     func: object
994     *
995     deterministic: bool = False
996 
997 Creates a new function.
998 [clinic start generated code]*/
999 
1000 static PyObject *
pysqlite_connection_create_function_impl(pysqlite_Connection * self,PyTypeObject * cls,const char * name,int narg,PyObject * func,int deterministic)1001 pysqlite_connection_create_function_impl(pysqlite_Connection *self,
1002                                          PyTypeObject *cls, const char *name,
1003                                          int narg, PyObject *func,
1004                                          int deterministic)
1005 /*[clinic end generated code: output=8a811529287ad240 input=b3e8e1d8ddaffbef]*/
1006 {
1007     int rc;
1008     int flags = SQLITE_UTF8;
1009 
1010     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1011         return NULL;
1012     }
1013 
1014     if (deterministic) {
1015 #if SQLITE_VERSION_NUMBER < 3008003
1016         PyErr_SetString(self->NotSupportedError,
1017                         "deterministic=True requires SQLite 3.8.3 or higher");
1018         return NULL;
1019 #else
1020         if (sqlite3_libversion_number() < 3008003) {
1021             PyErr_SetString(self->NotSupportedError,
1022                             "deterministic=True requires SQLite 3.8.3 or higher");
1023             return NULL;
1024         }
1025         flags |= SQLITE_DETERMINISTIC;
1026 #endif
1027     }
1028     callback_context *ctx = create_callback_context(cls, func);
1029     if (ctx == NULL) {
1030         return NULL;
1031     }
1032     rc = sqlite3_create_function_v2(self->db, name, narg, flags, ctx,
1033                                     func_callback,
1034                                     NULL,
1035                                     NULL,
1036                                     &destructor_callback);  // will decref func
1037 
1038     if (rc != SQLITE_OK) {
1039         /* Workaround for SQLite bug: no error code or string is available here */
1040         PyErr_SetString(self->OperationalError, "Error creating function");
1041         return NULL;
1042     }
1043     Py_RETURN_NONE;
1044 }
1045 
1046 #ifdef HAVE_WINDOW_FUNCTIONS
1047 /*
1048  * Regarding the 'inverse' aggregate callback:
1049  * This method is only required by window aggregate functions, not
1050  * ordinary aggregate function implementations.  It is invoked to remove
1051  * a row from the current window.  The function arguments, if any,
1052  * correspond to the row being removed.
1053  */
1054 static void
inverse_callback(sqlite3_context * context,int argc,sqlite3_value ** params)1055 inverse_callback(sqlite3_context *context, int argc, sqlite3_value **params)
1056 {
1057     PyGILState_STATE gilstate = PyGILState_Ensure();
1058 
1059     callback_context *ctx = (callback_context *)sqlite3_user_data(context);
1060     assert(ctx != NULL);
1061 
1062     int size = sizeof(PyObject *);
1063     PyObject **cls = (PyObject **)sqlite3_aggregate_context(context, size);
1064     assert(cls != NULL);
1065     assert(*cls != NULL);
1066 
1067     PyObject *method = PyObject_GetAttr(*cls, ctx->state->str_inverse);
1068     if (method == NULL) {
1069         set_sqlite_error(context,
1070                 "user-defined aggregate's 'inverse' method not defined");
1071         goto exit;
1072     }
1073 
1074     PyObject *args = _pysqlite_build_py_params(context, argc, params);
1075     if (args == NULL) {
1076         set_sqlite_error(context,
1077                 "unable to build arguments for user-defined aggregate's "
1078                 "'inverse' method");
1079         goto exit;
1080     }
1081 
1082     PyObject *res = PyObject_CallObject(method, args);
1083     Py_DECREF(args);
1084     if (res == NULL) {
1085         set_sqlite_error(context,
1086                 "user-defined aggregate's 'inverse' method raised error");
1087         goto exit;
1088     }
1089     Py_DECREF(res);
1090 
1091 exit:
1092     Py_XDECREF(method);
1093     PyGILState_Release(gilstate);
1094 }
1095 
1096 /*
1097  * Regarding the 'value' aggregate callback:
1098  * This method is only required by window aggregate functions, not
1099  * ordinary aggregate function implementations.  It is invoked to return
1100  * the current value of the aggregate.
1101  */
1102 static void
value_callback(sqlite3_context * context)1103 value_callback(sqlite3_context *context)
1104 {
1105     PyGILState_STATE gilstate = PyGILState_Ensure();
1106 
1107     callback_context *ctx = (callback_context *)sqlite3_user_data(context);
1108     assert(ctx != NULL);
1109 
1110     int size = sizeof(PyObject *);
1111     PyObject **cls = (PyObject **)sqlite3_aggregate_context(context, size);
1112     assert(cls != NULL);
1113     assert(*cls != NULL);
1114 
1115     PyObject *res = PyObject_CallMethodNoArgs(*cls, ctx->state->str_value);
1116     if (res == NULL) {
1117         int attr_err = PyErr_ExceptionMatches(PyExc_AttributeError);
1118         set_sqlite_error(context, attr_err
1119                 ? "user-defined aggregate's 'value' method not defined"
1120                 : "user-defined aggregate's 'value' method raised error");
1121     }
1122     else {
1123         int rc = _pysqlite_set_result(context, res);
1124         Py_DECREF(res);
1125         if (rc < 0) {
1126             set_sqlite_error(context,
1127                     "unable to set result from user-defined aggregate's "
1128                     "'value' method");
1129         }
1130     }
1131 
1132     PyGILState_Release(gilstate);
1133 }
1134 
1135 /*[clinic input]
1136 _sqlite3.Connection.create_window_function as create_window_function
1137 
1138     cls: defining_class
1139     name: str
1140         The name of the SQL aggregate window function to be created or
1141         redefined.
1142     num_params: int
1143         The number of arguments the step and inverse methods takes.
1144     aggregate_class: object
1145         A class with step(), finalize(), value(), and inverse() methods.
1146         Set to None to clear the window function.
1147     /
1148 
1149 Creates or redefines an aggregate window function. Non-standard.
1150 [clinic start generated code]*/
1151 
1152 static PyObject *
create_window_function_impl(pysqlite_Connection * self,PyTypeObject * cls,const char * name,int num_params,PyObject * aggregate_class)1153 create_window_function_impl(pysqlite_Connection *self, PyTypeObject *cls,
1154                             const char *name, int num_params,
1155                             PyObject *aggregate_class)
1156 /*[clinic end generated code: output=5332cd9464522235 input=46d57a54225b5228]*/
1157 {
1158     if (sqlite3_libversion_number() < 3025000) {
1159         PyErr_SetString(self->NotSupportedError,
1160                         "create_window_function() requires "
1161                         "SQLite 3.25.0 or higher");
1162         return NULL;
1163     }
1164 
1165     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1166         return NULL;
1167     }
1168 
1169     int flags = SQLITE_UTF8;
1170     int rc;
1171     if (Py_IsNone(aggregate_class)) {
1172         rc = sqlite3_create_window_function(self->db, name, num_params, flags,
1173                                             0, 0, 0, 0, 0, 0);
1174     }
1175     else {
1176         callback_context *ctx = create_callback_context(cls, aggregate_class);
1177         if (ctx == NULL) {
1178             return NULL;
1179         }
1180         rc = sqlite3_create_window_function(self->db, name, num_params, flags,
1181                                             ctx,
1182                                             &step_callback,
1183                                             &final_callback,
1184                                             &value_callback,
1185                                             &inverse_callback,
1186                                             &destructor_callback);
1187     }
1188 
1189     if (rc != SQLITE_OK) {
1190         // Errors are not set on the database connection, so we cannot
1191         // use _pysqlite_seterror().
1192         PyErr_SetString(self->ProgrammingError, sqlite3_errstr(rc));
1193         return NULL;
1194     }
1195     Py_RETURN_NONE;
1196 }
1197 #endif
1198 
1199 /*[clinic input]
1200 _sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
1201 
1202     cls: defining_class
1203     /
1204     name: str
1205     n_arg: int
1206     aggregate_class: object
1207 
1208 Creates a new aggregate.
1209 [clinic start generated code]*/
1210 
1211 static PyObject *
pysqlite_connection_create_aggregate_impl(pysqlite_Connection * self,PyTypeObject * cls,const char * name,int n_arg,PyObject * aggregate_class)1212 pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
1213                                           PyTypeObject *cls,
1214                                           const char *name, int n_arg,
1215                                           PyObject *aggregate_class)
1216 /*[clinic end generated code: output=1b02d0f0aec7ff96 input=68a2a26366d4c686]*/
1217 {
1218     int rc;
1219 
1220     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1221         return NULL;
1222     }
1223 
1224     callback_context *ctx = create_callback_context(cls, aggregate_class);
1225     if (ctx == NULL) {
1226         return NULL;
1227     }
1228     rc = sqlite3_create_function_v2(self->db, name, n_arg, SQLITE_UTF8, ctx,
1229                                     0,
1230                                     &step_callback,
1231                                     &final_callback,
1232                                     &destructor_callback); // will decref func
1233     if (rc != SQLITE_OK) {
1234         /* Workaround for SQLite bug: no error code or string is available here */
1235         PyErr_SetString(self->OperationalError, "Error creating aggregate");
1236         return NULL;
1237     }
1238     Py_RETURN_NONE;
1239 }
1240 
1241 static int
authorizer_callback(void * ctx,int action,const char * arg1,const char * arg2,const char * dbname,const char * access_attempt_source)1242 authorizer_callback(void *ctx, int action, const char *arg1,
1243                     const char *arg2 , const char *dbname,
1244                     const char *access_attempt_source)
1245 {
1246     PyGILState_STATE gilstate = PyGILState_Ensure();
1247 
1248     PyObject *ret;
1249     int rc = SQLITE_DENY;
1250 
1251     assert(ctx != NULL);
1252     PyObject *callable = ((callback_context *)ctx)->callable;
1253     ret = PyObject_CallFunction(callable, "issss", action, arg1, arg2, dbname,
1254                                 access_attempt_source);
1255 
1256     if (ret == NULL) {
1257         print_or_clear_traceback(ctx);
1258         rc = SQLITE_DENY;
1259     }
1260     else {
1261         if (PyLong_Check(ret)) {
1262             rc = _PyLong_AsInt(ret);
1263             if (rc == -1 && PyErr_Occurred()) {
1264                 print_or_clear_traceback(ctx);
1265                 rc = SQLITE_DENY;
1266             }
1267         }
1268         else {
1269             rc = SQLITE_DENY;
1270         }
1271         Py_DECREF(ret);
1272     }
1273 
1274     PyGILState_Release(gilstate);
1275     return rc;
1276 }
1277 
1278 static int
progress_callback(void * ctx)1279 progress_callback(void *ctx)
1280 {
1281     PyGILState_STATE gilstate = PyGILState_Ensure();
1282 
1283     int rc;
1284     PyObject *ret;
1285 
1286     assert(ctx != NULL);
1287     PyObject *callable = ((callback_context *)ctx)->callable;
1288     ret = PyObject_CallNoArgs(callable);
1289     if (!ret) {
1290         /* abort query if error occurred */
1291         rc = -1;
1292     }
1293     else {
1294         rc = PyObject_IsTrue(ret);
1295         Py_DECREF(ret);
1296     }
1297     if (rc < 0) {
1298         print_or_clear_traceback(ctx);
1299     }
1300 
1301     PyGILState_Release(gilstate);
1302     return rc;
1303 }
1304 
1305 #ifdef HAVE_TRACE_V2
1306 /*
1307  * From https://sqlite.org/c3ref/trace_v2.html:
1308  * The integer return value from the callback is currently ignored, though this
1309  * may change in future releases. Callback implementations should return zero
1310  * to ensure future compatibility.
1311  */
1312 static int
trace_callback(unsigned int type,void * ctx,void * stmt,void * sql)1313 trace_callback(unsigned int type, void *ctx, void *stmt, void *sql)
1314 #else
1315 static void
1316 trace_callback(void *ctx, const char *sql)
1317 #endif
1318 {
1319 #ifdef HAVE_TRACE_V2
1320     if (type != SQLITE_TRACE_STMT) {
1321         return 0;
1322     }
1323 #endif
1324 
1325     PyGILState_STATE gilstate = PyGILState_Ensure();
1326 
1327     assert(ctx != NULL);
1328     pysqlite_state *state = ((callback_context *)ctx)->state;
1329     assert(state != NULL);
1330 
1331     PyObject *py_statement = NULL;
1332 #ifdef HAVE_TRACE_V2
1333     const char *expanded_sql = sqlite3_expanded_sql((sqlite3_stmt *)stmt);
1334     if (expanded_sql == NULL) {
1335         sqlite3 *db = sqlite3_db_handle((sqlite3_stmt *)stmt);
1336         if (sqlite3_errcode(db) == SQLITE_NOMEM) {
1337             (void)PyErr_NoMemory();
1338             goto exit;
1339         }
1340 
1341         PyErr_SetString(state->DataError,
1342                 "Expanded SQL string exceeds the maximum string length");
1343         print_or_clear_traceback((callback_context *)ctx);
1344 
1345         // Fall back to unexpanded sql
1346         py_statement = PyUnicode_FromString((const char *)sql);
1347     }
1348     else {
1349         py_statement = PyUnicode_FromString(expanded_sql);
1350         sqlite3_free((void *)expanded_sql);
1351     }
1352 #else
1353     if (sql == NULL) {
1354         PyErr_SetString(state->DataError,
1355                 "Expanded SQL string exceeds the maximum string length");
1356         print_or_clear_traceback((callback_context *)ctx);
1357         goto exit;
1358     }
1359     py_statement = PyUnicode_FromString(sql);
1360 #endif
1361     if (py_statement) {
1362         PyObject *callable = ((callback_context *)ctx)->callable;
1363         PyObject *ret = PyObject_CallOneArg(callable, py_statement);
1364         Py_DECREF(py_statement);
1365         Py_XDECREF(ret);
1366     }
1367     if (PyErr_Occurred()) {
1368         print_or_clear_traceback((callback_context *)ctx);
1369     }
1370 
1371 exit:
1372     PyGILState_Release(gilstate);
1373 #ifdef HAVE_TRACE_V2
1374     return 0;
1375 #endif
1376 }
1377 
1378 /*[clinic input]
1379 _sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
1380 
1381     cls: defining_class
1382     /
1383     authorizer_callback as callable: object
1384 
1385 Sets authorizer callback.
1386 [clinic start generated code]*/
1387 
1388 static PyObject *
pysqlite_connection_set_authorizer_impl(pysqlite_Connection * self,PyTypeObject * cls,PyObject * callable)1389 pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1390                                         PyTypeObject *cls,
1391                                         PyObject *callable)
1392 /*[clinic end generated code: output=75fa60114fc971c3 input=605d32ba92dd3eca]*/
1393 {
1394     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1395         return NULL;
1396     }
1397 
1398     int rc;
1399     if (callable == Py_None) {
1400         rc = sqlite3_set_authorizer(self->db, NULL, NULL);
1401         set_callback_context(&self->authorizer_ctx, NULL);
1402     }
1403     else {
1404         callback_context *ctx = create_callback_context(cls, callable);
1405         if (ctx == NULL) {
1406             return NULL;
1407         }
1408         rc = sqlite3_set_authorizer(self->db, authorizer_callback, ctx);
1409         set_callback_context(&self->authorizer_ctx, ctx);
1410     }
1411     if (rc != SQLITE_OK) {
1412         PyErr_SetString(self->OperationalError,
1413                         "Error setting authorizer callback");
1414         set_callback_context(&self->authorizer_ctx, NULL);
1415         return NULL;
1416     }
1417     Py_RETURN_NONE;
1418 }
1419 
1420 /*[clinic input]
1421 _sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1422 
1423     cls: defining_class
1424     /
1425     progress_handler as callable: object
1426     n: int
1427 
1428 Sets progress handler callback.
1429 [clinic start generated code]*/
1430 
1431 static PyObject *
pysqlite_connection_set_progress_handler_impl(pysqlite_Connection * self,PyTypeObject * cls,PyObject * callable,int n)1432 pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1433                                               PyTypeObject *cls,
1434                                               PyObject *callable, int n)
1435 /*[clinic end generated code: output=0739957fd8034a50 input=f7c1837984bd86db]*/
1436 {
1437     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1438         return NULL;
1439     }
1440 
1441     if (callable == Py_None) {
1442         /* None clears the progress handler previously set */
1443         sqlite3_progress_handler(self->db, 0, 0, (void*)0);
1444         set_callback_context(&self->progress_ctx, NULL);
1445     }
1446     else {
1447         callback_context *ctx = create_callback_context(cls, callable);
1448         if (ctx == NULL) {
1449             return NULL;
1450         }
1451         sqlite3_progress_handler(self->db, n, progress_callback, ctx);
1452         set_callback_context(&self->progress_ctx, ctx);
1453     }
1454     Py_RETURN_NONE;
1455 }
1456 
1457 /*[clinic input]
1458 _sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1459 
1460     cls: defining_class
1461     /
1462     trace_callback as callable: object
1463 
1464 Sets a trace callback called for each SQL statement (passed as unicode).
1465 [clinic start generated code]*/
1466 
1467 static PyObject *
pysqlite_connection_set_trace_callback_impl(pysqlite_Connection * self,PyTypeObject * cls,PyObject * callable)1468 pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1469                                             PyTypeObject *cls,
1470                                             PyObject *callable)
1471 /*[clinic end generated code: output=d91048c03bfcee05 input=351a94210c5f81bb]*/
1472 {
1473     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1474         return NULL;
1475     }
1476 
1477     if (callable == Py_None) {
1478         /*
1479          * None clears the trace callback previously set
1480          *
1481          * Ref.
1482          * - https://sqlite.org/c3ref/c_trace.html
1483          * - https://sqlite.org/c3ref/trace_v2.html
1484          */
1485 #ifdef HAVE_TRACE_V2
1486         sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1487 #else
1488         sqlite3_trace(self->db, 0, (void*)0);
1489 #endif
1490         set_callback_context(&self->trace_ctx, NULL);
1491     }
1492     else {
1493         callback_context *ctx = create_callback_context(cls, callable);
1494         if (ctx == NULL) {
1495             return NULL;
1496         }
1497 #ifdef HAVE_TRACE_V2
1498         sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, trace_callback, ctx);
1499 #else
1500         sqlite3_trace(self->db, trace_callback, ctx);
1501 #endif
1502         set_callback_context(&self->trace_ctx, ctx);
1503     }
1504 
1505     Py_RETURN_NONE;
1506 }
1507 
1508 #ifdef PY_SQLITE_ENABLE_LOAD_EXTENSION
1509 /*[clinic input]
1510 _sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1511 
1512     enable as onoff: bool(accept={int})
1513     /
1514 
1515 Enable dynamic loading of SQLite extension modules.
1516 [clinic start generated code]*/
1517 
1518 static PyObject *
pysqlite_connection_enable_load_extension_impl(pysqlite_Connection * self,int onoff)1519 pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1520                                                int onoff)
1521 /*[clinic end generated code: output=9cac37190d388baf input=5f00e93f7a9d3540]*/
1522 {
1523     int rc;
1524 
1525     if (PySys_Audit("sqlite3.enable_load_extension",
1526                     "OO", self, onoff ? Py_True : Py_False) < 0) {
1527         return NULL;
1528     }
1529 
1530     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1531         return NULL;
1532     }
1533 
1534     rc = sqlite3_enable_load_extension(self->db, onoff);
1535 
1536     if (rc != SQLITE_OK) {
1537         PyErr_SetString(self->OperationalError,
1538                         "Error enabling load extension");
1539         return NULL;
1540     } else {
1541         Py_RETURN_NONE;
1542     }
1543 }
1544 
1545 /*[clinic input]
1546 _sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1547 
1548     name as extension_name: str
1549     /
1550 
1551 Load SQLite extension module.
1552 [clinic start generated code]*/
1553 
1554 static PyObject *
pysqlite_connection_load_extension_impl(pysqlite_Connection * self,const char * extension_name)1555 pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1556                                         const char *extension_name)
1557 /*[clinic end generated code: output=47eb1d7312bc97a7 input=edd507389d89d621]*/
1558 {
1559     int rc;
1560     char* errmsg;
1561 
1562     if (PySys_Audit("sqlite3.load_extension", "Os", self, extension_name) < 0) {
1563         return NULL;
1564     }
1565 
1566     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1567         return NULL;
1568     }
1569 
1570     rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1571     if (rc != 0) {
1572         PyErr_SetString(self->OperationalError, errmsg);
1573         return NULL;
1574     } else {
1575         Py_RETURN_NONE;
1576     }
1577 }
1578 #endif
1579 
pysqlite_check_thread(pysqlite_Connection * self)1580 int pysqlite_check_thread(pysqlite_Connection* self)
1581 {
1582     if (self->check_same_thread) {
1583         if (PyThread_get_thread_ident() != self->thread_ident) {
1584             PyErr_Format(self->ProgrammingError,
1585                         "SQLite objects created in a thread can only be used in that same thread. "
1586                         "The object was created in thread id %lu and this is thread id %lu.",
1587                         self->thread_ident, PyThread_get_thread_ident());
1588             return 0;
1589         }
1590 
1591     }
1592     return 1;
1593 }
1594 
pysqlite_connection_get_isolation_level(pysqlite_Connection * self,void * unused)1595 static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
1596 {
1597     if (!pysqlite_check_connection(self)) {
1598         return NULL;
1599     }
1600     if (self->isolation_level != NULL) {
1601         return PyUnicode_FromString(self->isolation_level);
1602     }
1603     Py_RETURN_NONE;
1604 }
1605 
pysqlite_connection_get_total_changes(pysqlite_Connection * self,void * unused)1606 static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
1607 {
1608     if (!pysqlite_check_connection(self)) {
1609         return NULL;
1610     } else {
1611         return Py_BuildValue("i", sqlite3_total_changes(self->db));
1612     }
1613 }
1614 
pysqlite_connection_get_in_transaction(pysqlite_Connection * self,void * unused)1615 static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1616 {
1617     if (!pysqlite_check_connection(self)) {
1618         return NULL;
1619     }
1620     if (!sqlite3_get_autocommit(self->db)) {
1621         Py_RETURN_TRUE;
1622     }
1623     Py_RETURN_FALSE;
1624 }
1625 
1626 static int
pysqlite_connection_set_isolation_level(pysqlite_Connection * self,PyObject * isolation_level,void * Py_UNUSED (ignored))1627 pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
1628 {
1629     if (isolation_level == NULL) {
1630         PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1631         return -1;
1632     }
1633     if (Py_IsNone(isolation_level)) {
1634         self->isolation_level = NULL;
1635 
1636         // Execute a COMMIT to re-enable autocommit mode
1637         PyObject *res = pysqlite_connection_commit_impl(self);
1638         if (res == NULL) {
1639             return -1;
1640         }
1641         Py_DECREF(res);
1642         return 0;
1643     }
1644     if (!isolation_level_converter(isolation_level, &self->isolation_level)) {
1645         return -1;
1646     }
1647     return 0;
1648 }
1649 
1650 static PyObject *
pysqlite_connection_call(pysqlite_Connection * self,PyObject * args,PyObject * kwargs)1651 pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1652                          PyObject *kwargs)
1653 {
1654     PyObject* sql;
1655     pysqlite_Statement* statement;
1656 
1657     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1658         return NULL;
1659     }
1660 
1661     if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
1662         return NULL;
1663 
1664     if (!PyArg_ParseTuple(args, "U", &sql))
1665         return NULL;
1666 
1667     statement = pysqlite_statement_create(self, sql);
1668     if (statement == NULL) {
1669         return NULL;
1670     }
1671 
1672     return (PyObject*)statement;
1673 }
1674 
1675 /*[clinic input]
1676 _sqlite3.Connection.execute as pysqlite_connection_execute
1677 
1678     sql: unicode
1679     parameters: object = NULL
1680     /
1681 
1682 Executes an SQL statement.
1683 [clinic start generated code]*/
1684 
1685 static PyObject *
pysqlite_connection_execute_impl(pysqlite_Connection * self,PyObject * sql,PyObject * parameters)1686 pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
1687                                  PyObject *parameters)
1688 /*[clinic end generated code: output=5be05ae01ee17ee4 input=27aa7792681ddba2]*/
1689 {
1690     PyObject* result = 0;
1691 
1692     PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
1693     if (!cursor) {
1694         goto error;
1695     }
1696 
1697     result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 0, sql, parameters);
1698     if (!result) {
1699         Py_CLEAR(cursor);
1700     }
1701 
1702 error:
1703     Py_XDECREF(result);
1704 
1705     return cursor;
1706 }
1707 
1708 /*[clinic input]
1709 _sqlite3.Connection.executemany as pysqlite_connection_executemany
1710 
1711     sql: unicode
1712     parameters: object
1713     /
1714 
1715 Repeatedly executes an SQL statement.
1716 [clinic start generated code]*/
1717 
1718 static PyObject *
pysqlite_connection_executemany_impl(pysqlite_Connection * self,PyObject * sql,PyObject * parameters)1719 pysqlite_connection_executemany_impl(pysqlite_Connection *self,
1720                                      PyObject *sql, PyObject *parameters)
1721 /*[clinic end generated code: output=776cd2fd20bfe71f input=495be76551d525db]*/
1722 {
1723     PyObject* result = 0;
1724 
1725     PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
1726     if (!cursor) {
1727         goto error;
1728     }
1729 
1730     result = _pysqlite_query_execute((pysqlite_Cursor *)cursor, 1, sql, parameters);
1731     if (!result) {
1732         Py_CLEAR(cursor);
1733     }
1734 
1735 error:
1736     Py_XDECREF(result);
1737 
1738     return cursor;
1739 }
1740 
1741 /*[clinic input]
1742 _sqlite3.Connection.executescript as pysqlite_connection_executescript
1743 
1744     sql_script as script_obj: object
1745     /
1746 
1747 Executes multiple SQL statements at once.
1748 [clinic start generated code]*/
1749 
1750 static PyObject *
pysqlite_connection_executescript(pysqlite_Connection * self,PyObject * script_obj)1751 pysqlite_connection_executescript(pysqlite_Connection *self,
1752                                   PyObject *script_obj)
1753 /*[clinic end generated code: output=4c4f9d77aa0ae37d input=f6e5f1ccfa313db4]*/
1754 {
1755     PyObject* result = 0;
1756 
1757     PyObject *cursor = pysqlite_connection_cursor_impl(self, NULL);
1758     if (!cursor) {
1759         goto error;
1760     }
1761 
1762     PyObject *meth = self->state->str_executescript;  // borrowed ref.
1763     result = PyObject_CallMethodObjArgs(cursor, meth, script_obj, NULL);
1764     if (!result) {
1765         Py_CLEAR(cursor);
1766     }
1767 
1768 error:
1769     Py_XDECREF(result);
1770 
1771     return cursor;
1772 }
1773 
1774 /* ------------------------- COLLATION CODE ------------------------ */
1775 
1776 static int
collation_callback(void * context,int text1_length,const void * text1_data,int text2_length,const void * text2_data)1777 collation_callback(void *context, int text1_length, const void *text1_data,
1778                    int text2_length, const void *text2_data)
1779 {
1780     PyGILState_STATE gilstate = PyGILState_Ensure();
1781 
1782     PyObject* string1 = 0;
1783     PyObject* string2 = 0;
1784     PyObject* retval = NULL;
1785     long longval;
1786     int result = 0;
1787 
1788     /* This callback may be executed multiple times per sqlite3_step(). Bail if
1789      * the previous call failed */
1790     if (PyErr_Occurred()) {
1791         goto finally;
1792     }
1793 
1794     string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1795     string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
1796 
1797     if (!string1 || !string2) {
1798         goto finally; /* failed to allocate strings */
1799     }
1800 
1801     callback_context *ctx = (callback_context *)context;
1802     assert(ctx != NULL);
1803     PyObject *args[] = { NULL, string1, string2 };  // Borrowed refs.
1804     size_t nargsf = 2 | PY_VECTORCALL_ARGUMENTS_OFFSET;
1805     retval = PyObject_Vectorcall(ctx->callable, args + 1, nargsf, NULL);
1806     if (retval == NULL) {
1807         /* execution failed */
1808         goto finally;
1809     }
1810 
1811     longval = PyLong_AsLongAndOverflow(retval, &result);
1812     if (longval == -1 && PyErr_Occurred()) {
1813         PyErr_Clear();
1814         result = 0;
1815     }
1816     else if (!result) {
1817         if (longval > 0)
1818             result = 1;
1819         else if (longval < 0)
1820             result = -1;
1821     }
1822 
1823 finally:
1824     Py_XDECREF(string1);
1825     Py_XDECREF(string2);
1826     Py_XDECREF(retval);
1827     PyGILState_Release(gilstate);
1828     return result;
1829 }
1830 
1831 /*[clinic input]
1832 _sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1833 
1834 Abort any pending database operation.
1835 [clinic start generated code]*/
1836 
1837 static PyObject *
pysqlite_connection_interrupt_impl(pysqlite_Connection * self)1838 pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1839 /*[clinic end generated code: output=f193204bc9e70b47 input=75ad03ade7012859]*/
1840 {
1841     PyObject* retval = NULL;
1842 
1843     if (!pysqlite_check_connection(self)) {
1844         goto finally;
1845     }
1846 
1847     sqlite3_interrupt(self->db);
1848 
1849     retval = Py_NewRef(Py_None);
1850 
1851 finally:
1852     return retval;
1853 }
1854 
1855 /* Function author: Paul Kippes <[email protected]>
1856  * Class method of Connection to call the Python function _iterdump
1857  * of the sqlite3 module.
1858  */
1859 /*[clinic input]
1860 _sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1861 
1862 Returns iterator to the dump of the database in an SQL text format.
1863 [clinic start generated code]*/
1864 
1865 static PyObject *
pysqlite_connection_iterdump_impl(pysqlite_Connection * self)1866 pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1867 /*[clinic end generated code: output=586997aaf9808768 input=1911ca756066da89]*/
1868 {
1869     PyObject* retval = NULL;
1870     PyObject* module = NULL;
1871     PyObject* module_dict;
1872     PyObject* pyfn_iterdump;
1873 
1874     if (!pysqlite_check_connection(self)) {
1875         goto finally;
1876     }
1877 
1878     module = PyImport_ImportModule(MODULE_NAME ".dump");
1879     if (!module) {
1880         goto finally;
1881     }
1882 
1883     module_dict = PyModule_GetDict(module);
1884     if (!module_dict) {
1885         goto finally;
1886     }
1887 
1888     PyObject *meth = PyUnicode_InternFromString("_iterdump");
1889     if (meth == NULL) {
1890         goto finally;
1891     }
1892     pyfn_iterdump = PyDict_GetItemWithError(module_dict, meth);
1893     Py_DECREF(meth);
1894     if (!pyfn_iterdump) {
1895         if (!PyErr_Occurred()) {
1896             PyErr_SetString(self->OperationalError,
1897                             "Failed to obtain _iterdump() reference");
1898         }
1899         goto finally;
1900     }
1901 
1902     retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
1903 
1904 finally:
1905     Py_XDECREF(module);
1906     return retval;
1907 }
1908 
1909 /*[clinic input]
1910 _sqlite3.Connection.backup as pysqlite_connection_backup
1911 
1912     target: object(type='pysqlite_Connection *', subclass_of='clinic_state()->ConnectionType')
1913     *
1914     pages: int = -1
1915     progress: object = None
1916     name: str = "main"
1917     sleep: double = 0.250
1918 
1919 Makes a backup of the database.
1920 [clinic start generated code]*/
1921 
1922 static PyObject *
pysqlite_connection_backup_impl(pysqlite_Connection * self,pysqlite_Connection * target,int pages,PyObject * progress,const char * name,double sleep)1923 pysqlite_connection_backup_impl(pysqlite_Connection *self,
1924                                 pysqlite_Connection *target, int pages,
1925                                 PyObject *progress, const char *name,
1926                                 double sleep)
1927 /*[clinic end generated code: output=306a3e6a38c36334 input=c6519d0f59d0fd7f]*/
1928 {
1929     int rc;
1930     int sleep_ms = (int)(sleep * 1000.0);
1931     sqlite3 *bck_conn;
1932     sqlite3_backup *bck_handle;
1933 
1934     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1935         return NULL;
1936     }
1937 
1938     if (!pysqlite_check_connection(target)) {
1939         return NULL;
1940     }
1941 
1942     if (target == self) {
1943         PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1944         return NULL;
1945     }
1946 
1947 #if SQLITE_VERSION_NUMBER < 3008008
1948     /* Since 3.8.8 this is already done, per commit
1949        https://www.sqlite.org/src/info/169b5505498c0a7e */
1950     if (!sqlite3_get_autocommit(target->db)) {
1951         PyErr_SetString(self->OperationalError, "target is in transaction");
1952         return NULL;
1953     }
1954 #endif
1955 
1956     if (progress != Py_None && !PyCallable_Check(progress)) {
1957         PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1958         return NULL;
1959     }
1960 
1961     if (pages == 0) {
1962         pages = -1;
1963     }
1964 
1965     bck_conn = target->db;
1966 
1967     Py_BEGIN_ALLOW_THREADS
1968     bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1969     Py_END_ALLOW_THREADS
1970 
1971     if (bck_handle == NULL) {
1972         _pysqlite_seterror(self->state, bck_conn);
1973         return NULL;
1974     }
1975 
1976     do {
1977         Py_BEGIN_ALLOW_THREADS
1978         rc = sqlite3_backup_step(bck_handle, pages);
1979         Py_END_ALLOW_THREADS
1980 
1981         if (progress != Py_None) {
1982             int remaining = sqlite3_backup_remaining(bck_handle);
1983             int pagecount = sqlite3_backup_pagecount(bck_handle);
1984             PyObject *res = PyObject_CallFunction(progress, "iii", rc,
1985                                                   remaining, pagecount);
1986             if (res == NULL) {
1987                 /* Callback failed: abort backup and bail. */
1988                 Py_BEGIN_ALLOW_THREADS
1989                 sqlite3_backup_finish(bck_handle);
1990                 Py_END_ALLOW_THREADS
1991                 return NULL;
1992             }
1993             Py_DECREF(res);
1994         }
1995 
1996         /* Sleep for a while if there are still further pages to copy and
1997            the engine could not make any progress */
1998         if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1999             Py_BEGIN_ALLOW_THREADS
2000             sqlite3_sleep(sleep_ms);
2001             Py_END_ALLOW_THREADS
2002         }
2003     } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
2004 
2005     Py_BEGIN_ALLOW_THREADS
2006     rc = sqlite3_backup_finish(bck_handle);
2007     Py_END_ALLOW_THREADS
2008 
2009     if (rc != SQLITE_OK) {
2010         _pysqlite_seterror(self->state, bck_conn);
2011         return NULL;
2012     }
2013 
2014     Py_RETURN_NONE;
2015 }
2016 
2017 /*[clinic input]
2018 _sqlite3.Connection.create_collation as pysqlite_connection_create_collation
2019 
2020     cls: defining_class
2021     name: str
2022     callback as callable: object
2023     /
2024 
2025 Creates a collation function.
2026 [clinic start generated code]*/
2027 
2028 static PyObject *
pysqlite_connection_create_collation_impl(pysqlite_Connection * self,PyTypeObject * cls,const char * name,PyObject * callable)2029 pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
2030                                           PyTypeObject *cls,
2031                                           const char *name,
2032                                           PyObject *callable)
2033 /*[clinic end generated code: output=32d339e97869c378 input=f67ecd2e31e61ad3]*/
2034 {
2035     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
2036         return NULL;
2037     }
2038 
2039     callback_context *ctx = NULL;
2040     int rc;
2041     int flags = SQLITE_UTF8;
2042     if (callable == Py_None) {
2043         rc = sqlite3_create_collation_v2(self->db, name, flags,
2044                                          NULL, NULL, NULL);
2045     }
2046     else {
2047         if (!PyCallable_Check(callable)) {
2048             PyErr_SetString(PyExc_TypeError, "parameter must be callable");
2049             return NULL;
2050         }
2051         ctx = create_callback_context(cls, callable);
2052         if (ctx == NULL) {
2053             return NULL;
2054         }
2055         rc = sqlite3_create_collation_v2(self->db, name, flags, ctx,
2056                                          &collation_callback,
2057                                          &destructor_callback);
2058     }
2059 
2060     if (rc != SQLITE_OK) {
2061         /* Unlike other sqlite3_* functions, the destructor callback is _not_
2062          * called if sqlite3_create_collation_v2() fails, so we have to free
2063          * the context before returning.
2064          */
2065         if (callable != Py_None) {
2066             free_callback_context(ctx);
2067         }
2068         _pysqlite_seterror(self->state, self->db);
2069         return NULL;
2070     }
2071 
2072     Py_RETURN_NONE;
2073 }
2074 
2075 #ifdef PY_SQLITE_HAVE_SERIALIZE
2076 /*[clinic input]
2077 _sqlite3.Connection.serialize as serialize
2078 
2079     *
2080     name: str = "main"
2081         Which database to serialize.
2082 
2083 Serialize a database into a byte string.
2084 
2085 For an ordinary on-disk database file, the serialization is just a copy of the
2086 disk file. For an in-memory database or a "temp" database, the serialization is
2087 the same sequence of bytes which would be written to disk if that database
2088 were backed up to disk.
2089 [clinic start generated code]*/
2090 
2091 static PyObject *
serialize_impl(pysqlite_Connection * self,const char * name)2092 serialize_impl(pysqlite_Connection *self, const char *name)
2093 /*[clinic end generated code: output=97342b0e55239dd3 input=d2eb5194a65abe2b]*/
2094 {
2095     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
2096         return NULL;
2097     }
2098 
2099     /* If SQLite has a contiguous memory representation of the database, we can
2100      * avoid memory allocations, so we try with the no-copy flag first.
2101      */
2102     sqlite3_int64 size;
2103     unsigned int flags = SQLITE_SERIALIZE_NOCOPY;
2104     const char *data;
2105 
2106     Py_BEGIN_ALLOW_THREADS
2107     data = (const char *)sqlite3_serialize(self->db, name, &size, flags);
2108     if (data == NULL) {
2109         flags &= ~SQLITE_SERIALIZE_NOCOPY;
2110         data = (const char *)sqlite3_serialize(self->db, name, &size, flags);
2111     }
2112     Py_END_ALLOW_THREADS
2113 
2114     if (data == NULL) {
2115         PyErr_Format(self->OperationalError, "unable to serialize '%s'",
2116                      name);
2117         return NULL;
2118     }
2119     PyObject *res = PyBytes_FromStringAndSize(data, (Py_ssize_t)size);
2120     if (!(flags & SQLITE_SERIALIZE_NOCOPY)) {
2121         sqlite3_free((void *)data);
2122     }
2123     return res;
2124 }
2125 
2126 /*[clinic input]
2127 _sqlite3.Connection.deserialize as deserialize
2128 
2129     data: Py_buffer(accept={buffer, str})
2130         The serialized database content.
2131     /
2132     *
2133     name: str = "main"
2134         Which database to reopen with the deserialization.
2135 
2136 Load a serialized database.
2137 
2138 The deserialize interface causes the database connection to disconnect from the
2139 target database, and then reopen it as an in-memory database based on the given
2140 serialized data.
2141 
2142 The deserialize interface will fail with SQLITE_BUSY if the database is
2143 currently in a read transaction or is involved in a backup operation.
2144 [clinic start generated code]*/
2145 
2146 static PyObject *
deserialize_impl(pysqlite_Connection * self,Py_buffer * data,const char * name)2147 deserialize_impl(pysqlite_Connection *self, Py_buffer *data,
2148                  const char *name)
2149 /*[clinic end generated code: output=e394c798b98bad89 input=1be4ca1faacf28f2]*/
2150 {
2151     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
2152         return NULL;
2153     }
2154 
2155     /* Transfer ownership of the buffer to SQLite:
2156      * - Move buffer from Py to SQLite
2157      * - Tell SQLite to free buffer memory
2158      * - Tell SQLite that it is permitted to grow the resulting database
2159      *
2160      * Make sure we don't overflow sqlite3_deserialize(); it accepts a signed
2161      * 64-bit int as its data size argument.
2162      *
2163      * We can safely use sqlite3_malloc64 here, since it was introduced before
2164      * the serialize APIs.
2165      */
2166     if (data->len > 9223372036854775807) {  // (1 << 63) - 1
2167         PyErr_SetString(PyExc_OverflowError, "'data' is too large");
2168         return NULL;
2169     }
2170 
2171     sqlite3_int64 size = (sqlite3_int64)data->len;
2172     unsigned char *buf = sqlite3_malloc64(size);
2173     if (buf == NULL) {
2174         return PyErr_NoMemory();
2175     }
2176 
2177     const unsigned int flags = SQLITE_DESERIALIZE_FREEONCLOSE |
2178                                SQLITE_DESERIALIZE_RESIZEABLE;
2179     int rc;
2180     Py_BEGIN_ALLOW_THREADS
2181     (void)memcpy(buf, data->buf, data->len);
2182     rc = sqlite3_deserialize(self->db, name, buf, size, size, flags);
2183     Py_END_ALLOW_THREADS
2184 
2185     if (rc != SQLITE_OK) {
2186         (void)_pysqlite_seterror(self->state, self->db);
2187         return NULL;
2188     }
2189     Py_RETURN_NONE;
2190 }
2191 #endif  // PY_SQLITE_HAVE_SERIALIZE
2192 
2193 
2194 /*[clinic input]
2195 _sqlite3.Connection.__enter__ as pysqlite_connection_enter
2196 
2197 Called when the connection is used as a context manager.
2198 
2199 Returns itself as a convenience to the caller.
2200 [clinic start generated code]*/
2201 
2202 static PyObject *
pysqlite_connection_enter_impl(pysqlite_Connection * self)2203 pysqlite_connection_enter_impl(pysqlite_Connection *self)
2204 /*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
2205 {
2206     if (!pysqlite_check_connection(self)) {
2207         return NULL;
2208     }
2209     return Py_NewRef((PyObject *)self);
2210 }
2211 
2212 /*[clinic input]
2213 _sqlite3.Connection.__exit__ as pysqlite_connection_exit
2214 
2215     type as exc_type: object
2216     value as exc_value: object
2217     traceback as exc_tb: object
2218     /
2219 
2220 Called when the connection is used as a context manager.
2221 
2222 If there was any exception, a rollback takes place; otherwise we commit.
2223 [clinic start generated code]*/
2224 
2225 static PyObject *
pysqlite_connection_exit_impl(pysqlite_Connection * self,PyObject * exc_type,PyObject * exc_value,PyObject * exc_tb)2226 pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
2227                               PyObject *exc_value, PyObject *exc_tb)
2228 /*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
2229 {
2230     int commit = 0;
2231     PyObject* result;
2232 
2233     if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
2234         commit = 1;
2235         result = pysqlite_connection_commit_impl(self);
2236     }
2237     else {
2238         result = pysqlite_connection_rollback_impl(self);
2239     }
2240 
2241     if (result == NULL) {
2242         if (commit) {
2243             /* Commit failed; try to rollback in order to unlock the database.
2244              * If rollback also fails, chain the exceptions. */
2245             PyObject *exc, *val, *tb;
2246             PyErr_Fetch(&exc, &val, &tb);
2247             result = pysqlite_connection_rollback_impl(self);
2248             if (result == NULL) {
2249                 _PyErr_ChainExceptions(exc, val, tb);
2250             }
2251             else {
2252                 Py_DECREF(result);
2253                 PyErr_Restore(exc, val, tb);
2254             }
2255         }
2256         return NULL;
2257     }
2258     Py_DECREF(result);
2259 
2260     Py_RETURN_FALSE;
2261 }
2262 
2263 /*[clinic input]
2264 _sqlite3.Connection.setlimit as setlimit
2265 
2266     category: int
2267         The limit category to be set.
2268     limit: int
2269         The new limit. If the new limit is a negative number, the limit is
2270         unchanged.
2271     /
2272 
2273 Set connection run-time limits.
2274 
2275 Attempts to increase a limit above its hard upper bound are silently truncated
2276 to the hard upper bound. Regardless of whether or not the limit was changed,
2277 the prior value of the limit is returned.
2278 [clinic start generated code]*/
2279 
2280 static PyObject *
setlimit_impl(pysqlite_Connection * self,int category,int limit)2281 setlimit_impl(pysqlite_Connection *self, int category, int limit)
2282 /*[clinic end generated code: output=0d208213f8d68ccd input=9bd469537e195635]*/
2283 {
2284     if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
2285         return NULL;
2286     }
2287 
2288     int old_limit = sqlite3_limit(self->db, category, limit);
2289     if (old_limit < 0) {
2290         PyErr_SetString(self->ProgrammingError, "'category' is out of bounds");
2291         return NULL;
2292     }
2293     return PyLong_FromLong(old_limit);
2294 }
2295 
2296 /*[clinic input]
2297 _sqlite3.Connection.getlimit as getlimit
2298 
2299     category: int
2300         The limit category to be queried.
2301     /
2302 
2303 Get connection run-time limits.
2304 [clinic start generated code]*/
2305 
2306 static PyObject *
getlimit_impl(pysqlite_Connection * self,int category)2307 getlimit_impl(pysqlite_Connection *self, int category)
2308 /*[clinic end generated code: output=7c3f5d11f24cecb1 input=61e0849fb4fb058f]*/
2309 {
2310     return setlimit_impl(self, category, -1);
2311 }
2312 
2313 
2314 static const char connection_doc[] =
2315 PyDoc_STR("SQLite database connection object.");
2316 
2317 static PyGetSetDef connection_getset[] = {
2318     {"isolation_level",  (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
2319     {"total_changes",  (getter)pysqlite_connection_get_total_changes, (setter)0},
2320     {"in_transaction",  (getter)pysqlite_connection_get_in_transaction, (setter)0},
2321     {NULL}
2322 };
2323 
2324 static PyMethodDef connection_methods[] = {
2325     PYSQLITE_CONNECTION_BACKUP_METHODDEF
2326     PYSQLITE_CONNECTION_CLOSE_METHODDEF
2327     PYSQLITE_CONNECTION_COMMIT_METHODDEF
2328     PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
2329     PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
2330     PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
2331     PYSQLITE_CONNECTION_CURSOR_METHODDEF
2332     PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
2333     PYSQLITE_CONNECTION_ENTER_METHODDEF
2334     PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
2335     PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
2336     PYSQLITE_CONNECTION_EXECUTE_METHODDEF
2337     PYSQLITE_CONNECTION_EXIT_METHODDEF
2338     PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
2339     PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
2340     PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
2341     PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
2342     PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
2343     PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
2344     PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
2345     SETLIMIT_METHODDEF
2346     GETLIMIT_METHODDEF
2347     SERIALIZE_METHODDEF
2348     DESERIALIZE_METHODDEF
2349     CREATE_WINDOW_FUNCTION_METHODDEF
2350     BLOBOPEN_METHODDEF
2351     {NULL, NULL}
2352 };
2353 
2354 static struct PyMemberDef connection_members[] =
2355 {
2356     {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
2357     {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
2358     {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
2359     {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
2360     {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
2361     {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
2362     {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
2363     {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
2364     {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
2365     {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
2366     {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
2367     {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
2368     {NULL}
2369 };
2370 
2371 static PyType_Slot connection_slots[] = {
2372     {Py_tp_dealloc, connection_dealloc},
2373     {Py_tp_doc, (void *)connection_doc},
2374     {Py_tp_methods, connection_methods},
2375     {Py_tp_members, connection_members},
2376     {Py_tp_getset, connection_getset},
2377     {Py_tp_init, pysqlite_connection_init},
2378     {Py_tp_call, pysqlite_connection_call},
2379     {Py_tp_traverse, connection_traverse},
2380     {Py_tp_clear, connection_clear},
2381     {0, NULL},
2382 };
2383 
2384 static PyType_Spec connection_spec = {
2385     .name = MODULE_NAME ".Connection",
2386     .basicsize = sizeof(pysqlite_Connection),
2387     .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
2388               Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
2389     .slots = connection_slots,
2390 };
2391 
2392 int
pysqlite_connection_setup_types(PyObject * module)2393 pysqlite_connection_setup_types(PyObject *module)
2394 {
2395     PyObject *type = PyType_FromModuleAndSpec(module, &connection_spec, NULL);
2396     if (type == NULL) {
2397         return -1;
2398     }
2399     pysqlite_state *state = pysqlite_get_state(module);
2400     state->ConnectionType = (PyTypeObject *)type;
2401     return 0;
2402 }
2403