Debian Codegraph @

Modules/_sqlite/connection.c

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