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