blob: f060ef1ef42480a0f35cb106d531b7cd7de638b0 [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
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000095 self->begin_statement = NULL;
96
Oren Milman93c5a5d2017-10-10 22:27:46 +030097 Py_CLEAR(self->statement_cache);
98 Py_CLEAR(self->statements);
99 Py_CLEAR(self->cursors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000100
101 Py_INCREF(Py_None);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300102 Py_XSETREF(self->row_factory, Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000103
104 Py_INCREF(&PyUnicode_Type);
Oren Milman93c5a5d2017-10-10 22:27:46 +0300105 Py_XSETREF(self->text_factory, (PyObject*)&PyUnicode_Type);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000106
Antoine Pitrou902fc8b2013-02-10 00:02:44 +0100107 Py_BEGIN_ALLOW_THREADS
108 rc = sqlite3_open_v2(database, &self->db,
109 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
110 (uri ? SQLITE_OPEN_URI : 0), NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000111 Py_END_ALLOW_THREADS
112
Anders Lorentsena22a1272017-11-07 01:47:43 +0100113 Py_DECREF(database_obj);
114
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000115 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000116 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000117 return -1;
118 }
119
120 if (!isolation_level) {
Neal Norwitzefee9f52007-10-27 02:50:52 +0000121 isolation_level = PyUnicode_FromString("");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000122 if (!isolation_level) {
123 return -1;
124 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000125 } else {
126 Py_INCREF(isolation_level);
127 }
Oren Milman93c5a5d2017-10-10 22:27:46 +0300128 Py_CLEAR(self->isolation_level);
Erlend Egeberg Aasland0cb470e2021-07-30 14:01:22 +0200129 if (pysqlite_connection_set_isolation_level(self, isolation_level, NULL) != 0) {
Victor Stinnercb1f74e2013-12-19 16:38:03 +0100130 Py_DECREF(isolation_level);
131 return -1;
132 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000133 Py_DECREF(isolation_level);
134
Erlend Egeberg Aaslanda937ab42020-09-27 14:14:50 +0200135 self->statement_cache = (pysqlite_Cache*)PyObject_CallFunction((PyObject*)pysqlite_CacheType, "Oi", self, cached_statements);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000136 if (PyErr_Occurred()) {
137 return -1;
138 }
139
Gerhard Häringf9cee222010-03-05 15:20:03 +0000140 self->created_statements = 0;
141 self->created_cursors = 0;
142
143 /* Create lists of weak references to statements/cursors */
Thomas Wouters477c8d52006-05-27 19:21:47 +0000144 self->statements = PyList_New(0);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000145 self->cursors = PyList_New(0);
146 if (!self->statements || !self->cursors) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000147 return -1;
148 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000149
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000150 /* By default, the Cache class INCREFs the factory in its initializer, and
151 * decrefs it in its deallocator method. Since this would create a circular
152 * reference here, we're breaking it by decrementing self, and telling the
153 * cache class to not decref the factory (self) in its deallocator.
154 */
155 self->statement_cache->decref_factory = 0;
156 Py_DECREF(self);
157
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000158 self->detect_types = detect_types;
159 self->timeout = timeout;
160 (void)sqlite3_busy_timeout(self->db, (int)(timeout*1000));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000161 self->thread_ident = PyThread_get_thread_ident();
162 self->check_same_thread = check_same_thread;
163
gescheitb9a03762019-07-13 06:15:49 +0300164 self->function_pinboard_trace_callback = NULL;
165 self->function_pinboard_progress_handler = NULL;
166 self->function_pinboard_authorizer_cb = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000167
Oren Milman93c5a5d2017-10-10 22:27:46 +0300168 Py_XSETREF(self->collations, PyDict_New());
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000169 if (!self->collations) {
170 return -1;
171 }
172
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000173 self->Warning = pysqlite_Warning;
174 self->Error = pysqlite_Error;
175 self->InterfaceError = pysqlite_InterfaceError;
176 self->DatabaseError = pysqlite_DatabaseError;
177 self->DataError = pysqlite_DataError;
178 self->OperationalError = pysqlite_OperationalError;
179 self->IntegrityError = pysqlite_IntegrityError;
180 self->InternalError = pysqlite_InternalError;
181 self->ProgrammingError = pysqlite_ProgrammingError;
182 self->NotSupportedError = pysqlite_NotSupportedError;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000183
Erlend Egeberg Aaslandc96cc082021-05-02 23:25:17 +0200184 if (PySys_Audit("sqlite3.connect/handle", "O", self) < 0) {
185 return -1;
186 }
187
Erlend Egeberg Aasland0cb470e2021-07-30 14:01:22 +0200188 self->initialized = 1;
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
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700228static int
229connection_traverse(pysqlite_Connection *self, visitproc visit, void *arg)
230{
Miss Islington (bot)ff359d72021-05-31 02:12:27 -0700231 Py_VISIT(Py_TYPE(self));
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700232 Py_VISIT(self->isolation_level);
Miss Islington (bot)ff359d72021-05-31 02:12:27 -0700233 Py_VISIT(self->statement_cache);
234 Py_VISIT(self->statements);
235 Py_VISIT(self->cursors);
236 Py_VISIT(self->row_factory);
237 Py_VISIT(self->text_factory);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700238 Py_VISIT(self->function_pinboard_trace_callback);
239 Py_VISIT(self->function_pinboard_progress_handler);
240 Py_VISIT(self->function_pinboard_authorizer_cb);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700241 Py_VISIT(self->collations);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700242 return 0;
243}
244
245static int
246connection_clear(pysqlite_Connection *self)
247{
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700248 Py_CLEAR(self->isolation_level);
Miss Islington (bot)ff359d72021-05-31 02:12:27 -0700249 Py_CLEAR(self->statement_cache);
250 Py_CLEAR(self->statements);
251 Py_CLEAR(self->cursors);
252 Py_CLEAR(self->row_factory);
253 Py_CLEAR(self->text_factory);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700254 Py_CLEAR(self->function_pinboard_trace_callback);
255 Py_CLEAR(self->function_pinboard_progress_handler);
256 Py_CLEAR(self->function_pinboard_authorizer_cb);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700257 Py_CLEAR(self->collations);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700258 return 0;
259}
260
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100261static void
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700262connection_dealloc(pysqlite_Connection *self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200264 PyTypeObject *tp = Py_TYPE(self);
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -0700265 PyObject_GC_UnTrack(self);
266 tp->tp_clear((PyObject *)self);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000267
268 /* Clean up if user has not called .close() explicitly. */
269 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100270 sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000271 }
272
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200273 tp->tp_free(self);
274 Py_DECREF(tp);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000275}
276
Gerhard Häringf9cee222010-03-05 15:20:03 +0000277/*
278 * Registers a cursor with the connection.
279 *
280 * 0 => error; 1 => ok
281 */
282int pysqlite_connection_register_cursor(pysqlite_Connection* connection, PyObject* cursor)
283{
284 PyObject* weakref;
285
286 weakref = PyWeakref_NewRef((PyObject*)cursor, NULL);
287 if (!weakref) {
288 goto error;
289 }
290
291 if (PyList_Append(connection->cursors, weakref) != 0) {
292 Py_CLEAR(weakref);
293 goto error;
294 }
295
296 Py_DECREF(weakref);
297
298 return 1;
299error:
300 return 0;
301}
302
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100303/*[clinic input]
304_sqlite3.Connection.cursor as pysqlite_connection_cursor
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000305
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100306 factory: object = NULL
307
308Return a cursor for the connection.
309[clinic start generated code]*/
310
311static PyObject *
312pysqlite_connection_cursor_impl(pysqlite_Connection *self, PyObject *factory)
313/*[clinic end generated code: output=562432a9e6af2aa1 input=4127345aa091b650]*/
314{
315 PyObject* cursor;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000317 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 return NULL;
319 }
320
321 if (factory == NULL) {
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200322 factory = (PyObject*)pysqlite_CursorType;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 }
324
Petr Viktorinffd97532020-02-11 17:46:57 +0100325 cursor = PyObject_CallOneArg(factory, (PyObject *)self);
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300326 if (cursor == NULL)
327 return NULL;
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +0200328 if (!PyObject_TypeCheck(cursor, pysqlite_CursorType)) {
Serhiy Storchakaef113cd2016-08-29 14:29:55 +0300329 PyErr_Format(PyExc_TypeError,
330 "factory must return a cursor, not %.100s",
331 Py_TYPE(cursor)->tp_name);
332 Py_DECREF(cursor);
333 return NULL;
334 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000335
Gerhard Häringf9cee222010-03-05 15:20:03 +0000336 _pysqlite_drop_unused_cursor_references(self);
337
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000338 if (cursor && self->row_factory != Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000339 Py_INCREF(self->row_factory);
Serhiy Storchaka48842712016-04-06 09:45:48 +0300340 Py_XSETREF(((pysqlite_Cursor *)cursor)->row_factory, self->row_factory);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000341 }
342
343 return cursor;
344}
345
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100346/*[clinic input]
347_sqlite3.Connection.close as pysqlite_connection_close
348
349Closes the connection.
350[clinic start generated code]*/
351
352static PyObject *
353pysqlite_connection_close_impl(pysqlite_Connection *self)
354/*[clinic end generated code: output=a546a0da212c9b97 input=3d58064bbffaa3d3]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355{
356 int rc;
357
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000358 if (!pysqlite_check_thread(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000359 return NULL;
360 }
361
Erlend Egeberg Aasland0cb470e2021-07-30 14:01:22 +0200362 if (!self->initialized) {
363 PyErr_SetString(pysqlite_ProgrammingError,
364 "Base Connection.__init__ not called.");
365 return NULL;
366 }
367
Gerhard Häringf9cee222010-03-05 15:20:03 +0000368 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369
370 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100371 rc = sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372
373 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000374 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000375 return NULL;
376 } else {
377 self->db = NULL;
378 }
379 }
380
Berker Peksagfe21de92016-04-09 07:34:39 +0300381 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000382}
383
384/*
385 * Checks if a connection object is usable (i. e. not closed).
386 *
387 * 0 => error; 1 => ok
388 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000389int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000391 if (!con->initialized) {
392 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
393 return 0;
394 }
395
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000396 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000397 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000398 return 0;
399 } else {
400 return 1;
401 }
402}
403
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000404PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000405{
406 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000407 sqlite3_stmt* statement;
408
409 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100410 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement,
411 NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000412 Py_END_ALLOW_THREADS
413
414 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000415 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000416 goto error;
417 }
418
Benjamin Petersond7b03282008-09-13 15:58:53 +0000419 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300420 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000421 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000422 }
423
424 Py_BEGIN_ALLOW_THREADS
425 rc = sqlite3_finalize(statement);
426 Py_END_ALLOW_THREADS
427
428 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000429 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000430 }
431
432error:
433 if (PyErr_Occurred()) {
434 return NULL;
435 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200436 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000437 }
438}
439
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +0100440/*[clinic input]
441_sqlite3.Connection.commit as pysqlite_connection_commit
442
443Commit the current transaction.
444[clinic start generated code]*/
445
446static PyObject *
447pysqlite_connection_commit_impl(pysqlite_Connection *self)
448/*[clinic end generated code: output=3da45579e89407f2 input=39c12c04dda276a8]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000449{
450 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000451 sqlite3_stmt* statement;
452
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000453 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000454 return NULL;
455 }
456
Berker Peksag59da4b32016-09-12 07:16:43 +0300457 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000458
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000459 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100460 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000461 Py_END_ALLOW_THREADS
462 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000463 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 goto error;
465 }
466
Benjamin Petersond7b03282008-09-13 15:58:53 +0000467 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300468 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000469 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000470 }
471
472 Py_BEGIN_ALLOW_THREADS
473 rc = sqlite3_finalize(statement);
474 Py_END_ALLOW_THREADS
475 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000476 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000477 }
478
479 }
480
481error:
482 if (PyErr_Occurred()) {
483 return NULL;
484 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200485 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000486 }
487}
488
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100489/*[clinic input]
490_sqlite3.Connection.rollback as pysqlite_connection_rollback
491
492Roll back the current transaction.
493[clinic start generated code]*/
494
495static PyObject *
496pysqlite_connection_rollback_impl(pysqlite_Connection *self)
497/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000498{
499 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000500 sqlite3_stmt* statement;
501
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000502 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000503 return NULL;
504 }
505
Berker Peksag59da4b32016-09-12 07:16:43 +0300506 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000507 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508
509 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100510 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511 Py_END_ALLOW_THREADS
512 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000513 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000514 goto error;
515 }
516
Benjamin Petersond7b03282008-09-13 15:58:53 +0000517 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300518 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000519 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000520 }
521
522 Py_BEGIN_ALLOW_THREADS
523 rc = sqlite3_finalize(statement);
524 Py_END_ALLOW_THREADS
525 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000526 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000527 }
528
529 }
530
531error:
532 if (PyErr_Occurred()) {
533 return NULL;
534 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200535 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000536 }
537}
538
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200539static int
540_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000541{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200542 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000544 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200545 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
546 if (value == -1 && PyErr_Occurred())
547 return -1;
548 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000549 } else if (PyFloat_Check(py_val)) {
550 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000551 } else if (PyUnicode_Check(py_val)) {
Miss Islington (bot)2b1e7132021-08-06 10:59:51 -0700552 Py_ssize_t sz;
553 const char *str = PyUnicode_AsUTF8AndSize(py_val, &sz);
554 if (str == NULL) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200555 return -1;
Miss Islington (bot)2b1e7132021-08-06 10:59:51 -0700556 }
557 if (sz > INT_MAX) {
558 PyErr_SetString(PyExc_OverflowError,
559 "string is longer than INT_MAX bytes");
560 return -1;
561 }
562 sqlite3_result_text(context, str, (int)sz, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000563 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200564 Py_buffer view;
565 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100566 PyErr_SetString(PyExc_ValueError,
567 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200568 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200570 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100571 PyErr_SetString(PyExc_OverflowError,
572 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200573 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100574 return -1;
575 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200576 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
577 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000578 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200579 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000580 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200581 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000582}
583
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100584static PyObject *
585_pysqlite_build_py_params(sqlite3_context *context, int argc,
586 sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000587{
588 PyObject* args;
589 int i;
590 sqlite3_value* cur_value;
591 PyObject* cur_py_value;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000592
593 args = PyTuple_New(argc);
594 if (!args) {
595 return NULL;
596 }
597
598 for (i = 0; i < argc; i++) {
599 cur_value = argv[i];
600 switch (sqlite3_value_type(argv[i])) {
601 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500602 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000603 break;
604 case SQLITE_FLOAT:
605 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
606 break;
Miss Islington (bot)067d6d42021-06-04 11:54:39 -0700607 case SQLITE_TEXT: {
608 sqlite3 *db = sqlite3_context_db_handle(context);
609 const char *text = (const char *)sqlite3_value_text(cur_value);
610
611 if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
612 PyErr_NoMemory();
613 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000614 }
Miss Islington (bot)067d6d42021-06-04 11:54:39 -0700615
616 Py_ssize_t size = sqlite3_value_bytes(cur_value);
617 cur_py_value = PyUnicode_FromStringAndSize(text, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000618 break;
Miss Islington (bot)067d6d42021-06-04 11:54:39 -0700619 }
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200620 case SQLITE_BLOB: {
621 sqlite3 *db = sqlite3_context_db_handle(context);
622 const void *blob = sqlite3_value_blob(cur_value);
623
624 if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
625 PyErr_NoMemory();
626 goto error;
627 }
628
629 Py_ssize_t size = sqlite3_value_bytes(cur_value);
630 cur_py_value = PyBytes_FromStringAndSize(blob, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000631 break;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200632 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000633 case SQLITE_NULL:
634 default:
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100635 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000636 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000637
638 if (!cur_py_value) {
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200639 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000640 }
641
Erlend Egeberg Aaslande9194ea2021-04-23 13:21:08 +0200642 PyTuple_SET_ITEM(args, i, cur_py_value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643 }
644
645 return args;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200646
647error:
648 Py_DECREF(args);
649 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000650}
651
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100652static void
653_pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000654{
655 PyObject* args;
656 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000657 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200658 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000659
660 PyGILState_STATE threadstate;
661
662 threadstate = PyGILState_Ensure();
663
664 py_func = (PyObject*)sqlite3_user_data(context);
665
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000666 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000667 if (args) {
668 py_retval = PyObject_CallObject(py_func, args);
669 Py_DECREF(args);
670 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000671
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200672 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000673 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200674 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000675 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200676 }
677 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700678 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000679 PyErr_Print();
680 } else {
681 PyErr_Clear();
682 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200683 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000684 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000685
686 PyGILState_Release(threadstate);
687}
688
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000689static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000690{
691 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000692 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000693 PyObject* aggregate_class;
694 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000696
697 PyGILState_STATE threadstate;
698
699 threadstate = PyGILState_Ensure();
700
701 aggregate_class = (PyObject*)sqlite3_user_data(context);
702
703 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
704
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200705 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100706 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000709 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700710 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000711 PyErr_Print();
712 } else {
713 PyErr_Clear();
714 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200715 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000716 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000717 }
718 }
719
720 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000721 if (!stepmethod) {
722 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000723 }
724
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000725 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000726 if (!args) {
727 goto error;
728 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000729
730 function_result = PyObject_CallObject(stepmethod, args);
731 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000732
Thomas Wouters477c8d52006-05-27 19:21:47 +0000733 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700734 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000735 PyErr_Print();
736 } else {
737 PyErr_Clear();
738 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200739 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000740 }
741
Thomas Wouters477c8d52006-05-27 19:21:47 +0000742error:
743 Py_XDECREF(stepmethod);
744 Py_XDECREF(function_result);
745
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000746 PyGILState_Release(threadstate);
747}
748
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100749static void
750_pysqlite_final_callback(sqlite3_context *context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000751{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200752 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000753 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200754 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200755 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200756 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000757
758 PyGILState_STATE threadstate;
759
760 threadstate = PyGILState_Ensure();
761
Erlend Egeberg Aasland979b23c2021-02-19 12:20:32 +0100762 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
763 if (aggregate_instance == NULL) {
764 /* No rows matched the query; the step handler was never called. */
765 goto error;
766 }
767 else if (!*aggregate_instance) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000768 /* this branch is executed if there was an exception in the aggregate's
769 * __init__ */
770
Thomas Wouters477c8d52006-05-27 19:21:47 +0000771 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000772 }
773
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200774 /* Keep the exception (if any) of the last call to step() */
775 PyErr_Fetch(&exception, &value, &tb);
776
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200777 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200778
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200779 Py_DECREF(*aggregate_instance);
780
781 ok = 0;
782 if (function_result) {
783 ok = _pysqlite_set_result(context, function_result) == 0;
784 Py_DECREF(function_result);
785 }
786 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700787 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000788 PyErr_Print();
789 } else {
790 PyErr_Clear();
791 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200792 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000793 }
794
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200795 /* Restore the exception (if any) of the last call to step(),
796 but clear also the current exception if finalize() failed */
797 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200798
Thomas Wouters477c8d52006-05-27 19:21:47 +0000799error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000800 PyGILState_Release(threadstate);
801}
802
Gerhard Häringf9cee222010-03-05 15:20:03 +0000803static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000804{
805 PyObject* new_list;
806 PyObject* weakref;
807 int i;
808
809 /* we only need to do this once in a while */
810 if (self->created_statements++ < 200) {
811 return;
812 }
813
814 self->created_statements = 0;
815
816 new_list = PyList_New(0);
817 if (!new_list) {
818 return;
819 }
820
821 for (i = 0; i < PyList_Size(self->statements); i++) {
822 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000823 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000824 if (PyList_Append(new_list, weakref) != 0) {
825 Py_DECREF(new_list);
826 return;
827 }
828 }
829 }
830
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300831 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000832}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000833
Gerhard Häringf9cee222010-03-05 15:20:03 +0000834static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
835{
836 PyObject* new_list;
837 PyObject* weakref;
838 int i;
839
840 /* we only need to do this once in a while */
841 if (self->created_cursors++ < 200) {
842 return;
843 }
844
845 self->created_cursors = 0;
846
847 new_list = PyList_New(0);
848 if (!new_list) {
849 return;
850 }
851
852 for (i = 0; i < PyList_Size(self->cursors); i++) {
853 weakref = PyList_GetItem(self->cursors, i);
854 if (PyWeakref_GetObject(weakref) != Py_None) {
855 if (PyList_Append(new_list, weakref) != 0) {
856 Py_DECREF(new_list);
857 return;
858 }
859 }
860 }
861
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300862 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000863}
864
gescheitb9a03762019-07-13 06:15:49 +0300865static void _destructor(void* args)
866{
Miss Islington (bot)317e9ed2021-06-05 16:13:27 -0700867 // This function may be called without the GIL held, so we need to ensure
868 // that we destroy 'args' with the GIL
869 PyGILState_STATE gstate;
870 gstate = PyGILState_Ensure();
gescheitb9a03762019-07-13 06:15:49 +0300871 Py_DECREF((PyObject*)args);
Miss Islington (bot)317e9ed2021-06-05 16:13:27 -0700872 PyGILState_Release(gstate);
gescheitb9a03762019-07-13 06:15:49 +0300873}
874
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100875/*[clinic input]
876_sqlite3.Connection.create_function as pysqlite_connection_create_function
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000877
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100878 name: str
879 narg: int
880 func: object
881 *
882 deterministic: bool = False
883
884Creates a new function. Non-standard.
885[clinic start generated code]*/
886
887static PyObject *
888pysqlite_connection_create_function_impl(pysqlite_Connection *self,
889 const char *name, int narg,
890 PyObject *func, int deterministic)
891/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
892{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000893 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500894 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000895
Gerhard Häringf9cee222010-03-05 15:20:03 +0000896 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
897 return NULL;
898 }
899
Sergey Fedoseev08308582018-07-08 12:09:20 +0500900 if (deterministic) {
901#if SQLITE_VERSION_NUMBER < 3008003
902 PyErr_SetString(pysqlite_NotSupportedError,
903 "deterministic=True requires SQLite 3.8.3 or higher");
904 return NULL;
905#else
906 if (sqlite3_libversion_number() < 3008003) {
907 PyErr_SetString(pysqlite_NotSupportedError,
908 "deterministic=True requires SQLite 3.8.3 or higher");
909 return NULL;
910 }
911 flags |= SQLITE_DETERMINISTIC;
912#endif
913 }
gescheitb9a03762019-07-13 06:15:49 +0300914 rc = sqlite3_create_function_v2(self->db,
915 name,
916 narg,
917 flags,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100918 (void*)Py_NewRef(func),
gescheitb9a03762019-07-13 06:15:49 +0300919 _pysqlite_func_callback,
920 NULL,
921 NULL,
922 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000923
Thomas Wouters477c8d52006-05-27 19:21:47 +0000924 if (rc != SQLITE_OK) {
925 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000926 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000927 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000928 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500929 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000930}
931
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100932/*[clinic input]
933_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000934
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100935 name: str
936 n_arg: int
937 aggregate_class: object
938
939Creates a new aggregate. Non-standard.
940[clinic start generated code]*/
941
942static PyObject *
943pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
944 const char *name, int n_arg,
945 PyObject *aggregate_class)
946/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
947{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000948 int rc;
949
Gerhard Häringf9cee222010-03-05 15:20:03 +0000950 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
951 return NULL;
952 }
953
gescheitb9a03762019-07-13 06:15:49 +0300954 rc = sqlite3_create_function_v2(self->db,
955 name,
956 n_arg,
957 SQLITE_UTF8,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100958 (void*)Py_NewRef(aggregate_class),
gescheitb9a03762019-07-13 06:15:49 +0300959 0,
960 &_pysqlite_step_callback,
961 &_pysqlite_final_callback,
962 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000963 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000964 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000965 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000966 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000967 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500968 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000969}
970
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000971static 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 +0000972{
973 PyObject *ret;
974 int rc;
975 PyGILState_STATE gilstate;
976
977 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000978
Victor Stinnerd4095d92013-07-26 22:23:33 +0200979 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000980
Victor Stinnerd4095d92013-07-26 22:23:33 +0200981 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700982 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200983 PyErr_Print();
984 else
985 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200986
Victor Stinnerd4095d92013-07-26 22:23:33 +0200987 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200988 }
989 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200990 if (PyLong_Check(ret)) {
991 rc = _PyLong_AsInt(ret);
992 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700993 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200994 PyErr_Print();
995 else
996 PyErr_Clear();
997 rc = SQLITE_DENY;
998 }
999 }
1000 else {
1001 rc = SQLITE_DENY;
1002 }
1003 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001004 }
1005
1006 PyGILState_Release(gilstate);
1007 return rc;
1008}
1009
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001010static int _progress_handler(void* user_arg)
1011{
1012 int rc;
1013 PyObject *ret;
1014 PyGILState_STATE gilstate;
1015
1016 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +01001017 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001018
1019 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001020 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001021 PyErr_Print();
1022 } else {
1023 PyErr_Clear();
1024 }
1025
Mark Dickinson934896d2009-02-21 20:59:32 +00001026 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +00001027 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001028 } else {
1029 rc = (int)PyObject_IsTrue(ret);
1030 Py_DECREF(ret);
1031 }
1032
1033 PyGILState_Release(gilstate);
1034 return rc;
1035}
1036
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001037#ifdef HAVE_TRACE_V2
1038/*
1039 * From https://sqlite.org/c3ref/trace_v2.html:
1040 * The integer return value from the callback is currently ignored, though this
1041 * may change in future releases. Callback implementations should return zero
1042 * to ensure future compatibility.
1043 */
1044static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
1045#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001046static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001047#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001048{
1049 PyObject *py_statement = NULL;
1050 PyObject *ret = NULL;
1051
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001052 PyGILState_STATE gilstate;
1053
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001054#ifdef HAVE_TRACE_V2
1055 if (type != SQLITE_TRACE_STMT) {
1056 return 0;
1057 }
1058#endif
1059
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001060 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001061 py_statement = PyUnicode_DecodeUTF8(statement_string,
1062 strlen(statement_string), "replace");
1063 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001064 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001065 Py_DECREF(py_statement);
1066 }
1067
1068 if (ret) {
1069 Py_DECREF(ret);
1070 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001071 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001072 PyErr_Print();
1073 } else {
1074 PyErr_Clear();
1075 }
1076 }
1077
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001078 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001079#ifdef HAVE_TRACE_V2
1080 return 0;
1081#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001082}
1083
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001084/*[clinic input]
1085_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001086
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001087 authorizer_callback as authorizer_cb: object
1088
1089Sets authorizer callback. Non-standard.
1090[clinic start generated code]*/
1091
1092static PyObject *
1093pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1094 PyObject *authorizer_cb)
1095/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1096{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001097 int rc;
1098
Gerhard Häringf9cee222010-03-05 15:20:03 +00001099 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1100 return NULL;
1101 }
1102
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001103 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001104 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001105 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001106 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001107 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001108 } else {
1109 Py_INCREF(authorizer_cb);
1110 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001111 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001112 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001113}
1114
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001115/*[clinic input]
1116_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1117
1118 progress_handler: object
1119 n: int
1120
1121Sets progress handler callback. Non-standard.
1122[clinic start generated code]*/
1123
1124static PyObject *
1125pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1126 PyObject *progress_handler,
1127 int n)
1128/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001129{
Gerhard Häringf9cee222010-03-05 15:20:03 +00001130 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1131 return NULL;
1132 }
1133
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001134 if (progress_handler == Py_None) {
1135 /* None clears the progress handler previously set */
1136 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001137 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001138 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001139 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001140 Py_INCREF(progress_handler);
1141 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001142 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001143 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001144}
1145
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001146/*[clinic input]
1147_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1148
1149 trace_callback: object
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001150
1151Sets a trace callback called for each SQL statement (passed as unicode).
1152
1153Non-standard.
1154[clinic start generated code]*/
1155
1156static PyObject *
Dong-hee Na21793492020-12-19 00:41:33 +09001157pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1158 PyObject *trace_callback)
1159/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001160{
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001161 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1162 return NULL;
1163 }
1164
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001165 if (trace_callback == Py_None) {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001166 /*
1167 * None clears the trace callback previously set
1168 *
1169 * Ref.
1170 * - https://sqlite.org/c3ref/c_trace.html
1171 * - https://sqlite.org/c3ref/trace_v2.html
1172 */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001173#ifdef HAVE_TRACE_V2
1174 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1175#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001176 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001177#endif
gescheitb9a03762019-07-13 06:15:49 +03001178 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001179 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001180#ifdef HAVE_TRACE_V2
1181 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1182#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001183 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001184#endif
gescheitb9a03762019-07-13 06:15:49 +03001185 Py_INCREF(trace_callback);
1186 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001187 }
1188
Berker Peksagfe21de92016-04-09 07:34:39 +03001189 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001190}
1191
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001192#ifndef SQLITE_OMIT_LOAD_EXTENSION
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001193/*[clinic input]
1194_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1195
Dong-hee Na21793492020-12-19 00:41:33 +09001196 enable as onoff: bool(accept={int})
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001197 /
1198
1199Enable dynamic loading of SQLite extension modules. Non-standard.
1200[clinic start generated code]*/
1201
1202static PyObject *
1203pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1204 int onoff)
Dong-hee Na21793492020-12-19 00:41:33 +09001205/*[clinic end generated code: output=9cac37190d388baf input=5c0da5b121121cbc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001206{
1207 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001208
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001209 if (PySys_Audit("sqlite3.enable_load_extension",
1210 "OO", self, onoff ? Py_True : Py_False) < 0) {
1211 return NULL;
1212 }
1213
Gerhard Häringf9cee222010-03-05 15:20:03 +00001214 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1215 return NULL;
1216 }
1217
Gerhard Häringf9cee222010-03-05 15:20:03 +00001218 rc = sqlite3_enable_load_extension(self->db, onoff);
1219
1220 if (rc != SQLITE_OK) {
1221 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1222 return NULL;
1223 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001224 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001225 }
1226}
1227
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001228/*[clinic input]
1229_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1230
1231 name as extension_name: str
1232 /
1233
1234Load SQLite extension module. Non-standard.
1235[clinic start generated code]*/
1236
1237static PyObject *
1238pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1239 const char *extension_name)
1240/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001241{
1242 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001243 char* errmsg;
1244
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001245 if (PySys_Audit("sqlite3.load_extension", "Os", self, extension_name) < 0) {
1246 return NULL;
1247 }
1248
Gerhard Häringf9cee222010-03-05 15:20:03 +00001249 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1250 return NULL;
1251 }
1252
Gerhard Häringf9cee222010-03-05 15:20:03 +00001253 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1254 if (rc != 0) {
1255 PyErr_SetString(pysqlite_OperationalError, errmsg);
1256 return NULL;
1257 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001258 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001259 }
1260}
1261#endif
1262
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001263int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001264{
1265 if (self->check_same_thread) {
1266 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001267 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001268 "SQLite objects created in a thread can only be used in that same thread. "
1269 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001270 self->thread_ident, PyThread_get_thread_ident());
1271 return 0;
1272 }
1273
1274 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001275 return 1;
1276}
1277
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001278static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001279{
Erlend Egeberg Aasland0cb470e2021-07-30 14:01:22 +02001280 if (!pysqlite_check_connection(self)) {
1281 return NULL;
1282 }
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001283 return Py_NewRef(self->isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284}
1285
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001286static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001287{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001288 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001289 return NULL;
1290 } else {
1291 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1292 }
1293}
1294
Berker Peksag59da4b32016-09-12 07:16:43 +03001295static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1296{
1297 if (!pysqlite_check_connection(self)) {
1298 return NULL;
1299 }
1300 if (!sqlite3_get_autocommit(self->db)) {
1301 Py_RETURN_TRUE;
1302 }
1303 Py_RETURN_FALSE;
1304}
1305
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001306static int
1307pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001308{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001309 if (isolation_level == NULL) {
1310 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1311 return -1;
1312 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001313 if (isolation_level == Py_None) {
Erlend Egeberg Aasland0cb470e2021-07-30 14:01:22 +02001314 /* We might get called during connection init, so we cannot use
1315 * pysqlite_connection_commit() here. */
1316 if (self->db && !sqlite3_get_autocommit(self->db)) {
1317 int rc;
1318 Py_BEGIN_ALLOW_THREADS
1319 rc = sqlite3_exec(self->db, "COMMIT", NULL, NULL, NULL);
1320 Py_END_ALLOW_THREADS
1321 if (rc != SQLITE_OK) {
1322 return _pysqlite_seterror(self->db, NULL);
1323 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001324 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001325
Serhiy Storchaka28914922016-09-01 22:18:03 +03001326 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001327 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001328 const char * const *candidate;
1329 PyObject *uppercase_level;
1330 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001331
Serhiy Storchaka28914922016-09-01 22:18:03 +03001332 if (!PyUnicode_Check(isolation_level)) {
1333 PyErr_Format(PyExc_TypeError,
1334 "isolation_level must be a string or None, not %.100s",
1335 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001336 return -1;
1337 }
1338
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001339 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001340 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001341 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001342 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001343 return -1;
1344 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001345 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001346 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001347 break;
1348 }
1349 Py_DECREF(uppercase_level);
1350 if (!*candidate) {
1351 PyErr_SetString(PyExc_ValueError,
1352 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001353 return -1;
1354 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001355 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001356 }
1357
Serhiy Storchaka28914922016-09-01 22:18:03 +03001358 Py_INCREF(isolation_level);
1359 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001360 return 0;
1361}
1362
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +01001363static PyObject *
1364pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1365 PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001366{
1367 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001368 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001369 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001370
Gerhard Häringf9cee222010-03-05 15:20:03 +00001371 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1372 return NULL;
1373 }
1374
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001375 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001376 return NULL;
1377
Victor Stinnerc6a23202019-06-26 03:16:24 +02001378 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001379 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001380
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001381 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001382
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +02001383 statement = pysqlite_statement_create(self, sql);
1384 if (statement == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001385 return NULL;
1386 }
1387
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001388 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1389 if (weakref == NULL)
1390 goto error;
1391 if (PyList_Append(self->statements, weakref) != 0) {
1392 Py_DECREF(weakref);
1393 goto error;
1394 }
1395 Py_DECREF(weakref);
1396
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001397 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001398
1399error:
1400 Py_DECREF(statement);
1401 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001402}
1403
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001404/*[clinic input]
1405_sqlite3.Connection.execute as pysqlite_connection_execute
1406
1407 sql: unicode
1408 parameters: object = NULL
1409 /
1410
1411Executes a SQL statement. Non-standard.
1412[clinic start generated code]*/
1413
1414static PyObject *
1415pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
1416 PyObject *parameters)
1417/*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001418{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001419 _Py_IDENTIFIER(execute);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001420 PyObject* cursor = 0;
1421 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001422
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001423 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001424 if (!cursor) {
1425 goto error;
1426 }
1427
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001428 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001429 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001430 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431 }
1432
1433error:
1434 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001435
1436 return cursor;
1437}
1438
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001439/*[clinic input]
1440_sqlite3.Connection.executemany as pysqlite_connection_executemany
1441
1442 sql: unicode
1443 parameters: object
1444 /
1445
1446Repeatedly executes a SQL statement. Non-standard.
1447[clinic start generated code]*/
1448
1449static PyObject *
1450pysqlite_connection_executemany_impl(pysqlite_Connection *self,
1451 PyObject *sql, PyObject *parameters)
1452/*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001453{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001454 _Py_IDENTIFIER(executemany);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001455 PyObject* cursor = 0;
1456 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001457
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001458 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001459 if (!cursor) {
1460 goto error;
1461 }
1462
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001463 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
1464 parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001465 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001466 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001467 }
1468
1469error:
1470 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001471
1472 return cursor;
1473}
1474
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001475/*[clinic input]
1476_sqlite3.Connection.executescript as pysqlite_connection_executescript
1477
1478 sql_script as script_obj: object
1479 /
1480
Erlend Egeberg Aasland02e4c0c2021-07-15 01:02:01 +02001481Executes multiple SQL statements at once. Non-standard.
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001482[clinic start generated code]*/
1483
1484static PyObject *
1485pysqlite_connection_executescript(pysqlite_Connection *self,
1486 PyObject *script_obj)
Erlend Egeberg Aasland02e4c0c2021-07-15 01:02:01 +02001487/*[clinic end generated code: output=4c4f9d77aa0ae37d input=b27ae5c24ffb8b43]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001488{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001489 _Py_IDENTIFIER(executescript);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001490 PyObject* cursor = 0;
1491 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001492
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001493 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001494 if (!cursor) {
1495 goto error;
1496 }
1497
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001498 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
1499 script_obj, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001500 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001501 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001502 }
1503
1504error:
1505 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001506
1507 return cursor;
1508}
1509
1510/* ------------------------- COLLATION CODE ------------------------ */
1511
1512static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001513pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001514 void* context,
1515 int text1_length, const void* text1_data,
1516 int text2_length, const void* text2_data)
1517{
1518 PyObject* callback = (PyObject*)context;
1519 PyObject* string1 = 0;
1520 PyObject* string2 = 0;
1521 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001522 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001523 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001524 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001525 gilstate = PyGILState_Ensure();
1526
1527 if (PyErr_Occurred()) {
1528 goto finally;
1529 }
1530
Guido van Rossum98297ee2007-11-06 21:34:58 +00001531 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1532 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001533
1534 if (!string1 || !string2) {
1535 goto finally; /* failed to allocate strings */
1536 }
1537
1538 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1539
1540 if (!retval) {
1541 /* execution failed */
1542 goto finally;
1543 }
1544
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001545 longval = PyLong_AsLongAndOverflow(retval, &result);
1546 if (longval == -1 && PyErr_Occurred()) {
1547 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001548 result = 0;
1549 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001550 else if (!result) {
1551 if (longval > 0)
1552 result = 1;
1553 else if (longval < 0)
1554 result = -1;
1555 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001556
1557finally:
1558 Py_XDECREF(string1);
1559 Py_XDECREF(string2);
1560 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001561 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001562 return result;
1563}
1564
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001565/*[clinic input]
1566_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1567
1568Abort any pending database operation. Non-standard.
1569[clinic start generated code]*/
1570
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001571static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001572pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1573/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001574{
1575 PyObject* retval = NULL;
1576
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001577 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001578 goto finally;
1579 }
1580
1581 sqlite3_interrupt(self->db);
1582
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001583 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001584
1585finally:
1586 return retval;
1587}
1588
Christian Heimesbbe741d2008-03-28 10:53:29 +00001589/* Function author: Paul Kippes <kippesp@gmail.com>
1590 * Class method of Connection to call the Python function _iterdump
1591 * of the sqlite3 module.
1592 */
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001593/*[clinic input]
1594_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1595
1596Returns iterator to the dump of the database in an SQL text format.
1597
1598Non-standard.
1599[clinic start generated code]*/
1600
Christian Heimesbbe741d2008-03-28 10:53:29 +00001601static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001602pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1603/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001604{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001605 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001606 PyObject* retval = NULL;
1607 PyObject* module = NULL;
1608 PyObject* module_dict;
1609 PyObject* pyfn_iterdump;
1610
1611 if (!pysqlite_check_connection(self)) {
1612 goto finally;
1613 }
1614
1615 module = PyImport_ImportModule(MODULE_NAME ".dump");
1616 if (!module) {
1617 goto finally;
1618 }
1619
1620 module_dict = PyModule_GetDict(module);
1621 if (!module_dict) {
1622 goto finally;
1623 }
1624
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001625 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001626 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001627 if (!PyErr_Occurred()) {
1628 PyErr_SetString(pysqlite_OperationalError,
1629 "Failed to obtain _iterdump() reference");
1630 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001631 goto finally;
1632 }
1633
Petr Viktorinffd97532020-02-11 17:46:57 +01001634 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001635
1636finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001637 Py_XDECREF(module);
1638 return retval;
1639}
1640
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001641/*[clinic input]
1642_sqlite3.Connection.backup as pysqlite_connection_backup
1643
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001644 target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType')
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001645 *
1646 pages: int = -1
1647 progress: object = None
1648 name: str = "main"
1649 sleep: double = 0.250
1650
1651Makes a backup of the database. Non-standard.
1652[clinic start generated code]*/
1653
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001654static PyObject *
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001655pysqlite_connection_backup_impl(pysqlite_Connection *self,
1656 pysqlite_Connection *target, int pages,
1657 PyObject *progress, const char *name,
1658 double sleep)
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001659/*[clinic end generated code: output=306a3e6a38c36334 input=30ae45fc420bfd3b]*/
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001660{
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001661 int rc;
Pablo Galindoa6d63a22020-12-29 00:28:09 +00001662 int sleep_ms = (int)(sleep * 1000.0);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001663 sqlite3 *bck_conn;
1664 sqlite3_backup *bck_handle;
Victor Stinnerca405012018-04-30 12:22:17 +02001665
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001666 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1667 return NULL;
1668 }
1669
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001670 if (!pysqlite_check_connection(target)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001671 return NULL;
1672 }
1673
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001674 if (target == self) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001675 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1676 return NULL;
1677 }
1678
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001679#if SQLITE_VERSION_NUMBER < 3008008
1680 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001681 https://www.sqlite.org/src/info/169b5505498c0a7e */
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001682 if (!sqlite3_get_autocommit(target->db)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001683 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1684 return NULL;
1685 }
1686#endif
1687
1688 if (progress != Py_None && !PyCallable_Check(progress)) {
1689 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1690 return NULL;
1691 }
1692
1693 if (pages == 0) {
1694 pages = -1;
1695 }
1696
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001697 bck_conn = target->db;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001698
1699 Py_BEGIN_ALLOW_THREADS
1700 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1701 Py_END_ALLOW_THREADS
1702
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001703 if (bck_handle == NULL) {
1704 _pysqlite_seterror(bck_conn, NULL);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001705 return NULL;
1706 }
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001707
1708 do {
1709 Py_BEGIN_ALLOW_THREADS
1710 rc = sqlite3_backup_step(bck_handle, pages);
1711 Py_END_ALLOW_THREADS
1712
1713 if (progress != Py_None) {
1714 int remaining = sqlite3_backup_remaining(bck_handle);
1715 int pagecount = sqlite3_backup_pagecount(bck_handle);
1716 PyObject *res = PyObject_CallFunction(progress, "iii", rc,
1717 remaining, pagecount);
1718 if (res == NULL) {
1719 /* Callback failed: abort backup and bail. */
1720 Py_BEGIN_ALLOW_THREADS
1721 sqlite3_backup_finish(bck_handle);
1722 Py_END_ALLOW_THREADS
1723 return NULL;
1724 }
1725 Py_DECREF(res);
1726 }
1727
1728 /* Sleep for a while if there are still further pages to copy and
1729 the engine could not make any progress */
1730 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1731 Py_BEGIN_ALLOW_THREADS
1732 sqlite3_sleep(sleep_ms);
1733 Py_END_ALLOW_THREADS
1734 }
1735 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1736
1737 Py_BEGIN_ALLOW_THREADS
1738 rc = sqlite3_backup_finish(bck_handle);
1739 Py_END_ALLOW_THREADS
1740
1741 if (rc != SQLITE_OK) {
1742 _pysqlite_seterror(bck_conn, NULL);
1743 return NULL;
1744 }
1745
1746 Py_RETURN_NONE;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001747}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001748
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001749/*[clinic input]
1750_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
1751
1752 name: unicode
1753 callback as callable: object
1754 /
1755
1756Creates a collation function. Non-standard.
1757[clinic start generated code]*/
1758
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001759static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001760pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1761 PyObject *name, PyObject *callable)
1762/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001763{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001764 PyObject* uppercase_name = 0;
Victor Stinner35466c52010-04-22 11:23:23 +00001765 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001766 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001767 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001768 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001769 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001770 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001771
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001772 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001773 goto finally;
1774 }
1775
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001776 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1777 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001778 if (!uppercase_name) {
1779 goto finally;
1780 }
1781
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001782 if (PyUnicode_READY(uppercase_name))
1783 goto finally;
1784 len = PyUnicode_GET_LENGTH(uppercase_name);
1785 kind = PyUnicode_KIND(uppercase_name);
1786 data = PyUnicode_DATA(uppercase_name);
1787 for (i=0; i<len; i++) {
1788 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1789 if ((ch >= '0' && ch <= '9')
1790 || (ch >= 'A' && ch <= 'Z')
1791 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001792 {
Victor Stinner35466c52010-04-22 11:23:23 +00001793 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001794 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001795 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001796 goto finally;
1797 }
1798 }
1799
Serhiy Storchaka06515832016-11-20 09:13:07 +02001800 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001801 if (!uppercase_name_str)
1802 goto finally;
1803
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001804 if (callable != Py_None && !PyCallable_Check(callable)) {
1805 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1806 goto finally;
1807 }
1808
1809 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001810 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1811 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001812 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001813 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1814 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001815 }
1816
1817 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001818 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001819 SQLITE_UTF8,
1820 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001821 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001822 if (rc != SQLITE_OK) {
Serhiy Storchaka90731802021-01-31 17:42:38 +02001823 if (callable != Py_None) {
1824 if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1825 PyErr_Clear();
1826 }
1827 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001828 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001829 goto finally;
1830 }
1831
1832finally:
1833 Py_XDECREF(uppercase_name);
1834
1835 if (PyErr_Occurred()) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001836 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001837 }
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001838 return Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001839}
1840
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001841/*[clinic input]
1842_sqlite3.Connection.__enter__ as pysqlite_connection_enter
1843
1844Called when the connection is used as a context manager.
1845
1846Returns itself as a convenience to the caller.
1847[clinic start generated code]*/
1848
Christian Heimesbbe741d2008-03-28 10:53:29 +00001849static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001850pysqlite_connection_enter_impl(pysqlite_Connection *self)
1851/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001852{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001853 return Py_NewRef((PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001854}
1855
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001856/*[clinic input]
1857_sqlite3.Connection.__exit__ as pysqlite_connection_exit
1858
1859 type as exc_type: object
1860 value as exc_value: object
1861 traceback as exc_tb: object
1862 /
1863
1864Called when the connection is used as a context manager.
1865
1866If there was any exception, a rollback takes place; otherwise we commit.
1867[clinic start generated code]*/
1868
Christian Heimesbbe741d2008-03-28 10:53:29 +00001869static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001870pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
1871 PyObject *exc_value, PyObject *exc_tb)
1872/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001873{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001874 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001875 PyObject* result;
1876
Christian Heimesbbe741d2008-03-28 10:53:29 +00001877 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1878 method_name = "commit";
1879 } else {
1880 method_name = "rollback";
1881 }
1882
Victor Stinner3466bde2016-09-05 18:16:01 -07001883 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001884 if (!result) {
1885 return NULL;
1886 }
1887 Py_DECREF(result);
1888
1889 Py_RETURN_FALSE;
1890}
1891
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001892static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001893PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001894
1895static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001896 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1897 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001898 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001899 {NULL}
1900};
1901
1902static PyMethodDef connection_methods[] = {
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001903 PYSQLITE_CONNECTION_BACKUP_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001904 PYSQLITE_CONNECTION_CLOSE_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001905 PYSQLITE_CONNECTION_COMMIT_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001906 PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
1907 PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
1908 PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
1909 PYSQLITE_CONNECTION_CURSOR_METHODDEF
1910 PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
1911 PYSQLITE_CONNECTION_ENTER_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001912 PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
1913 PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
1914 PYSQLITE_CONNECTION_EXECUTE_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001915 PYSQLITE_CONNECTION_EXIT_METHODDEF
1916 PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
1917 PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
1918 PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
1919 PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
1920 PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
1921 PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
1922 PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001923 {NULL, NULL}
1924};
1925
1926static struct PyMemberDef connection_members[] =
1927{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001928 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1929 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1930 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1931 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1932 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1933 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1934 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1935 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1936 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1937 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001938 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1939 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001940 {NULL}
1941};
1942
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001943static PyType_Slot connection_slots[] = {
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001944 {Py_tp_dealloc, connection_dealloc},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001945 {Py_tp_doc, (void *)connection_doc},
1946 {Py_tp_methods, connection_methods},
1947 {Py_tp_members, connection_members},
1948 {Py_tp_getset, connection_getset},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001949 {Py_tp_init, pysqlite_connection_init},
1950 {Py_tp_call, pysqlite_connection_call},
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001951 {Py_tp_traverse, connection_traverse},
1952 {Py_tp_clear, connection_clear},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001953 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001954};
1955
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001956static PyType_Spec connection_spec = {
1957 .name = MODULE_NAME ".Connection",
1958 .basicsize = sizeof(pysqlite_Connection),
Miss Islington (bot)7297d742021-06-17 03:19:44 -07001959 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
1960 Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001961 .slots = connection_slots,
1962};
1963
1964PyTypeObject *pysqlite_ConnectionType = NULL;
1965
Erlend Egeberg Aasland38b6c2a2021-02-21 11:07:49 +01001966int
1967pysqlite_connection_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001968{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001969 pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
1970 if (pysqlite_ConnectionType == NULL) {
1971 return -1;
1972 }
1973 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001974}