blob: fb5411243c67988cd0da5e3344f91dd0b50b2753 [file] [log] [blame]
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001/* connection.c - the connection type
2 *
Florent Xiclunac934f322010-09-03 23:47:32 +00003 * Copyright (C) 2004-2010 Gerhard Häring <gh@ghaering.de>
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00004 *
5 * This file is part of pysqlite.
Victor Stinner86999502010-05-19 01:27:23 +00006 *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00007 * 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"
Victor Stinner4a21e572020-04-15 02:35:41 +020026#include "structmember.h" // PyMemberDef
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000027#include "connection.h"
28#include "statement.h"
29#include "cursor.h"
30#include "prepare_protocol.h"
31#include "util.h"
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000032
Gerhard Häringe7ea7452008-03-29 00:45:29 +000033#define ACTION_FINALIZE 1
34#define ACTION_RESET 2
35
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +020036#if SQLITE_VERSION_NUMBER >= 3014000
37#define HAVE_TRACE_V2
38#endif
39
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +010040#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
Martin v. Löwise75fc142013-11-07 18:46:53 +010047_Py_IDENTIFIER(cursor);
48
Serhiy Storchaka28914922016-09-01 22:18:03 +030049static const char * const begin_statements[] = {
50 "BEGIN ",
51 "BEGIN DEFERRED",
52 "BEGIN IMMEDIATE",
53 "BEGIN EXCLUSIVE",
54 NULL
55};
56
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +020057static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored));
Gerhard Häringf9cee222010-03-05 15:20:03 +000058static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +010060static int
61pysqlite_connection_init(pysqlite_Connection *self, PyObject *args,
62 PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000063{
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010064 static char *kwlist[] = {
65 "database", "timeout", "detect_types", "isolation_level",
66 "check_same_thread", "factory", "cached_statements", "uri",
67 NULL
68 };
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000069
Serhiy Storchaka8f87eef2020-04-12 14:58:27 +030070 const char* database;
Anders Lorentsena22a1272017-11-07 01:47:43 +010071 PyObject* database_obj;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000072 int detect_types = 0;
73 PyObject* isolation_level = NULL;
74 PyObject* factory = NULL;
75 int check_same_thread = 1;
76 int cached_statements = 100;
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010077 int uri = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000078 double timeout = 5.0;
79 int rc;
80
Anders Lorentsena22a1272017-11-07 01:47:43 +010081 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O&|diOiOip", kwlist,
82 PyUnicode_FSConverter, &database_obj, &timeout, &detect_types,
Antoine Pitrou902fc8b2013-02-10 00:02:44 +010083 &isolation_level, &check_same_thread,
84 &factory, &cached_statements, &uri))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000085 {
Gerhard Häringe7ea7452008-03-29 00:45:29 +000086 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000087 }
88
Erlend Egeberg Aaslandc96cc082021-05-02 23:25:17 +020089 if (PySys_Audit("sqlite3.connect", "O", database_obj) < 0) {
90 return -1;
91 }
92
Anders Lorentsena22a1272017-11-07 01:47:43 +010093 database = PyBytes_AsString(database_obj);
94
Gerhard Häringf9cee222010-03-05 15:20:03 +000095 self->initialized = 1;
96
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000097 self->begin_statement = NULL;
98
Oren Milman93c5a5d2017-10-10 22:27:46 +030099 Py_CLEAR(self->statement_cache);
100 Py_CLEAR(self->statements);
101 Py_CLEAR(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000102
103 Py_INCREF(Py_None);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300104 Py_XSETREF(self->row_factory, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000105
106 Py_INCREF(&PyUnicode_Type);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300107 Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000108
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100109 Py_BEGIN_ALLOW_THREADS
110 rc = sqlite3_open_v2(database, &self->db,
111 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
112 (uri ? SQLITE_OPEN_URI : 0), NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000113 Py_END_ALLOW_THREADS
114
Anders Lorentsena22a1272017-11-07 01:47:43 +0100115 Py_DECREF(database_obj);
116
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000118 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000119 return -1;
120 }
121
122 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000123 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124 if (!isolation_level) {
125 return -1;
126 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000127 } else {
128 Py_INCREF(isolation_level);
129 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300130 Py_CLEAR(self->isolation_level);
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +0200131 if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) < 0) {
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100132 Py_DECREF(isolation_level);
133 return -1;
134 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000135 Py_DECREF(isolation_level);
136
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200137 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000138 if (PyErr_Occurred()) {
139 return -1;
140 }
141
Gerhard Häringf9cee222010-03-05 15:20:03 +0000142 self->created_statements = 0;
143 self->created_cursors = 0;
144
145 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000146 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000147 self->cursors = PyList_New(0);
148 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000149 return -1;
150 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000151
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000152 /* By default, the Cache class INCREFs the factory in its initializer, and
153 * decrefs it in its deallocator method. Since this would create a circular
154 * reference here, we're breaking it by decrementing self, and telling the
155 * cache class to not decref the factory (self) in its deallocator.
156 */
157 self->statement_cache->decref_factory = 0;
158 Py_DECREF(self);
159
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000160 self->detect_types = detect_types;
161 self->timeout = timeout;
162 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000163 self->thread_ident = PyThread_get_thread_ident();
164 self->check_same_thread = check_same_thread;
165
gescheitb9a03762019-07-13 06:15:49 +0300166 self->function_pinboard_trace_callback = NULL;
167 self->function_pinboard_progress_handler = NULL;
168 self->function_pinboard_authorizer_cb = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169
Oren Milman93c5a5d2017-10-10 22:27:46 +0300170 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000171 if (!self->collations) {
172 return -1;
173 }
174
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000175 self->Warning = pysqlite_Warning;
176 self->Error = pysqlite_Error;
177 self->InterfaceError = pysqlite_InterfaceError;
178 self->DatabaseError = pysqlite_DatabaseError;
179 self->DataError = pysqlite_DataError;
180 self->OperationalError = pysqlite_OperationalError;
181 self->IntegrityError = pysqlite_IntegrityError;
182 self->InternalError = pysqlite_InternalError;
183 self->ProgrammingError = pysqlite_ProgrammingError;
184 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000185
Erlend Egeberg Aaslandc96cc082021-05-02 23:25:17 +0200186 if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
187 return -1;
188 }
189
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000190 return 0;
191}
192
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000193/* action in (ACTION_RESET, ACTION_FINALIZE) */
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100194static void
195pysqlite_do_all_statements(pysqlite_Connection *self, int action,
196 int reset_cursors)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000197{
Thomas Wouters477c8d52006-05-27 19:21:47 +0000198 int i;
199 PyObject* weakref;
200 PyObject* statement;
Gerhard Häringf9cee222010-03-05 15:20:03 +0000201 pysqlite_Cursor* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000202
Thomas Wouters477c8d52006-05-27 19:21:47 +0000203 for (i = 0; i < PyList_Size(self->statements); i++) {
204 weakref = PyList_GetItem(self->statements, i);
205 statement = PyWeakref_GetObject(weakref);
206 if (statement != Py_None) {
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500207 Py_INCREF(statement);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000208 if (action == ACTION_RESET) {
209 (void)pysqlite_statement_reset((pysqlite_Statement*)statement);
210 } else {
211 (void)pysqlite_statement_finalize((pysqlite_Statement*)statement);
212 }
Benjamin Peterson5c2b09e2011-05-31 21:31:37 -0500213 Py_DECREF(statement);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000214 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000215 }
Gerhard Häringf9cee222010-03-05 15:20:03 +0000216
217 if (reset_cursors) {
218 for (i = 0; i < PyList_Size(self->cursors); i++) {
219 weakref = PyList_GetItem(self->cursors, i);
220 cursor = (pysqlite_Cursor*)PyWeakref_GetObject(weakref);
221 if ((PyObject*)cursor != Py_None) {
222 cursor->reset = 1;
223 }
224 }
225 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000226}
227
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100228static void
229pysqlite_connection_dealloc(pysqlite_Connection *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200231 PyTypeObject *tp = Py_TYPE(self);
232
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000233 Py_XDECREF(self->statement_cache);
234
235 /* Clean up if user has not called .close() explicitly. */
236 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100237 sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000238 }
239
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240 Py_XDECREF(self->isolation_level);
gescheitb9a03762019-07-13 06:15:49 +0300241 Py_XDECREF(self->function_pinboard_trace_callback);
242 Py_XDECREF(self->function_pinboard_progress_handler);
243 Py_XDECREF(self->function_pinboard_authorizer_cb);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000244 Py_XDECREF(self->row_factory);
245 Py_XDECREF(self->text_factory);
246 Py_XDECREF(self->collations);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000247 Py_XDECREF(self->statements);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000248 Py_XDECREF(self->cursors);
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200249
250 tp->tp_free(self);
251 Py_DECREF(tp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252}
253
Gerhard Häringf9cee222010-03-05 15:20:03 +0000254/*
255 * Registers a cursor with the connection.
256 *
257 * 0 => error; 1 => ok
258 */
259int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
260{
261 PyObject* weakref;
262
263 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
264 if (!weakref) {
265 goto error;
266 }
267
268 if (PyList_Append(connection->cursors, weakref) != 0) {
269 Py_CLEAR(weakref);
270 goto error;
271 }
272
273 Py_DECREF(weakref);
274
275 return 1;
276error:
277 return 0;
278}
279
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100280/*[clinic input]
281_sqlite3.Connection.cursor as pysqlite_connection_cursor
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000282
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100283 factory: object = NULL
284
285Return a cursor for the connection.
286[clinic start generated code]*/
287
288static PyObject *
289pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
290/*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/
291{
292 PyObject* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000293
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000294 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000295 return NULL;
296 }
297
298 if (factory == NULL) {
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200299 factory = (PyObject*)pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000300 }
301
Petr Viktorinffd97532020-02-11 17:46:57 +0100302 cursor = PyObject_CallOneArg(factory, (PyObject *)self);
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300303 if (cursor == NULL)
304 return NULL;
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200305 if (!PyObject_TypeCheck(cursor, pysqlite_CursorType)) {
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300306 PyErr_Format(PyExc_TypeError,
307 "factory must return a cursor, not %.100s",
308 Py_TYPE(cursor)->tp_name);
309 Py_DECREF(cursor);
310 return NULL;
311 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312
Gerhard Häringf9cee222010-03-05 15:20:03 +0000313 _pysqlite_drop_unused_cursor_references(self);
314
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300317 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 }
319
320 return cursor;
321}
322
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100323/*[clinic input]
324_sqlite3.Connection.close as pysqlite_connection_close
325
326Closes the connection.
327[clinic start generated code]*/
328
329static PyObject *
330pysqlite_connection_close_impl(pysqlite_Connection *self)
331/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000332{
333 int rc;
334
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000335 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000336 return NULL;
337 }
338
Gerhard Häringf9cee222010-03-05 15:20:03 +0000339 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000340
341 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100342 rc = sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000343
344 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000345 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000346 return NULL;
347 } else {
348 self->db = NULL;
349 }
350 }
351
Berker Peksagfe21de92016-04-09 07:34:39 +0300352 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000353}
354
355/*
356 * Checks if a connection object is usable (i. e. not closed).
357 *
358 * 0 => error; 1 => ok
359 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000360int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000361{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000362 if (!con->initialized) {
363 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
364 return 0;
365 }
366
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000367 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000368 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369 return 0;
370 } else {
371 return 1;
372 }
373}
374
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000375PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000376{
377 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000378 sqlite3_stmt* statement;
379
380 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100381 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement,
382 NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000383 Py_END_ALLOW_THREADS
384
385 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000386 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000387 goto error;
388 }
389
Benjamin Petersond7b03282008-09-13 15:58:53 +0000390 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300391 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000392 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000393 }
394
395 Py_BEGIN_ALLOW_THREADS
396 rc = sqlite3_finalize(statement);
397 Py_END_ALLOW_THREADS
398
399 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000400 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000401 }
402
403error:
404 if (PyErr_Occurred()) {
405 return NULL;
406 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200407 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000408 }
409}
410
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +0100411/*[clinic input]
412_sqlite3.Connection.commit as pysqlite_connection_commit
413
414Commit the current transaction.
415[clinic start generated code]*/
416
417static PyObject *
418pysqlite_connection_commit_impl(pysqlite_Connection *self)
419/*[clinic end generated code: output=3da45579e89407f2 input=39c12c04dda276a8]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000420{
421 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000422 sqlite3_stmt* statement;
423
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000424 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425 return NULL;
426 }
427
Berker Peksag59da4b32016-09-12 07:16:43 +0300428 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000429
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100431 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000432 Py_END_ALLOW_THREADS
433 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000434 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000435 goto error;
436 }
437
Benjamin Petersond7b03282008-09-13 15:58:53 +0000438 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300439 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000440 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000441 }
442
443 Py_BEGIN_ALLOW_THREADS
444 rc = sqlite3_finalize(statement);
445 Py_END_ALLOW_THREADS
446 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000447 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 }
449
450 }
451
452error:
453 if (PyErr_Occurred()) {
454 return NULL;
455 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200456 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000457 }
458}
459
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100460/*[clinic input]
461_sqlite3.Connection.rollback as pysqlite_connection_rollback
462
463Roll back the current transaction.
464[clinic start generated code]*/
465
466static PyObject *
467pysqlite_connection_rollback_impl(pysqlite_Connection *self)
468/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000469{
470 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000471 sqlite3_stmt* statement;
472
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000473 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000474 return NULL;
475 }
476
Berker Peksag59da4b32016-09-12 07:16:43 +0300477 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000478 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000479
480 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100481 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000482 Py_END_ALLOW_THREADS
483 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000484 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000485 goto error;
486 }
487
Benjamin Petersond7b03282008-09-13 15:58:53 +0000488 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300489 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000490 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000491 }
492
493 Py_BEGIN_ALLOW_THREADS
494 rc = sqlite3_finalize(statement);
495 Py_END_ALLOW_THREADS
496 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000497 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000498 }
499
500 }
501
502error:
503 if (PyErr_Occurred()) {
504 return NULL;
505 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200506 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000507 }
508}
509
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200510static int
511_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000512{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200513 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000514 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000515 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200516 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
517 if (value == -1 && PyErr_Occurred())
518 return -1;
519 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520 } else if (PyFloat_Check(py_val)) {
521 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000522 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200523 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200524 if (str == NULL)
525 return -1;
526 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000527 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200528 Py_buffer view;
529 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100530 PyErr_SetString(PyExc_ValueError,
531 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200532 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000533 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200534 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100535 PyErr_SetString(PyExc_OverflowError,
536 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200537 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100538 return -1;
539 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200540 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
541 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000542 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200543 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000544 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200545 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000546}
547
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100548static PyObject *
549_pysqlite_build_py_params(sqlite3_context *context, int argc,
550 sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000551{
552 PyObject* args;
553 int i;
554 sqlite3_value* cur_value;
555 PyObject* cur_py_value;
556 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000557
558 args = PyTuple_New(argc);
559 if (!args) {
560 return NULL;
561 }
562
563 for (i = 0; i < argc; i++) {
564 cur_value = argv[i];
565 switch (sqlite3_value_type(argv[i])) {
566 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500567 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000568 break;
569 case SQLITE_FLOAT:
570 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
571 break;
572 case SQLITE_TEXT:
573 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000574 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000575 /* TODO: have a way to show errors here */
576 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000577 PyErr_Clear();
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100578 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579 }
580 break;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200581 case SQLITE_BLOB: {
582 sqlite3 *db = sqlite3_context_db_handle(context);
583 const void *blob = sqlite3_value_blob(cur_value);
584
585 if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
586 PyErr_NoMemory();
587 goto error;
588 }
589
590 Py_ssize_t size = sqlite3_value_bytes(cur_value);
591 cur_py_value = PyBytes_FromStringAndSize(blob, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592 break;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200593 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000594 case SQLITE_NULL:
595 default:
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100596 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000598
599 if (!cur_py_value) {
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200600 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000601 }
602
Erlend Egeberg Aaslande9194ea2021-04-23 13:21:08 +0200603 PyTuple_SET_ITEM(args, i, cur_py_value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000604 }
605
606 return args;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200607
608error:
609 Py_DECREF(args);
610 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000611}
612
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100613static void
614_pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000615{
616 PyObject* args;
617 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000618 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200619 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620
621 PyGILState_STATE threadstate;
622
623 threadstate = PyGILState_Ensure();
624
625 py_func = (PyObject*)sqlite3_user_data(context);
626
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000627 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000628 if (args) {
629 py_retval = PyObject_CallObject(py_func, args);
630 Py_DECREF(args);
631 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000632
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200633 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000634 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200635 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000636 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200637 }
638 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700639 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000640 PyErr_Print();
641 } else {
642 PyErr_Clear();
643 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200644 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000645 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646
647 PyGILState_Release(threadstate);
648}
649
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000650static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000651{
652 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000653 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654 PyObject* aggregate_class;
655 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000656 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000657
658 PyGILState_STATE threadstate;
659
660 threadstate = PyGILState_Ensure();
661
662 aggregate_class = (PyObject*)sqlite3_user_data(context);
663
664 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
665
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200666 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100667 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000668
Thomas Wouters477c8d52006-05-27 19:21:47 +0000669 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000670 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700671 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000672 PyErr_Print();
673 } else {
674 PyErr_Clear();
675 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200676 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000677 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000678 }
679 }
680
681 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 if (!stepmethod) {
683 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000684 }
685
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000686 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000687 if (!args) {
688 goto error;
689 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690
691 function_result = PyObject_CallObject(stepmethod, args);
692 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000693
Thomas Wouters477c8d52006-05-27 19:21:47 +0000694 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700695 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000696 PyErr_Print();
697 } else {
698 PyErr_Clear();
699 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200700 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000701 }
702
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703error:
704 Py_XDECREF(stepmethod);
705 Py_XDECREF(function_result);
706
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707 PyGILState_Release(threadstate);
708}
709
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100710static void
711_pysqlite_final_callback(sqlite3_context *context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000712{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200713 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000714 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200715 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200716 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200717 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000718
719 PyGILState_STATE threadstate;
720
721 threadstate = PyGILState_Ensure();
722
Erlend Egeberg Aasland979b23c2021-02-19 12:20:32 +0100723 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
724 if (aggregate_instance == NULL) {
725 /* No rows matched the query; the step handler was never called. */
726 goto error;
727 }
728 else if (!*aggregate_instance) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729 /* this branch is executed if there was an exception in the aggregate's
730 * __init__ */
731
Thomas Wouters477c8d52006-05-27 19:21:47 +0000732 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000733 }
734
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200735 /* Keep the exception (if any) of the last call to step() */
736 PyErr_Fetch(&exception, &value, &tb);
737
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200738 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200739
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200740 Py_DECREF(*aggregate_instance);
741
742 ok = 0;
743 if (function_result) {
744 ok = _pysqlite_set_result(context, function_result) == 0;
745 Py_DECREF(function_result);
746 }
747 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700748 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000749 PyErr_Print();
750 } else {
751 PyErr_Clear();
752 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200753 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000754 }
755
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200756 /* Restore the exception (if any) of the last call to step(),
757 but clear also the current exception if finalize() failed */
758 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200759
Thomas Wouters477c8d52006-05-27 19:21:47 +0000760error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000761 PyGILState_Release(threadstate);
762}
763
Gerhard Häringf9cee222010-03-05 15:20:03 +0000764static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000765{
766 PyObject* new_list;
767 PyObject* weakref;
768 int i;
769
770 /* we only need to do this once in a while */
771 if (self->created_statements++ < 200) {
772 return;
773 }
774
775 self->created_statements = 0;
776
777 new_list = PyList_New(0);
778 if (!new_list) {
779 return;
780 }
781
782 for (i = 0; i < PyList_Size(self->statements); i++) {
783 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000784 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000785 if (PyList_Append(new_list, weakref) != 0) {
786 Py_DECREF(new_list);
787 return;
788 }
789 }
790 }
791
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300792 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000793}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000794
Gerhard Häringf9cee222010-03-05 15:20:03 +0000795static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
796{
797 PyObject* new_list;
798 PyObject* weakref;
799 int i;
800
801 /* we only need to do this once in a while */
802 if (self->created_cursors++ < 200) {
803 return;
804 }
805
806 self->created_cursors = 0;
807
808 new_list = PyList_New(0);
809 if (!new_list) {
810 return;
811 }
812
813 for (i = 0; i < PyList_Size(self->cursors); i++) {
814 weakref = PyList_GetItem(self->cursors, i);
815 if (PyWeakref_GetObject(weakref) != Py_None) {
816 if (PyList_Append(new_list, weakref) != 0) {
817 Py_DECREF(new_list);
818 return;
819 }
820 }
821 }
822
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300823 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000824}
825
gescheitb9a03762019-07-13 06:15:49 +0300826static void _destructor(void* args)
827{
828 Py_DECREF((PyObject*)args);
829}
830
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100831/*[clinic input]
832_sqlite3.Connection.create_function as pysqlite_connection_create_function
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100834 name: str
835 narg: int
836 func: object
837 *
838 deterministic: bool = False
839
840Creates a new function. Non-standard.
841[clinic start generated code]*/
842
843static PyObject *
844pysqlite_connection_create_function_impl(pysqlite_Connection *self,
845 const char *name, int narg,
846 PyObject *func, int deterministic)
847/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
848{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000849 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500850 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000851
Gerhard Häringf9cee222010-03-05 15:20:03 +0000852 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
853 return NULL;
854 }
855
Sergey Fedoseev08308582018-07-08 12:09:20 +0500856 if (deterministic) {
857#if SQLITE_VERSION_NUMBER < 3008003
858 PyErr_SetString(pysqlite_NotSupportedError,
859 "deterministic=True requires SQLite 3.8.3 or higher");
860 return NULL;
861#else
862 if (sqlite3_libversion_number() < 3008003) {
863 PyErr_SetString(pysqlite_NotSupportedError,
864 "deterministic=True requires SQLite 3.8.3 or higher");
865 return NULL;
866 }
867 flags |= SQLITE_DETERMINISTIC;
868#endif
869 }
gescheitb9a03762019-07-13 06:15:49 +0300870 rc = sqlite3_create_function_v2(self->db,
871 name,
872 narg,
873 flags,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100874 (void*)Py_NewRef(func),
gescheitb9a03762019-07-13 06:15:49 +0300875 _pysqlite_func_callback,
876 NULL,
877 NULL,
878 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000879
Thomas Wouters477c8d52006-05-27 19:21:47 +0000880 if (rc != SQLITE_OK) {
881 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000882 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000883 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000884 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500885 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000886}
887
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100888/*[clinic input]
889_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000890
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100891 name: str
892 n_arg: int
893 aggregate_class: object
894
895Creates a new aggregate. Non-standard.
896[clinic start generated code]*/
897
898static PyObject *
899pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
900 const char *name, int n_arg,
901 PyObject *aggregate_class)
902/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
903{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000904 int rc;
905
Gerhard Häringf9cee222010-03-05 15:20:03 +0000906 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
907 return NULL;
908 }
909
gescheitb9a03762019-07-13 06:15:49 +0300910 rc = sqlite3_create_function_v2(self->db,
911 name,
912 n_arg,
913 SQLITE_UTF8,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100914 (void*)Py_NewRef(aggregate_class),
gescheitb9a03762019-07-13 06:15:49 +0300915 0,
916 &_pysqlite_step_callback,
917 &_pysqlite_final_callback,
918 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000919 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000920 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000921 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000922 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500924 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000925}
926
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000927static int _authorizer_callback(void* user_arg, int action, const char* arg1, const char* arg2 , const char* dbname, const char* access_attempt_source)
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000928{
929 PyObject *ret;
930 int rc;
931 PyGILState_STATE gilstate;
932
933 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000934
Victor Stinnerd4095d92013-07-26 22:23:33 +0200935 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000936
Victor Stinnerd4095d92013-07-26 22:23:33 +0200937 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700938 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200939 PyErr_Print();
940 else
941 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200942
Victor Stinnerd4095d92013-07-26 22:23:33 +0200943 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200944 }
945 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200946 if (PyLong_Check(ret)) {
947 rc = _PyLong_AsInt(ret);
948 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700949 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200950 PyErr_Print();
951 else
952 PyErr_Clear();
953 rc = SQLITE_DENY;
954 }
955 }
956 else {
957 rc = SQLITE_DENY;
958 }
959 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000960 }
961
962 PyGILState_Release(gilstate);
963 return rc;
964}
965
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000966static int _progress_handler(void* user_arg)
967{
968 int rc;
969 PyObject *ret;
970 PyGILState_STATE gilstate;
971
972 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100973 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000974
975 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700976 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000977 PyErr_Print();
978 } else {
979 PyErr_Clear();
980 }
981
Mark Dickinson934896d2009-02-21 20:59:32 +0000982 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +0000983 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000984 } else {
985 rc = (int)PyObject_IsTrue(ret);
986 Py_DECREF(ret);
987 }
988
989 PyGILState_Release(gilstate);
990 return rc;
991}
992
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +0200993#ifdef HAVE_TRACE_V2
994/*
995 * From https://sqlite.org/c3ref/trace_v2.html:
996 * The integer return value from the callback is currently ignored, though this
997 * may change in future releases. Callback implementations should return zero
998 * to ensure future compatibility.
999 */
1000static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
1001#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001002static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001003#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001004{
1005 PyObject *py_statement = NULL;
1006 PyObject *ret = NULL;
1007
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001008 PyGILState_STATE gilstate;
1009
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001010#ifdef HAVE_TRACE_V2
1011 if (type != SQLITE_TRACE_STMT) {
1012 return 0;
1013 }
1014#endif
1015
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001016 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001017 py_statement = PyUnicode_DecodeUTF8(statement_string,
1018 strlen(statement_string), "replace");
1019 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001020 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001021 Py_DECREF(py_statement);
1022 }
1023
1024 if (ret) {
1025 Py_DECREF(ret);
1026 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001027 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001028 PyErr_Print();
1029 } else {
1030 PyErr_Clear();
1031 }
1032 }
1033
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001034 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001035#ifdef HAVE_TRACE_V2
1036 return 0;
1037#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001038}
1039
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001040/*[clinic input]
1041_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001042
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001043 authorizer_callback as authorizer_cb: object
1044
1045Sets authorizer callback. Non-standard.
1046[clinic start generated code]*/
1047
1048static PyObject *
1049pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1050 PyObject *authorizer_cb)
1051/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1052{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001053 int rc;
1054
Gerhard Häringf9cee222010-03-05 15:20:03 +00001055 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1056 return NULL;
1057 }
1058
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001059 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001060 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001061 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001062 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001063 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001064 } else {
1065 Py_INCREF(authorizer_cb);
1066 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001067 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001068 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001069}
1070
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001071/*[clinic input]
1072_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1073
1074 progress_handler: object
1075 n: int
1076
1077Sets progress handler callback. Non-standard.
1078[clinic start generated code]*/
1079
1080static PyObject *
1081pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1082 PyObject *progress_handler,
1083 int n)
1084/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001085{
Gerhard Häringf9cee222010-03-05 15:20:03 +00001086 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1087 return NULL;
1088 }
1089
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001090 if (progress_handler == Py_None) {
1091 /* None clears the progress handler previously set */
1092 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001093 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001094 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001095 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001096 Py_INCREF(progress_handler);
1097 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001098 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001099 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001100}
1101
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001102/*[clinic input]
1103_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1104
1105 trace_callback: object
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001106
1107Sets a trace callback called for each SQL statement (passed as unicode).
1108
1109Non-standard.
1110[clinic start generated code]*/
1111
1112static PyObject *
Dong-hee Na21793492020-12-19 00:41:33 +09001113pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1114 PyObject *trace_callback)
1115/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001116{
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001117 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1118 return NULL;
1119 }
1120
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001121 if (trace_callback == Py_None) {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001122 /*
1123 * None clears the trace callback previously set
1124 *
1125 * Ref.
1126 * - https://sqlite.org/c3ref/c_trace.html
1127 * - https://sqlite.org/c3ref/trace_v2.html
1128 */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001129#ifdef HAVE_TRACE_V2
1130 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1131#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001132 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001133#endif
gescheitb9a03762019-07-13 06:15:49 +03001134 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001135 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001136#ifdef HAVE_TRACE_V2
1137 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1138#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001139 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001140#endif
gescheitb9a03762019-07-13 06:15:49 +03001141 Py_INCREF(trace_callback);
1142 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001143 }
1144
Berker Peksagfe21de92016-04-09 07:34:39 +03001145 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001146}
1147
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001148#ifndef SQLITE_OMIT_LOAD_EXTENSION
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001149/*[clinic input]
1150_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1151
Dong-hee Na21793492020-12-19 00:41:33 +09001152 enable as onoff: bool(accept={int})
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001153 /
1154
1155Enable dynamic loading of SQLite extension modules. Non-standard.
1156[clinic start generated code]*/
1157
1158static PyObject *
1159pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1160 int onoff)
Dong-hee Na21793492020-12-19 00:41:33 +09001161/*[clinic end generated code: output=9cac37190d388baf input=5c0da5b121121cbc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001162{
1163 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001164
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001165 if (PySys_Audit("sqlite3.enable_load_extension",
1166 "OO", self, onoff ? Py_True : Py_False) < 0) {
1167 return NULL;
1168 }
1169
Gerhard Häringf9cee222010-03-05 15:20:03 +00001170 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1171 return NULL;
1172 }
1173
Gerhard Häringf9cee222010-03-05 15:20:03 +00001174 rc = sqlite3_enable_load_extension(self->db, onoff);
1175
1176 if (rc != SQLITE_OK) {
1177 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1178 return NULL;
1179 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001180 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001181 }
1182}
1183
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001184/*[clinic input]
1185_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1186
1187 name as extension_name: str
1188 /
1189
1190Load SQLite extension module. Non-standard.
1191[clinic start generated code]*/
1192
1193static PyObject *
1194pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1195 const char *extension_name)
1196/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001197{
1198 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001199 char* errmsg;
1200
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001201 if (PySys_Audit("sqlite3.load_extension", "Os", self, extension_name) < 0) {
1202 return NULL;
1203 }
1204
Gerhard Häringf9cee222010-03-05 15:20:03 +00001205 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1206 return NULL;
1207 }
1208
Gerhard Häringf9cee222010-03-05 15:20:03 +00001209 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1210 if (rc != 0) {
1211 PyErr_SetString(pysqlite_OperationalError, errmsg);
1212 return NULL;
1213 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001214 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001215 }
1216}
1217#endif
1218
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001219int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001220{
1221 if (self->check_same_thread) {
1222 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001223 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001224 "SQLite objects created in a thread can only be used in that same thread. "
1225 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001226 self->thread_ident, PyThread_get_thread_ident());
1227 return 0;
1228 }
1229
1230 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001231 return 1;
1232}
1233
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001234static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001235{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001236 return Py_NewRef(self->isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001237}
1238
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001239static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001240{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001241 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001242 return NULL;
1243 } else {
1244 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1245 }
1246}
1247
Berker Peksag59da4b32016-09-12 07:16:43 +03001248static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1249{
1250 if (!pysqlite_check_connection(self)) {
1251 return NULL;
1252 }
1253 if (!sqlite3_get_autocommit(self->db)) {
1254 Py_RETURN_TRUE;
1255 }
1256 Py_RETURN_FALSE;
1257}
1258
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001259static int
1260pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001261{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001262 if (isolation_level == NULL) {
1263 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1264 return -1;
1265 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001267 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268 if (!res) {
1269 return -1;
1270 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271 Py_DECREF(res);
1272
Serhiy Storchaka28914922016-09-01 22:18:03 +03001273 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001274 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001275 const char * const *candidate;
1276 PyObject *uppercase_level;
1277 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001278
Serhiy Storchaka28914922016-09-01 22:18:03 +03001279 if (!PyUnicode_Check(isolation_level)) {
1280 PyErr_Format(PyExc_TypeError,
1281 "isolation_level must be a string or None, not %.100s",
1282 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001283 return -1;
1284 }
1285
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001286 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001287 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001288 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001289 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001290 return -1;
1291 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001292 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001293 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001294 break;
1295 }
1296 Py_DECREF(uppercase_level);
1297 if (!*candidate) {
1298 PyErr_SetString(PyExc_ValueError,
1299 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001300 return -1;
1301 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001302 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001303 }
1304
Serhiy Storchaka28914922016-09-01 22:18:03 +03001305 Py_INCREF(isolation_level);
1306 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001307 return 0;
1308}
1309
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +01001310static PyObject *
1311pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1312 PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001313{
1314 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001315 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001316 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001317 int rc;
1318
Gerhard Häringf9cee222010-03-05 15:20:03 +00001319 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1320 return NULL;
1321 }
1322
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001323 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001324 return NULL;
1325
Victor Stinnerc6a23202019-06-26 03:16:24 +02001326 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001328
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001329 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001330
Erlend Egeberg Aasland9031bd42020-10-01 15:24:31 +02001331 statement = PyObject_New(pysqlite_Statement, pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001332 if (!statement) {
1333 return NULL;
1334 }
1335
Victor Stinner0201f442010-03-13 03:28:34 +00001336 statement->db = NULL;
1337 statement->st = NULL;
1338 statement->sql = NULL;
1339 statement->in_use = 0;
1340 statement->in_weakreflist = NULL;
1341
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001342 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001343 if (rc != SQLITE_OK) {
1344 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001345 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001346 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001347 if (PyErr_ExceptionMatches(PyExc_TypeError))
1348 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001349 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001350 (void)pysqlite_statement_reset(statement);
1351 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001352 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001353 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001354 }
1355
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001356 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1357 if (weakref == NULL)
1358 goto error;
1359 if (PyList_Append(self->statements, weakref) != 0) {
1360 Py_DECREF(weakref);
1361 goto error;
1362 }
1363 Py_DECREF(weakref);
1364
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001365 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001366
1367error:
1368 Py_DECREF(statement);
1369 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370}
1371
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001372/*[clinic input]
1373_sqlite3.Connection.execute as pysqlite_connection_execute
1374
1375 sql: unicode
1376 parameters: object = NULL
1377 /
1378
1379Executes a SQL statement. Non-standard.
1380[clinic start generated code]*/
1381
1382static PyObject *
1383pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
1384 PyObject *parameters)
1385/*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001386{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001387 _Py_IDENTIFIER(execute);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001388 PyObject* cursor = 0;
1389 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001390
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001391 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001392 if (!cursor) {
1393 goto error;
1394 }
1395
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001396 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001397 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001398 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001399 }
1400
1401error:
1402 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001403
1404 return cursor;
1405}
1406
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001407/*[clinic input]
1408_sqlite3.Connection.executemany as pysqlite_connection_executemany
1409
1410 sql: unicode
1411 parameters: object
1412 /
1413
1414Repeatedly executes a SQL statement. Non-standard.
1415[clinic start generated code]*/
1416
1417static PyObject *
1418pysqlite_connection_executemany_impl(pysqlite_Connection *self,
1419 PyObject *sql, PyObject *parameters)
1420/*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001421{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001422 _Py_IDENTIFIER(executemany);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001423 PyObject* cursor = 0;
1424 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001425
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001426 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001427 if (!cursor) {
1428 goto error;
1429 }
1430
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001431 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
1432 parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001433 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001434 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001435 }
1436
1437error:
1438 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001439
1440 return cursor;
1441}
1442
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001443/*[clinic input]
1444_sqlite3.Connection.executescript as pysqlite_connection_executescript
1445
1446 sql_script as script_obj: object
1447 /
1448
1449Executes a multiple SQL statements at once. Non-standard.
1450[clinic start generated code]*/
1451
1452static PyObject *
1453pysqlite_connection_executescript(pysqlite_Connection *self,
1454 PyObject *script_obj)
1455/*[clinic end generated code: output=4c4f9d77aa0ae37d input=c0b14695aa6c81d9]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001456{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001457 _Py_IDENTIFIER(executescript);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001458 PyObject* cursor = 0;
1459 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001460
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001461 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001462 if (!cursor) {
1463 goto error;
1464 }
1465
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001466 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
1467 script_obj, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001468 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001469 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001470 }
1471
1472error:
1473 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001474
1475 return cursor;
1476}
1477
1478/* ------------------------- COLLATION CODE ------------------------ */
1479
1480static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001481pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001482 void* context,
1483 int text1_length, const void* text1_data,
1484 int text2_length, const void* text2_data)
1485{
1486 PyObject* callback = (PyObject*)context;
1487 PyObject* string1 = 0;
1488 PyObject* string2 = 0;
1489 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001491 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001492 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001493 gilstate = PyGILState_Ensure();
1494
1495 if (PyErr_Occurred()) {
1496 goto finally;
1497 }
1498
Guido van Rossum98297ee2007-11-06 21:34:58 +00001499 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1500 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001501
1502 if (!string1 || !string2) {
1503 goto finally; /* failed to allocate strings */
1504 }
1505
1506 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1507
1508 if (!retval) {
1509 /* execution failed */
1510 goto finally;
1511 }
1512
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001513 longval = PyLong_AsLongAndOverflow(retval, &result);
1514 if (longval == -1 && PyErr_Occurred()) {
1515 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001516 result = 0;
1517 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001518 else if (!result) {
1519 if (longval > 0)
1520 result = 1;
1521 else if (longval < 0)
1522 result = -1;
1523 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001524
1525finally:
1526 Py_XDECREF(string1);
1527 Py_XDECREF(string2);
1528 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001529 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001530 return result;
1531}
1532
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001533/*[clinic input]
1534_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1535
1536Abort any pending database operation. Non-standard.
1537[clinic start generated code]*/
1538
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001539static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001540pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1541/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001542{
1543 PyObject* retval = NULL;
1544
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001545 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001546 goto finally;
1547 }
1548
1549 sqlite3_interrupt(self->db);
1550
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001551 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001552
1553finally:
1554 return retval;
1555}
1556
Christian Heimesbbe741d2008-03-28 10:53:29 +00001557/* Function author: Paul Kippes <kippesp@gmail.com>
1558 * Class method of Connection to call the Python function _iterdump
1559 * of the sqlite3 module.
1560 */
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001561/*[clinic input]
1562_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1563
1564Returns iterator to the dump of the database in an SQL text format.
1565
1566Non-standard.
1567[clinic start generated code]*/
1568
Christian Heimesbbe741d2008-03-28 10:53:29 +00001569static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001570pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1571/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001572{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001573 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001574 PyObject* retval = NULL;
1575 PyObject* module = NULL;
1576 PyObject* module_dict;
1577 PyObject* pyfn_iterdump;
1578
1579 if (!pysqlite_check_connection(self)) {
1580 goto finally;
1581 }
1582
1583 module = PyImport_ImportModule(MODULE_NAME ".dump");
1584 if (!module) {
1585 goto finally;
1586 }
1587
1588 module_dict = PyModule_GetDict(module);
1589 if (!module_dict) {
1590 goto finally;
1591 }
1592
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001593 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001594 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001595 if (!PyErr_Occurred()) {
1596 PyErr_SetString(pysqlite_OperationalError,
1597 "Failed to obtain _iterdump() reference");
1598 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001599 goto finally;
1600 }
1601
Petr Viktorinffd97532020-02-11 17:46:57 +01001602 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001603
1604finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001605 Py_XDECREF(module);
1606 return retval;
1607}
1608
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001609/*[clinic input]
1610_sqlite3.Connection.backup as pysqlite_connection_backup
1611
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001612 target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType')
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001613 *
1614 pages: int = -1
1615 progress: object = None
1616 name: str = "main"
1617 sleep: double = 0.250
1618
1619Makes a backup of the database. Non-standard.
1620[clinic start generated code]*/
1621
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001622static PyObject *
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001623pysqlite_connection_backup_impl(pysqlite_Connection *self,
1624 pysqlite_Connection *target, int pages,
1625 PyObject *progress, const char *name,
1626 double sleep)
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001627/*[clinic end generated code: output=306a3e6a38c36334 input=30ae45fc420bfd3b]*/
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001628{
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001629 int rc;
Pablo Galindoa6d63a22020-12-29 00:28:09 +00001630 int sleep_ms = (int)(sleep * 1000.0);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001631 sqlite3 *bck_conn;
1632 sqlite3_backup *bck_handle;
Victor Stinnerca405012018-04-30 12:22:17 +02001633
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001634 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1635 return NULL;
1636 }
1637
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001638 if (!pysqlite_check_connection(target)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001639 return NULL;
1640 }
1641
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001642 if (target == self) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001643 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1644 return NULL;
1645 }
1646
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001647#if SQLITE_VERSION_NUMBER < 3008008
1648 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001649 https://www.sqlite.org/src/info/169b5505498c0a7e */
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001650 if (!sqlite3_get_autocommit(target->db)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001651 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1652 return NULL;
1653 }
1654#endif
1655
1656 if (progress != Py_None && !PyCallable_Check(progress)) {
1657 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1658 return NULL;
1659 }
1660
1661 if (pages == 0) {
1662 pages = -1;
1663 }
1664
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001665 bck_conn = target->db;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001666
1667 Py_BEGIN_ALLOW_THREADS
1668 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1669 Py_END_ALLOW_THREADS
1670
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001671 if (bck_handle == NULL) {
1672 _pysqlite_seterror(bck_conn, NULL);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001673 return NULL;
1674 }
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001675
1676 do {
1677 Py_BEGIN_ALLOW_THREADS
1678 rc = sqlite3_backup_step(bck_handle, pages);
1679 Py_END_ALLOW_THREADS
1680
1681 if (progress != Py_None) {
1682 int remaining = sqlite3_backup_remaining(bck_handle);
1683 int pagecount = sqlite3_backup_pagecount(bck_handle);
1684 PyObject *res = PyObject_CallFunction(progress, "iii", rc,
1685 remaining, pagecount);
1686 if (res == NULL) {
1687 /* Callback failed: abort backup and bail. */
1688 Py_BEGIN_ALLOW_THREADS
1689 sqlite3_backup_finish(bck_handle);
1690 Py_END_ALLOW_THREADS
1691 return NULL;
1692 }
1693 Py_DECREF(res);
1694 }
1695
1696 /* Sleep for a while if there are still further pages to copy and
1697 the engine could not make any progress */
1698 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1699 Py_BEGIN_ALLOW_THREADS
1700 sqlite3_sleep(sleep_ms);
1701 Py_END_ALLOW_THREADS
1702 }
1703 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1704
1705 Py_BEGIN_ALLOW_THREADS
1706 rc = sqlite3_backup_finish(bck_handle);
1707 Py_END_ALLOW_THREADS
1708
1709 if (rc != SQLITE_OK) {
1710 _pysqlite_seterror(bck_conn, NULL);
1711 return NULL;
1712 }
1713
1714 Py_RETURN_NONE;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001715}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001716
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001717/*[clinic input]
1718_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
1719
1720 name: unicode
1721 callback as callable: object
1722 /
1723
1724Creates a collation function. Non-standard.
1725[clinic start generated code]*/
1726
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001727static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001728pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1729 PyObject *name, PyObject *callable)
1730/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001731{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001732 PyObject* uppercase_name = 0;
Victor Stinner35466c52010-04-22 11:23:23 +00001733 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001734 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001735 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001736 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001737 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001738 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001739
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001740 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001741 goto finally;
1742 }
1743
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001744 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1745 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001746 if (!uppercase_name) {
1747 goto finally;
1748 }
1749
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001750 if (PyUnicode_READY(uppercase_name))
1751 goto finally;
1752 len = PyUnicode_GET_LENGTH(uppercase_name);
1753 kind = PyUnicode_KIND(uppercase_name);
1754 data = PyUnicode_DATA(uppercase_name);
1755 for (i=0; i<len; i++) {
1756 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1757 if ((ch >= '0' && ch <= '9')
1758 || (ch >= 'A' && ch <= 'Z')
1759 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001760 {
Victor Stinner35466c52010-04-22 11:23:23 +00001761 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001763 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001764 goto finally;
1765 }
1766 }
1767
Serhiy Storchaka06515832016-11-20 09:13:07 +02001768 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001769 if (!uppercase_name_str)
1770 goto finally;
1771
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001772 if (callable != Py_None && !PyCallable_Check(callable)) {
1773 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1774 goto finally;
1775 }
1776
1777 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001778 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1779 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001780 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001781 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1782 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001783 }
1784
1785 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001786 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001787 SQLITE_UTF8,
1788 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001789 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001790 if (rc != SQLITE_OK) {
Serhiy Storchaka90731802021-01-31 17:42:38 +02001791 if (callable != Py_None) {
1792 if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1793 PyErr_Clear();
1794 }
1795 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001796 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001797 goto finally;
1798 }
1799
1800finally:
1801 Py_XDECREF(uppercase_name);
1802
1803 if (PyErr_Occurred()) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001804 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001805 }
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001806 return Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001807}
1808
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001809/*[clinic input]
1810_sqlite3.Connection.__enter__ as pysqlite_connection_enter
1811
1812Called when the connection is used as a context manager.
1813
1814Returns itself as a convenience to the caller.
1815[clinic start generated code]*/
1816
Christian Heimesbbe741d2008-03-28 10:53:29 +00001817static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001818pysqlite_connection_enter_impl(pysqlite_Connection *self)
1819/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001820{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001821 return Py_NewRef((PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001822}
1823
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001824/*[clinic input]
1825_sqlite3.Connection.__exit__ as pysqlite_connection_exit
1826
1827 type as exc_type: object
1828 value as exc_value: object
1829 traceback as exc_tb: object
1830 /
1831
1832Called when the connection is used as a context manager.
1833
1834If there was any exception, a rollback takes place; otherwise we commit.
1835[clinic start generated code]*/
1836
Christian Heimesbbe741d2008-03-28 10:53:29 +00001837static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001838pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
1839 PyObject *exc_value, PyObject *exc_tb)
1840/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001841{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001842 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001843 PyObject* result;
1844
Christian Heimesbbe741d2008-03-28 10:53:29 +00001845 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1846 method_name = "commit";
1847 } else {
1848 method_name = "rollback";
1849 }
1850
Victor Stinner3466bde2016-09-05 18:16:01 -07001851 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001852 if (!result) {
1853 return NULL;
1854 }
1855 Py_DECREF(result);
1856
1857 Py_RETURN_FALSE;
1858}
1859
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001860static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001861PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001862
1863static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001864 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1865 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001866 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001867 {NULL}
1868};
1869
1870static PyMethodDef connection_methods[] = {
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001871 PYSQLITE_CONNECTION_BACKUP_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001872 PYSQLITE_CONNECTION_CLOSE_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001873 PYSQLITE_CONNECTION_COMMIT_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001874 PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
1875 PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
1876 PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
1877 PYSQLITE_CONNECTION_CURSOR_METHODDEF
1878 PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
1879 PYSQLITE_CONNECTION_ENTER_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001880 PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
1881 PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
1882 PYSQLITE_CONNECTION_EXECUTE_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001883 PYSQLITE_CONNECTION_EXIT_METHODDEF
1884 PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
1885 PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
1886 PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
1887 PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
1888 PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
1889 PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
1890 PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001891 {NULL, NULL}
1892};
1893
1894static struct PyMemberDef connection_members[] =
1895{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001896 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1897 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1898 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1899 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1900 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1901 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1902 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1903 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1904 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1905 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001906 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1907 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001908 {NULL}
1909};
1910
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001911static PyType_Slot connection_slots[] = {
1912 {Py_tp_dealloc, pysqlite_connection_dealloc},
1913 {Py_tp_doc, (void *)connection_doc},
1914 {Py_tp_methods, connection_methods},
1915 {Py_tp_members, connection_members},
1916 {Py_tp_getset, connection_getset},
1917 {Py_tp_new, PyType_GenericNew},
1918 {Py_tp_init, pysqlite_connection_init},
1919 {Py_tp_call, pysqlite_connection_call},
1920 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001921};
1922
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001923static PyType_Spec connection_spec = {
1924 .name = MODULE_NAME ".Connection",
1925 .basicsize = sizeof(pysqlite_Connection),
1926 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
1927 .slots = connection_slots,
1928};
1929
1930PyTypeObject *pysqlite_ConnectionType = NULL;
1931
Erlend Egeberg Aasland38b6c2a2021-02-21 11:07:49 +01001932int
1933pysqlite_connection_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001934{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001935 pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
1936 if (pysqlite_ConnectionType == NULL) {
1937 return -1;
1938 }
1939 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001940}