blob: 3cf757dda371cb0bcf677d7f2e4cf38b83c58db0 [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{
231 Py_VISIT(self->statement_cache);
232 Py_VISIT(self->isolation_level);
233 Py_VISIT(self->function_pinboard_trace_callback);
234 Py_VISIT(self->function_pinboard_progress_handler);
235 Py_VISIT(self->function_pinboard_authorizer_cb);
236 Py_VISIT(self->row_factory);
237 Py_VISIT(self->text_factory);
238 Py_VISIT(self->collations);
239 Py_VISIT(self->statements);
240 Py_VISIT(self->cursors);
241 Py_VISIT(Py_TYPE(self));
242 return 0;
243}
244
245static int
246connection_clear(pysqlite_Connection *self)
247{
248 Py_CLEAR(self->statement_cache);
249 Py_CLEAR(self->isolation_level);
250 Py_CLEAR(self->function_pinboard_trace_callback);
251 Py_CLEAR(self->function_pinboard_progress_handler);
252 Py_CLEAR(self->function_pinboard_authorizer_cb);
253 Py_CLEAR(self->row_factory);
254 Py_CLEAR(self->text_factory);
255 Py_CLEAR(self->collations);
256 Py_CLEAR(self->statements);
257 Py_CLEAR(self->cursors);
258 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;
579 const char* val_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000580
581 args = PyTuple_New(argc);
582 if (!args) {
583 return NULL;
584 }
585
586 for (i = 0; i < argc; i++) {
587 cur_value = argv[i];
588 switch (sqlite3_value_type(argv[i])) {
589 case SQLITE_INTEGER:
Sergey Fedoseevb6f5b9d2019-10-23 13:09:01 +0500590 cur_py_value = PyLong_FromLongLong(sqlite3_value_int64(cur_value));
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000591 break;
592 case SQLITE_FLOAT:
593 cur_py_value = PyFloat_FromDouble(sqlite3_value_double(cur_value));
594 break;
595 case SQLITE_TEXT:
596 val_str = (const char*)sqlite3_value_text(cur_value);
Guido van Rossum98297ee2007-11-06 21:34:58 +0000597 cur_py_value = PyUnicode_FromString(val_str);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000598 /* TODO: have a way to show errors here */
599 if (!cur_py_value) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000600 PyErr_Clear();
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100601 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000602 }
603 break;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200604 case SQLITE_BLOB: {
605 sqlite3 *db = sqlite3_context_db_handle(context);
606 const void *blob = sqlite3_value_blob(cur_value);
607
608 if (blob == NULL && sqlite3_errcode(db) == SQLITE_NOMEM) {
609 PyErr_NoMemory();
610 goto error;
611 }
612
613 Py_ssize_t size = sqlite3_value_bytes(cur_value);
614 cur_py_value = PyBytes_FromStringAndSize(blob, size);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000615 break;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200616 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000617 case SQLITE_NULL:
618 default:
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100619 cur_py_value = Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000620 }
Thomas Wouters477c8d52006-05-27 19:21:47 +0000621
622 if (!cur_py_value) {
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200623 goto error;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000624 }
625
Erlend Egeberg Aaslande9194ea2021-04-23 13:21:08 +0200626 PyTuple_SET_ITEM(args, i, cur_py_value);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000627 }
628
629 return args;
Erlend Egeberg Aasland5cb601f2021-04-14 23:09:11 +0200630
631error:
632 Py_DECREF(args);
633 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000634}
635
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100636static void
637_pysqlite_func_callback(sqlite3_context *context, int argc, sqlite3_value **argv)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000638{
639 PyObject* args;
640 PyObject* py_func;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000641 PyObject* py_retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200642 int ok;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000643
644 PyGILState_STATE threadstate;
645
646 threadstate = PyGILState_Ensure();
647
648 py_func = (PyObject*)sqlite3_user_data(context);
649
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000650 args = _pysqlite_build_py_params(context, argc, argv);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000651 if (args) {
652 py_retval = PyObject_CallObject(py_func, args);
653 Py_DECREF(args);
654 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000655
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200656 ok = 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000657 if (py_retval) {
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200658 ok = _pysqlite_set_result(context, py_retval) == 0;
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000659 Py_DECREF(py_retval);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200660 }
661 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700662 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000663 PyErr_Print();
664 } else {
665 PyErr_Clear();
666 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200667 sqlite3_result_error(context, "user-defined function raised exception", -1);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000668 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000669
670 PyGILState_Release(threadstate);
671}
672
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000673static void _pysqlite_step_callback(sqlite3_context *context, int argc, sqlite3_value** params)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000674{
675 PyObject* args;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000676 PyObject* function_result = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000677 PyObject* aggregate_class;
678 PyObject** aggregate_instance;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000679 PyObject* stepmethod = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000680
681 PyGILState_STATE threadstate;
682
683 threadstate = PyGILState_Ensure();
684
685 aggregate_class = (PyObject*)sqlite3_user_data(context);
686
687 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, sizeof(PyObject*));
688
Serhiy Storchaka0b3ec192017-03-23 17:53:47 +0200689 if (*aggregate_instance == NULL) {
Victor Stinner070c4d72016-12-09 12:29:18 +0100690 *aggregate_instance = _PyObject_CallNoArg(aggregate_class);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000691
Thomas Wouters477c8d52006-05-27 19:21:47 +0000692 if (PyErr_Occurred()) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000693 *aggregate_instance = 0;
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700694 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000695 PyErr_Print();
696 } else {
697 PyErr_Clear();
698 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200699 sqlite3_result_error(context, "user-defined aggregate's '__init__' method raised error", -1);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000700 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000701 }
702 }
703
704 stepmethod = PyObject_GetAttrString(*aggregate_instance, "step");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000705 if (!stepmethod) {
706 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000707 }
708
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000709 args = _pysqlite_build_py_params(context, argc, params);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000710 if (!args) {
711 goto error;
712 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000713
714 function_result = PyObject_CallObject(stepmethod, args);
715 Py_DECREF(args);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000716
Thomas Wouters477c8d52006-05-27 19:21:47 +0000717 if (!function_result) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700718 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000719 PyErr_Print();
720 } else {
721 PyErr_Clear();
722 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200723 sqlite3_result_error(context, "user-defined aggregate's 'step' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000724 }
725
Thomas Wouters477c8d52006-05-27 19:21:47 +0000726error:
727 Py_XDECREF(stepmethod);
728 Py_XDECREF(function_result);
729
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000730 PyGILState_Release(threadstate);
731}
732
Erlend Egeberg Aasland2bb0bf42021-02-19 12:59:24 +0100733static void
734_pysqlite_final_callback(sqlite3_context *context)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000735{
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200736 PyObject* function_result;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000737 PyObject** aggregate_instance;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200738 _Py_IDENTIFIER(finalize);
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200739 int ok;
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200740 PyObject *exception, *value, *tb;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000741
742 PyGILState_STATE threadstate;
743
744 threadstate = PyGILState_Ensure();
745
Erlend Egeberg Aasland979b23c2021-02-19 12:20:32 +0100746 aggregate_instance = (PyObject**)sqlite3_aggregate_context(context, 0);
747 if (aggregate_instance == NULL) {
748 /* No rows matched the query; the step handler was never called. */
749 goto error;
750 }
751 else if (!*aggregate_instance) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000752 /* this branch is executed if there was an exception in the aggregate's
753 * __init__ */
754
Thomas Wouters477c8d52006-05-27 19:21:47 +0000755 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000756 }
757
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200758 /* Keep the exception (if any) of the last call to step() */
759 PyErr_Fetch(&exception, &value, &tb);
760
Jeroen Demeyer762f93f2019-07-08 10:19:25 +0200761 function_result = _PyObject_CallMethodIdNoArgs(*aggregate_instance, &PyId_finalize);
Victor Stinnere9af4cf2013-07-18 01:42:04 +0200762
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +0200763 Py_DECREF(*aggregate_instance);
764
765 ok = 0;
766 if (function_result) {
767 ok = _pysqlite_set_result(context, function_result) == 0;
768 Py_DECREF(function_result);
769 }
770 if (!ok) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700771 if (_pysqlite_enable_callback_tracebacks) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000772 PyErr_Print();
773 } else {
774 PyErr_Clear();
775 }
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200776 sqlite3_result_error(context, "user-defined aggregate's 'finalize' method raised error", -1);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000777 }
778
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +0200779 /* Restore the exception (if any) of the last call to step(),
780 but clear also the current exception if finalize() failed */
781 PyErr_Restore(exception, value, tb);
Victor Stinner3a857322013-07-22 08:34:32 +0200782
Thomas Wouters477c8d52006-05-27 19:21:47 +0000783error:
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000784 PyGILState_Release(threadstate);
785}
786
Gerhard Häringf9cee222010-03-05 15:20:03 +0000787static void _pysqlite_drop_unused_statement_references(pysqlite_Connection* self)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000788{
789 PyObject* new_list;
790 PyObject* weakref;
791 int i;
792
793 /* we only need to do this once in a while */
794 if (self->created_statements++ < 200) {
795 return;
796 }
797
798 self->created_statements = 0;
799
800 new_list = PyList_New(0);
801 if (!new_list) {
802 return;
803 }
804
805 for (i = 0; i < PyList_Size(self->statements); i++) {
806 weakref = PyList_GetItem(self->statements, i);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000807 if (PyWeakref_GetObject(weakref) != Py_None) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000808 if (PyList_Append(new_list, weakref) != 0) {
809 Py_DECREF(new_list);
810 return;
811 }
812 }
813 }
814
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300815 Py_SETREF(self->statements, new_list);
Thomas Wouters477c8d52006-05-27 19:21:47 +0000816}
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000817
Gerhard Häringf9cee222010-03-05 15:20:03 +0000818static void _pysqlite_drop_unused_cursor_references(pysqlite_Connection* self)
819{
820 PyObject* new_list;
821 PyObject* weakref;
822 int i;
823
824 /* we only need to do this once in a while */
825 if (self->created_cursors++ < 200) {
826 return;
827 }
828
829 self->created_cursors = 0;
830
831 new_list = PyList_New(0);
832 if (!new_list) {
833 return;
834 }
835
836 for (i = 0; i < PyList_Size(self->cursors); i++) {
837 weakref = PyList_GetItem(self->cursors, i);
838 if (PyWeakref_GetObject(weakref) != Py_None) {
839 if (PyList_Append(new_list, weakref) != 0) {
840 Py_DECREF(new_list);
841 return;
842 }
843 }
844 }
845
Serhiy Storchaka57a01d32016-04-10 18:05:40 +0300846 Py_SETREF(self->cursors, new_list);
Gerhard Häringf9cee222010-03-05 15:20:03 +0000847}
848
gescheitb9a03762019-07-13 06:15:49 +0300849static void _destructor(void* args)
850{
851 Py_DECREF((PyObject*)args);
852}
853
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100854/*[clinic input]
855_sqlite3.Connection.create_function as pysqlite_connection_create_function
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000856
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100857 name: str
858 narg: int
859 func: object
860 *
861 deterministic: bool = False
862
863Creates a new function. Non-standard.
864[clinic start generated code]*/
865
866static PyObject *
867pysqlite_connection_create_function_impl(pysqlite_Connection *self,
868 const char *name, int narg,
869 PyObject *func, int deterministic)
870/*[clinic end generated code: output=07d1877dd98c0308 input=f2edcf073e815beb]*/
871{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000872 int rc;
Sergey Fedoseev08308582018-07-08 12:09:20 +0500873 int flags = SQLITE_UTF8;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000874
Gerhard Häringf9cee222010-03-05 15:20:03 +0000875 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
876 return NULL;
877 }
878
Sergey Fedoseev08308582018-07-08 12:09:20 +0500879 if (deterministic) {
880#if SQLITE_VERSION_NUMBER < 3008003
881 PyErr_SetString(pysqlite_NotSupportedError,
882 "deterministic=True requires SQLite 3.8.3 or higher");
883 return NULL;
884#else
885 if (sqlite3_libversion_number() < 3008003) {
886 PyErr_SetString(pysqlite_NotSupportedError,
887 "deterministic=True requires SQLite 3.8.3 or higher");
888 return NULL;
889 }
890 flags |= SQLITE_DETERMINISTIC;
891#endif
892 }
gescheitb9a03762019-07-13 06:15:49 +0300893 rc = sqlite3_create_function_v2(self->db,
894 name,
895 narg,
896 flags,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100897 (void*)Py_NewRef(func),
gescheitb9a03762019-07-13 06:15:49 +0300898 _pysqlite_func_callback,
899 NULL,
900 NULL,
901 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000902
Thomas Wouters477c8d52006-05-27 19:21:47 +0000903 if (rc != SQLITE_OK) {
904 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000905 PyErr_SetString(pysqlite_OperationalError, "Error creating function");
Thomas Wouters477c8d52006-05-27 19:21:47 +0000906 return NULL;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000907 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500908 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000909}
910
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100911/*[clinic input]
912_sqlite3.Connection.create_aggregate as pysqlite_connection_create_aggregate
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000913
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +0100914 name: str
915 n_arg: int
916 aggregate_class: object
917
918Creates a new aggregate. Non-standard.
919[clinic start generated code]*/
920
921static PyObject *
922pysqlite_connection_create_aggregate_impl(pysqlite_Connection *self,
923 const char *name, int n_arg,
924 PyObject *aggregate_class)
925/*[clinic end generated code: output=fbb2f858cfa4d8db input=c2e13bbf234500a5]*/
926{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000927 int rc;
928
Gerhard Häringf9cee222010-03-05 15:20:03 +0000929 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
930 return NULL;
931 }
932
gescheitb9a03762019-07-13 06:15:49 +0300933 rc = sqlite3_create_function_v2(self->db,
934 name,
935 n_arg,
936 SQLITE_UTF8,
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +0100937 (void*)Py_NewRef(aggregate_class),
gescheitb9a03762019-07-13 06:15:49 +0300938 0,
939 &_pysqlite_step_callback,
940 &_pysqlite_final_callback,
941 &_destructor); // will decref func
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000942 if (rc != SQLITE_OK) {
Thomas Wouters477c8d52006-05-27 19:21:47 +0000943 /* Workaround for SQLite bug: no error code or string is available here */
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000944 PyErr_SetString(pysqlite_OperationalError, "Error creating aggregate");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000945 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000946 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +0500947 Py_RETURN_NONE;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000948}
949
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +0000950static 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 +0000951{
952 PyObject *ret;
953 int rc;
954 PyGILState_STATE gilstate;
955
956 gilstate = PyGILState_Ensure();
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000957
Victor Stinnerd4095d92013-07-26 22:23:33 +0200958 ret = PyObject_CallFunction((PyObject*)user_arg, "issss", action, arg1, arg2, dbname, access_attempt_source);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000959
Victor Stinnerd4095d92013-07-26 22:23:33 +0200960 if (ret == NULL) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700961 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200962 PyErr_Print();
963 else
964 PyErr_Clear();
Victor Stinner41801f52013-07-21 13:05:38 +0200965
Victor Stinnerd4095d92013-07-26 22:23:33 +0200966 rc = SQLITE_DENY;
Victor Stinner41801f52013-07-21 13:05:38 +0200967 }
968 else {
Victor Stinnerd4095d92013-07-26 22:23:33 +0200969 if (PyLong_Check(ret)) {
970 rc = _PyLong_AsInt(ret);
971 if (rc == -1 && PyErr_Occurred()) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700972 if (_pysqlite_enable_callback_tracebacks)
Victor Stinnerd4095d92013-07-26 22:23:33 +0200973 PyErr_Print();
974 else
975 PyErr_Clear();
976 rc = SQLITE_DENY;
977 }
978 }
979 else {
980 rc = SQLITE_DENY;
981 }
982 Py_DECREF(ret);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000983 }
984
985 PyGILState_Release(gilstate);
986 return rc;
987}
988
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000989static int _progress_handler(void* user_arg)
990{
991 int rc;
992 PyObject *ret;
993 PyGILState_STATE gilstate;
994
995 gilstate = PyGILState_Ensure();
Victor Stinner070c4d72016-12-09 12:29:18 +0100996 ret = _PyObject_CallNoArg((PyObject*)user_arg);
Gerhard Häringe7ea7452008-03-29 00:45:29 +0000997
998 if (!ret) {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -0700999 if (_pysqlite_enable_callback_tracebacks) {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001000 PyErr_Print();
1001 } else {
1002 PyErr_Clear();
1003 }
1004
Mark Dickinson934896d2009-02-21 20:59:32 +00001005 /* abort query if error occurred */
Victor Stinner86999502010-05-19 01:27:23 +00001006 rc = 1;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001007 } else {
1008 rc = (int)PyObject_IsTrue(ret);
1009 Py_DECREF(ret);
1010 }
1011
1012 PyGILState_Release(gilstate);
1013 return rc;
1014}
1015
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001016#ifdef HAVE_TRACE_V2
1017/*
1018 * From https://sqlite.org/c3ref/trace_v2.html:
1019 * The integer return value from the callback is currently ignored, though this
1020 * may change in future releases. Callback implementations should return zero
1021 * to ensure future compatibility.
1022 */
1023static int _trace_callback(unsigned int type, void* user_arg, void* prepared_statement, void* statement_string)
1024#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001025static void _trace_callback(void* user_arg, const char* statement_string)
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001026#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001027{
1028 PyObject *py_statement = NULL;
1029 PyObject *ret = NULL;
1030
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001031 PyGILState_STATE gilstate;
1032
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001033#ifdef HAVE_TRACE_V2
1034 if (type != SQLITE_TRACE_STMT) {
1035 return 0;
1036 }
1037#endif
1038
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001039 gilstate = PyGILState_Ensure();
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001040 py_statement = PyUnicode_DecodeUTF8(statement_string,
1041 strlen(statement_string), "replace");
1042 if (py_statement) {
Petr Viktorinffd97532020-02-11 17:46:57 +01001043 ret = PyObject_CallOneArg((PyObject*)user_arg, py_statement);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001044 Py_DECREF(py_statement);
1045 }
1046
1047 if (ret) {
1048 Py_DECREF(ret);
1049 } else {
Benjamin Peterson7762e4d2018-07-09 21:20:23 -07001050 if (_pysqlite_enable_callback_tracebacks) {
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001051 PyErr_Print();
1052 } else {
1053 PyErr_Clear();
1054 }
1055 }
1056
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001057 PyGILState_Release(gilstate);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001058#ifdef HAVE_TRACE_V2
1059 return 0;
1060#endif
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001061}
1062
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001063/*[clinic input]
1064_sqlite3.Connection.set_authorizer as pysqlite_connection_set_authorizer
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001065
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001066 authorizer_callback as authorizer_cb: object
1067
1068Sets authorizer callback. Non-standard.
1069[clinic start generated code]*/
1070
1071static PyObject *
1072pysqlite_connection_set_authorizer_impl(pysqlite_Connection *self,
1073 PyObject *authorizer_cb)
1074/*[clinic end generated code: output=f18ba575d788b35c input=df079724c020d2f2]*/
1075{
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001076 int rc;
1077
Gerhard Häringf9cee222010-03-05 15:20:03 +00001078 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1079 return NULL;
1080 }
1081
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001082 rc = sqlite3_set_authorizer(self->db, _authorizer_callback, (void*)authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001083 if (rc != SQLITE_OK) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001084 PyErr_SetString(pysqlite_OperationalError, "Error setting authorizer callback");
gescheitb9a03762019-07-13 06:15:49 +03001085 Py_XSETREF(self->function_pinboard_authorizer_cb, NULL);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001086 return NULL;
gescheitb9a03762019-07-13 06:15:49 +03001087 } else {
1088 Py_INCREF(authorizer_cb);
1089 Py_XSETREF(self->function_pinboard_authorizer_cb, authorizer_cb);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001090 }
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001091 Py_RETURN_NONE;
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001092}
1093
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001094/*[clinic input]
1095_sqlite3.Connection.set_progress_handler as pysqlite_connection_set_progress_handler
1096
1097 progress_handler: object
1098 n: int
1099
1100Sets progress handler callback. Non-standard.
1101[clinic start generated code]*/
1102
1103static PyObject *
1104pysqlite_connection_set_progress_handler_impl(pysqlite_Connection *self,
1105 PyObject *progress_handler,
1106 int n)
1107/*[clinic end generated code: output=35a7c10364cb1b04 input=857696c25f964c64]*/
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001108{
Gerhard Häringf9cee222010-03-05 15:20:03 +00001109 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1110 return NULL;
1111 }
1112
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001113 if (progress_handler == Py_None) {
1114 /* None clears the progress handler previously set */
1115 sqlite3_progress_handler(self->db, 0, 0, (void*)0);
gescheitb9a03762019-07-13 06:15:49 +03001116 Py_XSETREF(self->function_pinboard_progress_handler, NULL);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001117 } else {
Sergey Fedoseev5b25f1d2018-12-05 22:50:26 +05001118 sqlite3_progress_handler(self->db, n, _progress_handler, progress_handler);
gescheitb9a03762019-07-13 06:15:49 +03001119 Py_INCREF(progress_handler);
1120 Py_XSETREF(self->function_pinboard_progress_handler, progress_handler);
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001121 }
Berker Peksagfe21de92016-04-09 07:34:39 +03001122 Py_RETURN_NONE;
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001123}
1124
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001125/*[clinic input]
1126_sqlite3.Connection.set_trace_callback as pysqlite_connection_set_trace_callback
1127
1128 trace_callback: object
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001129
1130Sets a trace callback called for each SQL statement (passed as unicode).
1131
1132Non-standard.
1133[clinic start generated code]*/
1134
1135static PyObject *
Dong-hee Na21793492020-12-19 00:41:33 +09001136pysqlite_connection_set_trace_callback_impl(pysqlite_Connection *self,
1137 PyObject *trace_callback)
1138/*[clinic end generated code: output=fb0e307b9924d454 input=56d60fd38d763679]*/
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001139{
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001140 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1141 return NULL;
1142 }
1143
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001144 if (trace_callback == Py_None) {
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001145 /*
1146 * None clears the trace callback previously set
1147 *
1148 * Ref.
1149 * - https://sqlite.org/c3ref/c_trace.html
1150 * - https://sqlite.org/c3ref/trace_v2.html
1151 */
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001152#ifdef HAVE_TRACE_V2
1153 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, 0, 0);
1154#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001155 sqlite3_trace(self->db, 0, (void*)0);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001156#endif
gescheitb9a03762019-07-13 06:15:49 +03001157 Py_XSETREF(self->function_pinboard_trace_callback, NULL);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001158 } else {
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001159#ifdef HAVE_TRACE_V2
1160 sqlite3_trace_v2(self->db, SQLITE_TRACE_STMT, _trace_callback, trace_callback);
1161#else
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001162 sqlite3_trace(self->db, _trace_callback, trace_callback);
Erlend Egeberg Aasland7f331c82020-09-05 22:43:31 +02001163#endif
gescheitb9a03762019-07-13 06:15:49 +03001164 Py_INCREF(trace_callback);
1165 Py_XSETREF(self->function_pinboard_trace_callback, trace_callback);
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001166 }
1167
Berker Peksagfe21de92016-04-09 07:34:39 +03001168 Py_RETURN_NONE;
Antoine Pitrou5bfa0622011-04-04 00:12:04 +02001169}
1170
Erlend Egeberg Aasland207c3212020-09-07 23:26:54 +02001171#ifndef SQLITE_OMIT_LOAD_EXTENSION
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001172/*[clinic input]
1173_sqlite3.Connection.enable_load_extension as pysqlite_connection_enable_load_extension
1174
Dong-hee Na21793492020-12-19 00:41:33 +09001175 enable as onoff: bool(accept={int})
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001176 /
1177
1178Enable dynamic loading of SQLite extension modules. Non-standard.
1179[clinic start generated code]*/
1180
1181static PyObject *
1182pysqlite_connection_enable_load_extension_impl(pysqlite_Connection *self,
1183 int onoff)
Dong-hee Na21793492020-12-19 00:41:33 +09001184/*[clinic end generated code: output=9cac37190d388baf input=5c0da5b121121cbc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001185{
1186 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001187
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001188 if (PySys_Audit("sqlite3.enable_load_extension",
1189 "OO", self, onoff ? Py_True : Py_False) < 0) {
1190 return NULL;
1191 }
1192
Gerhard Häringf9cee222010-03-05 15:20:03 +00001193 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1194 return NULL;
1195 }
1196
Gerhard Häringf9cee222010-03-05 15:20:03 +00001197 rc = sqlite3_enable_load_extension(self->db, onoff);
1198
1199 if (rc != SQLITE_OK) {
1200 PyErr_SetString(pysqlite_OperationalError, "Error enabling load extension");
1201 return NULL;
1202 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001203 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001204 }
1205}
1206
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001207/*[clinic input]
1208_sqlite3.Connection.load_extension as pysqlite_connection_load_extension
1209
1210 name as extension_name: str
1211 /
1212
1213Load SQLite extension module. Non-standard.
1214[clinic start generated code]*/
1215
1216static PyObject *
1217pysqlite_connection_load_extension_impl(pysqlite_Connection *self,
1218 const char *extension_name)
1219/*[clinic end generated code: output=47eb1d7312bc97a7 input=0b711574560db9fc]*/
Gerhard Häringf9cee222010-03-05 15:20:03 +00001220{
1221 int rc;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001222 char* errmsg;
1223
Erlend Egeberg Aasland7244c002021-04-27 01:16:46 +02001224 if (PySys_Audit("sqlite3.load_extension", "Os", self, extension_name) < 0) {
1225 return NULL;
1226 }
1227
Gerhard Häringf9cee222010-03-05 15:20:03 +00001228 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1229 return NULL;
1230 }
1231
Gerhard Häringf9cee222010-03-05 15:20:03 +00001232 rc = sqlite3_load_extension(self->db, extension_name, 0, &errmsg);
1233 if (rc != 0) {
1234 PyErr_SetString(pysqlite_OperationalError, errmsg);
1235 return NULL;
1236 } else {
Berker Peksagfe21de92016-04-09 07:34:39 +03001237 Py_RETURN_NONE;
Gerhard Häringf9cee222010-03-05 15:20:03 +00001238 }
1239}
1240#endif
1241
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001242int pysqlite_check_thread(pysqlite_Connection* self)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001243{
1244 if (self->check_same_thread) {
1245 if (PyThread_get_thread_ident() != self->thread_ident) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001246 PyErr_Format(pysqlite_ProgrammingError,
Takuya Akiba030345c2018-03-27 00:14:00 +09001247 "SQLite objects created in a thread can only be used in that same thread. "
1248 "The object was created in thread id %lu and this is thread id %lu.",
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001249 self->thread_ident, PyThread_get_thread_ident());
1250 return 0;
1251 }
1252
1253 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001254 return 1;
1255}
1256
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001257static PyObject* pysqlite_connection_get_isolation_level(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001258{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001259 return Py_NewRef(self->isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001260}
1261
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001262static PyObject* pysqlite_connection_get_total_changes(pysqlite_Connection* self, void* unused)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001263{
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001264 if (!pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001265 return NULL;
1266 } else {
1267 return Py_BuildValue("i", sqlite3_total_changes(self->db));
1268 }
1269}
1270
Berker Peksag59da4b32016-09-12 07:16:43 +03001271static PyObject* pysqlite_connection_get_in_transaction(pysqlite_Connection* self, void* unused)
1272{
1273 if (!pysqlite_check_connection(self)) {
1274 return NULL;
1275 }
1276 if (!sqlite3_get_autocommit(self->db)) {
1277 Py_RETURN_TRUE;
1278 }
1279 Py_RETURN_FALSE;
1280}
1281
Serhiy Storchakad4f9cf52018-11-27 19:34:35 +02001282static int
1283pysqlite_connection_set_isolation_level(pysqlite_Connection* self, PyObject* isolation_level, void *Py_UNUSED(ignored))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001284{
Zackery Spytz842acaa2018-12-17 07:52:45 -07001285 if (isolation_level == NULL) {
1286 PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1287 return -1;
1288 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001289 if (isolation_level == Py_None) {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001290 PyObject *res = pysqlite_connection_commit(self, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001291 if (!res) {
1292 return -1;
1293 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001294 Py_DECREF(res);
1295
Serhiy Storchaka28914922016-09-01 22:18:03 +03001296 self->begin_statement = NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001297 } else {
Serhiy Storchaka28914922016-09-01 22:18:03 +03001298 const char * const *candidate;
1299 PyObject *uppercase_level;
1300 _Py_IDENTIFIER(upper);
Neal Norwitzefee9f52007-10-27 02:50:52 +00001301
Serhiy Storchaka28914922016-09-01 22:18:03 +03001302 if (!PyUnicode_Check(isolation_level)) {
1303 PyErr_Format(PyExc_TypeError,
1304 "isolation_level must be a string or None, not %.100s",
1305 Py_TYPE(isolation_level)->tp_name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001306 return -1;
1307 }
1308
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001309 uppercase_level = _PyObject_CallMethodIdOneArg(
Serhiy Storchaka28914922016-09-01 22:18:03 +03001310 (PyObject *)&PyUnicode_Type, &PyId_upper,
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001311 isolation_level);
Serhiy Storchaka28914922016-09-01 22:18:03 +03001312 if (!uppercase_level) {
Georg Brandl3dbca812008-07-23 16:10:53 +00001313 return -1;
1314 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001315 for (candidate = begin_statements; *candidate; candidate++) {
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001316 if (_PyUnicode_EqualToASCIIString(uppercase_level, *candidate + 6))
Serhiy Storchaka28914922016-09-01 22:18:03 +03001317 break;
1318 }
1319 Py_DECREF(uppercase_level);
1320 if (!*candidate) {
1321 PyErr_SetString(PyExc_ValueError,
1322 "invalid value for isolation_level");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001323 return -1;
1324 }
Serhiy Storchaka28914922016-09-01 22:18:03 +03001325 self->begin_statement = *candidate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001326 }
1327
Serhiy Storchaka28914922016-09-01 22:18:03 +03001328 Py_INCREF(isolation_level);
1329 Py_XSETREF(self->isolation_level, isolation_level);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001330 return 0;
1331}
1332
Erlend Egeberg Aaslandbf838a62021-02-21 01:29:19 +01001333static PyObject *
1334pysqlite_connection_call(pysqlite_Connection *self, PyObject *args,
1335 PyObject *kwargs)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001336{
1337 PyObject* sql;
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001338 pysqlite_Statement* statement;
Thomas Wouters477c8d52006-05-27 19:21:47 +00001339 PyObject* weakref;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001340 int rc;
1341
Gerhard Häringf9cee222010-03-05 15:20:03 +00001342 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1343 return NULL;
1344 }
1345
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001346 if (!_PyArg_NoKeywords(MODULE_NAME ".Connection", kwargs))
Larry Hastings3b12e952015-05-08 07:45:10 -07001347 return NULL;
1348
Victor Stinnerc6a23202019-06-26 03:16:24 +02001349 if (!PyArg_ParseTuple(args, "U", &sql))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001350 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001351
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001352 _pysqlite_drop_unused_statement_references(self);
Thomas Wouters477c8d52006-05-27 19:21:47 +00001353
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001354 statement = PyObject_GC_New(pysqlite_Statement, pysqlite_StatementType);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001355 if (!statement) {
1356 return NULL;
1357 }
1358
Victor Stinner0201f442010-03-13 03:28:34 +00001359 statement->db = NULL;
1360 statement->st = NULL;
1361 statement->sql = NULL;
1362 statement->in_use = 0;
1363 statement->in_weakreflist = NULL;
1364
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001365 rc = pysqlite_statement_create(statement, self, sql);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001366 if (rc != SQLITE_OK) {
1367 if (rc == PYSQLITE_TOO_MUCH_SQL) {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001368 PyErr_SetString(pysqlite_Warning, "You can only execute one statement at a time.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001369 } else if (rc == PYSQLITE_SQL_WRONG_TYPE) {
Serhiy Storchaka42d67af2014-09-11 13:29:05 +03001370 if (PyErr_ExceptionMatches(PyExc_TypeError))
1371 PyErr_SetString(pysqlite_Warning, "SQL is of wrong type. Must be string.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001372 } else {
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001373 (void)pysqlite_statement_reset(statement);
1374 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001375 }
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001376 goto error;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001377 }
1378
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001379 weakref = PyWeakref_NewRef((PyObject*)statement, NULL);
1380 if (weakref == NULL)
1381 goto error;
1382 if (PyList_Append(self->statements, weakref) != 0) {
1383 Py_DECREF(weakref);
1384 goto error;
1385 }
1386 Py_DECREF(weakref);
1387
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001388 return (PyObject*)statement;
Victor Stinnerb3e1ef12013-11-05 14:46:13 +01001389
1390error:
1391 Py_DECREF(statement);
1392 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001393}
1394
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001395/*[clinic input]
1396_sqlite3.Connection.execute as pysqlite_connection_execute
1397
1398 sql: unicode
1399 parameters: object = NULL
1400 /
1401
1402Executes a SQL statement. Non-standard.
1403[clinic start generated code]*/
1404
1405static PyObject *
1406pysqlite_connection_execute_impl(pysqlite_Connection *self, PyObject *sql,
1407 PyObject *parameters)
1408/*[clinic end generated code: output=5be05ae01ee17ee4 input=fbd17c75c7140271]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001409{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001410 _Py_IDENTIFIER(execute);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001411 PyObject* cursor = 0;
1412 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001413
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001414 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001415 if (!cursor) {
1416 goto error;
1417 }
1418
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001419 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_execute, sql, parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001420 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001421 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001422 }
1423
1424error:
1425 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001426
1427 return cursor;
1428}
1429
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001430/*[clinic input]
1431_sqlite3.Connection.executemany as pysqlite_connection_executemany
1432
1433 sql: unicode
1434 parameters: object
1435 /
1436
1437Repeatedly executes a SQL statement. Non-standard.
1438[clinic start generated code]*/
1439
1440static PyObject *
1441pysqlite_connection_executemany_impl(pysqlite_Connection *self,
1442 PyObject *sql, PyObject *parameters)
1443/*[clinic end generated code: output=776cd2fd20bfe71f input=4feab80659ffc82b]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001444{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001445 _Py_IDENTIFIER(executemany);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001446 PyObject* cursor = 0;
1447 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001448
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001449 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001450 if (!cursor) {
1451 goto error;
1452 }
1453
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001454 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executemany, sql,
1455 parameters, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001456 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001457 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001458 }
1459
1460error:
1461 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001462
1463 return cursor;
1464}
1465
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001466/*[clinic input]
1467_sqlite3.Connection.executescript as pysqlite_connection_executescript
1468
1469 sql_script as script_obj: object
1470 /
1471
1472Executes a multiple SQL statements at once. Non-standard.
1473[clinic start generated code]*/
1474
1475static PyObject *
1476pysqlite_connection_executescript(pysqlite_Connection *self,
1477 PyObject *script_obj)
1478/*[clinic end generated code: output=4c4f9d77aa0ae37d input=c0b14695aa6c81d9]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001479{
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001480 _Py_IDENTIFIER(executescript);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001481 PyObject* cursor = 0;
1482 PyObject* result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001483
Jeroen Demeyer762f93f2019-07-08 10:19:25 +02001484 cursor = _PyObject_CallMethodIdNoArgs((PyObject*)self, &PyId_cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001485 if (!cursor) {
1486 goto error;
1487 }
1488
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001489 result = _PyObject_CallMethodIdObjArgs(cursor, &PyId_executescript,
1490 script_obj, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001491 if (!result) {
Alexandre Vassalotti1839bac2008-07-13 21:57:48 +00001492 Py_CLEAR(cursor);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001493 }
1494
1495error:
1496 Py_XDECREF(result);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001497
1498 return cursor;
1499}
1500
1501/* ------------------------- COLLATION CODE ------------------------ */
1502
1503static int
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001504pysqlite_collation_callback(
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001505 void* context,
1506 int text1_length, const void* text1_data,
1507 int text2_length, const void* text2_data)
1508{
1509 PyObject* callback = (PyObject*)context;
1510 PyObject* string1 = 0;
1511 PyObject* string2 = 0;
1512 PyGILState_STATE gilstate;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001513 PyObject* retval = NULL;
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001514 long longval;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001515 int result = 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001516 gilstate = PyGILState_Ensure();
1517
1518 if (PyErr_Occurred()) {
1519 goto finally;
1520 }
1521
Guido van Rossum98297ee2007-11-06 21:34:58 +00001522 string1 = PyUnicode_FromStringAndSize((const char*)text1_data, text1_length);
1523 string2 = PyUnicode_FromStringAndSize((const char*)text2_data, text2_length);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001524
1525 if (!string1 || !string2) {
1526 goto finally; /* failed to allocate strings */
1527 }
1528
1529 retval = PyObject_CallFunctionObjArgs(callback, string1, string2, NULL);
1530
1531 if (!retval) {
1532 /* execution failed */
1533 goto finally;
1534 }
1535
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001536 longval = PyLong_AsLongAndOverflow(retval, &result);
1537 if (longval == -1 && PyErr_Occurred()) {
1538 PyErr_Clear();
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001539 result = 0;
1540 }
Serhiy Storchaka3cf96ac2013-02-07 17:01:47 +02001541 else if (!result) {
1542 if (longval > 0)
1543 result = 1;
1544 else if (longval < 0)
1545 result = -1;
1546 }
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001547
1548finally:
1549 Py_XDECREF(string1);
1550 Py_XDECREF(string2);
1551 Py_XDECREF(retval);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001552 PyGILState_Release(gilstate);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001553 return result;
1554}
1555
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001556/*[clinic input]
1557_sqlite3.Connection.interrupt as pysqlite_connection_interrupt
1558
1559Abort any pending database operation. Non-standard.
1560[clinic start generated code]*/
1561
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001562static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001563pysqlite_connection_interrupt_impl(pysqlite_Connection *self)
1564/*[clinic end generated code: output=f193204bc9e70b47 input=4bd0ad083cf93aa7]*/
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001565{
1566 PyObject* retval = NULL;
1567
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001568 if (!pysqlite_check_connection(self)) {
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001569 goto finally;
1570 }
1571
1572 sqlite3_interrupt(self->db);
1573
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001574 retval = Py_NewRef(Py_None);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001575
1576finally:
1577 return retval;
1578}
1579
Christian Heimesbbe741d2008-03-28 10:53:29 +00001580/* Function author: Paul Kippes <kippesp@gmail.com>
1581 * Class method of Connection to call the Python function _iterdump
1582 * of the sqlite3 module.
1583 */
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001584/*[clinic input]
1585_sqlite3.Connection.iterdump as pysqlite_connection_iterdump
1586
1587Returns iterator to the dump of the database in an SQL text format.
1588
1589Non-standard.
1590[clinic start generated code]*/
1591
Christian Heimesbbe741d2008-03-28 10:53:29 +00001592static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001593pysqlite_connection_iterdump_impl(pysqlite_Connection *self)
1594/*[clinic end generated code: output=586997aaf9808768 input=53bc907cb5eedb85]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001595{
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001596 _Py_IDENTIFIER(_iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001597 PyObject* retval = NULL;
1598 PyObject* module = NULL;
1599 PyObject* module_dict;
1600 PyObject* pyfn_iterdump;
1601
1602 if (!pysqlite_check_connection(self)) {
1603 goto finally;
1604 }
1605
1606 module = PyImport_ImportModule(MODULE_NAME ".dump");
1607 if (!module) {
1608 goto finally;
1609 }
1610
1611 module_dict = PyModule_GetDict(module);
1612 if (!module_dict) {
1613 goto finally;
1614 }
1615
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001616 pyfn_iterdump = _PyDict_GetItemIdWithError(module_dict, &PyId__iterdump);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001617 if (!pyfn_iterdump) {
Serhiy Storchakafc662ac2018-12-10 16:06:08 +02001618 if (!PyErr_Occurred()) {
1619 PyErr_SetString(pysqlite_OperationalError,
1620 "Failed to obtain _iterdump() reference");
1621 }
Christian Heimesbbe741d2008-03-28 10:53:29 +00001622 goto finally;
1623 }
1624
Petr Viktorinffd97532020-02-11 17:46:57 +01001625 retval = PyObject_CallOneArg(pyfn_iterdump, (PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001626
1627finally:
Christian Heimesbbe741d2008-03-28 10:53:29 +00001628 Py_XDECREF(module);
1629 return retval;
1630}
1631
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001632/*[clinic input]
1633_sqlite3.Connection.backup as pysqlite_connection_backup
1634
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001635 target: object(type='pysqlite_Connection *', subclass_of='pysqlite_ConnectionType')
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001636 *
1637 pages: int = -1
1638 progress: object = None
1639 name: str = "main"
1640 sleep: double = 0.250
1641
1642Makes a backup of the database. Non-standard.
1643[clinic start generated code]*/
1644
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001645static PyObject *
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001646pysqlite_connection_backup_impl(pysqlite_Connection *self,
1647 pysqlite_Connection *target, int pages,
1648 PyObject *progress, const char *name,
1649 double sleep)
Erlend Egeberg Aaslandea465792021-02-11 00:04:02 +01001650/*[clinic end generated code: output=306a3e6a38c36334 input=30ae45fc420bfd3b]*/
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001651{
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001652 int rc;
Pablo Galindoa6d63a22020-12-29 00:28:09 +00001653 int sleep_ms = (int)(sleep * 1000.0);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001654 sqlite3 *bck_conn;
1655 sqlite3_backup *bck_handle;
Victor Stinnerca405012018-04-30 12:22:17 +02001656
Peter McCormickbfee9fa2020-09-19 23:40:46 -04001657 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
1658 return NULL;
1659 }
1660
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001661 if (!pysqlite_check_connection(target)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001662 return NULL;
1663 }
1664
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001665 if (target == self) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001666 PyErr_SetString(PyExc_ValueError, "target cannot be the same connection instance");
1667 return NULL;
1668 }
1669
Aviv Palivodabbf7bb72018-03-18 02:48:55 +02001670#if SQLITE_VERSION_NUMBER < 3008008
1671 /* Since 3.8.8 this is already done, per commit
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001672 https://www.sqlite.org/src/info/169b5505498c0a7e */
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001673 if (!sqlite3_get_autocommit(target->db)) {
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001674 PyErr_SetString(pysqlite_OperationalError, "target is in transaction");
1675 return NULL;
1676 }
1677#endif
1678
1679 if (progress != Py_None && !PyCallable_Check(progress)) {
1680 PyErr_SetString(PyExc_TypeError, "progress argument must be a callable");
1681 return NULL;
1682 }
1683
1684 if (pages == 0) {
1685 pages = -1;
1686 }
1687
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001688 bck_conn = target->db;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001689
1690 Py_BEGIN_ALLOW_THREADS
1691 bck_handle = sqlite3_backup_init(bck_conn, "main", self->db, name);
1692 Py_END_ALLOW_THREADS
1693
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001694 if (bck_handle == NULL) {
1695 _pysqlite_seterror(bck_conn, NULL);
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001696 return NULL;
1697 }
Erlend Egeberg Aaslandc1ae7412021-04-14 13:45:49 +02001698
1699 do {
1700 Py_BEGIN_ALLOW_THREADS
1701 rc = sqlite3_backup_step(bck_handle, pages);
1702 Py_END_ALLOW_THREADS
1703
1704 if (progress != Py_None) {
1705 int remaining = sqlite3_backup_remaining(bck_handle);
1706 int pagecount = sqlite3_backup_pagecount(bck_handle);
1707 PyObject *res = PyObject_CallFunction(progress, "iii", rc,
1708 remaining, pagecount);
1709 if (res == NULL) {
1710 /* Callback failed: abort backup and bail. */
1711 Py_BEGIN_ALLOW_THREADS
1712 sqlite3_backup_finish(bck_handle);
1713 Py_END_ALLOW_THREADS
1714 return NULL;
1715 }
1716 Py_DECREF(res);
1717 }
1718
1719 /* Sleep for a while if there are still further pages to copy and
1720 the engine could not make any progress */
1721 if (rc == SQLITE_BUSY || rc == SQLITE_LOCKED) {
1722 Py_BEGIN_ALLOW_THREADS
1723 sqlite3_sleep(sleep_ms);
1724 Py_END_ALLOW_THREADS
1725 }
1726 } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED);
1727
1728 Py_BEGIN_ALLOW_THREADS
1729 rc = sqlite3_backup_finish(bck_handle);
1730 Py_END_ALLOW_THREADS
1731
1732 if (rc != SQLITE_OK) {
1733 _pysqlite_seterror(bck_conn, NULL);
1734 return NULL;
1735 }
1736
1737 Py_RETURN_NONE;
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001738}
Emanuele Gaifasd7aed412018-03-10 23:08:31 +01001739
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001740/*[clinic input]
1741_sqlite3.Connection.create_collation as pysqlite_connection_create_collation
1742
1743 name: unicode
1744 callback as callable: object
1745 /
1746
1747Creates a collation function. Non-standard.
1748[clinic start generated code]*/
1749
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001750static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001751pysqlite_connection_create_collation_impl(pysqlite_Connection *self,
1752 PyObject *name, PyObject *callable)
1753/*[clinic end generated code: output=0f63b8995565ae22 input=5c3898813a776cf2]*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001754{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001755 PyObject* uppercase_name = 0;
Victor Stinner35466c52010-04-22 11:23:23 +00001756 Py_ssize_t i, len;
Martin v. Löwisbd928fe2011-10-14 10:20:37 +02001757 _Py_IDENTIFIER(upper);
Serhiy Storchaka85b0f5b2016-11-20 10:16:47 +02001758 const char *uppercase_name_str;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001759 int rc;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001760 unsigned int kind;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001761 const void *data;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001762
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001763 if (!pysqlite_check_thread(self) || !pysqlite_check_connection(self)) {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001764 goto finally;
1765 }
1766
Jeroen Demeyer59ad1102019-07-11 10:59:05 +02001767 uppercase_name = _PyObject_CallMethodIdOneArg((PyObject *)&PyUnicode_Type,
1768 &PyId_upper, name);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001769 if (!uppercase_name) {
1770 goto finally;
1771 }
1772
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001773 if (PyUnicode_READY(uppercase_name))
1774 goto finally;
1775 len = PyUnicode_GET_LENGTH(uppercase_name);
1776 kind = PyUnicode_KIND(uppercase_name);
1777 data = PyUnicode_DATA(uppercase_name);
1778 for (i=0; i<len; i++) {
1779 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
1780 if ((ch >= '0' && ch <= '9')
1781 || (ch >= 'A' && ch <= 'Z')
1782 || (ch == '_'))
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001783 {
Victor Stinner35466c52010-04-22 11:23:23 +00001784 continue;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001785 } else {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001786 PyErr_SetString(pysqlite_ProgrammingError, "invalid character in collation name");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001787 goto finally;
1788 }
1789 }
1790
Serhiy Storchaka06515832016-11-20 09:13:07 +02001791 uppercase_name_str = PyUnicode_AsUTF8(uppercase_name);
Victor Stinner35466c52010-04-22 11:23:23 +00001792 if (!uppercase_name_str)
1793 goto finally;
1794
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001795 if (callable != Py_None && !PyCallable_Check(callable)) {
1796 PyErr_SetString(PyExc_TypeError, "parameter must be callable");
1797 goto finally;
1798 }
1799
1800 if (callable != Py_None) {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001801 if (PyDict_SetItem(self->collations, uppercase_name, callable) == -1)
1802 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001803 } else {
Gerhard Häringf9cee222010-03-05 15:20:03 +00001804 if (PyDict_DelItem(self->collations, uppercase_name) == -1)
1805 goto finally;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001806 }
1807
1808 rc = sqlite3_create_collation(self->db,
Victor Stinner35466c52010-04-22 11:23:23 +00001809 uppercase_name_str,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001810 SQLITE_UTF8,
1811 (callable != Py_None) ? callable : NULL,
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001812 (callable != Py_None) ? pysqlite_collation_callback : NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001813 if (rc != SQLITE_OK) {
Serhiy Storchaka90731802021-01-31 17:42:38 +02001814 if (callable != Py_None) {
1815 if (PyDict_DelItem(self->collations, uppercase_name) < 0) {
1816 PyErr_Clear();
1817 }
1818 }
Gerhard Häringe7ea7452008-03-29 00:45:29 +00001819 _pysqlite_seterror(self->db, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001820 goto finally;
1821 }
1822
1823finally:
1824 Py_XDECREF(uppercase_name);
1825
1826 if (PyErr_Occurred()) {
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001827 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001828 }
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001829 return Py_NewRef(Py_None);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001830}
1831
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001832/*[clinic input]
1833_sqlite3.Connection.__enter__ as pysqlite_connection_enter
1834
1835Called when the connection is used as a context manager.
1836
1837Returns itself as a convenience to the caller.
1838[clinic start generated code]*/
1839
Christian Heimesbbe741d2008-03-28 10:53:29 +00001840static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001841pysqlite_connection_enter_impl(pysqlite_Connection *self)
1842/*[clinic end generated code: output=457b09726d3e9dcd input=127d7a4f17e86d8f]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001843{
Erlend Egeberg Aaslandbf64d902020-12-27 12:05:33 +01001844 return Py_NewRef((PyObject *)self);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001845}
1846
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001847/*[clinic input]
1848_sqlite3.Connection.__exit__ as pysqlite_connection_exit
1849
1850 type as exc_type: object
1851 value as exc_value: object
1852 traceback as exc_tb: object
1853 /
1854
1855Called when the connection is used as a context manager.
1856
1857If there was any exception, a rollback takes place; otherwise we commit.
1858[clinic start generated code]*/
1859
Christian Heimesbbe741d2008-03-28 10:53:29 +00001860static PyObject *
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001861pysqlite_connection_exit_impl(pysqlite_Connection *self, PyObject *exc_type,
1862 PyObject *exc_value, PyObject *exc_tb)
1863/*[clinic end generated code: output=0705200e9321202a input=bd66f1532c9c54a7]*/
Christian Heimesbbe741d2008-03-28 10:53:29 +00001864{
Serhiy Storchakae2f92de2017-11-11 13:06:26 +02001865 const char* method_name;
Christian Heimesbbe741d2008-03-28 10:53:29 +00001866 PyObject* result;
1867
Christian Heimesbbe741d2008-03-28 10:53:29 +00001868 if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
1869 method_name = "commit";
1870 } else {
1871 method_name = "rollback";
1872 }
1873
Victor Stinner3466bde2016-09-05 18:16:01 -07001874 result = PyObject_CallMethod((PyObject*)self, method_name, NULL);
Christian Heimesbbe741d2008-03-28 10:53:29 +00001875 if (!result) {
1876 return NULL;
1877 }
1878 Py_DECREF(result);
1879
1880 Py_RETURN_FALSE;
1881}
1882
Serhiy Storchaka2d06e842015-12-25 19:53:18 +02001883static const char connection_doc[] =
Thomas Wouters477c8d52006-05-27 19:21:47 +00001884PyDoc_STR("SQLite database connection object.");
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001885
1886static PyGetSetDef connection_getset[] = {
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001887 {"isolation_level", (getter)pysqlite_connection_get_isolation_level, (setter)pysqlite_connection_set_isolation_level},
1888 {"total_changes", (getter)pysqlite_connection_get_total_changes, (setter)0},
Berker Peksag59da4b32016-09-12 07:16:43 +03001889 {"in_transaction", (getter)pysqlite_connection_get_in_transaction, (setter)0},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001890 {NULL}
1891};
1892
1893static PyMethodDef connection_methods[] = {
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001894 PYSQLITE_CONNECTION_BACKUP_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001895 PYSQLITE_CONNECTION_CLOSE_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001896 PYSQLITE_CONNECTION_COMMIT_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001897 PYSQLITE_CONNECTION_CREATE_AGGREGATE_METHODDEF
1898 PYSQLITE_CONNECTION_CREATE_COLLATION_METHODDEF
1899 PYSQLITE_CONNECTION_CREATE_FUNCTION_METHODDEF
1900 PYSQLITE_CONNECTION_CURSOR_METHODDEF
1901 PYSQLITE_CONNECTION_ENABLE_LOAD_EXTENSION_METHODDEF
1902 PYSQLITE_CONNECTION_ENTER_METHODDEF
Erlend Egeberg Aasland3ccef1c2020-12-27 09:32:18 +01001903 PYSQLITE_CONNECTION_EXECUTEMANY_METHODDEF
1904 PYSQLITE_CONNECTION_EXECUTESCRIPT_METHODDEF
1905 PYSQLITE_CONNECTION_EXECUTE_METHODDEF
Erlend Egeberg Aasland1ba82bb2020-12-18 15:25:35 +01001906 PYSQLITE_CONNECTION_EXIT_METHODDEF
1907 PYSQLITE_CONNECTION_INTERRUPT_METHODDEF
1908 PYSQLITE_CONNECTION_ITERDUMP_METHODDEF
1909 PYSQLITE_CONNECTION_LOAD_EXTENSION_METHODDEF
1910 PYSQLITE_CONNECTION_ROLLBACK_METHODDEF
1911 PYSQLITE_CONNECTION_SET_AUTHORIZER_METHODDEF
1912 PYSQLITE_CONNECTION_SET_PROGRESS_HANDLER_METHODDEF
1913 PYSQLITE_CONNECTION_SET_TRACE_CALLBACK_METHODDEF
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001914 {NULL, NULL}
1915};
1916
1917static struct PyMemberDef connection_members[] =
1918{
Guido van Rossum10f07c42007-08-11 15:32:55 +00001919 {"Warning", T_OBJECT, offsetof(pysqlite_Connection, Warning), READONLY},
1920 {"Error", T_OBJECT, offsetof(pysqlite_Connection, Error), READONLY},
1921 {"InterfaceError", T_OBJECT, offsetof(pysqlite_Connection, InterfaceError), READONLY},
1922 {"DatabaseError", T_OBJECT, offsetof(pysqlite_Connection, DatabaseError), READONLY},
1923 {"DataError", T_OBJECT, offsetof(pysqlite_Connection, DataError), READONLY},
1924 {"OperationalError", T_OBJECT, offsetof(pysqlite_Connection, OperationalError), READONLY},
1925 {"IntegrityError", T_OBJECT, offsetof(pysqlite_Connection, IntegrityError), READONLY},
1926 {"InternalError", T_OBJECT, offsetof(pysqlite_Connection, InternalError), READONLY},
1927 {"ProgrammingError", T_OBJECT, offsetof(pysqlite_Connection, ProgrammingError), READONLY},
1928 {"NotSupportedError", T_OBJECT, offsetof(pysqlite_Connection, NotSupportedError), READONLY},
Thomas Woutersfc7bb8c2007-01-15 15:49:28 +00001929 {"row_factory", T_OBJECT, offsetof(pysqlite_Connection, row_factory)},
1930 {"text_factory", T_OBJECT, offsetof(pysqlite_Connection, text_factory)},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001931 {NULL}
1932};
1933
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001934static PyType_Slot connection_slots[] = {
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001935 {Py_tp_dealloc, connection_dealloc},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001936 {Py_tp_doc, (void *)connection_doc},
1937 {Py_tp_methods, connection_methods},
1938 {Py_tp_members, connection_members},
1939 {Py_tp_getset, connection_getset},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001940 {Py_tp_init, pysqlite_connection_init},
1941 {Py_tp_call, pysqlite_connection_call},
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001942 {Py_tp_traverse, connection_traverse},
1943 {Py_tp_clear, connection_clear},
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001944 {0, NULL},
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001945};
1946
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001947static PyType_Spec connection_spec = {
1948 .name = MODULE_NAME ".Connection",
1949 .basicsize = sizeof(pysqlite_Connection),
Miss Islington (bot)e8d9df02021-05-25 11:08:39 -07001950 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001951 .slots = connection_slots,
1952};
1953
1954PyTypeObject *pysqlite_ConnectionType = NULL;
1955
Erlend Egeberg Aasland38b6c2a2021-02-21 11:07:49 +01001956int
1957pysqlite_connection_setup_types(PyObject *module)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001958{
Erlend Egeberg Aasland256e54a2020-10-01 16:03:21 +02001959 pysqlite_ConnectionType = (PyTypeObject *)PyType_FromModuleAndSpec(module, &connection_spec, NULL);
1960 if (pysqlite_ConnectionType == NULL) {
1961 return -1;
1962 }
1963 return 0;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00001964}