blob: 4e0ad9ad89d275d791dc9030061316644a1e5c3c [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
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
Gerhard Häringf9cee222010-03-05 15:20:03 +0000362 pysqlite_do_all_statements(self, ACTION_FINALIZE, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000363
364 if (self->db) {
Erlend Egeberg Aaslandcf0b2392021-01-06 01:02:43 +0100365 rc = sqlite3_close_v2(self->db);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366
367 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000368 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000369 return NULL;
370 } else {
371 self->db = NULL;
372 }
373 }
374
Berker Peksagfe21de92016-04-09 07:34:39 +0300375 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000376}
377
378/*
379 * Checks if a connection object is usable (i. e. not closed).
380 *
381 * 0 => error; 1 => ok
382 */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000383int pysqlite_check_connection(pysqlite_Connection* con)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000384{
Gerhard Häringf9cee222010-03-05 15:20:03 +0000385 if (!con->initialized) {
386 PyErr_SetString(pysqlite_ProgrammingError, "Base Connection.__init__ not called.");
387 return 0;
388 }
389
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000390 if (!con->db) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000391 PyErr_SetString(pysqlite_ProgrammingError, "Cannot operate on a closed database.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000392 return 0;
393 } else {
394 return 1;
395 }
396}
397
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000398PyObject* _pysqlite_connection_begin(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000399{
400 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000401 sqlite3_stmt* statement;
402
403 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100404 rc = sqlite3_prepare_v2(self->db, self->begin_statement, -1, &statement,
405 NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000406 Py_END_ALLOW_THREADS
407
408 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000409 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000410 goto error;
411 }
412
Benjamin Petersond7b03282008-09-13 15:58:53 +0000413 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300414 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000415 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000416 }
417
418 Py_BEGIN_ALLOW_THREADS
419 rc = sqlite3_finalize(statement);
420 Py_END_ALLOW_THREADS
421
422 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000423 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000424 }
425
426error:
427 if (PyErr_Occurred()) {
428 return NULL;
429 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200430 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000431 }
432}
433
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +0100434/*[clinic input]
435_sqlite3.Connection.commit as pysqlite_connection_commit
436
437Commit the current transaction.
438[clinic start generated code]*/
439
440static PyObject *
441pysqlite_connection_commit_impl(pysqlite_Connection *self)
442/*[clinic end generated code: output=3da45579e89407f2 input=39c12c04dda276a8]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000443{
444 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000445 sqlite3_stmt* statement;
446
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000447 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000448 return NULL;
449 }
450
Berker Peksag59da4b32016-09-12 07:16:43 +0300451 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000452
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000453 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100454 rc = sqlite3_prepare_v2(self->db, "COMMIT", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000455 Py_END_ALLOW_THREADS
456 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000457 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000458 goto error;
459 }
460
Benjamin Petersond7b03282008-09-13 15:58:53 +0000461 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300462 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000463 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000464 }
465
466 Py_BEGIN_ALLOW_THREADS
467 rc = sqlite3_finalize(statement);
468 Py_END_ALLOW_THREADS
469 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000470 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000471 }
472
473 }
474
475error:
476 if (PyErr_Occurred()) {
477 return NULL;
478 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200479 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000480 }
481}
482
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100483/*[clinic input]
484_sqlite3.Connection.rollback as pysqlite_connection_rollback
485
486Roll back the current transaction.
487[clinic start generated code]*/
488
489static PyObject *
490pysqlite_connection_rollback_impl(pysqlite_Connection *self)
491/*[clinic end generated code: output=b66fa0d43e7ef305 input=12d4e8d068942830]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000492{
493 int rc;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000494 sqlite3_stmt* statement;
495
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000496 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000497 return NULL;
498 }
499
Berker Peksag59da4b32016-09-12 07:16:43 +0300500 if (!sqlite3_get_autocommit(self->db)) {
Gerhard Häringf9cee222010-03-05 15:20:03 +0000501 pysqlite_do_all_statements(self, ACTION_RESET, 1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000502
503 Py_BEGIN_ALLOW_THREADS
Erlend Egeberg Aasland5e48e832021-02-26 15:30:22 +0100504 rc = sqlite3_prepare_v2(self->db, "ROLLBACK", -1, &statement, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000505 Py_END_ALLOW_THREADS
506 if (rc != SQLITE_OK) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000507 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000508 goto error;
509 }
510
Benjamin Petersond7b03282008-09-13 15:58:53 +0000511 rc = pysqlite_step(statement, self);
Berker Peksag59da4b32016-09-12 07:16:43 +0300512 if (rc != SQLITE_DONE) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000513 _pysqlite_seterror(self->db, statement);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000514 }
515
516 Py_BEGIN_ALLOW_THREADS
517 rc = sqlite3_finalize(statement);
518 Py_END_ALLOW_THREADS
519 if (rc != SQLITE_OK && !PyErr_Occurred()) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000520 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000521 }
522
523 }
524
525error:
526 if (PyErr_Occurred()) {
527 return NULL;
528 } else {
Serhiy Storchaka228b12e2017-01-23 09:47:21 +0200529 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000530 }
531}
532
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200533static int
534_pysqlite_set_result(sqlite3_context* context, PyObject* py_val)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000535{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200536 if (py_val == Py_None) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000537 sqlite3_result_null(context);
Christian Heimes217cfd12007-12-02 14:31:20 +0000538 } else if (PyLong_Check(py_val)) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200539 sqlite_int64 value = _pysqlite_long_as_int64(py_val);
540 if (value == -1 && PyErr_Occurred())
541 return -1;
542 sqlite3_result_int64(context, value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000543 } else if (PyFloat_Check(py_val)) {
544 sqlite3_result_double(context, PyFloat_AsDouble(py_val));
Guido van Rossumbae07c92007-10-08 02:46:15 +0000545 } else if (PyUnicode_Check(py_val)) {
Serhiy Storchaka06515832016-11-20 09:13:07 +0200546 const char *str = PyUnicode_AsUTF8(py_val);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200547 if (str == NULL)
548 return -1;
549 sqlite3_result_text(context, str, -1, SQLITE_TRANSIENT);
Guido van Rossumbae07c92007-10-08 02:46:15 +0000550 } else if (PyObject_CheckBuffer(py_val)) {
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200551 Py_buffer view;
552 if (PyObject_GetBuffer(py_val, &view, PyBUF_SIMPLE) != 0) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100553 PyErr_SetString(PyExc_ValueError,
554 "could not convert BLOB to buffer");
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200555 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000556 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200557 if (view.len > INT_MAX) {
Victor Stinner83ed42b2013-11-18 01:24:31 +0100558 PyErr_SetString(PyExc_OverflowError,
559 "BLOB longer than INT_MAX bytes");
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200560 PyBuffer_Release(&view);
Victor Stinner83ed42b2013-11-18 01:24:31 +0100561 return -1;
562 }
Serhiy Storchaka4fdb6842015-02-03 01:21:08 +0200563 sqlite3_result_blob(context, view.buf, (int)view.len, SQLITE_TRANSIENT);
564 PyBuffer_Release(&view);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000565 } else {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200566 return -1;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000567 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200568 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000569}
570
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +0100571static PyObject *
572_pysqlite_build_py_params(sqlite3_context *context, int argc,
573 sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000574{
575 PyObject* args;
576 int i;
577 sqlite3_value* cur_value;
578 PyObject* cur_py_value;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000579
580 args = PyTuple_New(argc);
581 if (!args) {
582 return NULL;
583 }
584
585 for (i = 0; i < argc; i++) {
586 cur_value = argv[i];
587 switch (sqlite3_value_type(argv[i])) {
588 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500589 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000590 break;
591 case SQLITE_FLOAT:
592 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
593 break;
Miss Islington (bot)067d6d42021-06-04 11:54:39 -0700594 case SQLITE_TEXT: {
595 sqlite3 *db = sqlite3_context_db_handle(context);
596 const char *text = (const char *)sqlite3_value_text(cur_value);
597
598 if (text == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
599 PyErr_NoMemory();
600 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000601 }
Miss Islington (bot)067d6d42021-06-04 11:54:39 -0700602
603 Py_ssize_t size = sqlite3_value_bytes(cur_value);
604 cur_py_value = PyUnicode_FromStringAndSize(text, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000605 break;
Miss Islington (bot)067d6d42021-06-04 11:54:39 -0700606 }
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200607 case SQLITE_BLOB: {
608 sqlite3 *db = sqlite3_context_db_handle(context);
609 const void *blob = sqlite3_value_blob(cur_value);
610
611 if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
612 PyErr_NoMemory();
613 goto error;
614 }
615
616 Py_ssize_t size = sqlite3_value_bytes(cur_value);
617 cur_py_value = PyBytes_FromStringAndSize(blob, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000618 break;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200619 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620 case SQLITE_NULL:
621 default:
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100622 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000623 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624
625 if (!cur_py_value) {
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200626 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000627 }
628
Erlend Egeberg Aaslande9194ea2021-04-23 13:21:08 +0200629 PyTuple_SET_ITEM(args, i, cur_py_value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000630 }
631
632 return args;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200633
634error:
635 Py_DECREF(args);
636 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000637}
638
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100639static void
640_pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000641{
642 PyObject* args;
643 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000644 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200645 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000646
647 PyGILState_STATE threadstate;
648
649 threadstate = PyGILState_Ensure();
650
651 py_func = (PyObject*)sqlite3_user_data(context);
652
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000653 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000654 if (args) {
655 py_retval = PyObject_CallObject(py_func, args);
656 Py_DECREF(args);
657 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000658
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200659 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000660 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200661 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000662 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200663 }
664 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700665 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000666 PyErr_Print();
667 } else {
668 PyErr_Clear();
669 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200670 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000671 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000672
673 PyGILState_Release(threadstate);
674}
675
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000676static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677{
678 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680 PyObject* aggregate_class;
681 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000682 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000683
684 PyGILState_STATE threadstate;
685
686 threadstate = PyGILState_Ensure();
687
688 aggregate_class = (PyObject*)sqlite3_user_data(context);
689
690 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
691
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200692 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100693 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000694
Thomas Wouters477c8d52006-05-27 19:21:47 +0000695 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000696 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700697 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000698 PyErr_Print();
699 } else {
700 PyErr_Clear();
701 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200702 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000703 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000704 }
705 }
706
707 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000708 if (!stepmethod) {
709 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000710 }
711
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000712 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000713 if (!args) {
714 goto error;
715 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000716
717 function_result = PyObject_CallObject(stepmethod, args);
718 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000719
Thomas Wouters477c8d52006-05-27 19:21:47 +0000720 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700721 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000722 PyErr_Print();
723 } else {
724 PyErr_Clear();
725 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200726 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000727 }
728
Thomas Wouters477c8d52006-05-27 19:21:47 +0000729error:
730 Py_XDECREF(stepmethod);
731 Py_XDECREF(function_result);
732
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000733 PyGILState_Release(threadstate);
734}
735
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100736static void
737_pysqlite_final_callback(sqlite3_context *context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000738{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200739 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000740 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200741 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200742 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200743 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000744
745 PyGILState_STATE threadstate;
746
747 threadstate = PyGILState_Ensure();
748
Erlend Egeberg Aasland979b23c2021-02-19 12:20:32 +0100749 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
750 if (aggregate_instance == NULL) {
751 /* No rows matched the query; the step handler was never called. */
752 goto error;
753 }
754 else if (!*aggregate_instance) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000755 /* this branch is executed if there was an exception in the aggregate's
756 * __init__ */
757
Thomas Wouters477c8d52006-05-27 19:21:47 +0000758 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000759 }
760
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200761 /* Keep the exception (if any) of the last call to step() */
762 PyErr_Fetch(&exception, &value, &tb);
763
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200764 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200765
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200766 Py_DECREF(*aggregate_instance);
767
768 ok = 0;
769 if (function_result) {
770 ok = _pysqlite_set_result(context, function_result) == 0;
771 Py_DECREF(function_result);
772 }
773 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700774 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000775 PyErr_Print();
776 } else {
777 PyErr_Clear();
778 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200779 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000780 }
781
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200782 /* Restore the exception (if any) of the last call to step(),
783 but clear also the current exception if finalize() failed */
784 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200785
Thomas Wouters477c8d52006-05-27 19:21:47 +0000786error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000787 PyGILState_Release(threadstate);
788}
789
Gerhard Häringf9cee222010-03-05 15:20:03 +0000790static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000791{
792 PyObject* new_list;
793 PyObject* weakref;
794 int i;
795
796 /* we only need to do this once in a while */
797 if (self->created_statements++ < 200) {
798 return;
799 }
800
801 self->created_statements = 0;
802
803 new_list = PyList_New(0);
804 if (!new_list) {
805 return;
806 }
807
808 for (i = 0; i < PyList_Size(self->statements); i++) {
809 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000810 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000811 if (PyList_Append(new_list, weakref) != 0) {
812 Py_DECREF(new_list);
813 return;
814 }
815 }
816 }
817
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300818 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000819}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000820
Gerhard Häringf9cee222010-03-05 15:20:03 +0000821static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
822{
823 PyObject* new_list;
824 PyObject* weakref;
825 int i;
826
827 /* we only need to do this once in a while */
828 if (self->created_cursors++ < 200) {
829 return;
830 }
831
832 self->created_cursors = 0;
833
834 new_list = PyList_New(0);
835 if (!new_list) {
836 return;
837 }
838
839 for (i = 0; i < PyList_Size(self->cursors); i++) {
840 weakref = PyList_GetItem(self->cursors, i);
841 if (PyWeakref_GetObject(weakref) != Py_None) {
842 if (PyList_Append(new_list, weakref) != 0) {
843 Py_DECREF(new_list);
844 return;
845 }
846 }
847 }
848
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300849 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000850}
851
gescheitb9a03762019-07-13 06:15:49 +0300852static void _destructor(void* args)
853{
Miss Islington (bot)317e9ed2021-06-05 16:13:27 -0700854 // This function may be called without the GIL held, so we need to ensure
855 // that we destroy 'args' with the GIL
856 PyGILState_STATE gstate;
857 gstate = PyGILState_Ensure();
gescheitb9a03762019-07-13 06:15:49 +0300858 Py_DECREF((PyObject*)args);
Miss Islington (bot)317e9ed2021-06-05 16:13:27 -0700859 PyGILState_Release(gstate);
gescheitb9a03762019-07-13 06:15:49 +0300860}
861
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100862/*[clinic input]
863_sqlite3.Connection.create_function as pysqlite_connection_create_function
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100865 name: str
866 narg: int
867 func: object
868 *
869 deterministic: bool = False
870
871Creates a new function. Non-standard.
872[clinic start generated code]*/
873
874static PyObject *
875pysqlite_connection_create_function_impl(pysqlite_Connection *self,
876 const char *name, int narg,
877 PyObject *func, int deterministic)
878/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
879{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000880 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500881 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000882
Gerhard Häringf9cee222010-03-05 15:20:03 +0000883 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
884 return NULL;
885 }
886
Sergey Fedoseev08308582018-07-08 12:09:20 +0500887 if (deterministic) {
888#if SQLITE_VERSION_NUMBER < 3008003
889 PyErr_SetString(pysqlite_NotSupportedError,
890 "deterministic=True requires SQLite 3.8.3 or higher");
891 return NULL;
892#else
893 if (sqlite3_libversion_number() < 3008003) {
894 PyErr_SetString(pysqlite_NotSupportedError,
895 "deterministic=True requires SQLite 3.8.3 or higher");
896 return NULL;
897 }
898 flags |= SQLITE_DETERMINISTIC;
899#endif
900 }
gescheitb9a03762019-07-13 06:15:49 +0300901 rc = sqlite3_create_function_v2(self->db,
902 name,
903 narg,
904 flags,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100905 (void*)Py_NewRef(func),
gescheitb9a03762019-07-13 06:15:49 +0300906 _pysqlite_func_callback,
907 NULL,
908 NULL,
909 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000910
Thomas Wouters477c8d52006-05-27 19:21:47 +0000911 if (rc != SQLITE_OK) {
912 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000913 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000914 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000915 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500916 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000917}
918
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100919/*[clinic input]
920_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000921
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100922 name: str
923 n_arg: int
924 aggregate_class: object
925
926Creates a new aggregate. Non-standard.
927[clinic start generated code]*/
928
929static PyObject *
930pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
931 const char *name, int n_arg,
932 PyObject *aggregate_class)
933/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
934{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000935 int rc;
936
Gerhard Häringf9cee222010-03-05 15:20:03 +0000937 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
938 return NULL;
939 }
940
gescheitb9a03762019-07-13 06:15:49 +0300941 rc = sqlite3_create_function_v2(self->db,
942 name,
943 n_arg,
944 SQLITE_UTF8,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100945 (void*)Py_NewRef(aggregate_class),
gescheitb9a03762019-07-13 06:15:49 +0300946 0,
947 &_pysqlite_step_callback,
948 &_pysqlite_final_callback,
949 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000950 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000951 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000952 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000953 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000954 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500955 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000956}
957
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000958static 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 +0000959{
960 PyObject *ret;
961 int rc;
962 PyGILState_STATE gilstate;
963
964 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000965
Victor Stinnerd4095d92013-07-26 22:23:33 +0200966 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000967
Victor Stinnerd4095d92013-07-26 22:23:33 +0200968 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700969 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200970 PyErr_Print();
971 else
972 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200973
Victor Stinnerd4095d92013-07-26 22:23:33 +0200974 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200975 }
976 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200977 if (PyLong_Check(ret)) {
978 rc = _PyLong_AsInt(ret);
979 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700980 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200981 PyErr_Print();
982 else
983 PyErr_Clear();
984 rc = SQLITE_DENY;
985 }
986 }
987 else {
988 rc = SQLITE_DENY;
989 }
990 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000991 }
992
993 PyGILState_Release(gilstate);
994 return rc;
995}
996
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000997static int _progress_handler(void* user_arg)
998{
999 int rc;
1000 PyObject *ret;
1001 PyGILState_STATE gilstate;
1002
1003 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +01001004 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001005
1006 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001007 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001008 PyErr_Print();
1009 } else {
1010 PyErr_Clear();
1011 }
1012
Mark Dickinson934896d2009-02-21 20:59:32 +00001013 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +00001014 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001015 } else {
1016 rc = (int)PyObject_IsTrue(ret);
1017 Py_DECREF(ret);
1018 }
1019
1020 PyGILState_Release(gilstate);
1021 return rc;
1022}
1023
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001024#ifdef HAVE_TRACE_V2
1025/*
1026 * From https://sqlite.org/c3ref/trace_v2.html:
1027 * The integer return value from the callback is currently ignored, though this
1028 * may change in future releases. Callback implementations should return zero
1029 * to ensure future compatibility.
1030 */
1031static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
1032#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001033static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001034#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001035{
1036 PyObject *py_statement = NULL;
1037 PyObject *ret = NULL;
1038
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001039 PyGILState_STATE gilstate;
1040
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001041#ifdef HAVE_TRACE_V2
1042 if (type != SQLITE_TRACE_STMT) {
1043 return 0;
1044 }
1045#endif
1046
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001047 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001048 py_statement = PyUnicode_DecodeUTF8(statement_string,
1049 strlen(statement_string), "replace");
1050 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001051 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001052 Py_DECREF(py_statement);
1053 }
1054
1055 if (ret) {
1056 Py_DECREF(ret);
1057 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001058 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001059 PyErr_Print();
1060 } else {
1061 PyErr_Clear();
1062 }
1063 }
1064
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001065 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001066#ifdef HAVE_TRACE_V2
1067 return 0;
1068#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001069}
1070
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001071/*[clinic input]
1072_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001073
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001074 authorizer_callback as authorizer_cb: object
1075
1076Sets authorizer callback. Non-standard.
1077[clinic start generated code]*/
1078
1079static PyObject *
1080pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1081 PyObject *authorizer_cb)
1082/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1083{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001084 int rc;
1085
Gerhard Häringf9cee222010-03-05 15:20:03 +00001086 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1087 return NULL;
1088 }
1089
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001090 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001091 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001092 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001093 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001094 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001095 } else {
1096 Py_INCREF(authorizer_cb);
1097 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001098 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001099 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001100}
1101
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001102/*[clinic input]
1103_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1104
1105 progress_handler: object
1106 n: int
1107
1108Sets progress handler callback. Non-standard.
1109[clinic start generated code]*/
1110
1111static PyObject *
1112pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1113 PyObject *progress_handler,
1114 int n)
1115/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001116{
Gerhard Häringf9cee222010-03-05 15:20:03 +00001117 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1118 return NULL;
1119 }
1120
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001121 if (progress_handler == Py_None) {
1122 /* None clears the progress handler previously set */
1123 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001124 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001125 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001126 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001127 Py_INCREF(progress_handler);
1128 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001129 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001130 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001131}
1132
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001133/*[clinic input]
1134_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1135
1136 trace_callback: object
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001137
1138Sets a trace callback called for each SQL statement (passed as unicode).
1139
1140Non-standard.
1141[clinic start generated code]*/
1142
1143static PyObject *
Dong-hee Na21793492020-12-19 00:41:33 +09001144pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1145 PyObject *trace_callback)
1146/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001147{
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001148 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1149 return NULL;
1150 }
1151
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001152 if (trace_callback == Py_None) {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001153 /*
1154 * None clears the trace callback previously set
1155 *
1156 * Ref.
1157 * - https://sqlite.org/c3ref/c_trace.html
1158 * - https://sqlite.org/c3ref/trace_v2.html
1159 */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001160#ifdef HAVE_TRACE_V2
1161 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1162#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001163 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001164#endif
gescheitb9a03762019-07-13 06:15:49 +03001165 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001166 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001167#ifdef HAVE_TRACE_V2
1168 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1169#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001170 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001171#endif
gescheitb9a03762019-07-13 06:15:49 +03001172 Py_INCREF(trace_callback);
1173 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001174 }
1175
Berker Peksagfe21de92016-04-09 07:34:39 +03001176 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001177}
1178
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001179#ifndef SQLITE_OMIT_LOAD_EXTENSION
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001180/*[clinic input]
1181_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1182
Dong-hee Na21793492020-12-19 00:41:33 +09001183 enable as onoff: bool(accept={int})
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001184 /
1185
1186Enable dynamic loading of SQLite extension modules. Non-standard.
1187[clinic start generated code]*/
1188
1189static PyObject *
1190pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1191 int onoff)
Dong-hee Na21793492020-12-19 00:41:33 +09001192/*[clinic end generated code: output=9cac37190d388baf input=5c0da5b121121cbc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001193{
1194 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001195
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001196 if (PySys_Audit("sqlite3.enable_load_extension",
1197 "OO", self, onoff ? Py_True : Py_False) < 0) {
1198 return NULL;
1199 }
1200
Gerhard Häringf9cee222010-03-05 15:20:03 +00001201 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1202 return NULL;
1203 }
1204
Gerhard Häringf9cee222010-03-05 15:20:03 +00001205 rc = sqlite3_enable_load_extension(self->db, onoff);
1206
1207 if (rc != SQLITE_OK) {
1208 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1209 return NULL;
1210 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001211 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001212 }
1213}
1214
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001215/*[clinic input]
1216_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1217
1218 name as extension_name: str
1219 /
1220
1221Load SQLite extension module. Non-standard.
1222[clinic start generated code]*/
1223
1224static PyObject *
1225pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1226 const char *extension_name)
1227/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001228{
1229 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001230 char* errmsg;
1231
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001232 if (PySys_Audit("sqlite3.load_extension", "Os", self, extension_name) < 0) {
1233 return NULL;
1234 }
1235
Gerhard Häringf9cee222010-03-05 15:20:03 +00001236 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1237 return NULL;
1238 }
1239
Gerhard Häringf9cee222010-03-05 15:20:03 +00001240 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1241 if (rc != 0) {
1242 PyErr_SetString(pysqlite_OperationalError, errmsg);
1243 return NULL;
1244 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001245 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001246 }
1247}
1248#endif
1249
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001250int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001251{
1252 if (self->check_same_thread) {
1253 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001254 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001255 "SQLite objects created in a thread can only be used in that same thread. "
1256 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001257 self->thread_ident, PyThread_get_thread_ident());
1258 return 0;
1259 }
1260
1261 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001262 return 1;
1263}
1264
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001265static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001266{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001267 return Py_NewRef(self->isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001268}
1269
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001270static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001271{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001272 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001273 return NULL;
1274 } else {
1275 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1276 }
1277}
1278
Berker Peksag59da4b32016-09-12 07:16:43 +03001279static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1280{
1281 if (!pysqlite_check_connection(self)) {
1282 return NULL;
1283 }
1284 if (!sqlite3_get_autocommit(self->db)) {
1285 Py_RETURN_TRUE;
1286 }
1287 Py_RETURN_FALSE;
1288}
1289
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001290static int
1291pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001292{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001293 if (isolation_level == NULL) {
1294 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1295 return -1;
1296 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001297 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001298 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001299 if (!res) {
1300 return -1;
1301 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001302 Py_DECREF(res);
1303
Serhiy Storchaka28914922016-09-01 22:18:03 +03001304 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001305 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001306 const char * const *candidate;
1307 PyObject *uppercase_level;
1308 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001309
Serhiy Storchaka28914922016-09-01 22:18:03 +03001310 if (!PyUnicode_Check(isolation_level)) {
1311 PyErr_Format(PyExc_TypeError,
1312 "isolation_level must be a string or None, not %.100s",
1313 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001314 return -1;
1315 }
1316
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001317 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001318 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001319 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001320 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001321 return -1;
1322 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001323 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001324 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001325 break;
1326 }
1327 Py_DECREF(uppercase_level);
1328 if (!*candidate) {
1329 PyErr_SetString(PyExc_ValueError,
1330 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001331 return -1;
1332 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001333 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001334 }
1335
Serhiy Storchaka28914922016-09-01 22:18:03 +03001336 Py_INCREF(isolation_level);
1337 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001338 return 0;
1339}
1340
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +01001341static PyObject *
1342pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1343 PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001344{
1345 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001346 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001347 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001348
Gerhard Häringf9cee222010-03-05 15:20:03 +00001349 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1350 return NULL;
1351 }
1352
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001353 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001354 return NULL;
1355
Victor Stinnerc6a23202019-06-26 03:16:24 +02001356 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001357 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001358
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001359 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001360
Erlend Egeberg Aasland84d80f52021-06-03 18:38:09 +02001361 statement = pysqlite_statement_create(self, sql);
1362 if (statement == NULL) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001363 return NULL;
1364 }
1365
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001366 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1367 if (weakref == NULL)
1368 goto error;
1369 if (PyList_Append(self->statements, weakref) != 0) {
1370 Py_DECREF(weakref);
1371 goto error;
1372 }
1373 Py_DECREF(weakref);
1374
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001375 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001376
1377error:
1378 Py_DECREF(statement);
1379 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001380}
1381
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001382/*[clinic input]
1383_sqlite3.Connection.execute as pysqlite_connection_execute
1384
1385 sql: unicode
1386 parameters: object = NULL
1387 /
1388
1389Executes a SQL statement. Non-standard.
1390[clinic start generated code]*/
1391
1392static PyObject *
1393pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
1394 PyObject *parameters)
1395/*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001396{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001397 _Py_IDENTIFIER(execute);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001398 PyObject* cursor = 0;
1399 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001400
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001401 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001402 if (!cursor) {
1403 goto error;
1404 }
1405
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001406 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001407 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001408 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001409 }
1410
1411error:
1412 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001413
1414 return cursor;
1415}
1416
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001417/*[clinic input]
1418_sqlite3.Connection.executemany as pysqlite_connection_executemany
1419
1420 sql: unicode
1421 parameters: object
1422 /
1423
1424Repeatedly executes a SQL statement. Non-standard.
1425[clinic start generated code]*/
1426
1427static PyObject *
1428pysqlite_connection_executemany_impl(pysqlite_Connection *self,
1429 PyObject *sql, PyObject *parameters)
1430/*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001431{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001432 _Py_IDENTIFIER(executemany);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001433 PyObject* cursor = 0;
1434 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001435
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001436 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001437 if (!cursor) {
1438 goto error;
1439 }
1440
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001441 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
1442 parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001443 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001444 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001445 }
1446
1447error:
1448 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001449
1450 return cursor;
1451}
1452
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001453/*[clinic input]
1454_sqlite3.Connection.executescript as pysqlite_connection_executescript
1455
1456 sql_script as script_obj: object
1457 /
1458
1459Executes a multiple SQL statements at once. Non-standard.
1460[clinic start generated code]*/
1461
1462static PyObject *
1463pysqlite_connection_executescript(pysqlite_Connection *self,
1464 PyObject *script_obj)
1465/*[clinic end generated code: output=4c4f9d77aa0ae37d input=c0b14695aa6c81d9]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001466{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001467 _Py_IDENTIFIER(executescript);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001468 PyObject* cursor = 0;
1469 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001470
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001471 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001472 if (!cursor) {
1473 goto error;
1474 }
1475
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001476 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
1477 script_obj, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001478 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001479 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001480 }
1481
1482error:
1483 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001484
1485 return cursor;
1486}
1487
1488/* ------------------------- COLLATION CODE ------------------------ */
1489
1490static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001491pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001492 void* context,
1493 int text1_length, const void* text1_data,
1494 int text2_length, const void* text2_data)
1495{
1496 PyObject* callback = (PyObject*)context;
1497 PyObject* string1 = 0;
1498 PyObject* string2 = 0;
1499 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001500 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001501 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001502 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001503 gilstate = PyGILState_Ensure();
1504
1505 if (PyErr_Occurred()) {
1506 goto finally;
1507 }
1508
Guido van Rossum98297ee2007-11-06 21:34:58 +00001509 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1510 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001511
1512 if (!string1 || !string2) {
1513 goto finally; /* failed to allocate strings */
1514 }
1515
1516 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1517
1518 if (!retval) {
1519 /* execution failed */
1520 goto finally;
1521 }
1522
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001523 longval = PyLong_AsLongAndOverflow(retval, &result);
1524 if (longval == -1 && PyErr_Occurred()) {
1525 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001526 result = 0;
1527 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001528 else if (!result) {
1529 if (longval > 0)
1530 result = 1;
1531 else if (longval < 0)
1532 result = -1;
1533 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001534
1535finally:
1536 Py_XDECREF(string1);
1537 Py_XDECREF(string2);
1538 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001539 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001540 return result;
1541}
1542
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001543/*[clinic input]
1544_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1545
1546Abort any pending database operation. Non-standard.
1547[clinic start generated code]*/
1548
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001549static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001550pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1551/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001552{
1553 PyObject* retval = NULL;
1554
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001555 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001556 goto finally;
1557 }
1558
1559 sqlite3_interrupt(self->db);
1560
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001561 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001562
1563finally:
1564 return retval;
1565}
1566
Christian Heimesbbe741d2008-03-28 10:53:29 +00001567/* Function author: Paul Kippes <kippesp@gmail.com>
1568 * Class method of Connection to call the Python function _iterdump
1569 * of the sqlite3 module.
1570 */
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001571/*[clinic input]
1572_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1573
1574Returns iterator to the dump of the database in an SQL text format.
1575
1576Non-standard.
1577[clinic start generated code]*/
1578
Christian Heimesbbe741d2008-03-28 10:53:29 +00001579static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001580pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1581/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001582{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001583 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001584 PyObject* retval = NULL;
1585 PyObject* module = NULL;
1586 PyObject* module_dict;
1587 PyObject* pyfn_iterdump;
1588
1589 if (!pysqlite_check_connection(self)) {
1590 goto finally;
1591 }
1592
1593 module = PyImport_ImportModule(MODULE_NAME ".dump");
1594 if (!module) {
1595 goto finally;
1596 }
1597
1598 module_dict = PyModule_GetDict(module);
1599 if (!module_dict) {
1600 goto finally;
1601 }
1602
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001603 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001604 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001605 if (!PyErr_Occurred()) {
1606 PyErr_SetString(pysqlite_OperationalError,
1607 "Failed to obtain _iterdump() reference");
1608 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001609 goto finally;
1610 }
1611
Petr Viktorinffd97532020-02-11 17:46:57 +01001612 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001613
1614finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001615 Py_XDECREF(module);
1616 return retval;
1617}
1618
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001619/*[clinic input]
1620_sqlite3.Connection.backup as pysqlite_connection_backup
1621
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001622 target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType')
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001623 *
1624 pages: int = -1
1625 progress: object = None
1626 name: str = "main"
1627 sleep: double = 0.250
1628
1629Makes a backup of the database. Non-standard.
1630[clinic start generated code]*/
1631
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001632static PyObject *
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001633pysqlite_connection_backup_impl(pysqlite_Connection *self,
1634 pysqlite_Connection *target, int pages,
1635 PyObject *progress, const char *name,
1636 double sleep)
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001637/*[clinic end generated code: output=306a3e6a38c36334 input=30ae45fc420bfd3b]*/
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001638{
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001639 int rc;
Pablo Galindoa6d63a22020-12-29 00:28:09 +00001640 int sleep_ms = (int)(sleep * 1000.0);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001641 sqlite3 *bck_conn;
1642 sqlite3_backup *bck_handle;
Victor Stinnerca405012018-04-30 12:22:17 +02001643
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001644 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1645 return NULL;
1646 }
1647
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001648 if (!pysqlite_check_connection(target)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001649 return NULL;
1650 }
1651
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001652 if (target == self) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001653 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1654 return NULL;
1655 }
1656
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001657#if SQLITE_VERSION_NUMBER < 3008008
1658 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001659 https://www.sqlite.org/src/info/169b5505498c0a7e */
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001660 if (!sqlite3_get_autocommit(target->db)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001661 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1662 return NULL;
1663 }
1664#endif
1665
1666 if (progress != Py_None && !PyCallable_Check(progress)) {
1667 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1668 return NULL;
1669 }
1670
1671 if (pages == 0) {
1672 pages = -1;
1673 }
1674
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001675 bck_conn = target->db;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001676
1677 Py_BEGIN_ALLOW_THREADS
1678 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1679 Py_END_ALLOW_THREADS
1680
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001681 if (bck_handle == NULL) {
1682 _pysqlite_seterror(bck_conn, NULL);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001683 return NULL;
1684 }
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001685
1686 do {
1687 Py_BEGIN_ALLOW_THREADS
1688 rc = sqlite3_backup_step(bck_handle, pages);
1689 Py_END_ALLOW_THREADS
1690
1691 if (progress != Py_None) {
1692 int remaining = sqlite3_backup_remaining(bck_handle);
1693 int pagecount = sqlite3_backup_pagecount(bck_handle);
1694 PyObject *res = PyObject_CallFunction(progress, "iii", rc,
1695 remaining, pagecount);
1696 if (res == NULL) {
1697 /* Callback failed: abort backup and bail. */
1698 Py_BEGIN_ALLOW_THREADS
1699 sqlite3_backup_finish(bck_handle);
1700 Py_END_ALLOW_THREADS
1701 return NULL;
1702 }
1703 Py_DECREF(res);
1704 }
1705
1706 /* Sleep for a while if there are still further pages to copy and
1707 the engine could not make any progress */
1708 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1709 Py_BEGIN_ALLOW_THREADS
1710 sqlite3_sleep(sleep_ms);
1711 Py_END_ALLOW_THREADS
1712 }
1713 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1714
1715 Py_BEGIN_ALLOW_THREADS
1716 rc = sqlite3_backup_finish(bck_handle);
1717 Py_END_ALLOW_THREADS
1718
1719 if (rc != SQLITE_OK) {
1720 _pysqlite_seterror(bck_conn, NULL);
1721 return NULL;
1722 }
1723
1724 Py_RETURN_NONE;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001725}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001726
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001727/*[clinic input]
1728_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
1729
1730 name: unicode
1731 callback as callable: object
1732 /
1733
1734Creates a collation function. Non-standard.
1735[clinic start generated code]*/
1736
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001737static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001738pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1739 PyObject *name, PyObject *callable)
1740/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001741{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001742 PyObject* uppercase_name = 0;
Victor Stinner35466c52010-04-22 11:23:23 +00001743 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001744 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001745 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001746 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001747 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001748 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001749
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001750 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001751 goto finally;
1752 }
1753
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001754 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1755 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001756 if (!uppercase_name) {
1757 goto finally;
1758 }
1759
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001760 if (PyUnicode_READY(uppercase_name))
1761 goto finally;
1762 len = PyUnicode_GET_LENGTH(uppercase_name);
1763 kind = PyUnicode_KIND(uppercase_name);
1764 data = PyUnicode_DATA(uppercase_name);
1765 for (i=0; i<len; i++) {
1766 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1767 if ((ch >= '0' && ch <= '9')
1768 || (ch >= 'A' && ch <= 'Z')
1769 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001770 {
Victor Stinner35466c52010-04-22 11:23:23 +00001771 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001772 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001773 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001774 goto finally;
1775 }
1776 }
1777
Serhiy Storchaka06515832016-11-20 09:13:07 +02001778 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001779 if (!uppercase_name_str)
1780 goto finally;
1781
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001782 if (callable != Py_None && !PyCallable_Check(callable)) {
1783 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1784 goto finally;
1785 }
1786
1787 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001788 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1789 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001790 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001791 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1792 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001793 }
1794
1795 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001796 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001797 SQLITE_UTF8,
1798 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001799 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001800 if (rc != SQLITE_OK) {
Serhiy Storchaka90731802021-01-31 17:42:38 +02001801 if (callable != Py_None) {
1802 if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1803 PyErr_Clear();
1804 }
1805 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001806 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001807 goto finally;
1808 }
1809
1810finally:
1811 Py_XDECREF(uppercase_name);
1812
1813 if (PyErr_Occurred()) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001814 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001815 }
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001816 return Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001817}
1818
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001819/*[clinic input]
1820_sqlite3.Connection.__enter__ as pysqlite_connection_enter
1821
1822Called when the connection is used as a context manager.
1823
1824Returns itself as a convenience to the caller.
1825[clinic start generated code]*/
1826
Christian Heimesbbe741d2008-03-28 10:53:29 +00001827static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001828pysqlite_connection_enter_impl(pysqlite_Connection *self)
1829/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001830{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001831 return Py_NewRef((PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001832}
1833
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001834/*[clinic input]
1835_sqlite3.Connection.__exit__ as pysqlite_connection_exit
1836
1837 type as exc_type: object
1838 value as exc_value: object
1839 traceback as exc_tb: object
1840 /
1841
1842Called when the connection is used as a context manager.
1843
1844If there was any exception, a rollback takes place; otherwise we commit.
1845[clinic start generated code]*/
1846
Christian Heimesbbe741d2008-03-28 10:53:29 +00001847static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001848pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
1849 PyObject *exc_value, PyObject *exc_tb)
1850/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001851{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001852 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001853 PyObject* result;
1854
Christian Heimesbbe741d2008-03-28 10:53:29 +00001855 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1856 method_name = "commit";
1857 } else {
1858 method_name = "rollback";
1859 }
1860
Victor Stinner3466bde2016-09-05 18:16:01 -07001861 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001862 if (!result) {
1863 return NULL;
1864 }
1865 Py_DECREF(result);
1866
1867 Py_RETURN_FALSE;
1868}
1869
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001870static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001871PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001872
1873static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001874 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1875 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001876 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001877 {NULL}
1878};
1879
1880static PyMethodDef connection_methods[] = {
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001881 PYSQLITE_CONNECTION_BACKUP_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001882 PYSQLITE_CONNECTION_CLOSE_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001883 PYSQLITE_CONNECTION_COMMIT_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001884 PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
1885 PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
1886 PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
1887 PYSQLITE_CONNECTION_CURSOR_METHODDEF
1888 PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
1889 PYSQLITE_CONNECTION_ENTER_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001890 PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
1891 PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
1892 PYSQLITE_CONNECTION_EXECUTE_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001893 PYSQLITE_CONNECTION_EXIT_METHODDEF
1894 PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
1895 PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
1896 PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
1897 PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
1898 PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
1899 PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
1900 PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001901 {NULL, NULL}
1902};
1903
1904static struct PyMemberDef connection_members[] =
1905{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001906 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1907 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1908 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1909 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1910 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1911 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1912 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1913 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1914 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1915 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001916 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1917 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001918 {NULL}
1919};
1920
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001921static PyType_Slot connection_slots[] = {
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001922 {Py_tp_dealloc, connection_dealloc},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001923 {Py_tp_doc, (void *)connection_doc},
1924 {Py_tp_methods, connection_methods},
1925 {Py_tp_members, connection_members},
1926 {Py_tp_getset, connection_getset},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001927 {Py_tp_init, pysqlite_connection_init},
1928 {Py_tp_call, pysqlite_connection_call},
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001929 {Py_tp_traverse, connection_traverse},
1930 {Py_tp_clear, connection_clear},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001931 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001932};
1933
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001934static PyType_Spec connection_spec = {
1935 .name = MODULE_NAME ".Connection",
1936 .basicsize = sizeof(pysqlite_Connection),
Miss Islington (bot)7297d742021-06-17 03:19:44 -07001937 .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
1938 Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_IMMUTABLETYPE),
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001939 .slots = connection_slots,
1940};
1941
1942PyTypeObject *pysqlite_ConnectionType = NULL;
1943
Erlend Egeberg Aasland38b6c2a2021-02-21 11:07:49 +01001944int
1945pysqlite_connection_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001946{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001947 pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
1948 if (pysqlite_ConnectionType == NULL) {
1949 return -1;
1950 }
1951 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001952}